Change player's tool event?

Hello, i want to cancel the change of player’s tool (with the mouse or number in the keyboard).
there is a lot of event with inventory,
do you have any idea?

ChangeInventoryEvent.Held

1 Like

thx, i try it and i say what :wink:

i think it’s that, but how can i verify the tool do they change?
(i want put 3 item in inventory, they can switch in the 3, but not in the case of there is nothing)

for (SlotTransaction transaction : e.getTransactions()) {
    transaction.getOriginal(); // Original item
    transaction.getFinal(); // New item
    transaction.getSlot(); // Slot that's being changed
}
2 Likes

And if in the future i want get the name of the changed item (another code)
i have to use get directly or i have to use .createStack() before?

ItemStackSnapshot contains the data, you don’t need to convert to an ItemStack.
The only difference is that you need to use the immutable variant, .get(ImmutableDisplayNameData.class)

okey, but i brain and i have to need the item name too, i convert in all of case (i don’t know it’s english but i say it, french expression)

Not sure what you’re asking but is it the ItemType that you’re after? .getType().getName()

i wan’t 2, the display name and the type name…;

ItemStackSnapshot snapshot = // Something here
String itemName = snapshot.getType().getName(); // <-- the item type name
Optional<ImmutableDisplayNameData> optDisplayName = snapshot.get(ImmutableDisplayNameData.class);
if (optDisplayName.isPresent()) {
    Text displayName = optDisplayName.get().displayName().get(); // <-- The display name
}

thx and for the last question because i code this now, can i modify the name of changed item?

i explain, i want to create a time of load of gun, and if the player change his player’s tool, i want to change the name of the gun to “weapon failed”

how can i do it?

The display name is controlled via the Data API, either by using DisplayNameData or the Keys.DISPLAY_NAME key. I recommend taking a look at the docs.

Sadly the French translation is far from done. But that’s only understandable, seeing as we only recently had another set of changes.

sorry i don’t use any translator,
yes, i understand this for data manipulator and the keys, but when i get the item, it’s a copy of this item, and how can i apply it to my last player’s tool’s slot?

do you have any idea?

Use player.setItemInHand(itemStack);

Remember item stacks are copies so you always need to set the new one