Advertisement
EnderLance

MetalManipulation

Nov 19th, 2014
373
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.04 KB | None | 0 0
  1. package ca.carbogen.korra.metalmanipulation;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.List;
  5. import java.util.concurrent.ConcurrentHashMap;
  6.  
  7. import org.bukkit.Location;
  8. import org.bukkit.Material;
  9. import org.bukkit.entity.Entity;
  10. import org.bukkit.entity.Item;
  11. import org.bukkit.entity.LivingEntity;
  12. import org.bukkit.entity.Player;
  13. import org.bukkit.entity.Skeleton;
  14. import org.bukkit.entity.Zombie;
  15. import org.bukkit.inventory.ItemStack;
  16. import org.bukkit.util.Vector;
  17.  
  18. import com.projectkorra.ProjectKorra.Methods;
  19. import com.projectkorra.ProjectKorra.ProjectKorra;
  20.  
  21. public class MetalManipulation
  22. {
  23.     public static ConcurrentHashMap<Player, MetalManipulation> instances = new ConcurrentHashMap<Player, MetalManipulation>();
  24.     public static int armorTime = 10000;
  25.     public static int crushInterval = 1400;
  26.     public static int cooldown = 500;
  27.    
  28.     private Player player;
  29.     private LivingEntity target;
  30.     private boolean isBeingWorn = false;
  31.     private boolean isControlling = false;
  32.     private boolean canThrow = false;
  33.     public int metalclips = 0;
  34.     private long startTime;
  35.     private long time;
  36.    
  37.     private ItemStack[] oldarmor;
  38.     private List<Item> trackedIngots = new ArrayList<Item>();
  39.    
  40.     public MetalManipulation(Player player)
  41.     {
  42.         if(instances.containsKey(player))
  43.             return;
  44.        
  45.         this.player = player;
  46.        
  47.         shootMetal();
  48.        
  49.         instances.put(player, this);
  50.     }
  51.    
  52.     public void shootMetal()
  53.     {  
  54.         ItemStack is = new ItemStack(Material.IRON_INGOT, 1);
  55.  
  56.         if(!player.getInventory().containsAtLeast(is, 1))
  57.         {
  58.             //ProjectKorra.log.info("Player doesn't have enough ingots!");
  59.             remove();
  60.             return;
  61.         }
  62.         Item ii = player.getWorld().dropItemNaturally(player.getLocation(), is);
  63.         ii.setVelocity(player.getEyeLocation().getDirection().normalize().add(new Vector(0, .5, 0)));
  64.         trackedIngots.add(ii);
  65.         player.getInventory().removeItem(is);
  66.  
  67.         Methods.getBendingPlayer(player.getName()).addCooldown("MetalManipulation", cooldown);
  68.     }
  69.    
  70.     public void formArmor()
  71.     {
  72.         if(metalclips >= 4)
  73.             return;
  74.        
  75.         metalclips = (metalclips < 4) ? metalclips + 1 : 4;
  76.        
  77.         if(target instanceof Player)
  78.         {
  79.             Player target = (Player) this.target;
  80.             if(oldarmor == null)
  81.                 oldarmor = target.getInventory().getArmorContents();
  82.            
  83.             ItemStack[] metalarmor = new ItemStack[4];
  84.  
  85.             metalarmor[2] = (metalclips >= 1) ? new ItemStack(Material.IRON_CHESTPLATE, 1) : oldarmor[2];
  86.             metalarmor[0] = (metalclips >= 2) ? new ItemStack(Material.IRON_BOOTS, 1) : oldarmor[0];
  87.             metalarmor[1] = (metalclips >= 3) ? new ItemStack(Material.IRON_LEGGINGS, 1) : oldarmor[1];
  88.             metalarmor[3] = (metalclips >= 4) ? new ItemStack(Material.IRON_HELMET, 1) : oldarmor[3];
  89.            
  90.             target.getInventory().setArmorContents(metalarmor);
  91.         }
  92.        
  93.         else
  94.         {
  95.             if(oldarmor == null)
  96.                 oldarmor = target.getEquipment().getArmorContents();
  97.            
  98.             ItemStack[] metalarmor = new ItemStack[4];
  99.  
  100.             metalarmor[2] = (metalclips >= 1) ? new ItemStack(Material.IRON_CHESTPLATE, 1) : oldarmor[2];
  101.             metalarmor[0] = (metalclips >= 2) ? new ItemStack(Material.IRON_BOOTS, 1) : oldarmor[0];
  102.             metalarmor[1] = (metalclips >= 3) ? new ItemStack(Material.IRON_LEGGINGS, 1) : oldarmor[1];
  103.             metalarmor[3] = (metalclips >= 4) ? new ItemStack(Material.IRON_HELMET, 1) : oldarmor[3];
  104.            
  105.             target.getEquipment().setArmorContents(metalarmor);        
  106.         }
  107.  
  108.         if(metalclips == 4) time = System.currentTimeMillis();
  109.         startTime = System.currentTimeMillis();
  110.         isBeingWorn = true;
  111.     }
  112.    
  113.     public void resetArmor()
  114.     {
  115.         if(target == null || oldarmor == null)
  116.             return;
  117.        
  118.         if(target instanceof Player)
  119.             ((Player) target).getInventory().setArmorContents(oldarmor);
  120.         else
  121.             target.getEquipment().setArmorContents(oldarmor);
  122.        
  123.         player.getWorld().dropItem(target.getLocation(), new ItemStack(Material.IRON_INGOT, metalclips));
  124.        
  125.         isBeingWorn = false;
  126.     }
  127.    
  128.     public void control()
  129.     {
  130.         isControlling = true;
  131.     }
  132.    
  133.     public boolean controlling()
  134.     {
  135.         return isControlling;
  136.     }
  137.    
  138.     public void launch()
  139.     {
  140.         if(!canThrow)
  141.             return;
  142.        
  143.         Location location = player.getLocation();
  144.         double dx, dy, dz;
  145.         Location target = this.target.getLocation().clone();
  146.         dx = target.getX() - location.getX();
  147.         dy = target.getY() - location.getY();
  148.         dz = target.getZ() - location.getZ();
  149.         Vector vector = new Vector(dx, dy, dz);
  150.         vector.normalize();
  151.         this.target.setVelocity(vector.multiply(2));
  152.         remove();
  153.     }
  154.    
  155.     public void progress()
  156.     {
  157.         if(!player.isOnline() || player.isDead())
  158.         {
  159.             remove();
  160.             return;
  161.         }
  162.        
  163.         if(target != null)
  164.         {
  165.             if((target instanceof Player && !((Player) target).isOnline()) || target.isDead())
  166.             {
  167.                 remove();
  168.                 return;
  169.             }
  170.         }
  171.        
  172.         if(!player.isSneaking())
  173.         {
  174.             isControlling = false;
  175.         }
  176.        
  177.        
  178.         if(isBeingWorn && System.currentTimeMillis() > startTime + armorTime)
  179.         {
  180.             remove();
  181.             return;
  182.         }
  183.        
  184.         if(isControlling && player.isSneaking())
  185.         {
  186.             if(metalclips >= 1)
  187.             {
  188.                 Location oldLocation = target.getLocation();
  189.                 Location loc = Methods.getTargetedLocation(player,
  190.                         (int) player.getLocation().distance(oldLocation));
  191.                 double distance = loc.distance(oldLocation);
  192.                
  193.                 Vector v = Methods.getDirection(target.getLocation(), player.getLocation());
  194.                
  195.                 if(distance > 2.5)
  196.                     target.setVelocity(v.normalize().multiply(0.1));
  197.                
  198.                 Methods.breakBreathbendingHold(target);
  199.             }
  200.            
  201.             if(metalclips >= 2)
  202.             {
  203.                 Location oldLocation = target.getLocation();
  204.                 Location loc = Methods.getTargetedLocation(player,
  205.                         (int) player.getLocation().distance(oldLocation));
  206.                 double distance = loc.distance(oldLocation);
  207.                
  208.                 Vector v = Methods.getDirection(target.getLocation(), player.getLocation());
  209.                
  210.                 if(distance > 2.5)
  211.                     target.setVelocity(v.normalize().multiply(0.2));
  212.                
  213.                 Methods.breakBreathbendingHold(target);
  214.             }
  215.            
  216.             if(metalclips >= 3)
  217.             {
  218.                 Location oldLocation = target.getLocation();
  219.                 Location loc = Methods.getTargetedLocation(player,
  220.                         (int) player.getLocation().distance(oldLocation));
  221.                 double distance = loc.distance(oldLocation);
  222.                 double dx = loc.getX() - oldLocation.getX();
  223.                 double dy = loc.getY() - oldLocation.getY();
  224.                 double dz = loc.getZ() - oldLocation.getZ();
  225.                
  226.                 Vector v = new Vector(dx, dy, dz);
  227.                 if(distance > .5)
  228.                     target.setVelocity(v.normalize().multiply(.5));
  229.                 else
  230.                     target.setVelocity(new Vector(0, 0, 0));
  231.                
  232.                 target.setFallDistance(0);
  233.                 Methods.breakBreathbendingHold(target);
  234.             }
  235.            
  236.             if(metalclips == 4)
  237.             {
  238.                 if(System.currentTimeMillis() > time + crushInterval)
  239.                 {
  240.                     time = System.currentTimeMillis();
  241.                     Methods.damageEntity(player, target, 1);
  242.                 }
  243.             }
  244.         }
  245.        
  246.         for(Item ii : trackedIngots)
  247.         {
  248.             if(ii.getItemStack().getType() == Material.IRON_INGOT)
  249.             {
  250.                 if(true)
  251.                 {
  252.                     for(Entity e : Methods.getEntitiesAroundPoint(ii.getLocation(), 2))
  253.                     {
  254.                         if(e instanceof LivingEntity && e.getEntityId() != player.getEntityId())
  255.                         {
  256.                             Methods.damageEntity(player, e, 0.5);
  257.                            
  258.                             if(e instanceof Player ||
  259.                                     e instanceof Zombie ||
  260.                                     e instanceof Skeleton)
  261.                             {
  262.                                 if(target == null)
  263.                                     target =  (LivingEntity) e;
  264.                                
  265.                                 formArmor();
  266.                             }
  267.                            
  268.                             else
  269.                             {
  270.                                 ii.getWorld().dropItem(ii.getLocation(), ii.getItemStack());
  271.                                 remove();
  272.                             }
  273.  
  274.                             ii.remove();
  275.                         }
  276.                     }
  277.                 }
  278.             }
  279.         }
  280.        
  281.         removeDeadIngots();
  282.     }
  283.    
  284.     public void removeDeadIngots()
  285.     {
  286.         for(int i = 0; i < trackedIngots.size(); i++)
  287.         {
  288.             Item ii = trackedIngots.get(i);
  289.             if(ii.isDead())
  290.             {
  291.                 trackedIngots.remove(ii);
  292.             }
  293.         }
  294.     }
  295.    
  296.     public void remove()
  297.     {
  298.         for(Item i : trackedIngots)
  299.         {
  300.             i.remove();
  301.         }
  302.         resetArmor();
  303.         trackedIngots.clear();
  304.         instances.remove(player);
  305.     }
  306.    
  307.     public static void removeAll()
  308.     {
  309.         for(Player p : instances.keySet())
  310.         {
  311.             instances.get(p).remove();
  312.         }
  313.     }
  314.    
  315.     public static void progressAll()
  316.     {
  317.         for(Player p : instances.keySet())
  318.         {
  319.             instances.get(p).progress();
  320.         }
  321.     }
  322. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement