How to compare blocks in hand and in the world?

For example Ic2:generator:

  1. GetID in hand:
    ItemStack mainHandItem = player.getItemInHand(HandTypes.MAIN_HAND).get();
    DataContainer container = mainHandItem.toContainer();
    DataQuery query = DataQuery.of(’/’, “UnsafeDamage”);

     int unsafeDamage = Integer.parseInt(container.get(query).get().toString());
     String itemId = mainHandItem.getType().getId();
     if (unsafeDamage != 0) {
         itemId = itemId + ":" + unsafeDamage;
     }
    

Return itemId = “ic2:te:3”.
(mainHandItem.getType().getId() for this example return “ic2:te”, returns this the same for all items from IC2)

  1. GetID from Listener:
    @Listener
    public void onBlockPlace(ChangeBlockEvent.Place event, @Root Player player) {
    BlockSnapshot targetBlock = event.getTransactions().get(0).getFinal();
    final BlockState blockstate = targetBlock .getState();
    ItemStack itemStack = ItemStack.builder().fromBlockState(blockstate).build();
    id = itemStack.getType().getId();// + “.” + itemStack.getItemDamage();

                 DataContainer container = itemStack.toContainer();
                 DataQuery query = DataQuery.of('/', "UnsafeDamage");
                 int unsafeDamage = Integer.parseInt(container.get(query).get().toString());
                 if (unsafeDamage != 0) {
                     id = id + ":" + unsafeDamage;
                 }
    

This code Return Id is only (without “:3”) = “ic2:te”. returns this the same for all items from IC2

It doesn’t work right. How to implement the comparison of blocks in hand and blocks placed in the world?

Right. Your going this all wrong. Sponge has never supported BlockId’s nor ItemIds and they are removed in 1.13.

You want to compare the material. This is very simple.

ItemStack itemInHand;
Location<World> blockToCompare;

You need to find a common ground between the two. In this case its its either BlockType or ItemType depending on which one you rather use.

ItemType itemTypeInHand = itemInHand.getType();
BlockType blockType = blockToCompare.getType();

The current problem is you cannot compare a BlockType with a ItemType. So you essentially need to convert one to the other.

For the example im going to show, its going to be conveting the BlockType to an ItemType

Optional<ItemType> opBlock = blockType.getItem();
if(opBlock.isPresent()){
  //Safe to compare using .equals()
} 

When you request the itemType from the blockType there is a chance that Sponge has no idea what the ItemType of that block is and thats why it returns optional.

I belive this way works for modded items such as your example of IC2