[SOLVED]Best way to set Armour and Item Durability to infinite?

Discussion in 'Plugin Development' started by CRAZYxMUNK3Y, Jun 13, 2012.

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

    CRAZYxMUNK3Y

    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.
     
  2. 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.
     
  3. Offline

    CRAZYxMUNK3Y

    kumpelblase2
    Sorry about the late reply, been busy...

    For EntityDamageByEntity i have this;
    Code:java
    1.  
    2. public void entityDamageEntity(EntityDamageByEntityEvent e){
    3. Entity p = e.getEntity();
    4. PlayerInventory i = ((HumanEntity) p).getInventory();
    5. ItemStack[] ac = i.getArmorContents();
    6. }
    7.  


    How would i go about from there checking the type of armour the player has, then setting durability?

    Thanks
     
  4. 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.
     
  5. Offline

    CRAZYxMUNK3Y

    I adjusted it to what i thought would be correct, but it doesn't seem to work... Here is the code;

    Code:java
    1.  
    2. public void entityDamageEntity(EntityDamageByEntityEvent e){
    3. Entity p = e.getEntity();
    4. PlayerInventory i = ((HumanEntity) p).getInventory();
    5. for(ItemStack ac : i.getArmorContents()){
    6. if(ac == diaArmour(ac)){
    7. ac.setDurability((short)-1);
    8. }
    9. }
    10. }
    11.  
    12.  
    13. public ItemStack diaArmour(ItemStack pac){
    14. ArrayList<Material> items = new ArrayList<Material>();
    15. items.add(Material.DIAMOND_BOOTS);
    16. items.add(Material.DIAMOND_CHESTPLATE);
    17. items.add(Material.DIAMOND_HELMET);
    18. items.add(Material.DIAMOND_LEGGINGS);
    19. return pac;
    20. }
    21.  


    That is the only way i could think of to try get it to work :S
     
  6. Offline

    r0306

    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);
    }
     
  7. Offline

    CRAZYxMUNK3Y

    r0306
    It didn't work for some reason...
     
  8. Offline

    r0306

    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());
    }
     
  9. Offline

    CRAZYxMUNK3Y

    Still nothing :(

    Are there any other ways of doing it that i could try?
     
  10. Offline

    r0306

    CRAZYxMUNK3Y
    You got the victim instead of the damager.
    Code:
    Entity p = e.getDamager();
     
  11. Offline

    CRAZYxMUNK3Y

    r0306
    Still nothing :S
     
  12. 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);
     
  13. Offline

    r0306

    Did you register the listener and implement Listener in the class? You also need to add the @EventHandler annotation.
     
  14. Offline

    CRAZYxMUNK3Y

    r0306
    Yes, full code if you want
    Digi
    Code (open)

    Code:java
    1.  
    2. package me.crazy.dnb;
    3.  
    4. import java.util.ArrayList;
    5.  
    6. import org.bukkit.Bukkit;
    7. import org.bukkit.Material;
    8. import org.bukkit.entity.Entity;
    9. import org.bukkit.entity.HumanEntity;
    10. import org.bukkit.entity.Player;
    11. import org.bukkit.event.EventHandler;
    12. import org.bukkit.event.Listener;
    13. import org.bukkit.event.entity.EntityDamageByEntityEvent;
    14. import org.bukkit.event.player.PlayerInteractEvent;
    15. import org.bukkit.inventory.ItemStack;
    16. import org.bukkit.inventory.PlayerInventory;
    17. import org.bukkit.plugin.java.JavaPlugin;
    18.  
    19. public class DiamondNeverBreaks extends JavaPlugin implements Listener {
    20.  
    21. public void onEnable() {
    22. Bukkit.getServer().getPluginManager().registerEvents(this, this);
    23. Bukkit.getServer().getPluginManager().registerEvents(this, this);
    24. }
    25.  
    26. public void onDisable() {
    27.  
    28. }
    29.  
    30. @EventHandler
    31. public void playerInteract(PlayerInteractEvent e) {
    32. Player player = e.getPlayer();
    33. Material i = player.getItemInHand().getType();
    34.  
    35. if (i == Material.DIAMOND_AXE || i == Material.DIAMOND_HOE
    36. || i == Material.DIAMOND_PICKAXE || i == Material.DIAMOND_SPADE
    37. || i == Material.DIAMOND_SWORD || i == Material.DIAMOND_BOOTS
    38. || i == Material.DIAMOND_CHESTPLATE
    39. || i == Material.DIAMOND_HELMET
    40. || i == Material.DIAMOND_LEGGINGS) {
    41. player.getItemInHand().setDurability((short) 0);
    42. player.sendMessage("Test");
    43. }
    44. }
    45.  
    46. /*
    47.   * @EventHandler public void entityDamageEntity(EntityDamageByEntityEvent e)
    48.   * { Entity p = e.getDamager(); PlayerInventory i = ((HumanEntity)
    49.   * p).getInventory(); for (@SuppressWarnings("unused") ItemStack ac :
    50.   * i.getArmorContents()) { setItemAsNew(i.getHelmet(),
    51.   * Material.DIAMOND_HELMET); setItemAsNew(i.getBoots(),
    52.   * Material.DIAMOND_BOOTS); setItemAsNew(i.getChestplate(),
    53.   * Material.DIAMOND_CHESTPLATE); setItemAsNew(i.getLeggings(),
    54.   * Material.DIAMOND_LEGGINGS); } }
    55.   *
    56.   * private void setItemAsNew(ItemStack item, Material type){ if(item != null
    57.   * && item.getType() == type) item.setDurability((short)0); }
    58.   */
    59.  
    60. @EventHandler
    61. public void entityDamageEntity(EntityDamageByEntityEvent e) {
    62. Entity p = e.getEntity();
    63. PlayerInventory i = ((HumanEntity) p).getInventory();
    64. for (ItemStack ac : i.getArmorContents()) {
    65. if (diaArmour(ac)) {
    66. ac.setDurability((short) 0);
    67. }
    68. }
    69. }
    70.  
    71. public boolean diaArmour(ItemStack pac) {
    72. ArrayList<Material> items = new ArrayList<Material>();
    73. items.add(Material.DIAMOND_BOOTS);
    74. items.add(Material.DIAMOND_CHESTPLATE);
    75. items.add(Material.DIAMOND_HELMET);
    76. items.add(Material.DIAMOND_LEGGINGS);
    77. return items.contains(pac.getType());
    78. }
    79. }
    80.  



    Comment is because i have tried both and neither worked...
     
  15. Offline

    r0306

    CRAZYxMUNK3Y
    Wait. Does the interact event work? Or is it both interact and damage that aren't working?
     
  16. Offline

    CRAZYxMUNK3Y

    Both don't seem to be...
     
  17. 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.
     
  18. Offline

    CRAZYxMUNK3Y

    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.
     
  19. Offline

    r0306

    CRAZYxMUNK3Y
    Try printing out messages to the console inside your event handlers to make sure that they are working correctly.
     
  20. Offline

    CRAZYxMUNK3Y

    Tried to send message to player and console... Nothing, on either(By you or digi's code)
     
  21. Offline

    r0306

    CRAZYxMUNK3Y
    Add the @Override annotation to your onEnable and onDisable methods.
     
  22. Offline

    CRAZYxMUNK3Y

  23. 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...
     
  24. Offline

    r0306

    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.
     
  25. Well, CRAZYxMUNK3Y, remove all other plugins... but why do you have 2 registerEvents() for the same class (this) ?
     
  26. Offline

    CRAZYxMUNK3Y

    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...
     
  27. Offline

    r0306

    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.
     
    CRAZYxMUNK3Y likes this.
  28. Offline

    CRAZYxMUNK3Y

    r0306
    I feel so stupid -_-
    Thanks
     
  29. Offline

    r0306

  30. 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 !
     
Thread Status:
Not open for further replies.

Share This Page