PutoPug Posted September 20, 2021 Share Posted September 20, 2021 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? Quote Link to comment Share on other sites More sharing options...
Luis_ST Posted September 20, 2021 Share Posted September 20, 2021 (edited) 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 September 20, 2021 by Luis_ST Quote Link to comment Share on other sites More sharing options...
PutoPug Posted September 20, 2021 Author Share Posted September 20, 2021 Any Quote Link to comment Share on other sites More sharing options...
Luis_ST Posted September 20, 2021 Share Posted September 20, 2021 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) Quote Link to comment Share on other sites More sharing options...
PutoPug Posted September 21, 2021 Author Share Posted September 21, 2021 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 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.