Jump to content

Event triggered on item durability loss?


PutoPug

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

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.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
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.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.