[Solved] Checking Item Lore


I’m trying to get the item in hand when the player right clicks and then check the lore of the item for key words
Here what I have tried

public void onRightClick(InteractBlockEvent.Secondary e){

    Player player = e.getCause().last(Player.class).get();
    Optional<ItemStack> optionalStack = player.getItemInHand(HandTypes.MAIN_HAND);
        ItemStack item;
        item = optionalStack.get();
                List<Text> lore = item.get(Keys.ITEM_LORE).get();
                 player.sendMessage(Text.of(TextColors.RED, "Works"));

A few things here.
First, you already know how to store an optional, so you should do that on the key lookup so as to not perform a lookup twice.
Second, why are you defining the ItemStack and initializing it on two separate lines? Just randomly curious.

And third, to answer your question, lore is a List<Text>. So it would not contain "test" under any circumstances, because "test" is a String and not a Text. Either iterate through them or use a stream expression, and check the value of toPlain().

Thanks for the reply!

The reason I defined the ItemStack on 2 different lines is because I took a java class last year and that’s how my teacher wrote his code… So it kinda became a habit.

Il fix the key lookup as soon as I can. i’ll try your suggestion and see if I have anymore problems.

The original question has been answered, but I’m going to intercept another potential issue. I presume you actually mean to use the InteractItemEvent.Secondary event, as the InteractBlockEvent is thrown when a player, well, interacts with a block.

As another note, I recommended using event filters instead of shifting through the Cause. It won’t work in every situation, but is worth the time to know. Your method header would be looking like

public void onInteractItemSecondary(InteractItemEvent.Secondary e, @First Player player) {

In regards to code convention, I’m self taught and have many of the same debates as to what type of convention I should use. While some are ‘better’ than others, my main advice is to use something that you understand and be consistent.

One example for me here is the method name, which I changed for my example as, in my mind, onRightClick doesn’t tell me enough about what the method does (It could be any right click, a block right click, etc. This also doesn’t account for players that have their mouse buttons switched). At the same time, I wouldn’t be afraid to give something new a try - things change over time, and you might find something you like now yet will come to despise in a months time.

I see what your’re saying here 100%. As someone who used to play quake, I use rightclick to move forward and use space for secondary action.

Also thank you for telling me the right event to use :slight_smile: