Solved Looping through config to find a variable

Discussion in 'Plugin Development' started by Ducyooo, Oct 2, 2020.

  1. Offline

    Ducyooo

    Hi, im trying to make a player leveling system based on: mining, combat, ecc..
    I got a class that stores the experience and level of a player when he joins/quits (in the config under PlayerLevels) and I need to check if the player xp is greater or equal than the next level to level them up.
    Here is what i came up so far but i cant figure out a way to do it..

    This is the class:
    Code:
    public class Main extends JavaPlugin {
    
        public static HashMap<UUID, PlayerLevelManager> levelManagerHashMap;
        public ScoreboardManager scoreboardManager = new ScoreboardManager(this);
    
        @Override
        public void onEnable() {
            this.levelManagerHashMap = new HashMap<>();
            this.getServer().getPluginManager().registerEvents(new JoinQuitEvent(this), this);
            this.getServer().getPluginManager().registerEvents(new BlockBreakEvent(this), this);
            this.getServer().getPluginManager().registerEvents(new OpenCraftingTable(), this);
            this.getServer().getPluginManager().registerEvents(new Menu(), this);
            this.getConfig().options().copyDefaults(true);
            this.saveConfig();
        }
    
        @Override
        public void onDisable() {
            this.levelManagerHashMap.clear();
        }
    
        public void xpCheck(Player player, PlayerLevelManager playerLevelManager) {
            int playerCurrentLevel = getConfig().getInt("PlayerLevels." + player.getUniqueId() + ".level");
    
            for(String livelli : this.getConfig().getConfigurationSection("Mining").getKeys(false)){
                Bukkit.broadcastMessage(livelli);
                }
            int xpNeeded = this.getConfig().getInt("Mining.1.xp");
            int playerXp = playerLevelManager.getXp();
            if (playerXp >= xpNeeded) {
                player.sendMessage("ยง6Livello avanzato!");
                playerLevelManager.setLevel(playerLevelManager.getLevel() + 1);
            }
        }
    }
    This is my config:
    Code:
    PlayerLevels:
    Mining:
      1:
        xp: 100
        recipe:
      2:
        xp: 200
        recipe:
      3:
        xp: 400
        recipe:
      4:
        xp: 800
        recipe:
      5:
        xp: 1600
        recipe:
      6:
        xp: 3200
        recipe:
      7:
        xp: 6400
        recipe:
      8:
        xp: 12800
        recipe:
    Foraging:
      1:
        xp: 100
        recipe:
      2:
        xp: 200
        recipe:
      3:
        xp: 400
        recipe:
      4:
        xp: 800
        recipe:
      5:
        xp: 1600
        recipe:
      6:
        xp: 3200
        recipe:
      7:
        xp: 6400
        recipe:
      8:
        xp: 12800
        recipe:
    Combat:
      1:
        xp: 100
        recipe:
      2:
        xp: 200
        recipe:
      3:
        xp: 400
        recipe:
      4:
        xp: 800
        recipe:
      5:
        xp: 1600
        recipe:
      6:
        xp: 3200
        recipe:
      7:
        xp: 6400
        recipe:
      8:
        xp: 12800
        recipe:
     
  2. Offline

    Strahan

    Well, if I were doing it I'd create custom data objects for each role and in the object I'd have a Map<Integer, Integer> to store the XP table. Then I'd make a method for the object like getLevel(int xp) that would check the Map and return the level for the XP passed.

    If you don't want to dick with custom objects, you could either do a multidimensional map (like Map<String, Map<Integer, Integer>>) or a map for each role (like Map<Integer, Integer> miningXP & Map<Integer, Integer> foragingXP, etc)
     
  3. Offline

    Ducyooo

    Thanks for replying i got it figured out!! ;)
     

Share This Page