Please help!

Discussion in 'Spigot Plugin Development' started by ZevoGaems, Jun 6, 2017.

  1. I was working on group manager plugin and I imported this into the server but when I did "/rank bob123 Admin" I was testing it to make sure it notified staff but it didn't do anything. Here is what I have so far...
    Code (Text):
            if(cmd.getName().equalsIgnoreCase("rank")) {
                if(!sender.hasPermission("minegrid.rank")) {
                    sender.sendMessage(ChatColor.RED + "Requested Permission Level \n OWNER");
                } else if(args.length != 2) {
                    sender.sendMessage(ChatColor.RED + "Usage, /rank (player) (rank)");
                } else if(args.length == 2) {
                    String p1 = args[0];
                    Player p = Bukkit.getPlayer(p1);
                    String pname = p.getName();
                    String rank = args[1];
                    if(rank == "Owner") {
                        Bukkit.broadcast(ChatColor.RED + sender.getName() + " has promoted " + pname + " to Owner!", "minegrid.notify");
                    }
                    if(rank == "Admin") {
                        Bukkit.broadcast(ChatColor.RED + sender.getName() + " has promoted " + pname + " to Admin!", "minegrid.notify");
                    }
                    if(rank == "Dev") {
                        Bukkit.broadcast(ChatColor.RED + sender.getName() + " has promoted " + pname + " to Dev!", "minegrid.notify");
                    }
                    if(rank == "Builder") {
                        Bukkit.broadcast(ChatColor.RED + sender.getName() + " has promoted " + pname + " to Builder!", "minegrid.notify");
                    }
                    if(rank == "Moderator") {
                        Bukkit.broadcast(ChatColor.RED + sender.getName() + " has promoted " + pname + " to Mod!", "minegrid.notify");
                    }

                    if(rank == "Trainee") {
                        Bukkit.broadcast(ChatColor.RED + sender.getName() + " has promoted " + pname + " to Trainee!", "minegrid.notify");
                    }
                }
            }
     
  2. Can you post any errors you are getting? Also, check if player is null, in case they type an invalid player.

    EDIT: Also, make sure you are registering your commands in the Main class, and the plugin.yml
     
  3. You can try changing rank == "Owner" to rank.equals("Owner")
     
  4. Yes, didn't catch this. Compare strings using #equals, not ==.
     
  5. Instead of using
    Code (Text):
    (rank == "Owner")
    Use
    Code (Text):
    rank.equals("Owner")
     
  6. == is used to test for equal pointers. If you have a variable A with a pointer to 0xF7 and a variable B with a pointer to 0xF3, then A == B won't work.. If a is 0xF3 and B = 0F3, A == B will return true. It is the same with strings. Only there is one problem with strings, they have a lot of optimalization when saved in ram. When you say
    Code (Java):
    String A = "test";
    String B = "test";
    , then the compiler will set the two variables to the same pointer. They will become the same object and A == B will work. Wen using String.split(char) there are also some optimalizations that can make it so A == B will return true. However, when a String is created dynamicly, what is happening with the strings you are using, they will be two different pointers. The first pointer is to a place in the ram with the value "Owner". The second pointer (rank) will be a pointer to the value that is in the array. So when the argument string from bukkit command takes up 0x57 - 0x73 for example, the pointer of args[0] will be at 0x57 and the pointer of args[1] at 0x58. They will share the same value as the original string that has been split. The pointer to the argument string can never be the same of the "Owner" string (the arguments is dynamicly created) so the args[0] variable will never point to the same location as the "Owner" variable. That is why "Owner" == args[0] will not return true.
    Point me out if there is anything wrong about it.

    In short:
    Never, really never, use == to compare two variables unless you want to specifically test for the same pointer or they are primitive types

    In very short:

    Use .equals() method in the string class!
     
    • Like Like x 1