Solved Ranks sorted by order

Discussion in 'Spigot Plugin Development' started by 1Mangomaster1, Nov 18, 2019.

  1. Hai... I know this question is really common, but the only difference is that instead of making score groups and then it sorts on its own if you name it right, I used a HashMap that contains <UUID, Rank> Rank is an enum I made. The reasons I did that and not using score groups are:

    1. Way more efficient by code, instead of having many scores for many ranks you just have one hashmap
    2. The score method didn't work for my anyways lmao, like.. at all, I copied from the internet and it made errors... and the hashmap way worked but unsorted to groups. Is there a way to sort it altho I am using this hashmap method... btw if you wonder how I changed the ranks is using:

    p.setPlayerListName
    p.setDisplayName


    I appreciate every comment :D
     
  2. It may be a good idea for your Rank enum to contain some sort of parent/child reference.

    Code (Java):
    enum Rank {

        // ENUMS

        Rank parent;

        // Constructor, getters setters, other methods
    }
    Or if your ranks plugin uses some sort of number system, store that number
    This will make it easier to check if a players rank is their "highest" rank, then you can get prefix, suffix, etc.
     
  3. What are you trying to do exactly? Do you have a list of players, and you want to sort them based on their rank?
     
  4. I want to sort the tab list by ranks
     

  5. Honestly I have no idea how to work with that, all I know is that I can make an enum and put values in it like this:


    Code (Java):
    //assuming we are inside the enum class

    Rank1(),Rank2(),Rank3(),Rank4();

    //etc etc lol... this is all I know about doing enums
     
  6. Enums have a natural sort order based off of the underlying int that represents them. This means you can do any standard sort directly on the Enum value, and it will sort based off of the order that you put the Enums in the Enum class.
     
    • Agree Agree x 1
  7. No it doesn't.... I put them in order and yet one of my players as owner is after the other user which is member rank
     
  8. Can you show your Enum, as well as the code you are using to sort them at the moment?
     
  9. Alrighty:

    Enum:
    Code (Java):
    //Package info

    public enum Rank {

        Owner(), Admin(), Mod(), Helper(), Member(), None();

    }
    RankDisplay(Member rank does not display member but the type of magic that is used... fantasy magic thingy server theme)


    Code (Java):
    //Inside the BukkitRunnable to make sure rank always updates if changed

    if (plugin.Rank.get(p.getUniqueId()) == Rank.Owner) {
                    p.setPlayerListName(ChatColor.RED + "[Owner] " + ChatColor.WHITE + p.getName());
                    p.setDisplayName(ChatColor.RED + "[Owner] " + ChatColor.WHITE + p.getName());

                } else if (plugin.Rank.get(p.getUniqueId()) == Rank.Admin) {
                    p.setPlayerListName(ChatColor.BLUE + "[Admin] " + ChatColor.WHITE + p.getName());
                    p.setDisplayName(ChatColor.BLUE + "[Admin] " + ChatColor.WHITE + p.getName());

                } else if (plugin.Rank.get(p.getUniqueId()) == Rank.Mod) {
                    p.setPlayerListName(ChatColor.GREEN + "[Mod] " + ChatColor.WHITE + p.getName());
                    p.setDisplayName(ChatColor.GREEN + "[Mod] " + ChatColor.WHITE + p.getName());

                } else if (plugin.Rank.get(p.getUniqueId()) == Rank.Helper) {
                    p.setPlayerListName(ChatColor.YELLOW + "[Helper] " + ChatColor.WHITE + p.getName());
                    p.setDisplayName(ChatColor.YELLOW + "[Helper] " + ChatColor.WHITE + p.getName());

                } else if (plugin.Rank.get(p.getUniqueId()) == Rank.Member
                        && plugin.MagicType.get(p.getUniqueId()) == Magic.Fire) {
                    p.setPlayerListName(ChatColor.RED + "[Fire] " + ChatColor.WHITE + p.getName());
                    p.setDisplayName(ChatColor.RED + "[Fire] " + ChatColor.WHITE + p.getName());

                } else if (plugin.Rank.get(p.getUniqueId()) == Rank.Member
                        && plugin.MagicType.get(p.getUniqueId()) == Magic.Water) {
                    p.setPlayerListName(ChatColor.BLUE + "[Water] " + ChatColor.WHITE + p.getName());
                    p.setDisplayName(ChatColor.BLUE + "[Water] " + ChatColor.WHITE + p.getName());

                } else if (plugin.Rank.get(p.getUniqueId()) == Rank.Member
                        && plugin.MagicType.get(p.getUniqueId()) == Magic.Earth) {
                    p.setPlayerListName(ChatColor.DARK_GREEN + "[Earth] " + ChatColor.WHITE + p.getName());
                    p.setDisplayName(ChatColor.DARK_GREEN + "[Earth] " + ChatColor.WHITE + p.getName());

                } else if (plugin.Rank.get(p.getUniqueId()) == Rank.Member
                        && plugin.MagicType.get(p.getUniqueId()) == Magic.Wind) {
                    p.setPlayerListName(ChatColor.GRAY + "[Wind] " + ChatColor.WHITE + p.getName());
                    p.setDisplayName(ChatColor.GRAY + "[Wind] " + ChatColor.WHITE + p.getName());

                } else if (plugin.Rank.get(p.getUniqueId()) == Rank.Member
                        && plugin.MagicType.get(p.getUniqueId()) == Magic.Light) {
                    p.setPlayerListName(ChatColor.YELLOW + "[Light] " + ChatColor.WHITE + p.getName());
                    p.setDisplayName(ChatColor.YELLOW + "[Light] " + ChatColor.WHITE + p.getName());

                } else if (plugin.Rank.get(p.getUniqueId()) == Rank.Member
                        && plugin.MagicType.get(p.getUniqueId()) == Magic.Shadow) {
                    p.setPlayerListName(ChatColor.DARK_GRAY + "[Shadow] " + ChatColor.WHITE + p.getName());
                    p.setDisplayName(ChatColor.DARK_GRAY + "[Shadow] " + ChatColor.WHITE + p.getName());

                } else if (plugin.Rank.get(p.getUniqueId()) == Rank.Member
                        && plugin.MagicType.get(p.getUniqueId()) == Magic.Lightning) {
                    p.setPlayerListName(ChatColor.AQUA + "[Lightning] " + ChatColor.WHITE + p.getName());
                    p.setDisplayName(ChatColor.AQUA + "[Lightning] " + ChatColor.WHITE + p.getName());
                } else if (plugin.Rank.get(p.getUniqueId()) == Rank.Member
                        && plugin.MagicType.get(p.getUniqueId()) == Magic.Void) {
                    p.setPlayerListName(ChatColor.DARK_PURPLE + "[Void] " + ChatColor.WHITE + p.getName());
                    p.setDisplayName(ChatColor.DARK_PURPLE + "[Void] " + ChatColor.WHITE + p.getName());
                } else if (plugin.Rank.get(p.getUniqueId()) == Rank.None) {
                    p.setPlayerListName(ChatColor.WHITE + p.getName());
                    p.setDisplayName(ChatColor.WHITE + p.getName());
                }
     
  10. Well there is no code (that is shown) that has anything to do with sorting? The first thing that comes to my mind is get the list off players that are on the server, fetch all their ranks from the HashMap into a tuple, sort based off of rank (again you can use the natural value of the enum), then display the sorted results.

    If you have a repeating task doing this every few seconds, you won't notice any performance problem.
     
  11. I did not sort because I had no idea how to... so there is no code of sorting... I never really worked with tuples so I am not sure what it is but if I am not wrong it is like.. a list of connected items that doesn't necessarily have anything to do with each other??? And how will it sort if I pass it to a tuple? How to even sort it lmao.. I never have done anything like it.... sorry for noob questions
     
  12. There is a hacky workaround to get the tablist to order names. Essentialy before any name on the tablist you can prefix with something like this:
    Code (Text):
    &0&rPlayerName
    &0&rPlayerName
    &0&rPlayerName
    &1&rPlayerName
    &1&rPlayerName
    &2&rPlayerName
    The tablist should auto-sort itself based off of the number key format in front, and then the &r resets the format so you can apply your formatting after.

    All the players with &0 will show first, then the players &1 next, then &2, etc..

    This way you shouldn't have to change your code much.
     
  13. Maybe a bit too hacky.

    You don't have to mess around with prefixes etc for players names on tablist, you can sort them by team names.
    Ie:
    Code (Text):
    CREATE Team "A"
    ADD Players who are in corresponding group to team a

    Create Team "B"
    Add Players who are in corresponding group to team b
    Create all the teams correctly, and the tablist will autosort itself based on alphabetic sorting, ie a will be at the top, b will be the next etc.
     
  14. In the first message, I said that the team method did not really work well for me... This is why I try this way around to sort them, I will try what @Hunky524 said :)


    Addition:

    Altho I doubt it will work because if that was true then the ranks should have already sorted themselves in some order by the color code I gave them, and that didn't happen, but maybe just because I used ChatColor.Color and not & color codes
     
  15. To me it just looks like OP is struggling to understand the suggested solutions? Maybe I'm missing something but I don't see OP disregarding the suggestions like you're stating.

    Perhaps these links will help;
    The earlier suggestion of an enum could work. Problem with that is that you'd be hard coding ranks. Not exactly ideal when it can simply be made configurable, like any other permissions plugin.
     
  16. Ok so It took me long to realize how team even works, but I did, and I made it all by teams... the command works, when I use /rank it does switch the teams.. but it doesn't change the prefix altho I did set one... why?

    Btw, it's not that I didn't like the team solution, I just tried it before the first time I wanted ranks and it didn't work no matter what I tried, now it does work.. almost.. just need to display the prefixes


    Addition:

    I made a "RankManager" class with static actions such as setRank and getRank and etc... just to make it easier for me


    And I sent a message to the playyer whenever team changes to show the team, it does change. so Ik it is changing my teams properly, I just gotta realize why:

    1. Does not put prefixes
    2. Does not sort players by teams in tab list

    (I did call the teams by the names: a_owner b_admin c_mod d_helper e_member, so it will sort it alphabetically)
     
    #16 1Mangomaster1, Nov 19, 2019
    Last edited: Nov 19, 2019
  17. Oof... can anyone help me with it?? I can send you the code I used if you guys want... I just need your help :)
     
  18. This isn't using enums to their full potential. You can use enums a lot like classes, and have them store variables like the prefix.
    The rest of your code shows that you don't know java very well. I strongly suggest learning java before jumping into spigot development.

    enums are sorted into order internally. use Rank#values()
    We can't just spoonfeed you code. People have been trying to help but you need to learn fundamentals of java first.
     
    #18 AtomicZombies, Nov 20, 2019
    Last edited: Nov 20, 2019
  19. Lo no.. I completely changed my code....


    Here:


    In main class: Making the scoreboards:


    Code (Java):
    ScoreboardManager manager = Bukkit.getScoreboardManager();
        Scoreboard score = manager.getNewScoreboard();

        public Team owner = score.registerNewTeam("a_owner");

        public Team admin = score.registerNewTeam("b_admin");

        public Team mod = score.registerNewTeam("c_mod");

        public Team helper = score.registerNewTeam("d_helper");

        public Team member = score.registerNewTeam("e_member");


    Defining teams options also in main class:


    Code (Java):
    public void registerTeamsOptions() {
            owner.setPrefix("§0§c[Owner]");
            owner.setAllowFriendlyFire(true);
            owner.setCanSeeFriendlyInvisibles(false);

            admin.setPrefix("§1§9[Admin]");
            admin.setAllowFriendlyFire(true);
            admin.setCanSeeFriendlyInvisibles(false);

            mod.setPrefix("§1§a[Mod]");
            mod.setAllowFriendlyFire(true);
            mod.setCanSeeFriendlyInvisibles(false);

            helper.setPrefix("§1§e[Helper]");
            helper.setAllowFriendlyFire(true);
            helper.setCanSeeFriendlyInvisibles(false);

            member.setPrefix("§1§7[Member]");
            member.setAllowFriendlyFire(true);
            member.setCanSeeFriendlyInvisibles(false);
        }

    doesn't it suppose to change prefix to user once on that team??? They are on that team I checked using

    Code (Java):
    p.sendMessage("" + RankManager.getRank(p,plugin))
     
  20. Btw this is the RankManger class


    Code (Java):
    //Inside class

    public static Team getRank(Player tar, Arcane plugin) {
            if (plugin.owner.getEntries().contains(tar.getName()))
                return plugin.owner;
            else if (plugin.admin.getEntries().contains(tar.getName()))
                return plugin.admin;
            else if (plugin.mod.getEntries().contains(tar.getName()))
                return plugin.mod;
            else if (plugin.helper.getEntries().contains(tar.getName()))
                return plugin.helper;
            else if (plugin.member.getEntries().contains(tar.getName()))
                return plugin.member;
            else
                return null;
        }

        public static void setRank(Player p, String team, Arcane plugin) {
            if (team.equalsIgnoreCase("owner"))
                plugin.owner.addEntry(p.getName());
            else if (team.equalsIgnoreCase("admin"))
                plugin.admin.addEntry(p.getName());
            else if (team.equalsIgnoreCase("mod"))
                plugin.mod.addEntry(p.getName());
            else if (team.equalsIgnoreCase("helper"))
                plugin.helper.addEntry(p.getName());
            else if (team.equalsIgnoreCase("member"))
                plugin.member.addEntry(p.getName());
            else
                return;
        }