-
Notifications
You must be signed in to change notification settings - Fork 3
3. Using custom blocks and items in plugin code
You can use custom blocks and items in plugins, just like vanilla blocks and items.
By default, custom blocks and items will work with all existing Paper / Bukkit plugins (for example, for shops, logging or protections).
If you want to write code that specifically uses your blocks and items (for example, a plugin that sends a message when someone picks up a diamond nugget), you can get a Material instance with Material.matchMaterial:
public static final Material DIAMOND_NUGGET = Material.matchMaterial("diamond_currency:diamond_nugget");
@EventHandler
public void onEntityPickupItem(EntityPickupItemEvent event) {
if (event.getEntity() instanceof Player player) {
if (event.getItem().getItemStack().getType() == DIAMOND_NUGGET) {
player.sendMessage("You found a diamond nugget.");
}
}
}You can also use the more modern BlockType and ItemType:
public static final BlockType WILLOW_LOG = Registry.BLOCK.get(Key.key("willow_trees:willow_log"));
public static final ItemType WILLOW_BARK = Registry.ITEM.get(Key.key("willow_trees:willow_bark"));Don't use Material enum properties (like Material.name(), Material.ordinal(), Material.valueOf(..)).
In the future, Paper will change Material from enum to interface, and the enum properties will be removed.
While comparing namespaced keys (NamespacedKey, Key) works and can be useful sometimes, it is more stable to compare types (Material, BlockType, ItemType) directly.
So, avoid this:
boolean isGreenPaint(ItemStack itemStack) {
return itemStack.getType().key().equals(Key.key("artistry:green_paint"));
}Map<NamespacedKey, Integer> blockBrokenCount = new HashMap<>();Instead, do this:
Material GREEN_PAINT = Material.matchMaterial("artistry:green_paint");
boolean isGreenPaint(ItemStack itemStack) {
return itemStack.getType() == GREEN_PAINT;
}Map<BlockType, Integer> blockBrokenCount = new HashMap<>();