What's wrong with this code?

Discussion in 'Plugin Development' started by emilandersson91, Feb 20, 2015.

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

    emilandersson91

    Basically a messy mix of permissions.
    When the commands are entered in-game, they do nothing.

    What have I missed, and is there a cleaner way to do this?


    Code:
    package me.emilandersson91;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class RockPromotion extends JavaPlugin{
       
        public void onEnable() {
            getLogger().info("RockPromotion has been enabled");
           
        }
       
        public void onDisable() {
            getLogger().info("RockPromotion has been disabled");
           
        }
       
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
       
            Player player = (Player) sender;
           
            if (cmd.getName().equalsIgnoreCase("rockwin")) {
                if(args.length ==1) {
                    if(sender.hasPermission("rock.win")) {
                    } else {
                    player.sendMessage(ChatColor.DARK_RED + "You do not have permission to use this command.");
                    }
                        Player target = getServer().getPlayer(args[0]);
                        if(!target.hasPermission("gymcheck.rock")) {
                            Bukkit.getServer().dispatchCommand(Bukkit.getConsoleSender(), "pex user"+target.getName()+"add gymcheck.rock");
            if (cmd.getName().equalsIgnoreCase("waterwin")) {
                if(args.length ==1) {
                    if(sender.hasPermission("water.win")) {
                    } else {
                    player.sendMessage(ChatColor.DARK_RED + "You do not have permission to use this command.");
                    }
                        if(!target.hasPermission("gymcheck.water")) {
                            if(target.hasPermission("gymcheck.rock")) {
                                Bukkit.getServer().dispatchCommand(Bukkit.getConsoleSender(), "pex user"+target.getName()+"add gymcheck.water");
            if (cmd.getName().equalsIgnoreCase("electricwin")) {
                if(args.length ==1) {
                    if(sender.hasPermission("electric.win")) {
                    } else {
                    player.sendMessage(ChatColor.DARK_RED + "You do not have permission to use this command.");
                    }
                        if(!target.hasPermission("gymcheck.electric")) {
                            if(target.hasPermission("gymcheck.water")) {
                                if(target.hasPermission("gymcheck.rock")) {
                                    Bukkit.getServer().dispatchCommand(Bukkit.getConsoleSender(),  "pex user"+target.getName()+"add gymcheck.electric");
            if (cmd.getName().equalsIgnoreCase("grasswin")) {
                if(args.length ==1) {
                    if(sender.hasPermission("grass.win")) {
                    } else {
                    player.sendMessage(ChatColor.DARK_RED + "You do not have permission to use this command.");
                    }
                        if(!target.hasPermission("gymcheck.grass")) {
                            if(target.hasPermission("gymcheck.electric")) {
                                if(target.hasPermission("gymcheck.water")) {
                                    if(target.hasPermission("gymcheck.rock")) {
                                        Bukkit.getServer().dispatchCommand(Bukkit.getConsoleSender(), "pex user"+target.getName()+"add gymcheck.grass");
            if (cmd.getName().equalsIgnoreCase("grasswin")) {
                if(args.length ==1) {
                    if(sender.hasPermission("poison.win")) {
                    } else {
                    player.sendMessage(ChatColor.DARK_RED + "You do not have permission to use this command.");
                    }
                        if(!target.hasPermission("gymcheck.poison")) {
                            if(target.hasPermission("gymcheck.grass")) {
                                if(target.hasPermission("gymcheck.electric")) {
                                    if(target.hasPermission("gymcheck.water")) {
                                        if(target.hasPermission("gymcheck.rock")) {
                                            Bukkit.getServer().dispatchCommand(Bukkit.getConsoleSender(), "pex user"+target.getName()+"add gymcheck.poison");
                                        }
                                    }
                                }
                            }
                        }
                }
            }
                                    }
                                }
                            }
                        }
                }
            }
                                }
                            }
                        }
                }
            }
                            }
                        }
                }
            }
                        }
                }
            }
            return false;
        }
    }
    
    
    plugin.yml:
    Code:
    main: me.emilandersson91.RockPromotion
    name: RockPromotion
    version: 1.0
    author: emilandersson91
    description: Give player Badges.
    commands:
      rockwin:
        description: Award a player with a Badge.
        usage: /<command>
      waterwin:
        description: Award a player with a Badge.
        usage: /<command>
      electricwin:
        description: Award a player with a Badge.
        usage: /<command>
      grasswin:
        description: Award a player with a Badge.
        usage: /<command>
      poisonwin:
        description: Award a player with a Badge.
        usage: /<command>
      psychicwin:
        description: Award a player with a Badge.
        usage: /<command>
      firewin:
        description: Award a player with a Badge.
        usage: /<command>
      variouswin:
        description: Award a player with a Badge.
        usage: /<command>
    permissions:
      rock.win:
        description: Give Rock Badge
        default: false
      water.win:
        description: Give Water Badge
        default: false
      electric.win:
        description: Give Electric Badge
        default: false
      grass.win:
        description: Give Grass Badge
        default: false
      poison.win:
        description: Give Poison Badge
        default: false
      psychic.win:
        description: Give Poison Badge
        default: false
      fire.win:
        description: Give Fire Badge
        default: false
      various.win:
        description: Give Various Badge
        default: false
     
  2. Offline

    TheEntropy

    @emilandersson91 It looks like the if statement for cmd.getName().equalsIgnoreCase("rockwin") spans most of your onCommand method. :eek:

    edit: that's at least what eclipse says
     
  3. Offline

    caderape

    @emilandersson91

    Your code will never work. what did u do with the brackets.

    You need to close them before to check a new command.
     
    candyfloss20 likes this.
  4. Offline

    candyfloss20

    Just looking at that formatting is making my eyes burn :eek:

    @emilandersson91
    Check the commands like this :
    Code:
        @Override
        public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
            if (command.getName().equalsIgnoreCase("Command1")) {
                if (sender.hasPermission("command.1")) {
                    sender.sendMessage("You can use /Command1 :)");
                }
                else {
                    sender.sendMessage("You can't use /Command1 :(");
                }
            }
            else if (command.getName().equalsIgnoreCase("Command2")) {
                if (sender.hasPermission("command.2")) {
                    sender.sendMessage("You can use /Command2 :)");
                }
                else {
                    sender.sendMessage("You can't use /Command2 :(");
                }
            }
            else if (command.getName().equalsIgnoreCase("Command3")) {
                if (sender.hasPermission("command.3")) {
                    sender.sendMessage("You can use /Command3 :)");
                }
                else {
                    sender.sendMessage("You can't use /Command3 :(");
                }
            }
        return true;
        }
    *PS : I would recommend putting each command in its own class file.*

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 10, 2016
  5. Offline

    emilandersson91

    I see, that looks so much cleaner, I tried doing it, Question is, would this example work?
    (No errors at all)
    (By the way, I started to make sense of the curly braces, no huge feat but no learning tools except trying!)

    Code:
    package me.emilandersson91;
    
    import org.bukkit.Bukkit;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class GymPromotion extends JavaPlugin {
       
        @Override
        public void onEnable() {
            getLogger().info("GymPromotion Plugin enabled");
        }
    
        @Override
        public void onDisable() {
            getLogger().info("GymPromotion Plugin disabled");
           
        }
       
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
            Player target = Bukkit.getServer().getPlayer(args[0]);
            if (cmd.getName().equalsIgnoreCase("rockwin")) {
                if (sender.hasPermission("rock.win") && !target.hasPermission("gymcheck.rock")) {
                    Bukkit.getServer().dispatchCommand(Bukkit.getConsoleSender(), "pex user"+target.getName()+"add gymcheck.rock");
                    Bukkit.getServer().dispatchCommand(Bukkit.getConsoleSender(), "give"+target.getName()+"4182");
                }
                else {
                    sender.sendMessage("You can't use this command");
                }
            }
            else if (cmd.getName().equalsIgnoreCase("waterwin")) {
                if (sender.hasPermission("water.win") && !target.hasPermission("gymcheck.water") && target.hasPermission("gymcheck.rock")) {
                    Bukkit.getServer().dispatchCommand(Bukkit.getConsoleSender(), "pex user"+target.getName()+"add gymcheck.water");
                    Bukkit.getServer().dispatchCommand(Bukkit.getConsoleSender(), "give"+target.getName()+"4183");
                }
                else {
                    sender.sendMessage("You can't use this command");
                }
            }
            else if (cmd.getName().equalsIgnoreCase("electricwin")) {
                if (sender.hasPermission("electric.win") && !target.hasPermission("gymcheck.electric") && target.hasPermission("gymcheck.rock") && target.hasPermission("gymcheck.water")) {
                    Bukkit.getServer().dispatchCommand(Bukkit.getConsoleSender(), "pex user"+target.getName()+"add gymcheck.electric");
                    Bukkit.getServer().dispatchCommand(Bukkit.getConsoleSender(),  "give"+target.getName()+"4209");
                }
                else {
                    sender.sendMessage("You can't use this command");
                }
            }
            return true;
        }
    }
    
    Big thanks for your involvation guys, it really helps and makes it much more fun!
     
Thread Status:
Not open for further replies.

Share This Page