Solved Returns usage

Discussion in 'Plugin Development' started by KarimAKL, Apr 1, 2018.

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

    KarimAKL

    I just created a plugin and i just tried it but it just returns the usage from the plugin.yml
    This is my command class:
    Code:
    public class ClassName implements CommandExecutor {
       
        public static final List<UUID> listName = new ArrayList<UUID>();
       
        private Main plugin;
       
        public ClassName(Main plugin) {
            this.plugin = plugin;
        }
    
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if (cmd.getName().equalsIgnoreCase("whatevercommandname")) {
                if (plugin.getConfig().getBoolean("whateverinconfig")) {
                    if (args.length == 1) {
                        if (sender.hasPermission("whateverpermission")) {
                            for (Player p : Bukkit.getOnlinePlayers()) {
                                if (p.getName().equals(args[0])) {
                                    if (listName.contains(p.getUniqueId())) {
                                        listName.remove(p.getUniqueId());
                                        sender.sendMessage(ChatColorUtil.chat(plugin.getConfig().getString("whateverinconfig")));
                                        return true;
                                    } else {
                                        listName.add(p.getUniqueId());
                                        sender.sendMessage(ChatColorUtil.chat(plugin.getConfig().getString("whateverinconfig")));
                                        return true;
                                    }
                                } else {
                                    sender.sendMessage(ChatColorUtil.chat("&cPlayer not found."));
                                    return true;
                                }
                            }
                        }
                    } else if (args.length == 0) {
                        sender.sendMessage(ChatColorUtil.chat("&cYou need to specify what player you wanna use this command on."));
                        return true;
                    }
     else {
                        sender.sendMessage(ChatColorUtil.chat("&cToo many args! Usage /whatevercommandname <player>"));
                    }
                }
            }
            return false;
        }
    
    }
    
    If you need to see anything other than this class then ask and i'll send it.
     
  2. Offline

    timtower Administrator Administrator Moderator

    @KarimAKL What message do you get before the usage?
     
  3. Offline

    KarimAKL

    @timtower Message before the usage? What do you mean? Like when i use it in-game? If so then the only thing that happens is that i get the usage, nothing else.
     
  4. Offline

    RcExtract

    According to the code, you have not add the insufficient permission message with an else clause. In that case, probably you did not permission "whateverpermission", so none of the if / else clause ran (cuz you probably met the args requirement) and finally returns false. When the onCommand method returns false, bukkit will print the usage.
     
  5. Offline

    KarimAKL

    @RcExtract Oh yeah i forgot to add the message if they don't have permission, thanks. I'll try adding that and see if it works. :)

    EDIT: I have added the message if they don't have permission but it didn't change anything, should i put messages in the class? (i think you call it debug or something like that)

    EDIT 2: I also added the message sent if the command isn't enabled in config (if (plugin.getConfig().getBoolean("whateverinconfig"))) and it still didn't change anything so should i try doing that debug thing?
     
    Last edited by a moderator: Apr 2, 2018
  6. Offline

    RcExtract

    Whats that debug thing?
    You forgot to add return true; after sender.sendMessage(ChatColorUtil.chat("&cToo many args! Usage /whatevercommandname <player>"));

    EDIT: As I remember, when some error occurs within onCommand, the usage will be printed.
     
  7. Offline

    KarimAKL

    @RcExtract I should return true with everything in the onCommand? I'll try doing that and see if it works, thanks.

    EDIT: It still doesn't work. This is my class right now:
    EDIT 2: I updated my code to my current one so here it is:
    Code:
    public class ClassName implements CommandExecutor {
       
        public static final List<UUID> listName = new ArrayList<UUID>();
       
        private Main plugin;
       
        public ClassName(Main plugin) {
            this.plugin = plugin;
        }
    
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if (cmd.getName().equalsIgnoreCase("whatevercommand")) {
                if (plugin.getConfig().getBoolean("whateverinconfig")) {
                    if (sender.hasPermission("whateverpermission")) {
                        if (args.length == 1) {
                            for (Player p : Bukkit.getOnlinePlayers()) {
                                if (p.getName().equalsIgnoreCase(args[0])) {
                                    if (listName.contains(p.getUniqueId())) {
                                        listName.remove(p.getUniqueId());
                                        sender.sendMessage(ChatColorUtil.chat(plugin.getConfig().getString("whateverinconfig")));
                                        if (plugin.getConfig().getBoolean("whateverinconfig")) {
                                            if (p.hasPermission("whateverpermission")) {
                                                if (p != sender) {
                                                    p.sendMessage(ChatColorUtil.chat(plugin.getConfig().getString("whateverinconfig")));
                                                }
                                            }
                                        }
                                        return true;
                                    } else {
                                        listName.add(p.getUniqueId());
                                        sender.sendMessage(ChatColorUtil.chat(plugin.getConfig().getString("whateverinconfig")));
                                        if (plugin.getConfig().getBoolean("whateverinconfig")) {
                                            if (p.hasPermission("whateverpermission")) {
                                                if (p != sender) {
                                                    p.sendMessage(ChatColorUtil.chat(plugin.getConfig().getString("whateverinconfig")));
                                                }
                                            }
                                        }
                                        return true;
                                    }
                                } else {
                                    sender.sendMessage(ChatColorUtil.chat(plugin.getConfig().getString("whateverinconfig")));
                                    return true;
                                }
                            }
                        } else if (args.length < 1) {
                            sender.sendMessage(ChatColorUtil.chat(plugin.getConfig().getString("whateverinconfig")));
                            return true;
                        } else if (args.length > 1) {
                            sender.sendMessage(ChatColorUtil.chat(plugin.getConfig().getString("whateverinconfig")));
                            return true;
                        }
                    } else {
                        sender.sendMessage(ChatColorUtil.chat(plugin.getConfig().getString("whateverinconfig")));
                        return true;
                    }
                } else {
                    sender.sendMessage(ChatColorUtil.chat(plugin.getConfig().getString("whateverinconfig")));
                    return true;
                }
            }
            return false;
        }
    
    }
    
    
     
    Last edited by a moderator: Apr 3, 2018
  8. Offline

    KarimAKL

    @RcExtract bump, i still have this problem and now even with 2 different classes. Any idea what it could be?
    Updated code from before:
    Code:
    public class ClassName implements CommandExecutor {
       
        public static final List<UUID> listName = new ArrayList<UUID>();
       
        private Main plugin;
       
        public ClassName(Main plugin) {
            this.plugin = plugin;
        }
    
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if (cmd.getName().equalsIgnoreCase("whatevercommand")) {
                if (plugin.getConfig().getBoolean("whateverinconfig")) {
                    if (!(sender instanceof Player)) {
                        Player ps = (Player) sender;
                        String whatever = ChatColorUtil.chat(plugin.getConfig().getString("whateverinconfig").replace("{whatever}", ps.getName()).replace("{whatever}", args[0]));
                        String whatever = ChatColorUtil.chat(plugin.getConfig().getString("whateverinconfig").replace("{whatever}", ps.getName()).replace("{whatever}", args[0]));
                        String whatever = ChatColorUtil.chat(plugin.getConfig().getString("whateverinconfig").replace("{whatever}", ps.getName()).replace("{whatever}", args[0]));
                        String whatever = ChatColorUtil.chat(plugin.getConfig().getString("whateverinconfig").replace("{whatever}", ps.getName()).replace("{whatever}", args[0]));
                        if (ps.hasPermission("whateverpermission")) {
                            if (args.length == 1) {
                                for (Player p : Bukkit.getOnlinePlayers()) {
                                    if (p.getName().equalsIgnoreCase(args[0])) {
                                        if (listName.contains(p.getUniqueId())) {
                                            listName.remove(p.getUniqueId());
                                            ps.sendMessage(whatever);
                                            if (plugin.getConfig().getBoolean("whateverinconfig")) {
                                                if (p.hasPermission("whateverpermission")) {
                                                    if (p != ps) {
                                                        p.sendMessage(whatever);
                                                        return true;
                                                    } else {
                                                        ps.sendMessage(ChatColorUtil.chat(plugin.getConfig().getString("whateverinconfig")));
                                                        return true;
                                                    }
                                                }
                                            }
                                        } else {
                                            listName.add(p.getUniqueId());
                                            ps.sendMessage(whatever);
                                            if (plugin.getConfig().getBoolean("whateverinconfig")) {
                                                if (p.hasPermission("whateverpermission")) {
                                                    if (p != ps) {
                                                        p.sendMessage(whatever);
                                                        return true;
                                                    } else {
                                                        ps.sendMessage(ChatColorUtil.chat(plugin.getConfig().getString("whateverinconfig")));
                                                        return true;
                                                    }
                                                }
                                            }
                                        }
                                    } else {
                                        ps.sendMessage(ChatColorUtil.chat(plugin.getConfig().getString("whateverinconfig")));
                                        return true;
                                    }
                                }
                            } else if (args.length < 1) {
                                ps.sendMessage(ChatColorUtil.chat(plugin.getConfig().getString("whateverinconfig")));
                                return true;
                            } else if (args.length > 1) {
                                ps.sendMessage(ChatColorUtil.chat(plugin.getConfig().getString("whateverinconfig")));
                                return true;
                            }
                        } else {
                            ps.sendMessage(ChatColorUtil.chat(plugin.getConfig().getString("whateverinconfig")));
                            return true;
                        }
                    } else {
                        String whatever = ChatColorUtil.chat(plugin.getConfig().getString("whateverinconfig").replace("{whatever}", "Console").replace("{whatever}", args[0]));
                        String whatever = ChatColorUtil.chat(plugin.getConfig().getString("whateverinconfig").replace("{whatever}", "Console").replace("{whatever}", args[0]));
                        String whatever = ChatColorUtil.chat(plugin.getConfig().getString("whateverinconfig").replace("{whatever}", "Console").replace("{whatever}", args[0]));
                        String whatever = ChatColorUtil.chat(plugin.getConfig().getString("whateverinconfig").replace("{whatever}", "Console").replace("{whatever}", args[0]));
                        if (args.length == 1) {
                            for (Player p : Bukkit.getOnlinePlayers()) {
                                if (p.getName().equalsIgnoreCase(args[0])) {
                                    if (listName.contains(p.getUniqueId())) {
                                        listName.remove(p.getUniqueId());
                                        sender.sendMessage(whatever);
                                        if (plugin.getConfig().getBoolean("whateverinconfig")) {
                                            if (p.hasPermission("whateverpermission")) {
                                                if (p != sender) {
                                                    p.sendMessage(whatever);
                                                    return true;
                                                }
                                            }
                                        }
                                    } else {
                                        listName.add(p.getUniqueId());
                                        sender.sendMessage(whatever);
                                        if (plugin.getConfig().getBoolean("whateverinconfig")) {
                                            if (p.hasPermission("whateverpermission")) {
                                                if (p != sender) {
                                                    p.sendMessage(whatever);
                                                    return true;
                                                }
                                            }
                                        }
                                    }
                                } else {
                                    sender.sendMessage(ChatColorUtil.chat(plugin.getConfig().getString("whateverinconfig")));
                                    return true;
                                }
                            }
                        } else if (args.length < 1) {
                            sender.sendMessage(ChatColorUtil.chat(plugin.getConfig().getString("whateverinconfig")));
                            return true;
                        } else if (args.length > 1) {
                            sender.sendMessage(ChatColorUtil.chat(plugin.getConfig().getString("whateverinconfig")));
                            return true;
                        }
                    }
                } else {
                    return false;
                }
            }
            return false;
        }
    
    }
    
    
    I have done all of the "whatever", "whateverinconfig", "whateverpermission" correctly in the code but i don't know what is wrong.
     
  9. Offline

    RcExtract

    How can u have 4 variables with the same name?
    The code starts becoming messy and overflowing. I would suggest you to make checks, and finally do what the command really does. For example:
    Code:
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
        if (cmd.getName().equalsIgnoreCase("suicide") {
            //First check
            if (!(sender.hasPermission("suicide"))) {
                sender.sendMessage("You are not permitted to do this!");
                return true;
            }
            //Second check
            if (!(sender instanceof Player)) {
                sender.sendMessage("You must be a player to suicide!");
                return true;
            }
            //Execute
            ((Player) sender).setHealth(0);
            return true;
        }
        //Command not processed
        return false;
    }
    Let's make the code compact before fixing the problem, cuz when the code is messy and overflowing, it will be hard for others to read (including me)
    • Remove the duplicated variables
    • When the args.length is not 1 and not smaller than 1, it must be bigger than 1
    • Declare variables for objects used many times
    • You dont want to loop through all players, check if their names are equal to the arg and execute what u do. Use Bukkit.getPlayer(args[?]) instead. (Actually they works the same, but the second method will reduce what u need to type in the file, and also reduce the jar size)
    • You dont want to cast sender to player when sender is not an instance of Player (are u ok?)
     
  10. Offline

    KarimAKL

    @RcExtract 1. What do you mean duplicated variables? If you mean the "String whatever = ChatColorUtil.chat(plugin.getConfig().getString("whateverinconfig").replace("{whatever}", "Console").replace("{whatever}", args[0]))" part then i did make them all have diffrent names they are not just known as whatever, but like whatever1, whatever2, whatever3, whatever4. 2. Does it really matter if i do > 1 or == 0? 3. What? I don't understand. 4. I see, i'll try changing that then, thanks. 5. I do "ps" for the player and "sender" for the console, what do you mean?
    EDIT: Whoops i meant "< 1" not "> 1"
     
    Last edited by a moderator: Apr 6, 2018
  11. Offline

    RcExtract

    I think it will be messy for me to help u with an example code but not ur real code. Plz post ur real code.
    Well you dont need to make ur code compact. It will not cause problems. But it will make u write something wrong easily.
    1. Understood
    2. Yeah ur right it does not matter
    3. Sometimes you want to use the same object from a method for multiple times. Declare a variable for it and refer to the variable instead of constantly calling the same method to reduce code required.
    4. Noted
    5. Line 16 "Player ps = (Player) sender;" will definitely cause ClassCastException.
     
  12. Offline

    KarimAKL

    @RcExtract I don’t really understand number 3 because i haven’t read Java, though i plan to. Could you explain what exactly i’m doing at line 16 and what i want to do?
     
  13. Offline

    RcExtract

    What do u mean by u haven't read Java?
    Line 16 is done inside the if (!(sender instanceof Player)) clause, which means the sender is definitely not an instance of Player, so casting the sender to Player will cause a ClassCastException.
     
  14. Offline

    KarimAKL

    @RcExtract I mean i don't know anything about it really, but i wanna learn. Okay so i should just move line 16 outside of the "(!(sender instanceof Player))" ? Or should i just remove it and use sender?
     
  15. Offline

    RcExtract

    So you don't know about inheritances. Try reading this.

    Let's take ur code as example. We have CommandSender sender as a parameter of the onCommand method, which Bukkit will pass a CommandSender into our method when executing. But note that CommandSender has no "final" keyword in the class (actually interface) declaration, which means it can be inherited by other classes. But how do we know what type of object will Bukkit put in? Player, ConsoleCommandSender, etc. they all inherit CommandSender. Therefore, we can use instanceof to compare a type of an object to a Class. And also, we use casting to access Player methods after ensuring that what Bukkit is going to pass in is Player but not other classes inheriting CommandSender like ConsoleCommandSender.

    Now get back into ur situation. You cast CommandSender to Player inside an if clause, where it is only executed when the real type of variable sender is NOT Player. As a result, casting CommandSender to Player in line 16 is illegal and a ClassCastException will be thrown to indicate this incident.

    Replying to ur problem: use CommandSender because sendMessage method is provided within it. Also, remove the if clause but keep the content inside because it is unnecessary anymore if u use CommandSender but not Player.

    EDIT: I also found something you can change. In the static list, change the type parameter UUID to Player. Then, compare players with == operator.
     
    Last edited: Apr 9, 2018
  16. Offline

    KarimAKL

    @RcExtract I don't know if this is what you meant but i found out that i had done "if (!(sender instanceof Player)) {" instead of "if (sender instanceof Player) {" so i changed that but it still comes with the usage. :/
     
  17. Offline

    RcExtract

    @KarimAKL We are not directly fixing the problem. But it is nearly impossible when u have a large code. We are fixing problems that can be seen easily as much as possible until a moment that there is only one problem, and we will know that is causing Bukkit to return usage. (You cannot refuse fixing the problems that can be seen easily cuz they will usually cause other problems, just probably we don't know currently)

    Keeping the if clause "if (sender instanceof Player)" is ok but note that the code inside only executes when a player executes the command.

    It is hard to help u when ur code is full of "whatever" because the real text of "whatever" MAY be what causing usage to be returned. Also, tiding up ur code is appreciated.
     
  18. Offline

    KarimAKL

    @RcExtract Okay, so you want me to write the code without all the "whatever"? If so then here it is:
    Code:
    public class CommandHacker implements CommandExecutor {
     
        public static List<UUID> hackerList = new ArrayList<UUID>();
     
        private Main plugin;
     
        public CommandHacker(Main plugin) {
            this.plugin = plugin;
        }
    
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if (cmd.getName().equalsIgnoreCase("hacker")) {
                if (plugin.getConfig().getBoolean("Hacker.enabled")) {
                    if (sender instanceof Player) {
                        Player ps = (Player) sender;
                        String removeHacker = ChatColorUtil.chat(plugin.getConfig().getString("Hacker.remove-hacker").replace("{player}", ps.getName()).replace("{hacker}", args[0]));
                        String broadcastRemove = ChatColorUtil.chat(plugin.getConfig().getString("Hacker.broadcast-remove").replace("{player}", ps.getName()).replace("{hacker}", args[0]));
                        String addHacker = ChatColorUtil.chat(plugin.getConfig().getString("Hacker.add-hacker").replace("{player}", ps.getName()).replace("{hacker}", args[0]));
                        String broadcastAdd = ChatColorUtil.chat(plugin.getConfig().getString("Hacker.broadcast-add").replace("{player}", ps.getName()).replace("{hacker}", args[0]));
                        if (ps.hasPermission("hacker.use")) {
                            if (args.length == 1) {
                                for (Player p : Bukkit.getOnlinePlayers()) {
                                    if (p.getName().equalsIgnoreCase(args[0])) {
                                        if (hackerList.contains(p.getUniqueId())) {
                                            hackerList.remove(p.getUniqueId());
                                            ps.sendMessage(removeHacker);
                                            if (plugin.getConfig().getBoolean("Hacker.staff-broadcast")) {
                                                if (p.hasPermission("hacker.see")) {
                                                    if (p != ps) {
                                                        p.sendMessage(broadcastRemove);
                                                        return true;
                                                    } else {
                                                        ps.sendMessage(ChatColorUtil.chat(plugin.getConfig().getString("Hacker.add-yourself-error")));
                                                        return true;
                                                    }
                                                }
                                            }
                                        } else {
                                            hackerList.add(p.getUniqueId());
                                            ps.sendMessage(addHacker);
                                            if (plugin.getConfig().getBoolean("Hacker.staff-broadcast")) {
                                                if (p.hasPermission("hacker.see")) {
                                                    if (p != ps) {
                                                        p.sendMessage(broadcastAdd);
                                                        return true;
                                                    } else {
                                                        ps.sendMessage(ChatColorUtil.chat(plugin.getConfig().getString("Hacker.add-yourself-error")));
                                                        return true;
                                                    }
                                                }
                                            }
                                        }
                                    } else {
                                        ps.sendMessage(ChatColorUtil.chat(plugin.getConfig().getString("Hacker.no-player-found")));
                                        return true;
                                    }
                                }
                            } else if (args.length < 1) {
                                ps.sendMessage(ChatColorUtil.chat(plugin.getConfig().getString("Hacker.no-player-specified")));
                                return true;
                            } else if (args.length > 1) {
                                ps.sendMessage(ChatColorUtil.chat(plugin.getConfig().getString("Hacker.incorrect-usage")));
                                return true;
                            }
                        } else {
                            ps.sendMessage(ChatColorUtil.chat(plugin.getConfig().getString("Hacker.no-perm")));
                            return true;
                        }
                    } else {
                        String removeHacker = ChatColorUtil.chat(plugin.getConfig().getString("Hacker.remove-hacker").replace("{player}", "Console").replace("{hacker}", args[0]));
                        String broadcastRemove = ChatColorUtil.chat(plugin.getConfig().getString("Hacker.broadcast-remove").replace("{player}", "Console").replace("{hacker}", args[0]));
                        String addHacker = ChatColorUtil.chat(plugin.getConfig().getString("Hacker.add-hacker").replace("{player}", "Console").replace("{hacker}", args[0]));
                        String broadcastAdd = ChatColorUtil.chat(plugin.getConfig().getString("Hacker.broadcast-add").replace("{player}", "Console").replace("{hacker}", args[0]));
                        if (args.length == 1) {
                            for (Player p : Bukkit.getOnlinePlayers()) {
                                if (p.getName().equalsIgnoreCase(args[0])) {
                                    if (hackerList.contains(p.getUniqueId())) {
                                        hackerList.remove(p.getUniqueId());
                                        sender.sendMessage(removeHacker);
                                        if (plugin.getConfig().getBoolean("Hacker.staff-broadcast")) {
                                            if (p.hasPermission("hacker.see")) {
                                                if (p != sender) {
                                                    p.sendMessage(broadcastRemove);
                                                    return true;
                                                }
                                            }
                                        }
                                    } else {
                                        hackerList.add(p.getUniqueId());
                                        sender.sendMessage(addHacker);
                                        if (plugin.getConfig().getBoolean("Hacker.staff-broadcast")) {
                                            if (p.hasPermission("hacker.see")) {
                                                if (p != sender) {
                                                    p.sendMessage(broadcastAdd);
                                                    return true;
                                                }
                                            }
                                        }
                                    }
                                } else {
                                    sender.sendMessage(ChatColorUtil.chat(plugin.getConfig().getString("Hacker.no-player-found")));
                                    return true;
                                }
                            }
                        } else if (args.length < 1) {
                            sender.sendMessage(ChatColorUtil.chat(plugin.getConfig().getString("Hacker.no-player-specified")));
                            return true;
                        } else if (args.length > 1) {
                            sender.sendMessage(ChatColorUtil.chat(plugin.getConfig().getString("Hacker.incorrect-usage")));
                            return true;
                        }
                    }
                } else {
                    return false;
                }
            }
            return false;
        }
    
    }
    
    
    This is my code right now, hope you can see the problem, also what do you mean that i should "note that the code inside only executes when a player executes the command"? I'm checking if the sender is instanceof Player so the code inside that should be if a player executes the command, right? (What i want is to check if the sender is a player or the console and then if it's the console i want to make it so that the console can use the command on players even if they are opped)
    EDIT: If you didn't already know then i want the command to be a toggle for the player in arg 1
     
  19. Offline

    RcExtract

    Wow this is much better. I forgot to told u two things on the last post:
    • Exceptions occurred inside onCommand will cause Bukkit to return usage because onCommand did not correctly return true
    • If ur variables were all "whatever", idk which "whatever" u are referring to when u are referring to a variable.
    Here are some corrections:
    1. Warning: In line 3, you should not use List<SOMETHING>, use Set<SOMETHING> instead. You are suggested to instantiate a HashSet<SOMETHING> and assign it to the static variable on line 3. Feel free to use any other Set implementations if HashSet does not provide all methods you want to use. You should not use list because you only want to store the reported players, and they are not required to be stored in order.
    2. Suggestion: In line 3, using a Set<UUID> to store reported hackers is not necessary. You can simply use Set<Player> if you are working with online players or Set<OfflinePlayer> if you are working for offline players or both online and offline players.
    3. Warning: Redundant Code. I found two clauses, one is executed when sender is Player and another is executed when sender is not Player. But i don't see the usage of guaranteeing that sender is a Player before executing the if clause "if (sender instanceof Player) {...}", because no methods which are only available through Player type are executed inside it. After using the search tool in chrome, i found that the only method that is executed on the CommandSender "sender" is "sendMessage", which is available from CommandSender, and not only Player. To fix this, remove the entire clause after "if (sender instanceof Player) {...}", which should look like "else {...}". Regarding to your feature request (You only want to check if Player has permission to execute command, but not for other type of CommandSender), i will tell you how to do it later.
    4. Suggestion: From line 23 to 24, you are trying to obtain the Player instance which its name is equal to the one provided by CommandSender. Use Bukkit.getPlayer(args[0]) instead. Don't worry about getting the wrong player, because the method will only return online players. Let's store "Bukkit.getPlayer(args[o])" into a variable called "target".
    5. Error: As mentioned in point 2, static variable hackerList is changed from Set<UUID> to Set<Player>, so we must change the code by removing all ".getUniqueId()", probably with the Eclipse text replacing tool.
    6. Warning: Redundant Code. From line 25 to 53, you have duplicated checks. Let's fix this step by step.
      • Both "add(Player)" and "remove(Player)" methods will return boolean which will tell you whether the execution worked out. For "add" method, it returns true if the collection did not originally contains the element, and false if contains. For "remove" method, it returns true if the collection did originally contains the element, and false if not contains. With one of the returned boolean value, we can remove the "hackerList.contains(Player)". Let's use "add(Player)" as example.
      • As u want to send messages differently according to whether the player is removed or added, we want to save a boolean from "hackerList.add(target)" to a variable like "added", then we want the statements below to determine whether messages for removing or adding hacker should be sent by this variable.
      • Then, we want to remove the target player if "add(Player)" returns false, which means the target player is already contained in hackerList originally:
        Code:Java
        1. if (!(added)) hackerList.remove(target);
      • Then, you want to send message to the CommandSender to let him or her confirm that the target player is added / removed. In this situation, the "BOOLEAN ? OBJECT : OBJECT" statement is recommended. This statement basically returns an object according to the boolean before the "?". If true, the first object (object before ":") will be returned, and otherwise, the second object (object after ":") will be returned. So, the code will be:
        Code:Java
        1. sender.sendMessage(added ? addHacker : removeHacker);
      • Then, continue what you want to do:
        Code:Java
        1. if (plugin.getConfig().getBoolean("Hacker.staff-broadcast")) {
        2. if (target.hasPermission("hacker.see")) {
        3. if (p != ps) {
        4. p.sendMessage(added ? broadcastAdd : broadcastRemove);
        5. return true;
        6. } else {
        7. ps.sendMessage(ChatColorUtil.chat(plugin.getConfig().getString("Hacker.add-yourself-error")));
        8. return true;
        9. }
        10. }
        11. }
      • Remove line 39 to 53
    7. You are near. Let's view the code after making modifications above:
      Code:Java
      1. public class CommandHacker implements CommandExecutor {
      2.  
      3. public static final Set<Player> hackerList = new HashSet<Player>();
      4.  
      5. private Main plugin;
      6.  
      7. public CommandHacker(Main plugin) {
      8. this.plugin = plugin;
      9. }
      10.  
      11. @Override
      12. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
      13. if (cmd.getName().equalsIgnoreCase("hacker")) {
      14. if (plugin.getConfig().getBoolean("Hacker.enabled")) {
      15. String removeHacker = ChatColorUtil.chat(plugin.getConfig().getString("Hacker.remove-hacker").replace("{player}", ps.getName()).replace("{hacker}", args[0]));
      16. String broadcastRemove = ChatColorUtil.chat(plugin.getConfig().getString("Hacker.broadcast-remove").replace("{player}", ps.getName()).replace("{hacker}", args[0]));
      17. String addHacker = ChatColorUtil.chat(plugin.getConfig().getString("Hacker.add-hacker").replace("{player}", ps.getName()).replace("{hacker}", args[0]));
      18. String broadcastAdd = ChatColorUtil.chat(plugin.getConfig().getString("Hacker.broadcast-add").replace("{player}", ps.getName()).replace("{hacker}", args[0]));
      19. if (sender.hasPermission("hacker.use")) {
      20. if (args.length == 1) {
      21. Player target = Bukkit.getPlayer(args[0]);
      22. boolean added = hackerList.add(target);
      23. if (!(added)) hackerList.remove(target);
      24. ps.sendMessage(added ? addHacker : removeHacker);
      25. if (plugin.getConfig().getBoolean("Hacker.staff-broadcast")) {
      26. if (p.hasPermission("hacker.see")) {
      27. if (p != ps) {
      28. p.sendMessage(added ? broadcastAdd : broadcastRemove);
      29. return true;
      30. } else {
      31. ps.sendMessage(ChatColorUtil.chat(plugin.getConfig().getString("Hacker.add-yourself-error")));
      32. return true;
      33. }
      34. }
      35. }
      36. } else {
      37. ps.sendMessage(ChatColorUtil.chat(plugin.getConfig().getString("Hacker.no-player-found")));
      38. return true;
      39. }
      40. } else if (args.length < 1) {
      41. ps.sendMessage(ChatColorUtil.chat(plugin.getConfig().getString("Hacker.no-player-specified")));
      42. return true;
      43. } else if (args.length > 1) {
      44. ps.sendMessage(ChatColorUtil.chat(plugin.getConfig().getString("Hacker.incorrect-usage")));
      45. return true;
      46. }
      47. } else {
      48. ps.sendMessage(ChatColorUtil.chat(plugin.getConfig().getString("Hacker.no-perm")));
      49. return true;
      50. }
      51. } else {
      52. return false;
      53. }
      54. }
      55. return false;
      56. }
      57.  
      58. }
      DO NOT SIMPLY COPY THE FINAL CODE. I POST IT FOR YOU TO CONFIRM THAT YOU MAKE CHANGES CORRECTLY, INSTEAD OF LETTING U TO COPY. IF YOU ONLY COPY AND DON'T DIGEST THE POINTS ABOVE, YOU WILL NOT LEARN JAVA.
    8. Error: Then you will have some error with brackets. Thats because we haven't change correctly the check that checks whether the player with that name exists (From line 36 to 39, that is the unchanged check). You want to execute the target removal or adding when the target with the name provided by CommandSender exists. Therefore, remove line 36 to 39, and wrap line 22 to 35 with an if clause "if (target != null) {move line 26 - 39 to here}". Then, add else clause behind. Within it, send
      Code:Java
      1. ChatColorUtil.chat(plugin.getConfig().getString("Hacker.no-player-found"))
    9. Warning: According to point 3, i said i will delay the task bolded there. When you want to check the CommandSender has or does not have a permission only when its real type is Player, do the following:
      Code:Java
      1. if (sender instanceof Player && !(sender.hasPermission("hacker.use"))) {
      2. sender.sendMessage(ChatColorUtil.chat(plugin.getConfig().getString("Hacker.no-perm")));
      3. return true;
      4. }
      This check will block CommandSender who is actually a player and without the permission from executing ur command. However, only check-formatted if clause works, so add the code before "if (args.length == 1)", then remove line 19, 43 to 46. So, the onCommand will only send the "Hacker.no-perm" message when sender is actually a player and without permission "hacker.use". If sent, return true.
    After modifications above, you final code should be:
    Code:Java
    1. public class CommandHacker implements CommandExecutor {
    2.  
    3. public static final Set<Player> hackerList = new HashSet<Player>();
    4.  
    5. private Main plugin;
    6.  
    7. public CommandHacker(Main plugin) {
    8. this.plugin = plugin;
    9. }
    10.  
    11. @Override
    12. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    13. if (cmd.getName().equalsIgnoreCase("hacker")) {
    14. if (plugin.getConfig().getBoolean("Hacker.enabled")) {
    15. String removeHacker = ChatColorUtil.chat(plugin.getConfig().getString("Hacker.remove-hacker").replace("{player}", ps.getName()).replace("{hacker}", args[0]));
    16. String broadcastRemove = ChatColorUtil.chat(plugin.getConfig().getString("Hacker.broadcast-remove").replace("{player}", ps.getName()).replace("{hacker}", args[0]));
    17. String addHacker = ChatColorUtil.chat(plugin.getConfig().getString("Hacker.add-hacker").replace("{player}", ps.getName()).replace("{hacker}", args[0]));
    18. String broadcastAdd = ChatColorUtil.chat(plugin.getConfig().getString("Hacker.broadcast-add").replace("{player}", ps.getName()).replace("{hacker}", args[0]));
    19. if (sender instanceof Player && !(sender.hasPermission("hacker.use"))) {
    20. sender.sendMessage(ChatColorUtil.chat(plugin.getConfig().getString("Hacker.no-perm")));
    21. return true;
    22. }
    23. if (args.length == 1) {
    24. Player target = Bukkit.getPlayer(args[0]);
    25. boolean added = hackerList.add(target);
    26. if (!(added)) hackerList.remove(target);
    27. ps.sendMessage(added ? addHacker : removeHacker);
    28. if (plugin.getConfig().getBoolean("Hacker.staff-broadcast")) {
    29. if (p.hasPermission("hacker.see")) {
    30. if (p != ps) {
    31. p.sendMessage(added ? broadcastAdd : broadcastRemove);
    32. return true;
    33. } else {
    34. ps.sendMessage(ChatColorUtil.chat(plugin.getConfig().getString("Hacker.add-yourself-error")));
    35. return true;
    36. }
    37. }
    38. }
    39. } else if (args.length < 1) {
    40. ps.sendMessage(ChatColorUtil.chat(plugin.getConfig().getString("Hacker.no-player-specified")));
    41. return true;
    42. } else if (args.length > 1) {
    43. ps.sendMessage(ChatColorUtil.chat(plugin.getConfig().getString("Hacker.incorrect-usage")));
    44. return true;
    45. }
    46. } else {
    47. return false;
    48. }
    49. }
    50. return false;
    51. }
    52.  
    53. }

    DO NOT SIMPLY COPY THE FINAL CODE. I POST IT FOR YOU TO CONFIRM THAT YOU MAKE CHANGES CORRECTLY, INSTEAD OF LETTING U TO COPY. IF YOU ONLY COPY AND DON'T DIGEST THE POINTS ABOVE, YOU WILL NOT LEARN JAVA.
     
  20. Offline

    KarimAKL

    @RcExtract Okay i just did this but the “p” and “ps” is deleted now so what should i do? For “ps” i could just change it to “sender” couldn’t i? But what would i do for “p”? (p is the player in arg 1 if you didn’t remember) EDIT: Would “p” be “target” instead? Edit2: How would i do this with UUID instead of Player? I tried changing “public static final Set<Player> hackerList = new HashSet<Player>();” to “public static final Set<UUID> hackerList = new HashSet<Player>();” but it came with an error so i pressed undo, anyway i can send you the current code when i’m back at my pc. Also could you explain what the “if (!(added)) hackerList.remove(target);“ does? I’ve tried to figure it out and i came up with “if target is not added then remove the target from hackerList” but that wouldn’t make any sense so that’s why i asked. EDIT3: My current code is:
    Code:
    public class CommandHacker implements CommandExecutor {
       
        public static final Set<Player> hackerList = new HashSet<Player>();
       
        private Main plugin;
       
        public CommandHacker(Main plugin) {
            this.plugin = plugin;
        }
    
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if (cmd.getName().equalsIgnoreCase("hacker")) {
                if (plugin.getConfig().getBoolean("Hacker.enabled")) {
                    String removeHacker = ChatColorUtil.chat(plugin.getConfig().getString("Hacker.remove-hacker").replace("{player}", sender.getName()).replace("{hacker}", args[0]));
                    String broadcastRemove = ChatColorUtil.chat(plugin.getConfig().getString("Hacker.broadcast-remove").replace("{player}", sender.getName()).replace("{hacker}", args[0]));
                    String addHacker = ChatColorUtil.chat(plugin.getConfig().getString("Hacker.add-hacker").replace("{player}", sender.getName()).replace("{hacker}", args[0]));
                    String broadcastAdd = ChatColorUtil.chat(plugin.getConfig().getString("Hacker.broadcast-add").replace("{player}", sender.getName()).replace("{hacker}", args[0]));
                    if (sender instanceof Player && !(sender.hasPermission("hacker.use"))) {
                        sender.sendMessage(ChatColorUtil.chat(plugin.getConfig().getString("Hacker.no-perm")));
                        return true;
                    } else {
                        if (args.length == 1) {
                            Player target = Bukkit.getPlayer(args[0]);
                            boolean added = hackerList.add(target);
                            if (!(added)) hackerList.remove(target);
                            if (target != null) {
                                sender.sendMessage(added ? addHacker : removeHacker);
                                if (plugin.getConfig().getBoolean("Hacker.staff-broadcast")) {
                                    if (target.hasPermission("hacker.see")) {
                                        if (target != sender) {
                                            target.sendMessage(added ? broadcastAdd : broadcastRemove);
                                            return true;
                                        } else {
                                            sender.sendMessage(ChatColorUtil.chat(plugin.getConfig().getString("Hacker.add-yourself-error")));
                                            return true;
                                        }
                                    }
                                }
                            } else {
                                sender.sendMessage(ChatColorUtil.chat(plugin.getConfig().getString("Hacker.no-player-found")));
                            }
                        } else if (args.length < 1) {
                            sender.sendMessage(ChatColorUtil.chat(plugin.getConfig().getString("Hacker.no-player-specified")));
                            return true;
                        } else if (args.length > 1) {
                            sender.sendMessage(ChatColorUtil.chat(plugin.getConfig().getString("Hacker.incorrect-usage")));
                            return true;
                        }
                    }
                } else {
                    return false;
                }
            }
            return false;
        }
    
    }
    
    I hope i did this correctly.
     
    Last edited by a moderator: Apr 11, 2018
  21. Offline

    RcExtract

    Great. Replying to ur problems:
    1. Yeah "ps" variable used to store the Player which is casted from CommandSender "sender", so use "sender".
    2. Yeah, i mean "target" by "p"
    3. If you don't want memory leaks, u can save UUIDs or save Players. But if you choose the save Players, remove the players from the Set when they leave. You can obtain UUID of a Player by doing "getUniqueId()", and you can obtain a Player with UUID by doing "getOnlinePlayer(UUID)"
    4. The logic come out to ur mind was right, just u did not understand it. Unlike List, Set only allow an object to be inside for once, so if target is not added, which means it is previously added, then remove it.
    Btw is Bukkit still returning usage?
     
  22. Offline

    KarimAKL

    @RcExtract Okay, thanks for that. :p And yes it still returns the usage. :/ And how would i get the UUID instead? As i said when i try chaning “public static final Set<Player> hackerList = new HashSet<Player>();” to “public static final Set<UUID> hackerList = new HashSet<Player>();” but it came with an error. :( And yes i know how to get the UUID of a player with the .getUniqueId() but the error is at the “public static final Set<UUID> hackerList = new HashSet<UUID>();” part.
     
  23. Offline

    RcExtract

    "Set<UUID>" line should not cause error. What's the error message?

    Besides, does the console display any error log before or after usage returned?
     
  24. Offline

    KarimAKL

    @RcExtract “public static final Set<UUID> hackerList = new HashSet<UUID>();” comes with the following error:
    Multiple markers at this line
    -UUID cannot be resolved to a type
    -UUID cannot be resolved to a type
     
  25. Offline

    Zombie_Striker

  26. Offline

    KarimAKL

    @Zombie_Striker Wow i’m an idiot. xD My bad. @RcExtract No it doesn’t come with an error in console when it returns usage. EDIT: I have a warning now, i can send you the code when i’m back at my pc
     
    Last edited by a moderator: Apr 11, 2018
  27. Offline

    RcExtract

    Actually i thought of that lol.
    Try to print variables and see if some are null.
     
  28. Offline

    KarimAKL

    @RcExtract Print variables? This is my current code:
    Code:
    public class CommandHacker implements CommandExecutor {
      
        public static final Set<UUID> hackerList = new HashSet<UUID>();
      
        private Main plugin;
      
        public CommandHacker(Main plugin) {
            this.plugin = plugin;
        }
    
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if (cmd.getName().equalsIgnoreCase("hacker")) {
                if (plugin.getConfig().getBoolean("Hacker.enabled")) {
                    String removeHacker = ChatColorUtil.chat(plugin.getConfig().getString("Hacker.remove-hacker").replace("{player}", sender.getName()).replace("{hacker}", args[0]));
                    String broadcastRemove = ChatColorUtil.chat(plugin.getConfig().getString("Hacker.broadcast-remove").replace("{player}", sender.getName()).replace("{hacker}", args[0]));
                    String addHacker = ChatColorUtil.chat(plugin.getConfig().getString("Hacker.add-hacker").replace("{player}", sender.getName()).replace("{hacker}", args[0]));
                    String broadcastAdd = ChatColorUtil.chat(plugin.getConfig().getString("Hacker.broadcast-add").replace("{player}", sender.getName()).replace("{hacker}", args[0]));
                    if (sender instanceof Player && !(sender.hasPermission("hacker.use"))) {
                        sender.sendMessage(ChatColorUtil.chat(plugin.getConfig().getString("Hacker.no-perm")));
                        return true;
                    } else {
                        if (args.length == 1) {
                            Player target = Bukkit.getPlayer(args[0]);
                            boolean added = hackerList.add(target.getUniqueId());
                            if (!(added)) hackerList.remove(target.getUniqueId());
                            if (target != null) {
                                sender.sendMessage(added ? addHacker : removeHacker);
                                if (plugin.getConfig().getBoolean("Hacker.staff-broadcast")) {
                                    if (target.hasPermission("hacker.see")) {
                                        if (target != sender) {
                                            target.sendMessage(added ? broadcastAdd : broadcastRemove);
                                            return true;
                                        } else {
                                            sender.sendMessage(ChatColorUtil.chat(plugin.getConfig().getString("Hacker.add-yourself-error")));
                                            return true;
                                        }
                                    }
                                }
                            } else {
                                sender.sendMessage(ChatColorUtil.chat(plugin.getConfig().getString("Hacker.no-player-found")));
                                return true;
                            }
                        } else if (args.length < 1) {
                            sender.sendMessage(ChatColorUtil.chat(plugin.getConfig().getString("Hacker.no-player-specified")));
                            return true;
                        } else if (args.length > 1) {
                            sender.sendMessage(ChatColorUtil.chat(plugin.getConfig().getString("Hacker.incorrect-usage")));
                            return true;
                        }
                    }
                } else {
                    return false;
                }
            }
            return false;
        }
    
    }
    
    The warning is at this part:
    Code:
    } else {
                                sender.sendMessage(ChatColorUtil.chat(plugin.getConfig().getString("Hacker.no-player-found")));
                                return true;
                            }
    
    The warning is "Dead code"
     
    Last edited by a moderator: Apr 12, 2018
  29. Offline

    RcExtract

    Dead code means the block will never be run logically.
    Hey, sorry. After Player target = Bukkit.getPlayer(args[0]), you cannot immediately do anything related to it until it is confirmed that target is not null. Move line 25 and 26 under if (target != null).
    No idea why there is dead code.

    EDIT: I get the reason. It is dead code because after Player target = Bukkit.getPlayer(args[0]) u do something with it without checking if it is not null, so if null NullPointerException will be thrown, if not null only the if (target != null) block will be executed. There is no chance for else {...} to be executed.
     
    Last edited: Apr 14, 2018
  30. Offline

    KarimAKL

    @RcExtract Yeah, i moved the "if (target != null) {" up 2 times as you said and it doesn't come with the "dead code" anymore, thanks. :D
    EDIT: Current code:
    Code:
    public class CommandHacker implements CommandExecutor {
      
        public static final Set<UUID> hackerList = new HashSet<UUID>();
      
        private Main plugin;
      
        public CommandHacker(Main plugin) {
            this.plugin = plugin;
        }
    
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if (cmd.getName().equalsIgnoreCase("hacker")) {
                if (plugin.getConfig().getBoolean("Hacker.enabled")) {
                    String removeHacker = ChatColorUtil.chat(plugin.getConfig().getString("Hacker.remove-hacker").replace("{player}", sender.getName()).replace("{hacker}", args[0]));
                    String broadcastRemove = ChatColorUtil.chat(plugin.getConfig().getString("Hacker.broadcast-remove").replace("{player}", sender.getName()).replace("{hacker}", args[0]));
                    String addHacker = ChatColorUtil.chat(plugin.getConfig().getString("Hacker.add-hacker").replace("{player}", sender.getName()).replace("{hacker}", args[0]));
                    String broadcastAdd = ChatColorUtil.chat(plugin.getConfig().getString("Hacker.broadcast-add").replace("{player}", sender.getName()).replace("{hacker}", args[0]));
                    if (sender instanceof Player && !(sender.hasPermission("hacker.use"))) {
                        sender.sendMessage(ChatColorUtil.chat(plugin.getConfig().getString("Hacker.no-perm")));
                        return true;
                    } else {
                        if (args.length == 1) {
                            Player target = Bukkit.getPlayer(args[0]);
                            if (target != null) {
                            boolean added = hackerList.add(target.getUniqueId());
                            if (!(added)) hackerList.remove(target.getUniqueId());
                                sender.sendMessage(added ? addHacker : removeHacker);
                                if (plugin.getConfig().getBoolean("Hacker.staff-broadcast")) {
                                    if (target.hasPermission("hacker.see")) {
                                        if (target != sender) {
                                            target.sendMessage(added ? broadcastAdd : broadcastRemove);
                                            return true;
                                        } else {
                                            sender.sendMessage(ChatColorUtil.chat(plugin.getConfig().getString("Hacker.add-yourself-error")));
                                            return true;
                                        }
                                    }
                                }
                            } else {
                                sender.sendMessage(ChatColorUtil.chat(plugin.getConfig().getString("Hacker.no-player-found")));
                                return true;
                            }
                        } else if (args.length < 1) {
                            sender.sendMessage(ChatColorUtil.chat(plugin.getConfig().getString("Hacker.no-player-specified")));
                            return true;
                        } else if (args.length > 1) {
                            sender.sendMessage(ChatColorUtil.chat(plugin.getConfig().getString("Hacker.incorrect-usage")));
                            return true;
                        }
                    }
                } else {
                    return false;
                }
            }
            return false;
        }
    
    }
    
    It still shows usage, what am i doing wrong?
    EDIT 2: I thought you were probably gonna ask this so i just told you before hand: It doesn't come with any errors or warnings in console or in the class.
     
Thread Status:
Not open for further replies.

Share This Page