Solved Permission Rank not working?

Discussion in 'Plugin Development' started by __Sour, Feb 8, 2015.

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

    __Sour

    I have making a ranking system using enums and I am try to use the compareTo method, to see if a player has a rank that is defined to use a certain feature, and if the player has the rank defined or a rank higher up in the rankings return true and let the player use it, but it returns true no matter what rank the player has. I think I am messing up in the compareTo area in it any help?

    The ranks class:
    Code:
    package bukkit.seed.permissions;
    
    import org.bukkit.ChatColor;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.entity.Player;
    
    import bukkit.seed.Seed;
    import bukkit.seed.storage.Userdata;
    import bukkit.seed.utility.Chat;
    
    public class Rank {
       
        static Seed s;
        public static Rank ins = new Rank();
       
        public static Rank get() {
            return ins;
        }
       
        public static void setup(Seed sed) {
            s = sed;
        }
       
        public enum Ranks {
            OWNER("Owner", ChatColor.DARK_RED),
            DEVELOPER("Dev", ChatColor.DARK_GREEN),
            ADMIN("Admin", ChatColor.RED),
            MODERATOR("Mod", ChatColor.BLUE),
            HELPER("Helper", ChatColor.GREEN),
            ALL("All", ChatColor.AQUA);
           
            private ChatColor color;
            public String prefix;
           
            private Ranks(String prefix, ChatColor color) {
                this.prefix = prefix;
                this.color = color;
            }
           
            public String getTag(boolean bold, boolean uppercase) {
                String name = prefix;
                if (uppercase) {
                    name = prefix.toUpperCase();
                }
                if (bold) {
                    return color + "§l" + name;
                }
                return color + name;
            }
           
            public ChatColor getColor() {
                return color;
            }
           
        }
       
        String head = Chat.get().getHeader() + "Permissions > ";
        ChatColor body = Chat.get().getBody();
        ChatColor toggle = Chat.get().getToggle();
       
        public ChatColor getColor(Player player) {
            Ranks rank = getPlayerRank(player);
            return rank.getColor();
        }
       
        public String getRank(Player player) {
            Ranks rank = getPlayerRank(player);
            return rank.getTag(true, true) + ChatColor.RESET;
        }
       
        public String per(Player player) {
            return ChatColor.DARK_GRAY + "(" + getColor(player) + player.getName() + ChatColor.DARK_GRAY + ")" + Chat.get().getBody();
        }
       
        public String noPermission() {
            return head + "You don't have permission.";
        }
       
        public Ranks getPlayerRank(Player player) {
            boolean exists = Userdata.get().exists(player.getUniqueId().toString());
            FileConfiguration name = Userdata.get().getConfig(player.getUniqueId().toString());
            if (exists) {
                for (Ranks rank : Ranks.values()) {
                    if (name.getString("rank").equalsIgnoreCase(rank.toString())) {
                        return rank;
                    }
                }
            }
            return Ranks.ALL;
        }
       
        public boolean has(Ranks rank) {
            return has(null, rank, false);
        }
       
        public boolean has(Player player, Ranks rank, boolean inform) {
            for (Ranks all : Ranks.values()) {
                if (all.compareTo(rank) <= 0) {
                    return true;
                }
            }
            if (inform) {
                Chat.get().sendMessage(player, Chat.get().getHeader() + "Permissions > " + Chat.get().getBody() + "This requires Permission Rank [" + rank.getColor() + rank + body + "].");
            }
            return false;
        }
    
    }
    
    Rank enum order:
    Code:
    public enum Ranks {
            OWNER("Owner", ChatColor.DARK_RED),
            DEVELOPER("Dev", ChatColor.DARK_GREEN),
            ADMIN("Admin", ChatColor.RED),
            MODERATOR("Mod", ChatColor.BLUE),
            HELPER("Helper", ChatColor.GREEN),
            ALL("All", ChatColor.AQUA);
           
            private ChatColor color;
            public String prefix;
           
            private Ranks(String prefix, ChatColor color) {
                this.prefix = prefix;
                this.color = color;
            }
           
            public String getTag(boolean bold, boolean uppercase) {
                String name = prefix;
                if (uppercase) {
                    name = prefix.toUpperCase();
                }
                if (bold) {
                    return color + "§l" + name;
                }
                return color + name;
            }
           
            public ChatColor getColor() {
                return color;
            }
           
        }
    The method to check for a rank:
    Code:
        public boolean has(Ranks rank) {
            return has(null, rank, false);
        }
       
        public boolean has(Player player, Ranks rank, boolean inform) {
            for (Ranks all : Ranks.values()) {
                if (all.compareTo(rank) <= 0) {
                    return true;
                }
            }
            if (inform) {
                Chat.get().sendMessage(player, Chat.get().getHeader() + "Permissions > " + Chat.get().getBody() + "This requires Permission Rank [" + rank.getColor() + rank + body + "].");
            }
            return false;
        }
     
  2. Offline

    Konato_K

    @__Sour Why don't you use permissions?
     
  3. Offline

    __Sour

    @Konato_K Because this way will be a lot easier, then having to add a few hundred permissions to groups, which is annoying, and this way is more simple and lightweight, and I will have it connecting to a database soon so the ranks will be across all of my servers, and coding it like this means I don't have to wait for other developers that manage other permission plugins to update there plugins to new updates and what not.
     
  4. Offline

    Konato_K

    @__Sour I might be blind, but I don't see your "compareTo" method in the codes you show

    Anyway, the permissions API exist for a reason, it's better, more scalable and you don't have to reinvent the wheel, if you really need hundred of permissions then you should stop using whatever plugins you use that need hundred permissions.

    Anyway, back to your issue, try using Enum#ordinal?
     
  5. Offline

    __Sour

    @Konato_K I will try that and it is inside of the "has" method
    Code:
     public boolean has(Player player, Ranks rank, boolean inform) {
            for (Ranks all : Ranks.values()) {
                if (all.compareTo(rank) <= 0) {
                    return true;
                }
            }
            if (inform) {
                Chat.get().sendMessage(player, Chat.get().getHeader() + "Permissions > " + Chat.get().getBody() + "This requires Permission Rank [" + rank.getColor() + rank + body + "].");
            }
            return false;
        }
    "if(all.compareTo(rank) <= 0)"

    @Konato_K Tried ordinal and now it won't let me use the command at all no matter what rank I am, rather than eailer with anyone be able to use it.

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

    Konato_K

    @__Sour Maybe if I knew what the problem is...
     
  7. Offline

    __Sour

    @Konato_K The problem is, is that the boolean is return true, no matter what the rank a player has. Say if a command "/clean" request Rank admin to use, but your rank is just Helper(lower than admin) the helper can still use it because it is returning true.
     
  8. Offline

    Konato_K

    @__Sour
    Edit: Hell, I wasn't aware compareTo was a method in Enum types, anyway, now I see (yes, I didn't bother to read your code) you're looping through all the enums, that will give you 0 at some point.
     
    Last edited: Feb 8, 2015
  9. Offline

    __Sour

  10. Offline

    Konato_K

    @__Sour I don't think Enums extend a number, but yes you're right, I didn't realize that, check my updated answer above.
     
Thread Status:
Not open for further replies.

Share This Page