Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

Event triggered on item durability loss?


PutoPug
 Share

Recommended Posts

there are the PlayerDestroyItemEvent (which is fierd when a Player destroys a Item) and the LivingEntityUseItemEvent.Finish (which is fierd after a Item is used) unfortunately there is no Event which is directly fierd when a Item is damaged

Edit: you can combine a Start and Finish SubEvents of LivingEntityUseItemEvent check the difference (you need to store the data in a Capability)

 

Edited by Luis_ST
Link to comment
Share on other sites

I present to you, nasty hack number 703:

@SubscribeEvent
public static void playerLoggedIn(PlayerEvent.PlayerLoggedInEvent event)  {
    if (event.getPlayer() instanceof ServerPlayer player) {
        var stackRef = new MutableObject<WeakReference<ItemStack>>();
        var durabilityRef = new MutableInt(0);

        ItemDurabilityTrigger.TriggerInstance triggerInstance = new ItemDurabilityTrigger.TriggerInstance(
                EntityPredicate.Composite.ANY, ItemPredicate.ANY, MinMaxBounds.Ints.ANY, MinMaxBounds.Ints.ANY
        ) {
            @Override
            public boolean matches(ItemStack stack, int durability) {
                stackRef.setValue(new WeakReference<>(stack));
                durabilityRef.v = durability;
                return true;
            }
        };
        var advancement = player.getServer().getServerResources().getAdvancements().getAllAdvancements()
                .stream().findAny().orElseThrow();

        var listener = new CriterionTrigger.Listener<>(triggerInstance, advancement, "<dummy>") {
            @Override
            public void run(PlayerAdvancements playerAdvancements) {
                var stackWeakRef = stackRef.getValue();
                if (stackWeakRef != null) {
                    var stack = stackWeakRef.get();
                    if (stack != null) {
                        System.out.println("Durability changed for player " + player.getDisplayName().getString() + " with stack " + stack + " " + stack.getTag() + ", new durability: " + durabilityRef.v);
                    }
                }
            }
        };
        CriteriaTriggers.ITEM_DURABILITY_CHANGED.addPlayerListener(
                player.getAdvancements(), listener
        );
    }
}

 

Link to comment
Share on other sites

56 minutes ago, diesieben07 said:

I present to you, nasty hack number 703:

simple question why is there no Event for this?
an implementation for such an event is really easy
(one new Class and a new line in the patch file of ItemStack)
 

Link to comment
Share on other sites

15 hours ago, diesieben07 said:

I present to you, nasty hack number 703:

@SubscribeEvent
public static void playerLoggedIn(PlayerEvent.PlayerLoggedInEvent event)  {
    if (event.getPlayer() instanceof ServerPlayer player) {
        var stackRef = new MutableObject<WeakReference<ItemStack>>();
        var durabilityRef = new MutableInt(0);

        ItemDurabilityTrigger.TriggerInstance triggerInstance = new ItemDurabilityTrigger.TriggerInstance(
                EntityPredicate.Composite.ANY, ItemPredicate.ANY, MinMaxBounds.Ints.ANY, MinMaxBounds.Ints.ANY
        ) {
            @Override
            public boolean matches(ItemStack stack, int durability) {
                stackRef.setValue(new WeakReference<>(stack));
                durabilityRef.v = durability;
                return true;
            }
        };
        var advancement = player.getServer().getServerResources().getAdvancements().getAllAdvancements()
                .stream().findAny().orElseThrow();

        var listener = new CriterionTrigger.Listener<>(triggerInstance, advancement, "<dummy>") {
            @Override
            public void run(PlayerAdvancements playerAdvancements) {
                var stackWeakRef = stackRef.getValue();
                if (stackWeakRef != null) {
                    var stack = stackWeakRef.get();
                    if (stack != null) {
                        System.out.println("Durability changed for player " + player.getDisplayName().getString() + " with stack " + stack + " " + stack.getTag() + ", new durability: " + durabilityRef.v);
                    }
                }
            }
        };
        CriteriaTriggers.ITEM_DURABILITY_CHANGED.addPlayerListener(
                player.getAdvancements(), listener
        );
    }
}

 

Oh wow, very dirty 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share



×
×
  • Create New...

Important Information

By using this site, you agree to our Privacy Policy.