Solved ItemStacks Durability not changing, it only changes once.

Discussion in 'Plugin Development' started by ChipDev, Dec 20, 2014.

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

    ChipDev

    Hello bukkit!
    I have tried many ways, to make an item go from almost 1 durability then pretty much go up to full. Even though it only changes once!
    I know the Runnable works, I've tested with debugs.
    Code:
    ItemStack mgm = e.getItem();
                            Bukkit.getScheduler().scheduleSyncDelayedTask(this.plugin, new Runnable(){
                                @Override
                                public void run() {
                                   
                                    Bukkit.getScheduler().scheduleSyncDelayedTask(InteractEvent.plugin, new Runnable(){
    
                                        @Override
                                        public void run() {
                                            mgm.setDurability( (short) 29);
                                            e.getPlayer().updateInventory();
                                            Bukkit.getScheduler().scheduleSyncDelayedTask(InteractEvent.plugin, new Runnable(){
    
                                                @Override
                                                public void run() {
                                                    mgm.setDurability( (short) 24);
                                                    e.getPlayer().updateInventory();
                                                    Bukkit.getScheduler().scheduleSyncDelayedTask(InteractEvent.plugin, new Runnable(){
    
                                                        @Override
                                                        public void run() {
                                                            mgm.setDurability((short) 17);
                                                            e.getPlayer().updateInventory();
                                                            Bukkit.getScheduler().scheduleSyncDelayedTask(InteractEvent.plugin, new Runnable(){
    
                                                                @Override
                                                                public void run() {
                                                                    mgm.setDurability((short) 12);
                                                                    e.getPlayer().updateInventory();
                                                                    Bukkit.getScheduler().scheduleSyncDelayedTask(InteractEvent.plugin, new Runnable(){
    
                                                                        @Override
                                                                        public void run() {
                                                                            mgm.setDurability((short) 4);
                                                                            e.getPlayer().updateInventory();
                                                                            Bukkit.getScheduler().scheduleSyncDelayedTask(InteractEvent.plugin, new Runnable(){
    
                                                                                @Override
                                                                                public void run() {
                                                                                    mgm.setDurability((short) 0);
                                                                                    e.getPlayer().updateInventory();
                                                                                }
                                                                               
                                                                            }, 4);
                                                                        }
                                                                       
                                                                    }, 4);
                                                                }
                                                               
                                                            }, 4);
                                                        }
                                                       
                                                    }, 4);
                                                }
                                               
                                            }, 4);
                                        }
                                       
                                    }, 4);
                                }
                               
                            }, 4);
    Thanks,
    me.
     
  2. Offline

    mine-care

    why so many shedulers!? instead use one repeating, and every so decrease the durability by X when it reaches 0 stop the task :p
     
    Hawktasard likes this.
  3. Offline

    teej107

    Your code has a huuuuge indent in it.
     
  4. Offline

    ChipDev

    Yeah.. good idea :)
    @teej107 does that affect the code?
     
  5. Offline

    teej107

  6. Offline

    ChipDev

    Thats why you can scroll.
     
  7. Offline

    Hawktasard

    That's not the problem, It will just hurt our eyes if we try to read it.
    Edit: You're executing the code inside the run() methods after 4 ticks, 20 ticks = 1 second
     
  8. Offline

    ChipDev

    Ok, Yes.. My problem is that my item seems to only be able to be changed once or outside of the scheduler?
     
  9. Offline

    Hawktasard

    @ChipDev
    Explain? What do you mean by "changed once"

    I didn't test your code or anything, I just kindof assumed that was the problem since that would be executing the run methods with very little delay.

    Edit: Also, you should definitely check out what @mine-care said.
     
  10. Offline

    ChipDev

    Did that!
    Code:
    ItemStack mgm = e.getItem();
                            e.getItem().setDurability((short) 31);
                            Bukkit.getScheduler().scheduleSyncRepeatingTask(this.plugin, new Runnable() {
                                @Override
                                public void run() {
                                        e.getItem().setDurability((short) 5);
                                }
                               
                            }, 1, 3);
    It only sets the items durability to 31 (Lowest before it breaks , for my golden hoe) and it doesn't regenerate health. It just stays there.
     
  11. Offline

    Hawktasard

    I guess I'll check it out on a testing server, But if that is the code you're using it'll just stay at 5.

    @ChipDev
    Right, I edited the code a little bit and it seemed to work for me?

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

    ChipDev

    Can you show me your code? Usually this works for me, I must be making a moony mistake!
     
  13. Offline

    Hawktasard

    @ChipDev
    Well, people are going to hate me for this but why not:
    Code:php
    1. final ItemStack mgm = e.getPlayer().getItemInHand();
    2. mgm.setDurability((short) mgm.getType().getMaxDurability());
    3. new BukkitRunnable() {
    4. short next = mgm.getDurability();
    5.  
    6. @Override
    7. public void run() {
    8. next -= 5;
    9. mgm.setDurability(next);
    10. if(e.getPlayer() != null) e.getPlayer().updateInventory();
    11. if(mgm.getDurability() <= 0) this.cancel();
    12.  
    13. }
    14. }.runTaskTimer(main, 0L, 3L);
     
    ChipDev likes this.
  14. Offline

    ChipDev

    That still keeps the item's durability at 0, nothing changes :/
    EDIT: It gives me 5 debug messages, because it is increasing by 5 .. And the max durability is 32. (It shows it as 1 durability, because I changed your code to show item.getMaxDurability() - 1)
    Does this mean my client is not rendering this correctly?
     
  15. Offline

    Hawktasard

    @ChipDev
    Post your current code? that worked for me
     
  16. Offline

    ChipDev

    I actually got it working by giving the player a new itemstack. Thanks ;)
     
Thread Status:
Not open for further replies.

Share This Page