How can I get Sub ID item in hand?

How get string minecraft:granite for block Granite in hand ?

@Listener
public void onInteractBlock(final InteractBlockEvent.Secondary event, @First Player player)
{
ItemType itemInHand = ItemTypes.NONE;
if (player.getItemInHand(HandTypes.MAIN_HAND).isPresent())
if (player.getItemInHand(HandTypes.MAIN_HAND).isPresent())itemInHand = player.getItemInHand(HandTypes.MAIN_HAND).get().getItem();

player.sendMessage(Text.builder("HAND_NAME: "+itemInHand.getName() ).color(TextColors.YELLOW).build());
player.sendMessage(Text.builder("HAND_TYPE: "+itemInHand.getType() ).color(TextColors.YELLOW).build());
player.sendMessage(Text.builder("HAND_ID: "+itemInHand.getId() ).color(TextColors.YELLOW).build());
}

HAND_NAME: minecraft:stone
HAND_TYPE: ItemMultiTexture{Name=stone}
HAND_ID: minecraft:stone

Granite is a variant of “minecraft:stone” with block data 1. “minecraft:granite” does not exists.

@Listener
public void onInteractBlock(final InteractBlockEvent.Secondary event, @First Player player)
{
    ItemStack stackInHand = player.getItemInHand(HandTypes.MAIN_HAND).orElseGet(()-> ItemStack.of(ItemTypes.NONE, 0));
    player.sendMessage(Text.builder("HAND_ID: "+stackInHand.getItem().getName() ).color(TextColors.YELLOW).build());
    player.sendMessage(Text.builder("HAND_DURABILITY: "+stackInHand.get(DurabilityData.class).map(DurabilityData::durability).map(BaseValue::get) ).color(TextColors.YELLOW).build());
    player.sendMessage(Text.builder("HAND_STATE: "+stackInHand.get(BlockItemData.class).map(BlockItemData::state).map(BaseValue::get) ).color(TextColors.YELLOW).build());
    player.sendMessage(Text.builder("HAND_NAME_ID: "+stackInHand.getTranslation().getId() ).color(TextColors.YELLOW).build());
    player.sendMessage(Text.builder("HAND_NAME_EN: "+stackInHand.getTranslation().get(Locale.ENGLISH) ).color(TextColors.YELLOW).build());
    player.sendMessage(Text.builder("HAND_NAME_DEF: ").color(TextColors.YELLOW).append(Text.of(stackInHand.getTranslation())).build());
}

Results:

[03:30:49] [Client thread/INFO]: [CHAT] HAND_ID: minecraft:stone
[03:30:49] [Client thread/INFO]: [CHAT] HAND_DURABILITY: Optional.empty
[03:30:49] [Client thread/INFO]: [CHAT] HAND_STATE: Optional[minecraft:stone[variant=granite]]
[03:30:49] [Client thread/INFO]: [CHAT] HAND_NAME_ID: tile.stone.granite.name
[03:30:49] [Client thread/INFO]: [CHAT] HAND_NAME_EN: Granite
[03:30:49] [Client thread/INFO]: [CHAT] HAND_NAME_DEF: Granito

The HAND_NAME_DEF example respects the player’s language setting. My game is configured to Brazilian Portugese

Right clicking with a sword:

[03:33:38] [Client thread/INFO]: [CHAT] HAND_ID: minecraft:diamond_sword
[03:33:38] [Client thread/INFO]: [CHAT] HAND_DURABILITY: Optional[1561]
[03:33:38] [Client thread/INFO]: [CHAT] HAND_STATE: Optional.empty
[03:33:38] [Client thread/INFO]: [CHAT] HAND_NAME_ID: item.swordDiamond.name
[03:33:38] [Client thread/INFO]: [CHAT] HAND_NAME_EN: Diamond Sword
[03:33:38] [Client thread/INFO]: [CHAT] HAND_NAME_DEF: Espada de Diamante

Notice how the durability and state data are only available where it makes sense.

3 Likes

It is represented, in the way you are looking for, as minecraft:stone[variant=granite]. This is an ID representation of a BlockState, and can be gotten from the state with getId and turned into a state through the GameRegistry.

4 Likes

Eclipse can’t find BlockItemData::state and BaseValue::get in HAND_STATE

May be you need to import it or you are not using java 8…

The methods are:
https://jd.spongepowered.org/5.0.0/org/spongepowered/api/data/manipulator/mutable/item/BlockItemData.html#state--
https://jd.spongepowered.org/5.0.0/org/spongepowered/api/data/value/BaseValue.html#get--

You can replace it by:

stackInHand.get(BlockItemData.class).map(it-> it.state()).map(it-> it.get())
1 Like

Replacing that won’t make any difference; if :: isn’t supported, -> isn’t either.

A simpler way is to use Keys, and you don’t have to bother with mapping statements.

Optional<BlockState> state = item.get(Keys.ITEM_BLOCKSTATE);
if (state.isPresent()) {
    player.sendMessage(Text.of(state.get().getId()));
}
2 Likes

big thanks, i changed old eclipse to new version

thanks for nice code