Solved Getting a players rank through enum

Discussion in 'Spigot Plugin Development' started by dznSpencer, May 23, 2016.

  1. Hello, I have setup a ranks enum with 5 different ranks, I'm storing the rank as an integer in a MySQL DB. I have a method to get the integer from the DB, But I'm trying to figure out how to turn the number into an enum value

    (See class)
    Code (Text):
    package com.nationsmc.core.ranks;

    import static com.nationsmc.core.utils.Chat.colorMsg;

    * Created by Spencer on 2016-05-23.
    public enum Ranks {

        DEFAULT(0, colorMsg("&eD"), colorMsg("&eDefault")), HELPER(1, colorMsg("&aH"), colorMsg("&aHelper")), MODERATOR(2, colorMsg("&bM"), colorMsg("&bModerator")), ADMIN(3, colorMsg("&cA"), colorMsg("&cAdministrator")), OWNER(4, colorMsg("&cO"), colorMsg("&cOwner"));

        private int rankNum;
        private String letter;
        private String name;

        Ranks (int rankNum, String letter, String name) {
            this.rankNum = rankNum;
            this.letter = letter;
   = name;

        public int getInt() {
            return rankNum;

        public String getLetter() {
            return letter;

        public String getName() {
            return name;

  2. Code (Text):
    int x = 12345;
  3. Cannot apply to an integer.
  4. In the enum class:
    Code (Java):

    public static intToRank(Integer i){
      for (Ranks r : Ranks.values(){
        if (r.getInt() == i) return r;
      return null;
  5. It's a Ranks object?..

    EDIT: I got it working, Thanks for the help!
  6. Keep in mind also that Enum#values() creates the array every time the method is called, so you may want to store it in a variable, and reuse that instead.