You can intercept player clicks using two methods. Note that you should use either the synchronous onClick() or the asynchronous onClickAsync(), but not both.
onClick(BiFunction<Integer, AnvilGUI.StateSnapshot, List<AnvilGUI.ResponseAction>>)
Called when a player clicks any slot. It receives the clicked slot index and a StateSnapshot. You must return a List of AnvilGUI.ResponseActions.
onClickAsync(ClickHandler)
Identical to onClick(), but returns a CompletableFuture<AnvilGUI.ResponseAction>. This allows you to perform asynchronous calculations (like database lookups) before returning the actions. The resulting actions will be executed on the main server thread.
Available AnvilGUI.ResponseActions:
AnvilGUI.ResponseAction.close(): Closes the inventory.AnvilGUI.ResponseAction.replaceInputText(String): Replaces the current input text.AnvilGUI.ResponseAction.updateTitle(String, boolean): Updates the inventory title.AnvilGUI.ResponseAction.openInventory(Inventory): Opens a different inventory.AnvilGUI.ResponseAction.run(Runnable): Executes generic code.Collections.emptyList(): Performs no action.
// Synchronous example
builder.onClick((slot, stateSnapshot) -> {
if (slot != AnvilGUI.Slot.OUTPUT) {
return Collections.emptyList();
}
if (stateSnapshot.getText().equalsIgnoreCase("you")) {
return Arrays.asList(AnvilGUI.ResponseAction.close());
} else {
return Arrays.asList(AnvilGUI.ResponseAction.replaceInputText("Try again"));
}
});
// Asynchronous example
builder.onClickAsync((slot, stateSnapshot) -> CompletedFuture.supplyAsync(() -> {
if (database.isMagical(stateSnapshot.getText())) {
return Arrays.asList(AnvilGUI.ResponseAction.close());
} else {
return Arrays.asList(AnvilGUI.ResponseAction.replaceInputText("Try again"));
}
}));