Registry
The CommandPanels Registry allows you to hook into the plugin by registering custom implementations at startup. There are four registries available:
| Registry | Description |
|---|---|
| MATERIAL_COMPONENTS | Create a custom Material Tag that builds an ItemStack from a tag string. |
| ITEM_COMPONENTS | Apply custom attributes or data to existing ItemStacks after they have been built. |
| REQUIREMENT_TAG_RESOLVERS | Add a custom Requirement Tag that checks a condition and optionally consumes a resource. |
| COMMAND_TAG_RESOLVERS | Add a custom Command Tag that executes logic when a player clicks a panel item. |
It is best to register your implementations in onEnable(), or anywhere that would run before any player would have the chance to open a panel.
import me.rockyhawk.commandpanels.api.Registry;
Registry.MATERIAL_COMPONENTS.insert(
new YourMaterialTag()
);
Registry.ITEM_COMPONENTS.insert(
new YourItemTag()
);
Registry.REQUIREMENT_TAG_RESOLVERS.insert(
new YourRequirementTag()
);
Registry.COMMAND_TAG_RESOLVERS.insert(
new YourCommandTag()
);
MaterialComponent
Implement MaterialComponent to resolve a custom material tag into a base ItemStack. The engine calls isCorrectTag for every unrecognised material string and delegates to the first matching component. Attributes are then applied on top of the returned item.
public interface MaterialComponent {
boolean isCorrectTag(String tag);
ItemStack createItem(Context ctx, String itemID, Player player, PanelItem item);
}
Return null from createItem to signal failure. CommandPanels will fall back gracefully.
For example, adding a [craftengine] material tag might look like this:
public class CraftEngineComponent implements MaterialComponent {
// Define the name of your material tag
@Override
public boolean isCorrectTag(String tag) {
return tag.toLowerCase().startsWith("[craftengine]");
}
// Create your custom ItemStack here
@Override
public ItemStack createItem(Context ctx, String itemID, Player player, PanelItem item) {
var definition = CraftEngineItems.byId(itemID);
if (definition == null) {
return null;
}
return definition.buildBukkitItem(player);
}
}
ItemComponent
Implement ItemComponent to apply additional data to an already-built ItemStack. This is for modifying the item after the ItemStack has been constructed in every Panel. For example, you.
public interface ItemComponent {
ItemStack apply(Context ctx, ItemStack itemStack, Player player, PanelItem item);
}
Return the modified ItemStack from apply. The returned stack replaces the current item in the build pipeline.
To make no changes, just return the ItemStack without modifying it.
For example, the below will set the stack amount to 5 if the custom-model-data value equals 'five':
public class CustomModelDataComponent implements ItemComponent {
@Override
public ItemStack apply(Context ctx, ItemStack itemStack, Player player, PanelItem item) {
if(!item.customModelData().equals("five")) return itemStack;
itemStack.setAmount(5);
return itemStack;
}
}
RequirementTagResolver
Implement RequirementTagResolver to add a custom requirement tag. The engine calls check first. If it returns false, the fail command list runs instead of commands. If check returns true, execute is called to consume the resource (e.g. withdraw currency) before commands runs.
The execute code can be left empty if you do not wish to take anything from the player, some requirement tags might only be doing a check rather than exchange.
public interface RequirementTagResolver {
boolean isCorrectTag(String tag);
boolean check(Context ctx, Panel panel, Player player, String raw, String args);
void execute(Context ctx, Panel panel, Player player, String raw, String args);
}
The args String is everything after the tag, with any placeholders already parsed. The raw String is the same but without parsing the placeholders. You will need to use raw if the action you are performing needs the placeholders left unparsed.
Here is a simple example, it will confirm if a player has a specific permission:
public class PermissionTag implements RequirementTagResolver {
@Override
public boolean isCorrectTag(String tag) {
return tag.equalsIgnoreCase("[permission]");
}
@Override
public boolean check(Context ctx, Panel panel, Player player, String raw, String args) {
return player.hasPermission(args);
}
@Override
public void execute(Context ctx, Panel panel, Player player, String raw, String args) {
// nothing to take from the player, left empty
}
}
CommandTagResolver
Implement CommandTagResolver to handle a custom command tag. handle is called when a player interacts with a panel item whose command list contains your tag.
public interface CommandTagResolver {
boolean isCorrectTag(String tag);
void handle(Context ctx, Panel panel, Player player, String raw, String command);
}
The command String is everything after the tag, with any placeholders already parsed. The raw String is the same but without parsing the placeholders. You will need to use raw if the action you are performing needs the placeholders left unparsed.
The below example will broadcast a message to the server:
public class BroadcastTag implements CommandTagResolver {
@Override
public boolean isCorrectTag(String tag) {
return tag.startsWith("[broadcast]");
}
@Override
public void handle(Context ctx, Panel panel, Player player, String raw, String command) {
Bukkit.broadcastMessage(command);
}
}