Code Monkey home page Code Monkey logo

player-inputs's Introduction

player-inputs

Utilities to get a player input for those who don't want to use the Conversation API

Usage

Explanation: https://www.spigotmc.org/threads/player-chat-inputs.437565/ In this example I'll be using a command

import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.ciPlugin.Plugin;
import io.github.nemo_64.chatinput.bukkit.BukkitChatInput;
import io.github.nemo_64.chatinput.bukkit.BukkitChatInputBuilder;

public final class TestCommand implements CommandExecutor {

    private final Plugin plugin;
    
    public TestCommand(final Plugin plugin) {
        this.plugin = plugin;
    }

    @Override
    public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
        if (!(sender instanceof Player)) { // PlayerChatInput only works with players
               sender.sendMessage("Only for players");
               return false;
        }
        // This comand will ask for a number n and will send to the player n! so we will
        // work with integers
        // BukkitChatInputBuilder.integer(..., ...).otherStuffs();
        final BukkitChatInput<Integer> chatInput = BukkitChatInputBuilder.<Integer>builder(plugin, (Player) sender)
            .isValidInput((player, input) -> { // Set the validation
                try {
                    int val = Integer.valueOf(input);
                    return val > 0;// We only accept numbers greater than 0
                } catch (Exception e) {
                    return false;// The input was not an integer
                }
            })
            .setValue((player, input) -> {
                // We convert the input string to a number
                return Integer.valueOf(input);
            })
            .onInvalidInput((player, input) -> {
                // Send a message if the input is invalid
                player.sendMessage("That is not a number");
                // Send the messages stablished with invalidInputMessage(String) and sendValueMessage(String)
                return true;
            })
            .onFinish((player, value) -> {
                // when the player inputs a string that is a number greater that 0 we send a message
                player.sendMessage(value + "! is " + this.factorialOf(value));
            })
            .onCancel(player -> {
                // if the player cancels, we send a message
                player.sendMessage("Canceled the factorial-calculation");
            })
            .onExpire(player - {
                // if the input time expires.
                player.sendMessage("Input expired!");
            })
            .expire(20L * 30L)
            .repeat(true)
            .invalidInputMessage("That is not a number/Can calculate the factorial of it");// Message if the input is invalid
            .sendValueMessage("Send a number to calculate"); // Asking for the number
            .toCancel("cancel"); // Message that the player must send to cancel
            .build(); // Build the PlayerChatInput
        chatInput.start(); // Ask for the number
        return false;
    }

    private long factorialOf(final int num) {
        if (num <= 1)
            return 1;
        return this.factorialOf(num - 1) * num;
    }

}

player-inputs's People

Contributors

mrnemo64 avatar portlek avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.