Create a confirmation inventory
This example shows how to create and open a confirmation inventory.
Confirmation inventories are used to prevent accidental actions by requiring a second confirmation from the player.
→ Deep dive: [[Confirm Inventories|Core-API/Inventory-&-UI/Confirm-Inventories]] & [[InventoryUtil|Core-API/Inventory-&-UI/InventoryUtil]]
Steps
- Create a
ConfirmContentusingConfirmContentBuilder - Open the inventory using
InventoryHandler - Handle the result in the callback
Implementation
ConfirmContent confirmContent = ConfirmContentBuilder.create(
gigaDiamond.clone()
)
.confirmText("<green>Redeem Giga Diamond</green>")
.confirmDescription("<gray>You receive two extra hearts!</gray>")
.declineText("<red>Don't redeem</red>")
.build();
InventoryHandler inventoryHandler = BukkitCoreLibrary.getInventoryHandler();
inventoryHandler.openConfirmInventory(
player,
"Redeem giga diamond",
confirmContent,
redeem -> {
// player declined or closed the inventory
if (redeem == null || !redeem) return;
PlayerInventory inventory = player.getInventory();
if (InventoryUtil.getItemAmount(inventory, gigaDiamond) > 0) {
InventoryUtil.removeItem(inventory, gigaDiamond, 1);
player.setMaximumHealth(player.getMaximumHealth() + 2);
player.setHealth(player.getMaximumHealth());
player.playSound(Sound.UI_TOAST_CHALLENGE_COMPLETE);
player.sendTitle(
"+2 ❤️",
"",
0, 60, 20
);
}
}
);
Explanation
ConfirmContentBuilderdefines the confirmation UI.confirmText(...)sets the confirm button text.confirmDescription(...)adds additional information.declineText(...)sets the cancel buttonopenConfirmInventory(...)opens the UI- The callback receives the result (
true= confirmed,false= declined,null= closed)
Notes
- Always validate the result (
null/false) - Use confirmation inventories for important or irreversible actions
- Keep the UI clear and understandable for players