#Unable to get current item stack held in player hand in InventoryTick() override.

24 messages · Page 1 of 1 (latest)

acoustic saddle
#

I'm making a tool that uses a custom keybind for additional functionality and used inventoryTick() to check for key presses. Now i want to do a check if the correct item is held in hand, but no matter what i do i can't manage to get the current item held in player's hand. It always outputs the item the player held at the time of joining the world after building the debug project and it never changes after that.

@Override
public void inventoryTick(ItemStack stack, ServerWorld world, Entity entity, EquipmentSlot slot)
{
    if (entity.isPlayer()) {
        ItemStack itemStack = entity.getEntity().getStackInHand(Hand.MAIN_HAND);
        KeyBinding keyBinding = KeyBinding.byId("key.blockpallettemod.test");
        
        ClientTickEvents.END_CLIENT_TICK.register(client -> {
            while(keyBinding.wasPressed()) {
                if (itemStack.isOf(stack.getItem())) {
                    client.player.sendMessage(Text.literal("Pallette opened"), false);
                }
            }
        });
    }
}

What java magic do i do to get the currently held item stack?

#

im at 1.21.11 but use the yarn mapping, because it was more convenient when i was following the yt tutorial series

uneven shoal
#

Wow do we not have a proper page on keybindings

#

Yes but not relevant to the problem at hand

uneven shoal
#

Which means after 5 seconds you have 100 listeners

#

You should only be registering one ClientTickEvent

#

*is create

acoustic saddle
#

i coded that in the custom tool item class and that was the only overridable method that allowed me to register pressed keyboard key

uneven shoal
acoustic saddle
#

so i'll need to make the key press listener in the ClientModInitializer and the result "send" to the custom item?

#

i attempted to do that before i resulted to that monstrosity, so i wasn't able to do that lol

uneven shoal
#

define send to custom item

acoustic saddle
#

the idea is that the custom tool im trying to make has a sort of a submenu, that i want to open with a keybind

uneven shoal
#

On keybind pressed, iterate through the inventory (or just check stacks in hands) to see if any of them are your custom item, and if so, open

acoustic saddle
#

i tried to do that, but i don't know how to access the player entity from the clientmodinitializer

uneven shoal
#

You have access to the client in the client ticke vent

#

*event

acoustic saddle
#

last time i did that it was returning null, weird
i guess i did some redundant code that messed things up

uneven shoal
#

It can return null when you're outside a world so I just put a null check and stop worrying about it

acoustic saddle
#

yea that's a correct approach

#

btw the tool will be a part of a group (has tool material variants) so i guess i'll need to create a custom tag for them

acoustic saddle
#

alright it works now, thanks!