Posted January 23, 20241 yr 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); }
January 24, 20241 yr You should follow the approach described here and then when a key press is detected you need to call 'use()' and also send a packet to the server to make sure the server also calls 'use()'.
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.