Custom Ranking System

Jul 22, 2017
Custom Ranking System
  • Hi there. By the end (if not during) this tutorial, you will be able to write a custom rank system. This is rated at a scale of intermediate. You should be familiar with Java Enums (http://docs.oracle.com/javase/tutorial/java/javaOO/enum.html) before starting.

    Starting off, we will create our Enum with the prefix in the constructor. This will allow us to keep things tidy and easily accessible.
    Code (Text):

    public enum Rank {

        OWNER(ChatColor.DARK_RED + "OWNER"),
        ADMIN(ChatColor.RED + "ADMIN"),
        MOD(ChatColor.GREEN + "MOD"),
        MEMBER("");

        private String prefix;

        Rank(String prefix) {
            this.prefix = prefix;
        }

        public String getPrefix() {
            return prefix;
        }
    }
    Now, we're going to need to store the player's rank. In this example, we will use a flat file storage system, but this can also be achieved through a database. To set up our config file, we will do the following in our onEnable and onDisable:
    Code (Text):

    @Override
    public void onEnable() {
        saveDefaultConfig();
    }

    @Override
    public void onDisable() {
        saveConfig();
    }
     
    In a desired class, the following methods will be used to access the stored ranks:
    Code (Text):

    private FileConfiguration config = Main.getInstance().getConfig();

    public void setRank(Player player, Rank rank) {
        config.set("Ranks." + player.getUniqueId(), rank.toString());
    }

    public Rank getRank(Player player) {
        String val = config.getString("Ranks." + player.getUniqueId());
        return (val == null ? Rank.MEMBER : Rank.valueOf(val));
    }

    public boolean hasRank(Player player, Rank rank) {
        return (getRank(player).compareTo(rank) <= 0);
    }
     
    As for the base of this system, you're done! You can now freely show the player's rank in chat, etc, with the previous methods. Happy coding!
  • Loading...
  • Loading...