// Simple block handler
public class ExplosiveBlockHandler implements BlockHandler {
@Override
public void onDestroy(@NotNull Destroy destroy) {
Pos pos = destroy.getBlockPosition();
Instance instance = destroy.getInstance();
// Create explosion
instance.explode(pos, 4, true);
}
@Override
public @NotNull NamespaceID getNamespaceId() {
return NamespaceID.from("mymod:explosive_block");
}
}
// Interactive block handler
public class ButtonBlockHandler implements BlockHandler {
@Override
public boolean onInteract(@NotNull Interaction interaction) {
Player player = interaction.getPlayer();
Block block = interaction.getBlock();
// Toggle button state
boolean powered = block.getProperty("powered").equals("true");
Block newBlock = block.withProperty("powered", String.valueOf(!powered));
interaction.getInstance().setBlock(interaction.getBlockPosition(), newBlock);
player.sendMessage("Button " + (!powered ? "pressed" : "released"));
return true;
}
@Override
public @NotNull NamespaceID getNamespaceId() {
return NamespaceID.from("mymod:custom_button");
}
}
// Custom crafting block
public class CustomCraftingTableHandler implements BlockHandler {
@Override
public boolean onInteract(@NotNull Interaction interaction) {
Player player = interaction.getPlayer();
// Open custom crafting GUI
player.openInventory(createCraftingInventory());
return true;
}
private Inventory createCraftingInventory() {
// Create custom crafting inventory
return Inventory.builder()
.title("Custom Crafting")
.size(54)
.build();
}
@Override
public @NotNull NamespaceID getNamespaceId() {
return NamespaceID.from("mymod:custom_crafting_table");
}
}
// Apply handlers to blocks
Block explosiveBlock = Block.TNT.withHandler(new ExplosiveBlockHandler());
Block buttonBlock = Block.STONE_BUTTON.withHandler(new ButtonBlockHandler());
Block craftingBlock = Block.CRAFTING_TABLE.withHandler(new CustomCraftingTableHandler());