Solved Plugin sends '/toggle' after using the command?

Discussion in 'Spigot Plugin Development' started by Keubix, Jun 8, 2017.

  1. I have a plugin, and when I use any command in it, it sends the feedback I have made for it, but the server also sends a white '/commandname' such as /toggle. Any idea on how to fix this? Might just be an invalid plugin.yml or something, but does anyone know what causes this?
     
  2. If you are being sent in chat the command that you just typed then the code that you have written for when it is executed is not properly linked to the definition of the command in your plugin.yml. While the latter is true, for future reference, in this case the problem was in the plugin.yml the usage was set to /<command> and the usage is printed if you return false in your onCommand method. If you're looking for help can you send the plugin.yml and the class containing your command's code? :)
     
    #2 JayBrmn, Jun 8, 2017
    Last edited: Jun 8, 2017
    • Like Like x 1
  3. Ok, how would I got about fixing this?

    Code (Text):
    public void onEnable() {
    this.getCommand("sc").setExecutor((CommandExecutor)new CommandHandler());
    }
    Code (Text):
    public boolean onCommand(CommandSender sender, Command cmd, String CommandLabel, String[] args) {
            if (args.length == 0) {
                sender.sendMessage(Main.getInstance.prefix  + Main.getInstance.colorize("&6Commands:"));
                sender.sendMessage(Main.getInstance.prefix  + Main.getInstance.colorize("&6/sc lock <password> &8- &eLocks the chest you are looking at with your 4-8 digit password."));
                sender.sendMessage(Main.getInstance.prefix  + Main.getInstance.colorize("&6/sc unlock <password> &8- &eUnlock the chest you are looking at with the password. (10 Seconds to open after entering password.)"));
                sender.sendMessage(Main.getInstance.prefix  + Main.getInstance.colorize("&6/sc remove <password> &8- &eRemove the password from the chest you are looking at. (Requires original password!)"));
                return false;
            }
            if (args[0].equalsIgnoreCase("help")) {
                sender.sendMessage(Main.getInstance.prefix  + Main.getInstance.colorize("&6Commands:"));
                sender.sendMessage(Main.getInstance.prefix  + Main.getInstance.colorize("&6/sc lock <password> &8- &eLocks the chest you are looking at with your 4-8 digit password."));
                sender.sendMessage(Main.getInstance.prefix  + Main.getInstance.colorize("&6/sc unlock <password> &8- &eUnlock the chest you are looking at with the password. (10 Seconds to open after entering password.)"));
                sender.sendMessage(Main.getInstance.prefix  + Main.getInstance.colorize("&6/sc remove <password> &8- &eRemove the password from the chest you are looking at. (Requires original password!)"));
                return false;
            }
            if (args[0].equalsIgnoreCase("reload")) {
                Main.getInstance.reloadConfig();
                sender.sendMessage(Main.getInstance.prefix  + Main.getInstance.colorize("&ePlugin reloaded!:"));
                return false;
            }
            if (args[0].equalsIgnoreCase("lock")) {
                //if (!sender.hasPermission("safechest.lock")) {
                    //sender.sendMessage(Main.getInstance.prefix  + Main.getInstance.colorize("&cYou don't have permission for this!"));
                    //return false;
                //}
                if (args.length == 1) {
                    sender.sendMessage(Main.getInstance.prefix  + Main.getInstance.colorize("&cYou need to provide a &n4-8 digit&r &cpassword!"));
                    return false;
                }
                //if (checkIfUsed(args[1]) == true) {
                //    sender.sendMessage(Main.getInstance.prefix  + Main.getInstance.colorize("&cThis password is already in use! Try another."));
                //    return false;
                //}
                if (args[1].length() >= 4 && args[1].length() <= 8) {
                    if (Main.getInstance.getTargetBlock((Player) sender, 6).getType() == Material.CHEST) {
                        Block chest = Main.getInstance.getTargetBlock((Player) sender, 6);
                        Location loc = chest.getLocation();
                        Main.getInstance.getConfig().set("locked." + args[1] + ".LocX", loc.getX());
                        Main.getInstance.getConfig().set("locked." + args[1] + ".LocY", loc.getY());
                        Main.getInstance.getConfig().set("locked." + args[1] + ".LocZ", loc.getZ());
                        Main.getInstance.saveConfig();
                        sender.sendMessage(Main.getInstance.prefix  + Main.getInstance.colorize(Main.getInstance.prefix + Main.getInstance.colorize("&eChest has been locked! (&7" + args[1].toString() + "&e)")));
                        sender.sendMessage(Main.getInstance.prefix  + Main.getInstance.colorize(Main.getInstance.prefix + Main.getInstance.colorize("&eX=" + loc.getX() + " &6| &eY=" + loc.getY() + " &6| &eZ=" + loc.getZ())));
                        return false;
                    }
                    else {
                        sender.sendMessage(Main.getInstance.prefix  + Main.getInstance.colorize("&cYou need to be looking at a chest within 6 blocks of you!"));
                        return false;
                    }
                }
                else {
                    sender.sendMessage(Main.getInstance.prefix  + Main.getInstance.colorize("&cYour password must be comprised of &n4-8 digits&r&c!"));
                    return false;
                }
            }
            if (args[0].equalsIgnoreCase("unlock")) {
                if (!sender.hasPermission("safechest.unlock")) {
                    sender.sendMessage(Main.getInstance.prefix  + Main.getInstance.colorize("&cYou don't have permission for this!"));
                    return false;
                }
                if (args.length == 1) {
                    sender.sendMessage(Main.getInstance.prefix  + Main.getInstance.colorize("&cYou need to provide a &n4-8 digit&r &cpassword!"));
                    return false;
                }
                if (checkIfUsed(args[1]) == false) {
                    sender.sendMessage(Main.getInstance.prefix  + Main.getInstance.colorize("&cPassword does not exist."));
                    return false;
                }
                if (Main.getInstance.getTargetBlock((Player) sender, 6).getType() == Material.CHEST) {
                    if (checkLoc(args[1].toString(), Main.getInstance.getTargetBlock((Player) sender, 6).getLocation()) == true) {
                        Main.getInstance.unlocked.put(sender.getName(), args[1].toString());
                        Main.getInstance.timer.put(sender.getName(), 10.0);
                    }
                }
                else {
                    sender.sendMessage(Main.getInstance.prefix  + Main.getInstance.colorize("&cYou need to be looking at a chest within 6 blocks of you!"));
                    return false;
                }
            }
            return false;
        }
       
        public boolean checkLoc(String code, Location loc) {
            for (String key: Main.getInstance.getConfig().getConfigurationSection("locked").getKeys(false)) {
                double x = Main.getInstance.getConfig().getDouble("locked." + code.toString() + ".LocX");
                double y = Main.getInstance.getConfig().getDouble("locked." + code.toString() + ".LocY");
                double z = Main.getInstance.getConfig().getDouble("locked." + code.toString() + ".LocZ");
                if (loc.getX() == x && loc.getY() == y && loc.getZ() == z) {
                    return true;
                }
                else {
                    return false;
                }
               
            }
            return false;
        }
       
        public boolean checkIfUsed(String code) {
            for (String key: Main.getInstance.getConfig().getConfigurationSection("locked").getKeys(false)) {
                System.out.println(key);
                if (code == key) {
                    return true;
                }
                else {
                    return false;
                }
            }
            return false;
        }
    Code (Text):
    name: SafeChest
    version: 1.0
    main: me.keubix.Main
    description: Keeps your chests safe.
    author: Keubix

    commands:
      sc:
        usage: /<command>
        aliases: [safechest]
        description: SafeChest Main command.
       
     
  4. Does your class CommandHandler implement CommandExecutor? Also in my plugin.ymls I indent by one space every time like so:
    Code (Text):
    commands:
     sc:
      usage: /<command>
      aliases: [safechest]
      description: SafeChest Main command.
     
  5. Mas

    Mas

    The usage message from the plugin.yml gets sent when you return false from onCommand. Return true instead.
     
    #5 Mas, Jun 8, 2017
    Last edited: Jun 8, 2017
    • Informative Informative x 1
  6. This is correct however the usage mesage is sent when you are returning false. So yes, return true instead.
     
    #6 JayBrmn, Jun 8, 2017
    Last edited: Jun 8, 2017
    • Agree Agree x 1
  7. Mas

    Mas

    Oops, meant to put false lol, edited.
     
  8. No problem.<3
    Also edited my initial post for future reference (the Spigot forums are a kind of database after all).
     
    • Like Like x 1
  9. Thanks! Very helpful. :)