-
Notifications
You must be signed in to change notification settings - Fork 8
Working with Prompts
This page will teach you how to query user input in Kelp using simple prompts.
Prompts can be opened via the KelpPlayer
class:
openAnvilPrompt()
openSignPrompt()
openSimpleChatPrompt()
They all return a prompt object (AnvilPrompt
, SimpleChatPrompt
, SignPrompt
), which can be further configured with fluent builder design.
All prompts can have timeouts, which means that they are closed after a specific amount of time and a given Runnable can be executed either asynchronously or on the main thread. Timeouts are optional, so you don't have to apply one if the player should have unlimited time for their input.
In this example, the player has to enter a nickname. The input is validated by checking if it does not exceed the length of a Minecraft name and does not contain any spaces.
player.openAnvilPrompt()
.initialText("Please enter your nickname")
.sourceMaterial(KelpMaterial.NAME_TAG)
.onClose(() -> player.sendMessage("§cYou exited the nickname prompt."))
.withAsyncTimeout(30, TimeUnit.SECONDS,
() -> player.sendMessage("§cThe operation took too long. Please try again."))
.handle(input -> {
if (input.contains(" ")) {
player.sendMessage("The nickname may not contain spaces. Please try again.");
return PromptResponseType.TRY_AGAIN;
}
if (input.length() > 16) {
player.sendMessage("Your nickname is too long. Please try again.");
return PromptResponseType.TRY_AGAIN;
}
// set the player's nickname
player.sendMessage("§aYour nickname has been changed successfully to " + input + "!");
return PromptResponseType.ACCEPTED;
});
This example asks the user to enter a beta key into the chat. If the player needs too long for the input or the input is invalid, they are kicked from the server. If the key is correct, their account is unlocked.
player.sendMessage("§6The server is currently in beta-phase and only specific users are allowed to join");
player.sendMessage("§6Please enter your beta key in order to continue.");
player.openSimpleChatPrompt()
.enableEcho("§8> §r", null)
.cancelFlag("!exit")
.onCancel(() -> player.kickPlayer("§cOnly users with a beta key are allowed to join!"))
.withAsyncTimeout(30, TimeUnit.SECONDS,
() -> player.kickPlayer("§cInput took too long. Please try again."))
.handle(input -> {
if (!betaKeyValid(input)) {
player.sendMessage("§cThe given beta key does not exist. Please try again");
return PromptResponseType.TRY_AGAIN;
}
player.sendMessage("§aThank you for being a part of beta testing the server!");
player.sendMessage("§aYour account is now unlocked!");
return PromptResponseType.ACCEPTED;
});
Sign Prompts are especially useful if you need more than one line of text from the player. In this example, a player sets up a party of 5 players (including him and 4 more players who are specified with the sign prompt).
Note that sign prompts are handled differently as they do not return a simple String
but a list of strings, representing the lines.
player.sendMessage("§6Please choose 4 more players to be with you in a party");
player.openSignPrompt()
.initialLines(Arrays.asList(
"Player1",
"Player2",
"Player3",
"Player4")) // set default input for the lines
.withAsyncTimeout(1, TimeUnit.MINUTES,
() -> player.sendMessage("§cInput took too long, please try again."))
.handle(lines -> {
for (String playerName : lines) {
if (playerName == null) {
continue;
}
player.sendMessage("§aAdded " + playerName + " to the party");
}
return PromptResponseType.ACCEPTED;
});
(c) 2019-2021 pxav.
Kelp is an open-source project maintained by multiple developers. If you have additions/questions/problems to report about the wiki, feel free to create an issue here on GitHub or join the Discord
- SQL Module coming soon
- Documentation in progress