Skip to content

Tool

A Tool class is an abstraction introduced by the library to represent an item in a toolbar. A tool could represent many things. For example:

  1. A moderation tool: banning/kicking/muting/warning a certain individual upon an entity interaction.
  2. A builder tool: replacing/selecting a collection of blocks, or to undo/redo a set of actions.
  3. A gameplay tool: opening a quest/shop/auction menu interface.

The tool abstraction accepts three arguments:

  1. A unique string - to identify the tool (the uniqueness is on a per-toolbar basis, so it is not global)
  2. An item (ItemStack) - to place the tool at the player’s hotbar.
  3. A slot (ItemSlot) - to identify where the tool will be placed at the player’s hotbar.

A Tool could be constructed in two ways, depending on your model and preference: directly or a mutable builder.

If all the data is available upfront, this approach could be used to construct a Tool.

public Tool constructGunTool() {
return Tool.of(
"gun",
new ItemStack(Material.STICK),
ToolAction.of(
PlayerInteractEvent.class,
(toolbar, event) -> event.getPlayer().sendMessage("You shot with a gun!")
),
ItemSlot.undefined() // the library will place the tool at the most suitable slot (e.g. at index 0)
);
}

If the data are gathered iteratively or across different parts of your application, then the builder approach could be used to construct a Tool.

public Tool constructGunTool() {
return Tool.newBuilder("gun")
.setToolAction(
ToolAction.of(
PlayerInteractEvent.class,
(toolbar, event) -> event.getPlayer().sendMessage("You shot with a gun!")
)
)
.build(new ItemStack(Material.STICK));
}