EntityDamageByEntityEvent Help

Discussion in 'Plugin Development' started by Slideroller, Nov 17, 2013.

Thread Status:
Not open for further replies.
  1. Offline

    Slideroller

    So I recently started coding for one of my friends then he asked me to code him a quite simple plugin so I accepted so I've done most of the code but then he asked to do this other part I've searched arround bukkit and I couldn't find the answer hopefully you guys can help me.

    Problem:

    How to add Poison effect to a block (lime wool block) then when a player hit other player they recieve that poison for in this case I want it 3 seconds long. (It is possible some people say it isn't but it is)

    Code:
    Code:java
    1. package me.slideroller.WoolWars;
    2.  
    3. import org.bukkit.Bukkit;
    4. import org.bukkit.ChatColor;
    5. import org.bukkit.Color;
    6. import org.bukkit.DyeColor;
    7. import org.bukkit.Material;
    8. import org.bukkit.command.Command;
    9. import org.bukkit.command.CommandSender;
    10. import org.bukkit.enchantments.Enchantment;
    11. import org.bukkit.entity.Player;
    12. import org.bukkit.inventory.ItemStack;
    13. import org.bukkit.inventory.PlayerInventory;
    14. import org.bukkit.inventory.meta.ItemMeta;
    15. import org.bukkit.inventory.meta.LeatherArmorMeta;
    16. import org.bukkit.plugin.java.JavaPlugin;
    17.  
    18. public class WoolWars extends JavaPlugin {
    19.  
    20. public void onEnable() {
    21. }
    22.  
    23. public void onDisable() {
    24.  
    25. }
    26.  
    27. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
    28. if (!(sender instanceof Player)) {
    29. sender.sendMessage(ChatColor.RED + "Sorry but only players can use commands from WoolWars!");
    30. return true;
    31. }
    32.  
    33. final Player p = (Player) sender;
    34. PlayerInventory pi = p.getInventory();
    35.  
    36. if (cmd.getName().equalsIgnoreCase("greenwool")) {
    37. ItemStack LimeWool = new ItemStack(Material.WOOL, 1, DyeColor.LIME.getData());
    38. ItemMeta meta = LimeWool.getItemMeta();
    39. meta.setDisplayName(ChatColor.GREEN + "GreenyWool");
    40. LimeWool.setItemMeta (meta);
    41. LimeWool.addUnsafeEnchantment(Enchantment.KNOCKBACK, 2);
    42. pi.addItem(new ItemStack[] { LimeWool });
    43.  
    44. ItemStack Chestplate = new ItemStack(Material.LEATHER_CHESTPLATE, 1);
    45. ItemMeta meta1 = Chestplate.getItemMeta();
    46. meta1.setDisplayName(ChatColor.GREEN + "Chestplate");
    47. Chestplate.setItemMeta (meta1);
    48. Chestplate.addUnsafeEnchantment(Enchantment.DURABILITY, 1);
    49. LeatherArmorMeta lam = (LeatherArmorMeta)Chestplate.getItemMeta();
    50. lam.setColor(Color.GREEN);
    51. Chestplate.setItemMeta(lam);
    52. p.getInventory().setChestplate(Chestplate);
    53.  
    54. ItemStack pants = new ItemStack(Material.LEATHER_LEGGINGS, 1);
    55. LeatherArmorMeta pant = (LeatherArmorMeta)pants.getItemMeta();
    56. Chestplate.addUnsafeEnchantment(Enchantment.DURABILITY, 1);
    57. pant.setColor(Color.GREEN);
    58. pants.setItemMeta(lam);
    59. p.getInventory().setLeggings(pants);
    60.  
    61. ItemStack boot = new ItemStack(Material.LEATHER_BOOTS, 1);
    62. Chestplate.addUnsafeEnchantment(Enchantment.DURABILITY, 1);
    63. pant.setColor(Color.GREEN);
    64. boot.setItemMeta(lam);
    65. p.getInventory().setBoots(boot);
    66. p.sendMessage(ChatColor.GOLD + "You have been given the GreenWoolKit!");
    67. return true;
    68. }
    69. if (cmd.getName().equalsIgnoreCase("sunwool")) {
    70. ItemStack SunWool = new ItemStack(Material.WOOL, 1, DyeColor.YELLOW.getData());
    71. ItemMeta meta = SunWool.getItemMeta();
    72. meta.setDisplayName(ChatColor.YELLOW + "YellowyWool");
    73. SunWool.setItemMeta (meta);
    74. SunWool.addUnsafeEnchantment(Enchantment.FIRE_ASPECT, 1);
    75. pi.addItem(new ItemStack[] { SunWool });
    76.  
    77. ItemStack Chestplate = new ItemStack(Material.LEATHER_CHESTPLATE, 1);
    78. ItemMeta meta1 = Chestplate.getItemMeta();
    79. meta1.setDisplayName(ChatColor.YELLOW + "Chestplate");
    80. Chestplate.setItemMeta (meta1);
    81. Chestplate.addUnsafeEnchantment(Enchantment.PROTECTION_ENVIRONMENTAL, 1);
    82. Chestplate.addUnsafeEnchantment(Enchantment.DURABILITY, 2);
    83. LeatherArmorMeta lam = (LeatherArmorMeta)Chestplate.getItemMeta();
    84. lam.setColor(Color.YELLOW);
    85. Chestplate.setItemMeta(lam);
    86. p.getInventory().setChestplate(Chestplate);
    87.  
    88. ItemStack pants = new ItemStack(Material.LEATHER_LEGGINGS, 1);
    89. LeatherArmorMeta pant = (LeatherArmorMeta)pants.getItemMeta();
    90. Chestplate.addUnsafeEnchantment(Enchantment.PROTECTION_ENVIRONMENTAL, 1);
    91. Chestplate.addUnsafeEnchantment(Enchantment.DURABILITY, 2);
    92. pant.setColor(Color.YELLOW);
    93. pants.setItemMeta(lam);
    94. p.getInventory().setLeggings(pants);
    95.  
    96. ItemStack boot = new ItemStack(Material.LEATHER_BOOTS, 1);
    97. Chestplate.addUnsafeEnchantment(Enchantment.PROTECTION_ENVIRONMENTAL, 1);
    98. Chestplate.addUnsafeEnchantment(Enchantment.DURABILITY, 2);
    99. pant.setColor(Color.YELLOW);
    100. boot.setItemMeta(lam);
    101. p.getInventory().setBoots(boot);
    102. p.sendMessage(ChatColor.GOLD + "You have been given the SunWoolKit!");
    103. return true;
    104. }
    105.  
    106. return false;
    107. }
    108. }


    - Slider
     
  2. Offline

    krazytraynz

    Just check if the Player gets hit by a different Player holding lime wool with the name "GreenyWool" in EntityDamageByEntityEvent, and add a poison potion effect for 3 seconds.
     
  3. Offline

    Slideroller

    Could you please inject it to my code? If you have the time.
     
  4. Offline

    Slideroller

  5. Offline

    drtshock Retired Staff

    Don't ask for people to write code for you. If you get stuck with code that you're writing then I'm sure someone will be more than happy to help :)
     
    chaseoes and JPG2000 like this.
  6. Offline

    XvBaseballkidvX

    Possibly something like this?

    Code:java
    1. @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
    2. public void onPoisonDaggerHit(EntityDamageByEntityEvent event){
    3. if(event.getDamager() instanceof Player && event.getEntity() instanceof Player){
    4. Player attacker = (Player) event.getDamager();
    5. Player damaged = (Player) event.getEntity();
    6. if(attacker.getItemInHand().getItemMeta() == null) return;
    7. if(attacker.getItemInHand().getItemMeta().equals(YourLimeWoolItemMeta.getItemMeta())){
    8. if(event.getDamage() <= 0){
    9. return;
    10. }else{
    11. PotionEffect effect = new PotionEffect(PotionEffectType.POISON, 2*20, 0);
    12. damaged.addPotionEffect(effect);
    13. }
    14. }
    15. }
    16. }


    Hope this helps!
     
  7. Offline

    agokhale1

    Slideroller
    There is the full code of your plugin with the event in it:
    Code:java
    1. package me.sideroller.WoolWars;
    2.  
    3. import org.bukkit.*;
    4. import org.bukkit.command.*;
    5. import org.bukkit.enchantments.*;
    6. import org.bukkit.entity.*;
    7. import org.bukkit.event.*;
    8. import org.bukkit.event.entity.*;
    9. import org.bukkit.inventory.*;
    10. import org.bukkit.inventory.meta.*;
    11. import org.bukkit.plugin.java.*;
    12. import org.bukkit.potion.PotionEffect;
    13. import org.bukkit.potion.PotionEffectType;
    14.  
    15. public class WoolWars extends JavaPlugin implements Listener {
    16.  
    17. public void onEnable() {
    18.  
    19. getServer().getPluginManager().registerEvents(this, this);
    20.  
    21. }
    22.  
    23. public void onDisable() {
    24.  
    25. }
    26.  
    27. @SuppressWarnings("deprecation")
    28. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
    29.  
    30. if (!(sender instanceof Player)) {
    31.  
    32. sender.sendMessage(ChatColor.RED + "Sorry but only players can use commands from WoolWars!");
    33. return true;
    34.  
    35. }
    36.  
    37. final Player p = (Player) sender;
    38. PlayerInventory pi = p.getInventory();
    39.  
    40. if (commandLabel.equalsIgnoreCase("greenwool")) {
    41.  
    42. ItemStack LimeWool = new ItemStack(Material.WOOL, 1, DyeColor.LIME.getData());
    43. ItemMeta meta = LimeWool.getItemMeta();
    44. meta.setDisplayName(ChatColor.GREEN + "GreenyWool");
    45. LimeWool.setItemMeta (meta);
    46. LimeWool.addUnsafeEnchantment(Enchantment.KNOCKBACK, 2);
    47. pi.addItem(new ItemStack[] { LimeWool });
    48.  
    49. ItemStack Chestplate = new ItemStack(Material.LEATHER_CHESTPLATE, 1);
    50. ItemMeta meta1 = Chestplate.getItemMeta();
    51. meta1.setDisplayName(ChatColor.GREEN + "Chestplate");
    52. Chestplate.setItemMeta (meta1);
    53. Chestplate.addUnsafeEnchantment(Enchantment.DURABILITY, 1);
    54. LeatherArmorMeta lam = (LeatherArmorMeta)Chestplate.getItemMeta();
    55. lam.setColor(Color.GREEN);
    56. Chestplate.setItemMeta(lam);
    57. pi.setChestplate(Chestplate);
    58.  
    59. ItemStack pants = new ItemStack(Material.LEATHER_LEGGINGS, 1);
    60. LeatherArmorMeta pant = (LeatherArmorMeta)pants.getItemMeta();
    61. Chestplate.addUnsafeEnchantment(Enchantment.DURABILITY, 1);
    62. pant.setColor(Color.GREEN);
    63. pants.setItemMeta(lam);
    64. pi.setLeggings(pants);
    65.  
    66. ItemStack boot = new ItemStack(Material.LEATHER_BOOTS, 1);
    67. Chestplate.addUnsafeEnchantment(Enchantment.DURABILITY, 1);
    68. pant.setColor(Color.GREEN);
    69. boot.setItemMeta(lam);
    70. pi.setBoots(boot);
    71.  
    72. p.sendMessage(ChatColor.GOLD + "You have been given the GreenWoolKit!");
    73. return true;
    74.  
    75. }
    76.  
    77. if (commandLabel.equalsIgnoreCase("sunwool")) {
    78.  
    79. ItemStack SunWool = new ItemStack(Material.WOOL, 1, DyeColor.YELLOW.getData());
    80. ItemMeta meta = SunWool.getItemMeta();
    81. meta.setDisplayName(ChatColor.YELLOW + "YellowyWool");
    82. SunWool.setItemMeta (meta);
    83. SunWool.addUnsafeEnchantment(Enchantment.FIRE_ASPECT, 1);
    84. pi.addItem(new ItemStack[] { SunWool });
    85.  
    86. ItemStack Chestplate = new ItemStack(Material.LEATHER_CHESTPLATE, 1);
    87. ItemMeta meta1 = Chestplate.getItemMeta();
    88. meta1.setDisplayName(ChatColor.YELLOW + "Chestplate");
    89. Chestplate.setItemMeta (meta1);
    90. Chestplate.addEnchantment(Enchantment.PROTECTION_ENVIRONMENTAL, 1);
    91. Chestplate.addEnchantment(Enchantment.DURABILITY, 2);
    92. LeatherArmorMeta lam = (LeatherArmorMeta)Chestplate.getItemMeta();
    93. lam.setColor(Color.YELLOW);
    94. Chestplate.setItemMeta(lam);
    95. pi.setChestplate(Chestplate);
    96.  
    97. ItemStack pants = new ItemStack(Material.LEATHER_LEGGINGS, 1);
    98. LeatherArmorMeta pant = (LeatherArmorMeta)pants.getItemMeta();
    99. Chestplate.addEnchantment(Enchantment.PROTECTION_ENVIRONMENTAL, 1);
    100. Chestplate.addEnchantment(Enchantment.DURABILITY, 2);
    101. pant.setColor(Color.YELLOW);
    102. pants.setItemMeta(lam);
    103. pi.setLeggings(pants);
    104.  
    105. ItemStack boot = new ItemStack(Material.LEATHER_BOOTS, 1);
    106. Chestplate.addEnchantment(Enchantment.PROTECTION_ENVIRONMENTAL, 1);
    107. Chestplate.addEnchantment(Enchantment.DURABILITY, 2);
    108. pant.setColor(Color.YELLOW);
    109. boot.setItemMeta(lam);
    110. pi.setBoots(boot);
    111. p.sendMessage(ChatColor.GOLD + "You have been given the SunWoolKit!");
    112. return true;
    113.  
    114. }
    115. return false;
    116. }
    117.  
    118. @SuppressWarnings("deprecation")
    119. @EventHandler
    120. public void onEntityDamage(EntityDamageByEntityEvent e) {
    121.  
    122. Entity victim = e.getEntity();
    123. Entity killer = e.getDamager();
    124.  
    125. if(victim instanceof Player && killer instanceof Player) {
    126.  
    127. Player p = (Player) killer;
    128. Player p1 = (Player) victim;
    129.  
    130. ItemStack LimeWool = new ItemStack(Material.WOOL, 1, DyeColor.LIME.getData());
    131. ItemMeta meta = LimeWool.getItemMeta();
    132. meta.setDisplayName(ChatColor.GREEN + "GreenyWool");
    133. LimeWool.setItemMeta (meta);
    134. LimeWool.addUnsafeEnchantment(Enchantment.KNOCKBACK, 2);
    135.  
    136. if(p.getInventory().getItemInHand().equals(LimeWool)) {
    137.  
    138. p1.addPotionEffect(new PotionEffect(PotionEffectType.POISON, 60, 1));
    139.  
    140. }
    141.  
    142. } else {
    143.  
    144. return;
    145.  
    146. }
    147.  
    148. }
    149.  
    150. }
     
  8. Offline

    drtshock Retired Staff

  9. Offline

    JPG2000

    agokhale1 Next time he'll be back, this thread will be useless. He won't know how to use the code, because you gave it to him.

    You were being nice, that matters, but next time, maybe try to add comments and teach him.
     
  10. Offline

    agokhale1

    drtshock JPG2000
    I'm sorry lol, it's just that I had a strong urge to fix his code and after I did, I just posted it :/
     
  11. Offline

    JPG2000

    agokhale1 Haha I get that too. Its okay man :)
     
  12. Offline

    Slideroller

    Thanks
    I have tried it and kept struggling I looked at the bukkit docs,forums etc. and I was still stomped
    Thanks It helped I understand most of it now.

    Also your tutorials are very useful I use them most of the time or refer to them when needed

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 5, 2016
  13. Offline

    JPG2000

    Slideroller Awesome! In my last signature set up, I could only include about 6, now I can include them all.

    I get that a lot from people, and it means a lot to me! Its pretty much like a floating stack of answers for people :p
     
    Slideroller likes this.
  14. Offline

    Slideroller

    Your welcome and yeah I tried everything to try to see the other coding I was curious why it wasn't working but now I can see it :) keep up the great work.
     
  15. Offline

    JPG2000

  16. Offline

    Slideroller

    No Problem.
     
    JPG2000 likes this.
  17. Offline

    XvBaseballkidvX

    Sorry about that then :( I guess I was thinking that he would look at it, think about what the code means, and learn in that way (because that is the way that I learned, also youtube videos lol).
     
Thread Status:
Not open for further replies.

Share This Page