Need help with registering commands!

Discussion in 'Spigot Plugin Development' started by MGPro, Jul 23, 2017.

  1. Hello spigot!
    So what i am trying to do is register the same command (/rtime) for 3 different classes.
    Example :
    Code (Text):
        private void registerCommands() {
            this.getCommand("time").setExecutor(new TimeCommand());
            this.getCommand("rtime").setExecutor(new ESTCommand());
            this.getCommand("rtime").setExecutor(new GMTCommand());
            this.getCommand("rtime").setExecutor(new BSTCommand());
    }
    The code works. But it only registers the last and first one. The command i am trying to do is. /rtime est, /rtime gmt and /rtime bst. only bst works. The code for bst (btw the code for est gmt and bst are the same) :
    Code (Text):
    package net.rdevs.plugins.rTime.commands;

    import java.text.DateFormat;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    import java.util.TimeZone;

    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;

    import net.rdevs.plugins.rTime.CoreConfiguration;

    public class ESTCommand implements CommandExecutor {
       
        public String est() {
        Date est = new Date();
        DateFormat estformat = new SimpleDateFormat("HH:mm:ss");
        estformat.setTimeZone(TimeZone.getTimeZone("EST"));
        return estformat.format(est);
        }

    public boolean onCommand(CommandSender sender, Command command, String label, String[] arg) {
        if(command.getName().equalsIgnoreCase("rtime")){
                if(arg.length < 1){
                    sender.sendMessage(CoreConfiguration.GREEN + CoreConfiguration.PLUGIN_NAME + CoreConfiguration.DARK_GRAY + " »" + CoreConfiguration.GRAY + " Created" + CoreConfiguration.GRAY + " by" + CoreConfiguration.GREEN + " §lPETTLES" + CoreConfiguration.GRAY + "\nhttps://www.spigotmc.org/members/mgpro.226980/\n" + CoreConfiguration.DARK_GRAY + "» " + CoreConfiguration.GREEN + "/rtime" + CoreConfiguration.DARK_GRAY + " - " + CoreConfiguration.GRAY + "Shows this page!\n" + CoreConfiguration.DARK_GRAY + "» " + CoreConfiguration.GREEN + "/rtime est" + CoreConfiguration.DARK_GRAY + " - " + CoreConfiguration.GRAY + "Shows the time for people in the est timezone!\n" + CoreConfiguration.DARK_GRAY + "» " + CoreConfiguration.GREEN + "/rtime bst" + CoreConfiguration.DARK_GRAY + " - " + CoreConfiguration.GRAY + "Shows the time for people in the bst timezone!\n" + CoreConfiguration.DARK_GRAY + "» " + CoreConfiguration.GREEN + "/rtime gmt" + CoreConfiguration.DARK_GRAY + " - " + CoreConfiguration.GRAY + "Shows the time for people in the gmt timezone!");
                    return false;
                } if(arg[0].equals("est")) {
                    sender.sendMessage(CoreConfiguration.DARK_GRAY + "§m-|--------------------------------|-");
                    sender.sendMessage("§7In the timezone §a§lEST §7The time is §a§l" + this.est());
                    sender.sendMessage(CoreConfiguration.DARK_GRAY + "§m-|--------------------------------|-");
                    return false;
                }else {
                    sender.sendMessage(CoreConfiguration.GREEN + CoreConfiguration.PLUGIN_NAME + CoreConfiguration.DARK_GRAY + " »" + CoreConfiguration.GRAY + " Created" + CoreConfiguration.GRAY + " by" + CoreConfiguration.GREEN + " §lPETTLES" + CoreConfiguration.GRAY + "\nhttps://www.spigotmc.org/members/mgpro.226980/\n" + CoreConfiguration.DARK_GRAY + "» " + CoreConfiguration.GREEN + "/rtime" + CoreConfiguration.DARK_GRAY + " - " + CoreConfiguration.GRAY + "Shows this page!\n" + CoreConfiguration.DARK_GRAY + "» " + CoreConfiguration.GREEN + "/rtime est" + CoreConfiguration.DARK_GRAY + " - " + CoreConfiguration.GRAY + "Shows the time for people in the est timezone!\n" + CoreConfiguration.DARK_GRAY + "» " + CoreConfiguration.GREEN + "/rtime bst" + CoreConfiguration.DARK_GRAY + " - " + CoreConfiguration.GRAY + "Shows the time for people in the bst timezone!\n" + CoreConfiguration.DARK_GRAY + "» " + CoreConfiguration.GREEN + "/rtime gmt" + CoreConfiguration.DARK_GRAY + " - " + CoreConfiguration.GRAY + "Shows the time for people in the gmt timezone!");
                    return false;
                    }}
        return false;}}
     
    Thanks!
     
  2. This is a basic programming concept: If you have this bit of code
    Code (Text):
    String a = "Hi";
    a = "Hello";
    a = "Hallo!"
    System.out.println(a);
    Your output will be: "Hallo!"

    This is because String a was last declared to "Hallo!" meaning that if you were to get its value right after, instead of showing "Hi", it would show "Hallo!"

    Back to your problem, in your code, you are telling the program to register the command "rtime" to the ESTCommand class. Then you are telling it to register it to the GMTCommand class. Then you are telling it to register it to the BSTCommand class. Then, since that is the last time you change what class the command gets registered to, the code then moves on and the "rtime" command is set registered to the BSTCommand class and the other two classes are not used.

    I suggest making one class, name it like TimeZoneCommand or something, and check to see if the first argument is equal to one of the time zones. If so, execute your code.
     
    • Informative Informative x 1
  3. Thanks for replying!

    I managed to fix it! Thanks anyway!