Need help to modify the Lore of an item

hello
i try to modify the lore of an item when i give it the item but i don’t how i can do it and i search the event who have the equivalent of PlayerInteractEvent
thanks
edit:sorry its not the good section:s

Hi terrainwax.

The events that get triggered when a player interacts with a block or an entity are InteractBlockEvent.Primary, InteractBlockEvent.Secondary, InteractEntityEvent.Primary or InteractEntityEvent.Secondary depending on the target and which mouse button was used. The Player interacting can be found in the Cause or by using event filters.

In order to set the lore of an item you need to offer them using the Data API. You may do so by first creating a List<Text> with the desired content of your lore and then calling stack.offer(Keys.ITEM_LORE, textList) on your desired stack. Bear in mind though that if you obtained an ItemStack from the Player#getItemInHand() method, that it will be a copy of the actual stack in the players hand. You’d have to temporarily store it while you make your modifications and then write it back to the players hand using the Player#setItemInHand(ItemStack) method.

Edit: I also took the liberty of moving your topic to a more appropriate section.

1 Like

when i get the display_name like that firstPlayer.get().sendMessages(Text.of(item.get().get(Keys.DISPLAY_NAME)));
i get Optional[Text{LootCase}] in chat but i want to get only LootCase how i can do ?

The method returns an Optional, meaning a value that may or may not be there. See our docs on how to deal with optionals and why we use them.

1 Like

Ok if i understand i need to make some thing like this
Optional t = item.get().get(Keys.DISPLAY_NAME);
if(t.isPresent()){
firstPlayer.get().sendMessage((Text)t.get());
firstPlayer.get().sendMessage(Text.of(“LootCase”));
if((Text)t.get() == Text.of(“LootCase”)){
firstPlayer.get().sendMessage(Text.of(“c’est le bon objet”));
}
but when i want to check if its equal they don’t work why ?
sorry for disturbing

  1. define t using generics: Optional<Text> t = item.get().get(Keys.DISPLAY_NAME). That way you will not need to cast when using get().
  2. when comparing objects (like Text) to each other, use a.equals(b) instead of a == b. The difference here is that .equals() checks if the objects are equivalent, while == checks if they are the exact same. Now if you called Text.of("LootCase") twice, you’ll have two different objects holding equal values - which makes them equivalent, but not the same.
1 Like