Jump to content

use custom item with keybind


Turesso

Recommended Posts

I'm working on my first Minecraft mod, and I've created an item that requires a keybind to activate. However, currently, I need to press my assigned keybind and then hold down the default Minecraft keybind (right mouse button) for the item to function. How can I make it so that the item only requires my custom keybind to work?

    public InteractionResultHolder<ItemStack> use(Level pLevel, Player pPlayer, InteractionHand pHand) {
        ItemStack itemstack = pPlayer.getItemInHand(pHand);

        ItemStack offhandStack = pPlayer.getItemInHand(InteractionHand.OFF_HAND);
        if (!offhandStack.isEmpty()) {

            return InteractionResultHolder.fail(itemstack);
        }
      
        if (isCharged(itemstack)) {
            // Verifica se está usando a mão correta
            if (pHand == InteractionHand.MAIN_HAND) {
                performShooting(pLevel, pPlayer, pHand, itemstack, getShootingPower(itemstack), 0F);
                setCharged(itemstack, false);
                return InteractionResultHolder.consume(itemstack);
            }
        } else if (hasAmmo(pPlayer)) {
            if(KeyBindings.INSTANCE.examplekey.isDown()){
            if (!isCharged(itemstack)) {
                if (pHand == InteractionHand.MAIN_HAND) {
                    this.startSoundPlayed = false;
                    pPlayer.startUsingItem(pHand);
                }
            }
            }

            return InteractionResultHolder.consume(itemstack);
        } else {
            pLevel.playSound((Player) null, pPlayer.getX(), pPlayer.getY(), pPlayer.getZ(), ModSounds.NOAMMO.get(), SoundSource.PLAYERS, 1.0F, 1.0F);
            return InteractionResultHolder.fail(itemstack);
        }

        return InteractionResultHolder.fail(itemstack);
    }

 

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.



×
×
  • Create New...

Important Information

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