Guest User

Weed.java

a guest
Dec 30th, 2012
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 16.83 KB | None | 0 0
  1. package com.gmail.lopezitospriter.Weed;
  2.  
  3. import java.io.File;
  4. import java.io.FileNotFoundException;
  5. import java.io.FileOutputStream;
  6. import java.io.IOException;
  7. import java.io.InputStream;
  8. import java.io.OutputStream;
  9. import java.util.ArrayList;
  10. import java.util.Collection;
  11. import java.util.HashMap;
  12. import java.util.HashSet;
  13. import java.util.UUID;
  14. import java.util.logging.Logger;
  15.  
  16. import net.minecraft.server.v1_4_6.MobEffect;
  17. import net.minecraft.server.v1_4_6.Packet42RemoveMobEffect;
  18.  
  19. import org.bukkit.Bukkit;
  20. import org.bukkit.Effect;
  21. import org.bukkit.Location;
  22. import org.bukkit.Material;
  23. import org.bukkit.configuration.InvalidConfigurationException;
  24. import org.bukkit.craftbukkit.v1_4_6.entity.CraftLivingEntity;
  25. import org.bukkit.craftbukkit.v1_4_6.entity.CraftPlayer;
  26. import org.bukkit.entity.LivingEntity;
  27. import org.bukkit.entity.Player;
  28. import org.bukkit.event.EventHandler;
  29. import org.bukkit.event.Listener;
  30. import org.bukkit.event.block.BlockBurnEvent;
  31. import org.bukkit.event.entity.EntityCombustEvent;
  32. import org.bukkit.event.entity.EntityDamageEvent;
  33. import org.bukkit.event.entity.EntityDamageEvent.DamageCause;
  34. import org.bukkit.plugin.java.JavaPlugin;
  35. import org.bukkit.potion.PotionEffect;
  36. import org.bukkit.potion.PotionEffectType;
  37. import org.mcstats.BukkitMetrics;
  38.  
  39. public class Weed extends JavaPlugin {
  40.     static class PI {
  41.         public PotionEffectType type;
  42.         public int duration;
  43.  
  44.         public PI(PotionEffectType type, int duration) {
  45.             this.type = type;
  46.             this.duration = duration;
  47.         }
  48.     }
  49.  
  50.     private Logger log = Logger.getLogger("Minecraft");
  51.  
  52.     public static boolean debug = false;
  53.  
  54.     public static Weed plugin;
  55.  
  56.     private HashMap<String, Drug> drugs = null;
  57.  
  58.     private static HashMap<UUID, HashSet<DamageEffect>> damageEffects = new HashMap<UUID, HashSet<DamageEffect>>();
  59.     private static HashMap<UUID, HashSet<PI>> immune = new HashMap<UUID, HashSet<PI>>();
  60.  
  61.     private Listener listener = new Listener() {
  62.         @EventHandler
  63.         public void onEntityCombust(EntityCombustEvent event) {
  64.             if (event.isCancelled())
  65.                 return;
  66.             for (Drug d : drugs.values())
  67.                 d.activate(event);
  68.         }
  69.  
  70.         @EventHandler
  71.         public void onBlockBurn(BlockBurnEvent event) {
  72.             if (event.isCancelled())
  73.                 return;
  74.             for (Drug d : drugs.values())
  75.                 d.activate(event);
  76.         }
  77.  
  78.         @EventHandler
  79.         public void onEntityDamage(EntityDamageEvent event) {
  80.             if (event.isCancelled())
  81.                 return;
  82.             if (event.getEntity() instanceof LivingEntity)
  83.                 if (damageEffects.containsKey(event.getEntity().getUniqueId()))
  84.                     for (DamageEffect effect : damageEffects.get(event
  85.                             .getEntity().getUniqueId()))
  86.                         if (effect.getCause().equals(event.getCause()))
  87.                             if (effect.getAmplifier().getResult(
  88.                                     event.getDamage(), effect.getAmplitude()) == 0)
  89.                                 event.setCancelled(true);
  90.                             else if (effect.getAmplifier().getResult(
  91.                                     event.getDamage(), effect.getAmplitude()) < 0) {
  92.                                 event.setCancelled(true);
  93.                                 if (((LivingEntity) event.getEntity())
  94.                                         .getHealth()
  95.                                         + -1
  96.                                         * effect.getAmplifier().getResult(
  97.                                                 event.getDamage(),
  98.                                                 effect.getAmplitude()) > ((LivingEntity) event
  99.                                         .getEntity()).getMaxHealth())
  100.                                     ((LivingEntity) event.getEntity())
  101.                                             .setHealth(((LivingEntity) event
  102.                                                     .getEntity())
  103.                                                     .getMaxHealth());
  104.                                 else
  105.                                     ((LivingEntity) event.getEntity())
  106.                                             .setHealth(((LivingEntity) event
  107.                                                     .getEntity()).getHealth()
  108.                                                     + -1
  109.                                                     * effect.getAmplifier()
  110.                                                             .getResult(
  111.                                                                     event.getDamage(),
  112.                                                                     effect.getAmplitude()));
  113.                             } else
  114.                                 event.setDamage(effect.getAmplifier()
  115.                                         .getResult(event.getDamage(),
  116.                                                 effect.getAmplitude()));
  117.         }
  118.     };
  119.  
  120.     @Override
  121.     public void onDisable() {
  122.         drugs.clear();
  123.         drugs = null;
  124.         getServer().getScheduler().cancelTasks(this);
  125.         log.info("[" + getDescription().getName() + "] v"
  126.                 + getDescription().getVersion() + " is disabled.");
  127.     }
  128.  
  129.     @Override
  130.     public void onEnable() {
  131.         plugin = this;
  132.         getServer().getPluginManager().registerEvents(listener, this);
  133.         getServer().getScheduler().scheduleSyncRepeatingTask(this,
  134.                 new Runnable() {
  135.                     class IDD {
  136.                         public UUID id;
  137.                         public DamageCause cause;
  138.  
  139.                         public IDD(UUID id, DamageCause cause) {
  140.                             this.id = id;
  141.                             this.cause = cause;
  142.                         }
  143.                     }
  144.  
  145.                     class IPP {
  146.                         public UUID id;
  147.                         public PotionEffectType effect;
  148.  
  149.                         public IPP(UUID id, PotionEffectType effect) {
  150.                             this.id = id;
  151.                             this.effect = effect;
  152.                         }
  153.                     }
  154.  
  155.                     @Override
  156.                     public void run() {
  157.                         HashSet<IDD> r = new HashSet<IDD>();
  158.                         HashSet<IPP> i = new HashSet<IPP>();
  159.  
  160.                         for (UUID id : damageEffects.keySet()) {
  161.                             for (DamageEffect effect : damageEffects.get(id))
  162.                                 if (effect.getDuration() - 1 > 0)
  163.                                     effect.setDuration(effect.getDuration() - 1);
  164.                                 else
  165.                                     r.add(new IDD(id, effect.getCause()));
  166.                         }
  167.                         for (UUID id : immune.keySet()) {
  168.                             for (PI p : immune.get(id))
  169.                                 if (p.duration - 1 > 0)
  170.                                     p.duration -= 1;
  171.                                 else
  172.                                     i.add(new IPP(id, p.type));
  173.                         }
  174.                         for (IDD id : r)
  175.                             removeDamageEffect(id.id, id.cause);
  176.                         for (IPP id : i)
  177.                             removeImmunePotionEffectType(id.id, id.effect);
  178.                         r.clear();
  179.                         i.clear();
  180.                         r = null;
  181.                         i = null;
  182.                         if (debug) {
  183.                             System.out.println("----------------------DEBUG");
  184.                             System.out.println("------POTION");
  185.                             for (Player p : Bukkit.getOnlinePlayers())
  186.                                 for (PotionEffect e : p
  187.                                         .getActivePotionEffects())
  188.                                     System.out.println(e.getType().getName()
  189.                                             + " " + e.getDuration() / 20 + " "
  190.                                             + e.getAmplifier());
  191.                             System.out.println("------DAMAGE");
  192.                             for (UUID id : damageEffects.keySet())
  193.                                 for (DamageEffect effect : damageEffects
  194.                                         .get(id))
  195.                                     System.out.println(effect.getCause() + " "
  196.                                             + effect.getDuration() + " "
  197.                                             + effect.getAmplitude());
  198.                             System.out.println("------IMMUNE");
  199.                             for (UUID id : immune.keySet())
  200.                                 for (PI a : immune.get(id))
  201.                                     System.out.println(a.type.getName() + " "
  202.                                             + a.duration);
  203.                         }
  204.  
  205.                     }
  206.                 }, 20, 20);
  207.         load();
  208.         log.info("[" + getDescription().getName() + "] v"
  209.                 + getDescription().getVersion() + " is enabled.");
  210.         try {
  211.             BukkitMetrics metrics = new BukkitMetrics(this);
  212.             metrics.start();
  213.         } catch (IOException e) {
  214.             e.printStackTrace();
  215.         }
  216.     }
  217.  
  218.     private void load() {
  219.         drugs = new HashMap<String, Drug>();
  220.         if (!getDataFolder().exists())
  221.             getDataFolder().mkdir();
  222.         try {
  223.             getConfig().load(new File(getDataFolder(), "Drugs.yml"));
  224.             debug = getConfig().getBoolean("Debug");
  225.             for (String id : getConfig().getConfigurationSection("").getKeys(
  226.                     false))
  227.                 if (!id.equals("Debug")) {
  228.                     HashSet<WEffect> effects = new HashSet<WEffect>();
  229.  
  230.                     for (String delay : getConfig().getConfigurationSection(
  231.                             id + ".effects").getKeys(false)) {
  232.                         for (String effect : getConfig()
  233.                                 .getConfigurationSection(
  234.                                         id + ".effects." + delay)
  235.                                 .getKeys(false)) {
  236.                             String s = getConfig().getString(
  237.                                     id + ".effects." + delay + "." + effect);
  238.  
  239.                             if (effect.startsWith("potion_"))
  240.                                 effects.add(new WEffectPotion(
  241.                                         PotionEffectType.getByName(effect
  242.                                                 .replace("potion_", "")),
  243.                                         Integer.parseInt(delay),
  244.                                         Integer.parseInt(s.split(" ")[0]),
  245.                                         Integer.parseInt(s.split(" ")[1]
  246.                                                 .substring(1)),
  247.                                         Integer.parseInt(s.split(" ")[2]),
  248.                                         Amplifier.parseAmplifier(s.split(" ")[1]
  249.                                                 .charAt(0))));
  250.                             else if (effect.startsWith("damage_"))
  251.                                 effects.add(new WEffectDamage(
  252.                                         DamageCause.valueOf(effect.replace(
  253.                                                 "damage_", "")), Integer
  254.                                                 .parseInt(delay), Integer
  255.                                                 .parseInt(s.split(" ")[0]),
  256.                                         Integer.parseInt(s.split(" ")[1]
  257.                                                 .substring(1)), Amplifier
  258.                                                 .parseAmplifier(s.split(" ")[1]
  259.                                                         .charAt(0))));
  260.                             else
  261.                                 log.warning("Invalid effect:" + effect);
  262.                             s = null;
  263.                         }
  264.                     }
  265.                     if (effects.size() > 0)
  266.                         drugs.put(
  267.                                 id,
  268.                                 new Drug(Activation.parseActivation(getConfig()
  269.                                         .getString(id + ".activation")),
  270.                                         getConfig().getString(
  271.                                                 id + ".activation_argument")
  272.                                                 .split(" "), effects));
  273.                 }
  274.         } catch (FileNotFoundException e) {
  275.             log.info("[" + getDescription().getName()
  276.                     + "] Creating configuration file due to first time usage.");
  277.             try {
  278.                 new File(getDataFolder(), "Drugs.yml").createNewFile();
  279.                 getConfig().load(new File(getDataFolder(), "Drugs.yml"));
  280.                 getConfig().set("Debug", debug);
  281.                 getConfig().set("Weed.activation", Activation.BURN.toString());
  282.                 getConfig().set("Weed.activation_argument",
  283.                         "LONG_GRASS:1 5 true");
  284.                 getConfig().set("Weed.effects.0.potion_SLOW", "58 =0 0");
  285.                 getConfig().set("Weed.effects.0.potion_CONFUSION", "58 =0 0");
  286.                 getConfig().set("Weed.effects.0.potion_POISON", "58 =0 0");
  287.                 getConfig().set("Weed.effects.0.potion_SPEED", "60 +2 6");
  288.                 getConfig().set("Weed.effects.10.potion_JUMP", "50 +2 10");
  289.                 getConfig().set("Weed.effects.10.damage_FALL", "60 =0");
  290.                 getConfig().set("Weed.effects.60.potion_SLOW", "15 +2 4");
  291.                 getConfig().set("Weed.effects.60.potion_CONFUSION", "22 +1 10");
  292.                 getConfig().set("Weed.effects.60.potion_POISON", "5 +1 4");
  293.                 getConfig().set("Weed.effects.80.potion_CONFUSION", "10 +2 10");
  294.                 try {
  295.                     getConfig().save(new File(getDataFolder(), "Drugs.yml"));
  296.                     load();
  297.                 } catch (IOException e2) {
  298.                     e2.printStackTrace();
  299.                 }
  300.             } catch (Exception e1) {
  301.                 e1.printStackTrace();
  302.             }
  303.         } catch (IOException e) {
  304.             log.warning("[" + getDescription().getName()
  305.                     + "] Cannot load configuration: " + e);
  306.         } catch (InvalidConfigurationException e) {
  307.             log.warning("[" + getDescription().getName()
  308.                     + "] Cannot read configuration: " + e);
  309.         }
  310.         if (!new File(getDataFolder(), "Instructions.txt").exists()
  311.                 || (new File(getDataFolder(), "Instructions.txt").exists() && !new File(
  312.                         getDataFolder(), "Instructions.txt").isFile())) {
  313.             InputStream is = null;
  314.             OutputStream os = null;
  315.  
  316.             log.info("[" + getDescription().getName()
  317.                     + "] Creating information file due to first time usage.");
  318.             try {
  319.                 new File(getDataFolder(), "Instructions.txt").createNewFile();
  320.                 is = this.getClass().getResourceAsStream("Instructions.txt");
  321.                 os = new FileOutputStream(new File(getDataFolder(),
  322.                         "Instructions.txt"), true);
  323.                 byte[] buffer = new byte[1024];
  324.                 int len;
  325.  
  326.                 while ((len = is.read(buffer)) > 0)
  327.                     os.write(buffer, 0, len);
  328.                 is.close();
  329.                 os.close();
  330.             } catch (Exception e) {
  331.                 log.warning("[" + getDescription().getName()
  332.                         + "] Cannot save information: " + e);
  333.             } finally {
  334.                 is = null;
  335.                 os = null;
  336.             }
  337.         }
  338.     }
  339.  
  340.     public static void smoke(Location location, double distance) {
  341.         for (double x = location.getX() - distance; x <= location.getX()
  342.                 + distance; x++)
  343.             for (double y = location.getY() - distance; y <= location.getY()
  344.                     + distance; y++)
  345.                 for (double z = location.getZ() - distance; z <= location
  346.                         .getZ() + distance; z++)
  347.                     if (getDistance(
  348.                             location,
  349.                             location.getWorld()
  350.                                     .getBlockAt((int) x, (int) y, (int) z)
  351.                                     .getLocation()) <= distance)
  352.                         location.getWorld().playEffect(
  353.                                 location.getWorld()
  354.                                         .getBlockAt((int) x, (int) y, (int) z)
  355.                                         .getLocation(), Effect.SMOKE, 0);
  356.     }
  357.  
  358.     public static double getDistance(Location l1, Location l2) {
  359.         if (!l1.getWorld().equals(l2.getWorld()))
  360.             return -1;
  361.         return Math.sqrt(Math.pow(l1.getX() - l2.getX(), 2)
  362.                 + Math.pow(l1.getY() - l2.getY(), 2)
  363.                 + Math.pow(l1.getZ() - l2.getZ(), 2));
  364.     }
  365.  
  366.     public static int getPotionEffectAmplitude(LivingEntity entity,
  367.             PotionEffectType effect) {
  368.         if (entity != null && !entity.isDead())
  369.             for (PotionEffect e : entity.getActivePotionEffects())
  370.                 if (e.getType().equals(effect))
  371.                     return e.getAmplifier();
  372.         return 0;
  373.     }
  374.  
  375.     public static int getPotionEffectDuration(LivingEntity entity,
  376.             PotionEffectType effect) {
  377.         if (entity != null && !entity.isDead())
  378.             for (PotionEffect e : entity.getActivePotionEffects())
  379.                 if (e.getType().equals(effect))
  380.                     return e.getDuration();
  381.         return 0;
  382.     }
  383.  
  384.     public static void addPotionEffect(LivingEntity entity, PotionEffect effect) {
  385.         if (entity != null && !entity.isDead())
  386.             if (effect.getAmplifier() > 0) {
  387.                 if (!isImmuneFromPotionEffectType(entity, effect.getType()))
  388.                     if (effect.getDuration() > 0) {
  389.                         removePotionEffect(entity, effect.getType());
  390.                         entity.addPotionEffect(effect, true);
  391.                     } else {
  392.                         int i = getPotionEffectDuration(entity,
  393.                                 effect.getType());
  394.  
  395.                         removePotionEffect(entity, effect.getType());
  396.                         entity.addPotionEffect(
  397.                                 new PotionEffect(effect.getType(), i, effect
  398.                                         .getAmplifier()), true);
  399.                     }
  400.             } else {
  401.                 if (effect.getDuration() > 0) {
  402.                     addImmunePotionEffectType(entity, effect.getType(),
  403.                             effect.getDuration() / 20);
  404.                 }
  405.                 removePotionEffect(entity, effect.getType());
  406.             }
  407.     }
  408.  
  409.     public static void removePotionEffect(LivingEntity entity,
  410.             PotionEffectType effect) {
  411.         for (Player p : entity.getLocation().getWorld().getPlayers())
  412.             ((CraftPlayer) p).getHandle().playerConnection
  413.                     .sendPacket(new Packet42RemoveMobEffect(entity
  414.                             .getEntityId(), new MobEffect(effect.getId(), 0, 0)));
  415.         ((CraftLivingEntity) entity).getHandle().getDataWatcher()
  416.                 .watch(8, Integer.valueOf(0));
  417.     }
  418.  
  419.     public static int getDamageEffectAmplitude(LivingEntity entity,
  420.             DamageCause cause) {
  421.         if (damageEffects.containsKey(entity.getUniqueId()))
  422.             for (DamageEffect effect : damageEffects.get(entity.getUniqueId()))
  423.                 if (effect.getCause().equals(cause))
  424.                     return effect.getAmplitude();
  425.         return 0;
  426.     }
  427.  
  428.     public static int getDamageEffectDuration(LivingEntity entity,
  429.             DamageCause cause) {
  430.         if (damageEffects.containsKey(entity.getUniqueId()))
  431.             for (DamageEffect effect : damageEffects.get(entity.getUniqueId()))
  432.                 if (effect.getCause().equals(cause))
  433.                     return effect.getDuration();
  434.         return 0;
  435.     }
  436.  
  437.     public static void addDamageEffect(LivingEntity entity, DamageEffect effect) {
  438.         removeDamageEffect(entity, effect.getCause());
  439.         if (!damageEffects.containsKey(entity.getUniqueId()))
  440.             damageEffects
  441.                     .put(entity.getUniqueId(), new HashSet<DamageEffect>());
  442.         damageEffects.get(entity.getUniqueId()).add(effect);
  443.     }
  444.  
  445.     public static void removeDamageEffect(LivingEntity entity, DamageCause cause) {
  446.         removeDamageEffect(entity.getUniqueId(), cause);
  447.     }
  448.  
  449.     public static void removeDamageEffect(UUID id, DamageCause cause) {
  450.         if (damageEffects.containsKey(id)) {
  451.             ArrayList<DamageEffect> c = new ArrayList<DamageEffect>();
  452.  
  453.             for (DamageEffect effect : damageEffects.get(id))
  454.                 if (effect.getCause().equals(cause))
  455.                     c.add(effect);
  456.             damageEffects.get(id).removeAll(c);
  457.             c.clear();
  458.             c = null;
  459.             if (!(damageEffects.get(id).size() > 0))
  460.                 damageEffects.remove(id);
  461.         }
  462.     }
  463.  
  464.     public static void addImmunePotionEffectType(LivingEntity entity,
  465.             PotionEffectType effect, int duration) {
  466.         removeImmunePotionEffectType(entity.getUniqueId(), effect);
  467.         if (!immune.containsKey(entity.getUniqueId()))
  468.             immune.put(entity.getUniqueId(), new HashSet<PI>());
  469.         immune.get(entity.getUniqueId()).add(new PI(effect, duration));
  470.     }
  471.  
  472.     public static void removeImmunePotionEffectType(UUID id,
  473.             PotionEffectType effect) {
  474.         if (immune.containsKey(id)) {
  475.             ArrayList<PI> c = new ArrayList<PI>();
  476.  
  477.             for (PI e : immune.get(id))
  478.                 if (e.type.equals(effect))
  479.                     c.add(e);
  480.             immune.get(id).removeAll(c);
  481.             c.clear();
  482.             c = null;
  483.             if (!(immune.get(id).size() > 0))
  484.                 immune.remove(id);
  485.         }
  486.     }
  487.  
  488.     public static boolean isImmuneFromPotionEffectType(LivingEntity entity,
  489.             PotionEffectType effect) {
  490.         if (immune.containsKey(entity.getUniqueId())) {
  491.             for (PI p : immune.get(entity.getUniqueId()))
  492.                 if (p.type.equals(effect))
  493.                     return true;
  494.         }
  495.         return false;
  496.     }
  497.  
  498.     public static ArrayList<LivingEntity> getNearbyLivingEntities(
  499.             Location location, double distance) {
  500.         Collection<LivingEntity> list = location.getWorld().getEntitiesByClass(
  501.                 LivingEntity.class);
  502.         ArrayList<LivingEntity> n = new ArrayList<LivingEntity>();
  503.  
  504.         for (LivingEntity e : list)
  505.             if (getDistance(location, e.getLocation()) <= distance) {
  506.                 n.add(e);
  507.             }
  508.         list.clear();
  509.         list = null;
  510.         return n;
  511.     }
  512.  
  513.     public static boolean isTheSame(Material m1, byte b1, Material m2, byte b2) {
  514.         return m1.equals(m2) && b1 == b2;
  515.     }
  516.  
  517. }
Advertisement
Add Comment
Please, Sign In to add comment