Jump to content

Recommended Posts

Posted (edited)

Thank you all in advance for your help!
I create a simple class for the item, here, if the player makes a right click, he gets a random item, and at the end I want the item to decrease the strength and when the strength is 0, the item is destroyed. But when I right click, the strength goes into the negative numbers and the item doesn't break :

public class DiarubiumWand extends Item {

    public DiarubiumWand(Properties pProperties) {
        super(pProperties);
    }

    @Override
    public InteractionResultHolder<ItemStack> use(Level pLevel, Player pPlayer, InteractionHand pUsedHand) {

        if(pLevel.isClientSide) {
            int random = (int) Math.floor(1 + Math.random() * 3);
            pPlayer.sendMessage(new TextComponent("Value: "+ random), pPlayer.getUUID());
            if(random == 1)
                pPlayer.addItem(Items.DIAMOND.getDefaultInstance());
            else if(random == 2)
                pPlayer.addItem(Blocks.DIRT.asItem().getDefaultInstance());
            else if(random == 3)
                pPlayer.addItem(ModItems.DIARUBIUM_RAW.get().getDefaultInstance());
            pPlayer.getItemInHand(InteractionHand.MAIN_HAND).hurt(1, new Random(), null);
        }
        return super.use(pLevel, pPlayer, pUsedHand);
    }
}


This is item registrator: 
 

public static final RegistryObject<Item> DIARUBIUM_WAND = ITEMS.register( 
  "diarubium_wand", 
  () -> new DiarubiumWand(
    new Item.Properties()
    .tab(Diarubium.TAB)
    .durability(10)));

 

Edited by Sniklz
Posted

Im found how to make it:

 

pPlayer.getItemInHand(pUsedHand)
                .hurtAndBreak(1,
                        pPlayer,
                        (player -> player.broadcastBreakEvent(player.getUsedItemHand())));


 

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.