Infinite Durability On Items

Discussion in 'Plugin Development' started by AakashKcX, Sep 4, 2015.

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

    AakashKcX

    How would I get infinite Durability on all items?
     
  2. Offline

    bean710

    You would create a listener to look out for whenever an item takes damage, and then you would cancel the event.
     
  3. Offline

    boomboompower

    @AakashKcX Try this
    Code:
    @EventHandler
        public void onBreak(BlockBreakEvent e) {
            Player p = e.getPlayer();
            short dura = p.getItemInHand().getDurability();
            ItemStack hand = p.getItemInHand();
          
            dura.setDurability((short) (hand - 1));
    }
    
    This way every time you break a block, it adds 1 durability to the item you were holding. Meaning you would technically lose/gain none.
     
  4. Offline

    mine-care

    @boomboompower In these forums, most active members including myself, are against spoonfeeding because from experience we have noticed how it does NOT help people. Most of the time the provided code is missused.
    Afterall it is them coding, not you...

    When providing code to people, spend the time to explan what it is doing step by step to make it a tutorial instead of a spoonfeed.

    Also, your signature code will not work :p == comparason checks if two objects point to the same object, use .equals() or .equalsIgnoreCase() instead.
     
    FisheyLP and boomboompower like this.
  5. Offline

    AakashKcX

    @bean710 I've been trying to find out the name of the Listener but I cannot find it. If I knew I wouldn't be asking.

    @boomboompower I have disabled block breaking in my server as it is PvP Based. I'm looking for armour and swords to have infinite durability.

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

    caderape

    @AakashKcX Check #BlockDamageEvent and #EntityDamageByEntityEvent

    check if the player has an item in hand, and set the durability to 0
     
  7. Offline

    AakashKcX

    @caderape
    Code:java
    1. if (event.getEntity() instanceof ItemStack){
    2. ItemStack item = (ItemStack) event.getEntity();
    3. item.setDurability((short) 0);
    4. }

    I don't know why this doesn't work
     
  8. Offline

    caderape

    @AakashKcX Maybe cuz the entity return a player or a mob
     
  9. Offline

    AakashKcX

    @caderape It checks if the entity is a ItemStack
     
  10. Offline

    caderape

    @AakashKcX if you use the EntityDamageByEntityEvent or BlockBreakEvent, the entity never return an itemstack. Cast the entity as a player and check the item in his hand. And note, we check the damager for the first event
     
  11. Offline

    boomboompower

    Ok, thx for the info, so basically what I do is tell them to do things e.g (very basic example) "Add a line to make sure the sender is a player" and not show them code.

    Also thx for the info on the signature, just realised xD
     
    mine-care likes this.
  12. Why not just a scheduler instead of so many different events?
     
  13. Offline

    AakashKcX

    I've got it working. Its a bit buggy though.
    Code:java
    1. @EventHandler
    2. public void onPlayerIteract(PlayerInteractEvent interacter) {
    3. Player player = interacter.getPlayer();
    4. if (player.getItemInHand() != null){
    5. Bukkit.getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
    6. public void run() {
    7. player.getItemInHand().setDurability((short) -1);
    8. }
    9. }, 1L);
    10. }
    11. }
    12.  
    13. @EventHandler
    14. public void onTestEntityDamage(EntityDamageByEntityEvent event){
    15. if (event.getEntity() instanceof Player){
    16. Player player = (Player) event.getEntity();
    17. for (ItemStack item : player.getInventory().getArmorContents()){
    18. if(item != null){item.setDurability((short) -1);}
    19. }
    20. }
    21. }
     
  14. Offline

    stormneo7

    This'll hopefully help you understand what you're doing wrongly.

    This constructor will repair any item.

    Code:
         public void repairItems(ItemStack... items) {
            for (ItemStack item : items) {
    
                // If Item is Nothing or the Item is AIR, the event is broken.
                if (item == null || item.getType() == Material.AIR)
                    return;
    
                // Declare the Material of the Item and the Material's Max Durability.
                Material material = item.getType();
                short maxDurability = material.getMaxDurability();
    
                // If the Material's Max Durability is not 0, that means that the Material has Durability is Damageable (Ex. Diamond Sword).
                // If it is 0, there is no use 'repairing' a non damageable item (Ex. Blaze Rod).
                // If you do not do this check, you could potentially change the data value of a item (Ex. Strengh Potion) to its Vanilla Item (Ex. Water Bottle)
                if (maxDurability != 0)
                    // Resets the Durability.
                    item.setDurability((short) 0);
            }
        }

    You can trigger this constructor using the following Events...

    Code:
        @EventHandler // Breaking Blocks
        public void onBreak(BlockBreakEvent evt) {
            this.repairItems(evt.getPlayer().getItemInHand());
        }
    
        @EventHandler // Durability Waste from Interactions such as a Fishing Rod
        public void onInteract(PlayerInteractEvent evt) {
            this.repairItems(evt.getPlayer().getItemInHand());
        }
    
        @EventHandler // Damaging from Player to Player to Others such as Fall Damage, Explosion, Lava, Snowballs, ETC
        public void onDamage(EntityDamageEvent evt) {
            if (evt.getEntity() instanceof Player) {
                final Player p = (Player) evt.getEntity();
                this.repairItems(p.getItemInHand());
                this.repairItems(p.getInventory().getArmorContents());
            }
        }
     
  15. Offline

    bean710

    Why not PlayerItemDamageEvent?
     
  16. Offline

    AakashKcX

    @stormneo7 This doesn't work for swords though, you have to add a delay to it.
     
  17. I have to say it again: Just us a scheduler to loop through all players inventory, set durability to 0, instead of so many events and there are still possibilities that it won't work everytime with the limited amount of events for item damage.
     
  18. Offline

    stormneo7

    lag
     
  19. Add the enchantment durability 1000.
     
Thread Status:
Not open for further replies.

Share This Page