Util Easier command util

Discussion in 'Resources' started by rfsantos1996, Mar 5, 2015.

Thread Status:
Not open for further replies.
  1. Offline

    rfsantos1996

    I've created a nice utility that make creating commands with a high amount of variances without having to if...else if...else if...else for an eternity of lines.

    Variances for the "Teleport command":
    • /teleport (player) (x) (y) (z) -> teleport another player to given coordinates on the same world
    • /teleport (x) (y) (z) -> teleport yourself to given coordinates on the same world
    • /teleport (world) (x) (y) (z) -> teleport yourself to another world at given coordinates
    • /teleport (player) (player) -> teleport a player to another player
    • /teleport (player) -> teleport yourself to a player
    • And you can think of many more [like /teleport (player) (world) (x) (y) (z)]
    And thinkin on that, I made this utility, you can basicly create an argument method that corresponds to the given arguments by the player. Example:
    • /teleport (player) (player) -> handleCommand(CommandSender sender, Player player1, Player player2)
    • /teleport (x) (y) (z) -> handleCommand(Player sender, Double x, Double y, Double z)
    • ... I could do this with all commands above, but I believe you already understood what this util do
    Code:
    handleCommand(<Sender class, CommandSender or Player> sender, <arguments for the command>)

    What about text?
    Bukkit splits the command arguments by space. On a command, if you input /command words words words it will return an array of Strings (length = 3), will I need to write a handleCommand just to handle each option?

    The answer is no, you can even create arrays of arguments, for example, what a /msg (player) (message) command would look like:
    Code:
    handleCommand(CommandSender sender, Player player, String[] message)


    Notes you need to consider:
    • this isn't a perfect system, it can fail if you start creating a thousand of variances
    • it can get consufing (you handling all variances and the system having to calculate which variance to use)
    • please, use arrays at the last argument, just like (String...) in Java: it must be the last argument on a method

    Where do I get this?
    Github - you can find the util here

    I would love to receive pull requests fixing stuff, or adding examples, feel free to colaborate! Please, do not modify the util making it better and more stable and keep it for yourself, share with the community <3

    How to create commands?
    You just need a base class (CommandExecutor - the same name as Bukkit, but it is an abstract class of mine):
    Code:
    public class LoginCommand extends CommandExecutor {
    
    public LoginCommand() {
    super("command name", "command description", "Comand usage message"); // I'll later replace this for an automatic help message showing all options that the sender can use (have permission and is compatible)
    }
    
    // Here you'll have your command handlers
    
    }
    How to create a command handler?
    Basicly what you need is:
    Code:
    @CommandHandler(additionalPermission = "") // This can be empty
    public HandleResponse handleCommand(CommandSender/Player sender, <arguments>) {
    
    return HandleResponse.RETURN; // same as return true
    
    return HandleResponse.RETURN_HELP; // It'll return the help message of the command
    
    return HandleResponse.RETURN_NO_PERMISSION; // It'll return the "you don't have permission" message from the command
    }
    How do I add/remove more types?
    You can add and remove more classes on what you need/have on your server, for instance, in my server I use a custom "Player" class (hash tabled by the name, on login -> create the new class, on leave -> delete the new class) well, I added that to the system, now I can use my custom player class as a Sender. Without having to "main.getCustomPlayerFor(sender.getName())"

    Example, you can add support for time (if you input "06:56" you can just create a method to parse to minecraft time/ticks/whatever you need, if it parsed successfully the system will alow you to use the argument)

    Code:
    public enum ArgumentType {
    
        LOCATION(Location.class),
        WORLD(World.class),
    
        NUMBER(Number.class), // You NEED to use Integer.class, Double.class, etc. Not 'double', 'int'
    
        ENTITY_TYPE(EntityType.class),
        POTION_TYPE(PotionType.class),
        MATERIAL(Material.class),
    
        PLAYER_NAME(Player.class), // The class to be used on arguments (handleCommand(CommandSender sender, Player player)) is now supported (;
        STRING(String.class);
    
        private final Class<?> clazz;
    
        private ArgumentType(Class<?> clazz) {
            this.clazz = clazz;
        }
    
        public Class<?> getClazz() {
            return clazz;
        }
    
        public static Argument handleArgument(CommandSender commandSender, String string) {
    // Do your handling, search for player names, material, etc
    
    // Example:
    final Argument argument = new Argument(string);
    
    { // Check for material
    Material material = Material.valueOf(string.toUpperCase());
    if(material != null)
    argument.addArgumentType(MATERIAL, material);
    }
    }
    So basicly you can add any class to the system, as long as you can parse string -> class. Like searching for all players for the most equal name.

    I don't remember
    of what I have to tell you... I believe this is it, hope you enjoy (;

    Changelog:
    - Fixed int, double, float, short, byte support (you don't need to use Integer, Double, etc) (28/03/15)
    - First release (some time ago)
     
    Last edited: Mar 28, 2015
    Totom3 and ChipDev like this.
Thread Status:
Not open for further replies.

Share This Page