"Make a block, when you interact with it with any golden item in the hand, spawn a zombie with a dragon head equipped"
xxxxxxxxxx
/* First, create a custom block class that extends Block and implements InteractionObject */
public class CustomBlock extends Block implements InteractionObject {
public CustomBlock(AbstractBlock.Properties properties) {
super(properties);
}
/* Override the use method to handle interaction with the block */
public InteractionResult use(BlockState state, Level world, BlockPos pos, Player player, InteractionHand hand, BlockHitResult hit) {
ItemStack heldItem = player.getItemInHand(hand);
/* Check if the held item is a golden item */
if (heldItem.getItem() instanceof ItemGoldIngot || heldItem.getItem() instanceof ItemGoldenSword || heldItem.getItem() instanceof ItemGoldenPickaxe || heldItem.getItem() instanceof ItemGoldenAxe || heldItem.getItem() instanceof ItemGoldenShovel || heldItem.getItem() instanceof ItemGoldenHoe) {
/* Spawn a zombie with a dragon head equipped */
Zombie zombie = EntityType.ZOMBIE.create(world);
zombie.setPos(pos.getX() + 0.5, pos.getY() + 1, pos.getZ() + 0.5);
zombie.setItemSlot(EquipmentSlot.HEAD, new ItemStack(Items.DRAGON_HEAD));
if (!world.isClientSide) {
world.addFreshEntity(zombie);
}
return InteractionResult.SUCCESS;
}
return InteractionResult.PASS;
}
}
/* Register the custom block in your mod's main class or a dedicated registry class */
public static final RegistryObject<Block> CUSTOM_BLOCK = BLOCKS.register("custom_block", () -> new CustomBlock(Block.Properties.of(Material.STONE).strength(3.0F, 3.0F)));