Jump to content

[1.16.5]Anyway to prevent BREAKABLE ItemStack from breaking and meanwhile run a function?


Recommended Posts

Hello everyone. Greeting.

My situation is: I need to stop an ItemStack (such as armors, tools) from break under certain condition (such as having certain Enchantment/tag value).

I've tried LivingEntityUseItemEvent and PlayerDestroyItemEvent. LivingEntityUseItemEvent didn't get fired when my 1-durability sword broke. PlayerDestroyItemEvent only got fired at the first try for unknown-reason, and here is my code for it:

    public static void doToolsFindTheWayEnchantmentEvent(PlayerDestroyItemEvent event){
            if(ItemUtil.isItemEnchanted(event.getOriginal(),EnchantmentRegistry.tools_find_the_way_enchantment.get())) {
                int expTotal = ((PlayerEntity) event.getEntityLiving()).experienceTotal;
                if (expTotal > 1) {
                    int expNeeded = event.getOriginal().getMaxDamage() * 2;
                    ItemStack itemStack = event.getOriginal().copy();
                    if (expNeeded <= expTotal) {
                        ((PlayerEntity) event.getEntityLiving()).giveExperiencePoints(-expNeeded);
                    } else {
                        itemStack.setDamage(itemStack.getMaxDamage() - (expTotal / 2));
                        if (expTotal % 2 == 1)
                            ((PlayerEntity) event.getEntityLiving()).giveExperiencePoints(-expTotal + 1);
                        else ((PlayerEntity) event.getEntityLiving()).giveExperiencePoints(-expTotal);
                    event.getPlayer().world.addEntity(new ItemEntity(event.getPlayer().world, event.getPlayer().getPosX(), event.getPlayer().getPosY(), event.getPlayer().getPosZ(), itemStack.copy()));


PlayerDestroyItemEvent only got fired at the first time I run the code and then no matter how I try (when I did not change the code) it never reach line 3. Seem like PlayerDestroyItemEvent did not got called.

Ok, back to the question: I got a idea that I can use PlayerTickEvent to scan the whole PlayerEntity's Inventory and check every ItemStack. But this solution seems so efficiency-expensive, Is there any solution or idea I can you?

Thank you so much! My English might not be so accurate but thank you.

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.

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.


  • Create New...

Important Information

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