Got 2 commands, 1 command executes both.

Discussion in 'Plugin Development' started by SuippoKala, May 29, 2016.

Thread Status:
Not open for further replies.
  1. Offline

    SuippoKala

    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:
    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:
    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. Offline

    I Al Istannen

    @SuippoKala
    Please, you wrote you are new to Java. Learn some more first. Java is an important requirement for Bukkit. Your error is due to not knowing enough Java.

    This bracket is closed right before the return statement. This means, the second command is also in it. If the if is false, the second command will therefore not be evaluated.

    This semicolon means, the if will have NO body. It is just as if the if didn't exist. Delete it, use brackets and include all logic for the second command in the body and all will be good.
     
    Zombie_Striker likes this.
  3. Offline

    SuippoKala

    Thank you for the fix, appreciate it! :)
     
  4. Offline

    EmeraldRailsMC

    Why did you import BungeeCord's ChatColor, instead of Bukkit's?

    @SuippoKala, not to be offensive, but if you're making those mistakes, please look up a tutorial on Java instead of posting syntax errors on the forums
     
    Zombie_Striker likes this.
Thread Status:
Not open for further replies.

Share This Page