1.15.2 Why is this code being triggered?

Discussion in 'Spigot Plugin Development' started by hidden1nin, Feb 14, 2020.

  1. i have a command that is used to open a menu in the below code, the command /is opens settings even when the command has 0 arguments, but doing the same thing with /island works correctly and doesnt open the settings menu, Can someone explain how its entering the if statement?
    Code (Java):
    if (cmd.getName().equalsIgnoreCase("island") && sender instanceof Player || cmd.getName().equalsIgnoreCase("is") && sender instanceof Player) {
                    if (args.length == 0) {
                        if (!firstEnable.PlayerHomes.containsKey(player.getName())) {
                            player.openInventory(Create);
                            return true;
                        }
                        player.openInventory(EasySkyGui);
                    } else {
                        if (args[0].equalsIgnoreCase("settings")) {
                            player.openInventory(EasySkySettingsGui);
                            return true;
                        }
    and yes the curly brackets are closed later in the code
     
  2. I did more testing and i made it log what the args where when i did /is and its somehow appending settings to the end?
     
  3. and when i change it to return false, seeing that its outputting /is as /island
     
  4. if firstEnable.PlayerHomes.containsKey(player.getName()) is true then the inventory will open because the return true; won't be run

    To make aliases to your command just use aliases on the comand on the plugin.yml
    https://www.spigotmc.org/wiki/plugin-yml/
     
  5. Strahan

    Benefactor

    Also the way you check sender is pointlessly duplicated. Check that first, then you don't have to worry with it later. I.E.
    Code (Text):
    if (!(sender instanceof Player)) {
      sender.sendMessage("Only for use in-game!");
      return true;
    }
    if (cmd.getName().equalsIgnoreCase("island") || cmd.getName...  etc etc
     
    • Agree Agree x 1
  6. im still wondering why if i do /is its saying that i have an arguement, of settings when i dont type anything after it, and the containskey is true in both cases of the command but i don't know why the argument length is changing. this code worked fine in 1.14 but after updating its a major bug
     
  7. this is the code im using to debug it,
    Code (Java):
        if (cmd.getName().equalsIgnoreCase("island") && sender instanceof Player || cmd.getName().equalsIgnoreCase("is") && sender instanceof Player) {
                    player.sendMessage(server + "test"+args.length);
                    if (args.length == 0) {
                        player.sendMessage(server + "test 2"+args.length);
                        if (!firstEnable.PlayerHomes.containsKey(player.getName())) {
                            player.openInventory(Create);
                            return true;
                        }
                        player.openInventory(EasySkyGui);
                    } else {
                        player.sendMessage(server + "testword"+args[0]);
                        if (args[0].equalsIgnoreCase("settings")) {
                            player.openInventory(EasySkySettingsGui);
                            return true;
                        }
    but its saying that /is has 1 arguement, while /island has none
     
  8. Try registering /is as an alias of /islang on your plugin.yml and remove the cmd.getName().equalsIgnoreCase("is")
     
    • Like Like x 1
  9. did that but for some odd reason /is opens settings, and /island works fine XD
    i really don't know whats happening
     
  10. Can we see the new code and the plugin.yml?
     
  11. Are there any commands in your commands.yml that could interfering?

    Also after player.openInventory(EasySkyGui); return true instead of letting the code continue on.

    P.S.

    Place your commands in their own class ...
    Code (Java):
    public class Island implements CommandExecutor {
    Then in your main class' onEnable do
    Code (Java):
    getCommand("island").setExecutor(new Island());
    In your plugin.yml remove the listing for /is and put aliases: [ is ] in the listing for /island
    At the beginning just do one
    Code (Java):
    if (!(sender instanceof Player)) return false;
    Now all you have left to check are arguments, if you want to know what they typed in use the label, thats what that is.
     
    #11 Tarluin, Feb 14, 2020
    Last edited: Feb 14, 2020
    • Like Like x 1