NextDevv's Docs Help

Inputs

fun askForInput(player: Player, request: String): CompletableFuture<String> { waitingForPlayer.add(player.uniqueId) player.closeInventory() player.sendMessage(request.tac()) return CompletableFuture.supplyAsync { try { val uuid = player.uniqueId while (responses[uuid].isNullOrBlank()) { Thread.sleep(1) } return@supplyAsync responses[uuid] }catch (e: Exception) { e.printStackTrace() return@supplyAsync "" } } }

Description

The askForInput function is a utility method that allows you to prompt a player for input in a GUI. It adds the player to a list of players waiting for input, closes their current inventory, and sends them a message requesting input. The function returns a CompletableFuture<String> that will be completed with the player's response once they have entered it.

The function uses a CompletableFuture to handle the asynchronous nature of player input. It continuously checks for the player's response in a loop until it is received, sleeping for 1 millisecond between each check. Once the response is received, the CompletableFuture is completed with the response.

How to use

You can use the askForInput function to prompt a player for input and receive the response as a CompletableFuture<String>. The function takes two parameters:

  • player: The Player object representing the player you want to prompt.

  • request: The message you want to display to the player as the input request.

Here is an example of how to use the askForInput function in Kotlin:

val inv = kGui.builder(player) .setTitle("Input Test") .setSize(3) .addButton(Alignment.CENTER, GuiButton().setItemStack( KItemStack.builder() .setName("&aClick me to input") .setMaterial(Material.PAPER) .build() ).setOnClick { builder, player -> askForInput(player, "Enter your name:") .thenAccept { response -> player.sendMessage("You entered: $response") } }) .build() player.openInventory(inv)

In this example, we create a GUI with a button that, when clicked, prompts the player to enter their name. The response is then displayed to the player in a chat message.

Summary

The askForInput function is a useful utility method for handling player input in a GUI. It allows you to prompt players for input and receive their responses asynchronously, making it easy to create interactive and engaging user interfaces in your Minecraft plugin or mod.

Last modified: 03 July 2024