Create an input inventory
This example shows how to open an input form and process player input.
Input inventories are used to collect text input from players.
→ Deep dive: [[User Text Input|Core-API/Inventory-&-UI/User-Text-Input]] & [[Utility Classes|Core-API/Utility-Classes]] & [[Chat Filter|Core-API/Command-&-Interaction/Chat-Filter]]
Steps
- Create an input form using
InputFormBuilder - Open the form using
InputHandler - Handle the result in the callback
- Validate and apply the input
Implementation
InputHandler inputHandler = BukkitCoreLibrary.getInputHandler();
InputForm inputForm = InputFormBuilder.create(
"Enter your new home name"
)
.description("The minimum length of a home name is 3 and the maximum is 32. You can use color codes.")
.buttonName("<green>Change name</green>")
.maxLength(64)
.build();
inputHandler.openForm(player, inputForm, output -> {
// player closed the input
if (output == null) return;
// remove invalid characters
output = MessageUtil.removeUnAllowedCharacters(output);
// handle color permissions
if (player.getRank().equals(PixelRank.USER)) {
output = MessageUtil.removeColor(output);
} else {
output = MessageUtil.translateUserInput(player, output);
}
// filter check
if (BukkitCoreLibrary.getFilterHandler().isFiltered(output)) {
player.sendMessage(PixelColor.RED, "Invalid name! The given name is against our rules!");
return;
}
// length validation
String colorlessOutput = MessageUtil.removeColor(output);
if (colorlessOutput.length() < 3 || colorlessOutput.length() > 32) {
player.sendMessage(PixelColor.RED, "New name is too short or too long!");
return;
}
// apply result
home.setDisplayName(output);
player.playSound(Sound.UI_CARTOGRAPHY_TABLE_TAKE_RESULT);
player.sendMessage("<green>New home name:</green> " + output);
});
Explanation
InputFormBuilderdefines the input UI.description(...)adds additional information.buttonName(...)defines the confirm button.maxLength(...)limits the input lengthopenForm(...)opens the form and returns the result via callback
Notes
- Always check for
null(player closed the input) - Always validate user input (length, filter, format)
- Keep validation logic clear and structured
Next step
[[Create custom items|Core-API/Build-your-first-feature/Items/Create-custom-items]]