Got 2 commands, 1 command executes both.

Discussion in 'Programming' started by Wife, May 29, 2016.

  1. Hi!

    I'm fairly new to Java and i noticed this error while trying to execute the 2nd command.

    - 2nd command (gmc) does nothing
    - 1st command (ci) executes both commands

    i'd like to know how to fix this, thanks in advance!

    Code (Text):
    package me.Brightest;

    import org.bukkit.plugin.java.JavaPlugin;

    public class Main extends JavaPlugin {
       
        public void onEnable() {
            getCommand("ci").setExecutor(new Commands(this)); // Importtaa komennot toisesta class tiedostosta.
            getCommand("gmc").setExecutor(new Commands(this));
        }

    }
     
    Code (Text):
    package me.Brightest;

    import org.bukkit.Bukkit;
    import org.bukkit.GameMode;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;

    import net.md_5.bungee.api.ChatColor;

    public class Commands implements CommandExecutor {
       
        private Main main; //Vaadittava
        public Commands(Main main) { //Vaadittava
            this.main = main; //Vaadittava
        }
       
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            Player player = (Player) sender;
           
            if(cmd.getName().equalsIgnoreCase("ci")) {
                if(player.hasPermission("nalkapeli.ci")) {
                player.getInventory().clear();
                player.updateInventory();
                sender.sendMessage(ChatColor.AQUA + "Tavaraluettelo tyhjennetty!");
                } else {
                    sender.sendMessage(ChatColor.RED + "Sinulla ei ole tarvittavia oikeuksia komentoon!");
                   
                }
               
                if (cmd.getName().equalsIgnoreCase("gmc"));
                if(player.hasPermission("nalkapeli.gmc")) {
                            player.setGameMode(GameMode.CREATIVE);
                        sender.sendMessage(ChatColor.AQUA + "Luova tilassa!");
                    } else {
                        sender.sendMessage(ChatColor.RED + "Sinulla ei ole tarvittavia oikeuksia komentoon!");

                    }
            }
            return true;
        }
    }
     
  2. 1. Create just one Commands instance.
    2. Your second if is inside the first which causes the problem
     
  3. If you're using a separate class for your commands, you may as well separate both commands into their own classes - it'll be more organized, and will remove the need for boilerplate code to check the command.
     
  4. Your code looks like this:

    Code (Text):
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
        /* ... */
           
        if(cmd.getName().equalsIgnoreCase("ci")) {
           /* ... */

          if (cmd.getName().equalsIgnoreCase("gmc"));
                /* ... */
          }
         
        }
    }
    So

    1. Your syntax is wrong at
    Code (Text):
    if (cmd.getName().equalsIgnoreCase("gmc"));
    It's an if-clause, the semicolon is wrong. Don't know why your IDE wasn't saying anything.

    2. You have a logic mistake there. The if-clause for the gmc command is inside the one for the ci command, so it will never be true. It would be correct like that:

    Code (Text):
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
        /* ... */
           
        if(cmd.getName().equalsIgnoreCase("ci")) {
           /* ... */
        }

        if (cmd.getName().equalsIgnoreCase("gmc")) {
                /* ... */
        }
    }
    However I suggest using two seperate command handlers for two commands.
     
  5. I suggest learning Java and OOP first before jumping into the Spigot API.