Jump to content
View in the app

A better way to browse. Learn more.

Forge Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Featured Replies

Posted

Hello, I am wondering is there any event that triggers on item durability loss?

If not, any other way to run code when item durability is lost?

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

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)
 

  • Author
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 

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...

Important Information

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

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.