Skip to content

Writing plugins

Martijn Muijsers edited this page Feb 21, 2024 · 2 revisions

Most Bukkit plugins already work with all new blocks and items in Fiddle!

Using Fiddle blocks and items in plugins

Get a custom Material

You can get and use all custom blocks and items with the regular Bukkit API with matchMaterial:

Material willowLog = Material.matchMaterial("willows:willow_log");

         ▻ Paper Javadoc for Material.matchMaterial(...)
             (returns null if the Material doesn't exist)

Save to a database or file

✔️ Use material.getKey().toString()

         Example return value: "willows:willow_log"
         ▻ Material.getKey()

❌ Do not use material.toString() or material.name()

         Doing this will usually work, but you should not rely on these.

Comparisons

Some of your pack's blocks or items may have been aliased. Therefore, you must never compared namespaced keys of Material instances, but instead compare the Material instances directly.

✔️ Comparing Material instances directly is good:

// Comparing two Materials is good! This example uses equals()
if (block.getType() == Material.matchMaterial("willows:willow_log")) {
    ...
}

❌ Do not do compare the strings or NamespacedKeys:

// Comparing two Materials' namespaced keys as strings is bad:
if (block.getType().getKey().toString().equals("willows:willow_log")) {
    ...
}
// Comparing two Materials' namespaced keys as NamespacedKeys is bad:
if (block.getType().getKey().equals(new NamespacedKey("willows", "willow_log"))) {
    ...
}

Keys in maps

In addition to the above, you must never use Material namespaced keys as a key in maps, but instead use Material directly.

✔️ Using Material directly is good:

// Using Material as keys in a map is good!
Map<Material, Integer> countPerMaterial = new HashMap<>();
...
countPerMaterial.put(Material.matchMaterial("willows:willow_log"), 5);
countPerMaterial.put(block.getType(), 6);

❌ Do not do use the strings or NamespacedKeys:

// Using Materials' namespaced keys (as strings) as keys in a map is bad:
Map<String, Integer> countPerMaterial = new HashMap<>();
...
countPerMaterial.put("willows:willow_log", 5);
countPerMaterial.put(block.getType().getKey().toString(), 6);
// Using Materials' namespaced keys (as NamespacedKeys) as keys in a map is bad:
Map<NamespacedKey, Integer> countPerMaterial = new HashMap<>();
...
countPerMaterial.put(new NamespacedKey("willows", "willow_log"), 5);
countPerMaterial.put(block.getType().getKey(), 6);
Clone this wiki locally