Easy errors that I can't fix

Discussion in 'Spigot Plugin Development' started by Kaneki1337, Jun 24, 2018.

  1. No errors in console, no messages, no nothing.

    No clue what I'm doing wrong.

    Code (Java):
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if(sender instanceof Player) {
                if(label.equalsIgnoreCase("supplydrop")) {
                   
                    sender.sendMessage("" + args.length);
                   
                    if(sender.hasPermission("supplydrop.admin")) {
                       
                        if(args.length > 1) {
                       
                            if(args[0].equalsIgnoreCase("help")) {
                               
                                sender.sendMessage(ChatColor.WHITE + "" + ChatColor.STRIKETHROUGH + "----------------------------------------------");
                                sender.sendMessage(ChatColor.GREEN + "" + ChatColor.BOLD + "/supplyDrop setItem [name]" + ChatColor.GRAY + " - " + ChatColor.RED + "Saves the sender's inventory as a yml file.");
                                sender.sendMessage(ChatColor.GREEN + "" + ChatColor.BOLD + "/supplyDrop setActive [name]" + ChatColor.GRAY + " - " + ChatColor.RED + "Sets the active yml file to grab the items from.");
                                sender.sendMessage(ChatColor.GREEN + "" + ChatColor.BOLD + "/supplyDrop info" + ChatColor.GRAY + " - " + ChatColor.RED + "Information about this plugin.");
                                sender.sendMessage(ChatColor.WHITE + "" + ChatColor.STRIKETHROUGH + "----------------------------------------------");
                               
                                return true;
                            }
                       
                            else if(args[0].equalsIgnoreCase("setItem")) {
                               
                                if(args.length < 2) {
                                    sender.sendMessage(ChatColor.RED + "TEST");
                                }
                                else {
                                   
                                    if(((Player) sender).getInventory().getSize() != 0) {
                                       
                                        String remover = args[1].replace(".yml", "");
                                        File saver = new File(getDataFolder(), remover + ".yml");
                                       
                                       
                                        FileConfiguration customConfig = YamlConfiguration.loadConfiguration(saver);
                                        customConfig.set(remover + ".items", ((Player) sender).getInventory().getContents());
                                        saveCustomYml(customConfig, saver);
                                       
                                        sender.sendMessage(ChatColor.WHITE + "" + ChatColor.BOLD + "Successfully created a item config " + ChatColor.RED + args[1] + ChatColor.WHITE + " With " + ((Player) sender).getInventory().getContents().length);
                                       
                                        return true;
                                       
                                    }
                                    sender.sendMessage(ChatColor.GRAY + "There was nothing in your inventory, nothing created.");
                                    return false;
                                }
                                sender.sendMessage(ChatColor.GOLD + "" + ChatColor.BOLD + "Wrong usage, use " + ChatColor.GREEN + " /supplydrop setItem [name]");
                                return false;
                            }
                           
                            else if(args[0].equalsIgnoreCase("setActive")) {
                               
                                if(args.length < 2 ) {
                                    sender.sendMessage(ChatColor.RED + "TEST");
                                }
                               
                                else {
                                   
                                    String remover = args[1].replaceAll(".yml", "");
                                    File checker = new File(getDataFolder(), remover + ".yml");
                           
                                    if(checker.exists()) {
                                        sender.sendMessage(ChatColor.GREEN + "" + ChatColor.BOLD + "Active item yml file changed to " + ChatColor.GOLD + "" + args[1]);
                                        this.getConfig().set("supplyDrop.config.defaultYML", args[1] + ".yml");
                                        return true;
                                    }
                                    sender.sendMessage(ChatColor.RED + "" + ChatColor.BOLD + "That yml file does not exist.");
                                    return false;
                                }
                                sender.sendMessage(ChatColor.RED + "" + ChatColor.BOLD + "Wrong usage, use " + ChatColor.GREEN + "/supplydrop setActive [filename]");
                                return false;
                               
                            }  
                                sender.sendMessage(ChatColor.WHITE + "" + ChatColor.BOLD + "Invalid command! Use " + ChatColor.GREEN + "/supplydrop help");
                                return false;  
                        }
                       
                        sender.sendMessage(ChatColor.WHITE + "" + ChatColor.STRIKETHROUGH + "----------------------------------------------");
                        sender.sendMessage(ChatColor.GREEN + "" + ChatColor.BOLD + "/supplyDrop setItem [name]" + ChatColor.GRAY + " - " + ChatColor.RED + "Saves the sender's inventory as a yml file.");
                        sender.sendMessage(ChatColor.GREEN + "" + ChatColor.BOLD + "/supplyDrop setActive [name]" + ChatColor.GRAY + " - " + ChatColor.RED + "Sets the active yml file to grab the items from.");
                        sender.sendMessage(ChatColor.GREEN + "" + ChatColor.BOLD + "/supplyDrop info" + ChatColor.GRAY + " - " + ChatColor.RED + "Information about this plugin.");
                        sender.sendMessage(ChatColor.WHITE + "" + ChatColor.STRIKETHROUGH + "----------------------------------------------");
                       
                       
                           
                    }
                }
               
                sender.sendMessage(ChatColor.WHITE + "" + ChatColor.STRIKETHROUGH + "----------------------------------------------");
                sender.sendMessage(ChatColor.GREEN + "" + ChatColor.BOLD + "supplyDrop " + ChatColor.GRAY + " - " + ChatColor.RED + "Created by "+ ChatColor.AQUA + "MyFuhrer");
                sender.sendMessage(ChatColor.GOLD + "Plugin version: " + ChatColor.YELLOW + " v1.0.0");
                sender.sendMessage(ChatColor.WHITE + "" + ChatColor.STRIKETHROUGH + "----------------------------------------------");
                   
                return true;
               
            }
            return false;
           
        }
     
  2. Maybe you should organize your code some more. What [part of the code are you running as well?

    Ask yourself these questions:
    • Have I added my command to the plugin.yml?
    • (If in another class) Have I registered it in the main class?
     
  3. Make sure you register the command and have it in your plugin.yml!
     
  4. Plugin.yml is complete.
    This is in the main class.

    Have both.
     
  5. Are you sure your plugin is loaded? Also add some debug messages to make it easier to what's happening.
     
  6. Yah, tab-completion shows up for /supplydrop, but no result when command is entered.
     
  7. Perhaps you have an outdated version loaded in? Also just throw in some more debug messages in your code to see where exactly it stops.

    May I also suggest changing your coding style with your if checks. I prefer to check against stuff and then return when I know it's wrong. For example, your code would look something like this:
    Code (Java):
    if (sender instanceof Player) {
      if (args.length == 2) {
        if (foo) {
          // do whatever
        } else {
            // whatever
        }
      } else {
          // args length is not 2
      }
    } else {
        // sender is not a player
    }
    But I find it much more organizational and readable to write it like this instead:
    Code (Java):
    if (!(sender instanceof Player) {
      // sender is not a player!
      return;
    }
    if (args.length != 2) {
      // incorrect usage!
      return;
    }
    if (!foo) {
      // whatever
      return;
    }
    // do whatever
    I used to write using the former style but switched over to my current style because of how much easier it is to follow.
     
    #7 xMrPoi, Jun 24, 2018
    Last edited: Jun 24, 2018
    • Agree Agree x 2
  8. Code (Text):
    if(args.length > 1) {
    Are you sure that's meant to be 1?

    You can either change > to >= or 1 to 0, because if the number of args has to be bigger than 1, there have to be 2, 3, 4, 5... and going on arguments.

    In other words, the code will only fire if you do something like this...

    /command arg anotherarg

    And not:

    /command arg

    Same for this:

    Code (Text):
    if(args.length < 2) {
    Here's a small tip if you're checking how long arguments are... if you use let's say args.length > 1 and it's true, then you can definitely get args[1]. If args.length > 0, you can definitely get args[0].

    Just a simple logic error that needed pointing out. I may be wrong but I've had the same issue before and it's easy to fix in the end.

    EDIT: Just checked the code twice and that no messages are showing up. Now I'm confused. :unsure:
     
  9. FrostedSnowman

    Resource Staff

    use Command#getName rather than the label provided. Also, there really should be no need to check for the command name unless you're having multiple commands under the same commandexecutor, which in this case, you're not, so..
     
    • Agree Agree x 1
  10. Not sure if this would matter at all, but I believe the plugin details (like version) should be inside the bracket directly above it. If you take a look at the code, if the label matches to be supplydrop but the player doesn't have admin permissions it should output the version stuff, correct? The way it's setup now is if the label doesn't match to supplydrop it will output the version info.

    EDIT: I'm pretty sure I read the brackets properly but if I didn't feel free to correct me
     
  11. Nothing no longer works, lmao I might just redo this
     
  12. it implements CommandExecuter? And theres no errors you didnt see when the plugin enables? (Scroll up)