What would be the best way to set an items durability to infinite, so once they break a block, the item will always be at full durability? Would it be best to use setDurability((Short) -1), or setDurability(100), or is there another way? Also, would you use the PlayerInteractEvent for all actions used with the items(Eg, pick to be used on blocks, players and mobs)? Sorry if it doesn't make much sense, and thanks in advanced.
You probably need to reset the durability back to 0 to not let it take damage. And I would not use PlayerInteractEvent for all actions. BlockBreak, EntityDamageByEntity would I use. Reason to not use InteractEvent: it gets fired even when the item doesn't take any damage. E.g. clicking a block, using sword to shield yourself.
@kumpelblase2 Sorry about the late reply, been busy... For EntityDamageByEntity i have this; Code:java public void entityDamageEntity(EntityDamageByEntityEvent e){ Entity p = e.getEntity(); PlayerInventory i = ((HumanEntity) p).getInventory(); ItemStack[] ac = i.getArmorContents(); } How would i go about from there checking the type of armour the player has, then setting durability? Thanks
The getArmorContents() is an array, loop through it, check each item against null and set it's durability to 0... something like: Code: for(ItemStack item : inventory.getArmorContents()) { if(item != null) item.setDurability((short)0); } (example code, correct it as required) You should still hook PlayerInteractEvent for items that are damaged by using rightclick.... hoe, flint & steel, fishing rod, etc.
I adjusted it to what i thought would be correct, but it doesn't seem to work... Here is the code; Code:java public void entityDamageEntity(EntityDamageByEntityEvent e){ Entity p = e.getEntity(); PlayerInventory i = ((HumanEntity) p).getInventory(); for(ItemStack ac : i.getArmorContents()){ if(ac == diaArmour(ac)){ ac.setDurability((short)-1); } } } public ItemStack diaArmour(ItemStack pac){ ArrayList<Material> items = new ArrayList<Material>(); items.add(Material.DIAMOND_BOOTS); items.add(Material.DIAMOND_CHESTPLATE); items.add(Material.DIAMOND_HELMET); items.add(Material.DIAMOND_LEGGINGS); return pac; } That is the only way i could think of to try get it to work :S
@CRAZYxMUNK3Y Some parts of your code are off a bit. Use this instead. Code: public void entityDamageEntity(EntityDamageByEntityEvent e){ Entity p = e.getEntity(); PlayerInventory i = ((HumanEntity) p).getInventory(); for(ItemStack ac : i.getArmorContents()){ if(diaArmour(ac)){ ac.setDurability((short) 0); } } } public boolean diaArmour(ItemStack pac){ ArrayList<Material> items = new ArrayList<Material>(); items.add(Material.DIAMOND_BOOTS); items.add(Material.DIAMOND_CHESTPLATE); items.add(Material.DIAMOND_HELMET); items.add(Material.DIAMOND_LEGGINGS); return items.contains(pac); }
@CRAZYxMUNK3Y Oh. Sorry. Forgot to change the ItemStack to a Material. Code: public boolean diaArmour(ItemStack pac){ ArrayList<Material> items = new ArrayList<Material>(); items.add(Material.DIAMOND_BOOTS); items.add(Material.DIAMOND_CHESTPLATE); items.add(Material.DIAMOND_HELMET); items.add(Material.DIAMOND_LEGGINGS); return items.contains(pac.getType()); }
@CRAZYxMUNK3Y You need both the attacker and victim, set the attacker's sword to 0 and victim's armor to 0. If you want to do it for specific armor types you're doing it wrong... and you should set it to 0 not -1. You first need to check if the player HAS an armor, if he doesn't, the item is null. Then, you can check individual inventory.getHelmet()/getChestplate()/etc... against your materials.... Code: private void setItemAsNew(ItemStack item, Material type) { if(item != null && item.getType() == type) item.setDurability((short)0); } // ... setItemAsNew(inventory.getHelmet(), Material.DIAMOND_HELMET); setItemAsNew(inventory.getChestplate(), Material.DIAMOND_CHESTPLATE); // etc... setItemAsNew(swordItem, Material.DIAMOND_SWORD);
Did you register the listener and implement Listener in the class? You also need to add the @EventHandler annotation.
@r0306 Yes, full code if you want @Digi Code:java package me.crazy.dnb; import java.util.ArrayList; import org.bukkit.Bukkit;import org.bukkit.Material;import org.bukkit.entity.Entity;import org.bukkit.entity.HumanEntity;import org.bukkit.entity.Player;import org.bukkit.event.EventHandler;import org.bukkit.event.Listener;import org.bukkit.event.entity.EntityDamageByEntityEvent;import org.bukkit.event.player.PlayerInteractEvent;import org.bukkit.inventory.ItemStack;import org.bukkit.inventory.PlayerInventory;import org.bukkit.plugin.java.JavaPlugin; public class DiamondNeverBreaks extends JavaPlugin implements Listener { public void onEnable() { Bukkit.getServer().getPluginManager().registerEvents(this, this); Bukkit.getServer().getPluginManager().registerEvents(this, this); } public void onDisable() { } @EventHandler public void playerInteract(PlayerInteractEvent e) { Player player = e.getPlayer(); Material i = player.getItemInHand().getType(); if (i == Material.DIAMOND_AXE || i == Material.DIAMOND_HOE || i == Material.DIAMOND_PICKAXE || i == Material.DIAMOND_SPADE || i == Material.DIAMOND_SWORD || i == Material.DIAMOND_BOOTS || i == Material.DIAMOND_CHESTPLATE || i == Material.DIAMOND_HELMET || i == Material.DIAMOND_LEGGINGS) { player.getItemInHand().setDurability((short) 0); player.sendMessage("Test"); } } /* * @EventHandler public void entityDamageEntity(EntityDamageByEntityEvent e) * { Entity p = e.getDamager(); PlayerInventory i = ((HumanEntity) * p).getInventory(); for (@SuppressWarnings("unused") ItemStack ac : * i.getArmorContents()) { setItemAsNew(i.getHelmet(), * Material.DIAMOND_HELMET); setItemAsNew(i.getBoots(), * Material.DIAMOND_BOOTS); setItemAsNew(i.getChestplate(), * Material.DIAMOND_CHESTPLATE); setItemAsNew(i.getLeggings(), * Material.DIAMOND_LEGGINGS); } } * * private void setItemAsNew(ItemStack item, Material type){ if(item != null * && item.getType() == type) item.setDurability((short)0); } */ @EventHandler public void entityDamageEntity(EntityDamageByEntityEvent e) { Entity p = e.getEntity(); PlayerInventory i = ((HumanEntity) p).getInventory(); for (ItemStack ac : i.getArmorContents()) { if (diaArmour(ac)) { ac.setDurability((short) 0); } } } public boolean diaArmour(ItemStack pac) { ArrayList<Material> items = new ArrayList<Material>(); items.add(Material.DIAMOND_BOOTS); items.add(Material.DIAMOND_CHESTPLATE); items.add(Material.DIAMOND_HELMET); items.add(Material.DIAMOND_LEGGINGS); return items.contains(pac.getType()); }} Comment is because i have tried both and neither worked...
@CRAZYxMUNK3Y Wait. Does the interact event work? Or is it both interact and damage that aren't working?
Why are you checking if player has armor item in hand ? AFAIK it does not get damaged if you hit stuff while holding armor in hand... Also, your damage armor code is the same as before... don't tag me if you're just going to ignore my posts.
I did that as a test as it wasn't working... It isn't really the same, i did test the way you suggested, but it didn't work either, so i commented it and went back to the other for some more tests. I did that so it is easier to swap and change between the two. Lie 46-58 is your code that i have tested.
@CRAZYxMUNK3Y Try printing out messages to the console inside your event handlers to make sure that they are working correctly.
@CRAZYxMUNK3Y It seems nothing works for you because you're doing most of stuff wrong because you don't understand them, like my code, you were using it inside that armor loop... without understanding what the loop does and what the method does Also, saying "it does not work" doesn't really help much... if you get errors, post'em, if it doesn't do anything, make it print messages with common variables you're using to see if you really get what you expect...
Still, nothing showed up even when he printed messages to console inside the handlers. Assuming that he did it right, that would mean that the listeners weren't registered properly or another plugin was cancelling them.
Well, @CRAZYxMUNK3Y, remove all other plugins... but why do you have 2 registerEvents() for the same class (this) ?
Sorry, i've been really busy so i was trying to keep it short I removed all the plugins, and now it does work (Derp!), will figure out which one it is later. I though you needed to register each event, no just one register per class. It seems to be working now, but i have one small issue.. When using this code, i get this stack trace/spam... I will understand if you don't want to help anymore, but thanks in advanced if you do...
@CRAZYxMUNK3Y That's because you're casting to player without checking whether or not the entity is acutally a player. Code: public void entityDamageEntity(EntityDamageByEntityEvent e){ Entity p = e.getEntity(); if (p instanceof Player) { PlayerInventory i = ((HumanEntity) p).getInventory(); for(ItemStack ac : i.getArmorContents()){ if(diaArmour(ac)){ ac.setDurability((short) 0); } } } } Also, if you have the Heroes plugin, I had some issues with it regarding entity damage as it seems to cancel some events.
You *could* listen to the event in the LOW or LOWEST priority, that might get before any other plugin cancels it... but I don't know why they would if you see it damaging people... But AGAIN, why are you freaking looping my example ??? Code: for (@SuppressWarnings("unused") ItemStack ac : i.getArmorContents()) { It's totally pointless... and as you see you also get a warning which you suppressed, because you're not using that variable anyway... pointless !