Decrease boots' durability when the player walk

Discussion in 'Plugin Development' started by syuko, Aug 16, 2021.

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



    I want to create a plugin with custom items.
    But I am blocked on a custom items.

    I want to create boots where durability drops when the player moves.

    I saw that the setDurability function of the ItemStack class is deprecated.
    So I tried to use the Damageable interface but I haven't been able

    Here is my code :
    protected void onPlayerMove(PlayerMoveEvent e) {
            Location from = Objects.requireNonNull(e.getFrom());
            Location to = Objects.requireNonNull(e.getTo());
            if (from.distance(to) > 0.15) {
                Damageable itemMeta = (Damageable) Objects.requireNonNull(this.getItemMeta());
                // e.getPlayer().sendMessage("test :" + itemMeta.getDamage());
                itemMeta.setDamage(itemMeta.getDamage() - 1);
                this.setItemMeta((ItemMeta) itemMeta);
    Could you help me ?:(

    Thank you
    Last edited: Aug 17, 2021
  2. Offline


    @syuko Try adding damage instead of subtracting it.

    Also, you should be using #distanceSquared instead of #distance for much better performance.
  3. Offline


    I have already try and it doesn't work :(
  4. Offline


    @syuko Do you have the EventHandler annotation? Is the Listener registered?
  5. Offline


    Hello, yes I have the EVentHandler annotation in a class thah extends the Listener
  6. Offline


    @syuko But did you register the listener?
  7. Offline


    You have to move super fast in order for that to work because PlayerMoveEvent only calls every so often, you need to either have a variable stored for each instance of the boots that records each time that event is called and then see if that is above 15 or however many units or you could set a random chance every one out of /*units* that will decrease the durability like how minecraft does with unbreaking. I am fairly certain that is the issue, though I could be wrong.
    Kars likes this.
  8. Offline


    Run the code to damage the boots in a runnable and call it in your event handler code. This might work
    1 class for the runnable and 1 class for the event handler

    EDIT: dont forget to register events
  9. Offline


    ^ this

    I would honestly do this by running a repeating task every second and comparing the player's current location to the previously known location.
    Also might wanna check if the player is actually walking and not flying, riding etc.
Thread Status:
Not open for further replies.

Share This Page