Solved Custom config not saving

Discussion in 'Plugin Development' started by FoxinatorDev, Apr 6, 2020.

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

    FoxinatorDev

    Whenever I use the ability, the variable updates but the config does not. I have no idea what's wrong. If you need any more code, just let me know

    MagicPlayer.java
    Code:
    private int combatLevel, combatExperience;
        private int farmingLevel, farmingExperience;
        private int miningLevel, miningExperience;
    
        private int kills;
        private int deaths;
    
        private int health, maxHealth;
        private int defense;
        private int mana, maxMana;
        private int strength;
    
        private FileConfiguration data;
    
        private Player player;
    
        public MagicPlayer(Player player) {
            FileConfiguration data = DataHandler.getInstance().getPlayerData(player.getUniqueId());
            this.data = data;
            this.player = player;
    
            this.combatLevel = data.getInt("Data.Combat.Level");
            this.combatExperience = data.getInt("Data.Combat.Experience");
    
            this.farmingLevel = data.getInt("Data.Farming.Level");
            this.farmingExperience = data.getInt("Data.Farming.Experience");
    
            this.miningLevel = data.getInt("Data.Mining.Level");
            this.miningExperience = data.getInt("Data.Mining.Experience");
    
            this.kills = data.getInt("Data.Kills");
            this.deaths = data.getInt("Data.Deaths");
    
            this.health = data.getInt("Data.Health");
            this.maxHealth = data.getInt("Data.MaxHealth");
            this.defense = data.getInt(("Data.Defense"));
            this.mana = data.getInt("Data.Mana");
            this.maxMana = data.getInt("Data.MaxMana");
            this.strength = data.getInt("Data.Strength");
        }
    
    public int getMana() {
            return mana;
        }
    
        public void setMana(int mana) {
            this.mana = mana;
            data.set("Data.Mana", mana);
            DataHandler.getInstance().savePlayerData(player.getUniqueId());
        }
    
        public void addMana(int mana) {
            setMana(getMana() + mana);
        }
    
        public void removeMana(int mana) {
            setMana(getMana() - mana);
        }
    useAbility():
    Code:
    @Override
        public void useAbility(MagicPlayer magicPlayer, int level, float mana) {
            Player player = magicPlayer.getPlayer();
    
            magicPlayer.removeMana(getManaRequired());
    
            SoundUtils.playAllSound(player.getLocation(), Sound.FIREWORK_BLAST, 100, (int) 1.3);
    
            Location location = player.getLocation();
            Vector direction = location.getDirection().normalize();
            for(double i = 0; i < (getRadius(level)*mana); i+=0.2) {
                double x,y,z;
                x = direction.getX() * i;
                y = direction.getY() * i + 1.5;
                z = direction.getZ() * i;
                location.add(x,y,z);
                if(location.getBlock().getType() != Material.AIR) {
                    return;
                }
                player.getWorld().spigot().playEffect(location, Effect.COLOURED_DUST, 0, 1, 1, 1, random.nextInt(2), 1, 0, 64);
    
                for(Entity entity : player.getWorld().getNearbyEntities(location, 0.2, 0.2, 0.2)) {
                    if(entity instanceof LivingEntity) {
                        if(entity != player) {
                            player.sendMessage("HIT ENTITY");
                            location.subtract(x, y, z);
                            return;
                        }
                    }
                }
    
                location.subtract(x, y, z);
            }
        }
    DataHandler.java
    Code:
    package magicast.data;
    
    import magicast.main.Main;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.configuration.file.YamlConfiguration;
    import org.bukkit.entity.Player;
    
    import java.io.File;
    import java.io.IOException;
    import java.util.UUID;
    
    public class DataHandler {
    
        private File playerFolder;
    
        private Main main = Main.getPlugin(Main.class);
    
        private static DataHandler dataHandler = new DataHandler();
    
        public void createPlayerFolder() {
            if(!main.getDataFolder().exists()) {
                main.getDataFolder().mkdir();
            }
    
            playerFolder = new File(main.getDataFolder(), "players");
            if(!playerFolder.exists()) {
                playerFolder.mkdir();
            }
        }
    
        public void createPlayerData(Player player) {
            createPlayerFolder();
            String name = player.getName();
            int health = 100;
            int maxHealth = 100;
            int mana = 100;
            int maxMana = 100;
    
            int combatLevel = 0, combatExperience = 0;
            int farmingLevel = 0, farmingExperience = 0;
            int miningLevel = 0, miningExperience = 0;
    
            int kills = 0;
            int deaths = 0;
    
            File file = new File(playerFolder, player.getUniqueId().toString() + ".yml");
    
            if(file.exists()) {
                System.err.println("Error: Player data already exists!");
                return;
            } else {
                try {
                    file.createNewFile();
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
    
            FileConfiguration config = YamlConfiguration.loadConfiguration(file);
    
            config.set("Data.Name", name);
            config.set("Data.Health", health);
            config.set("Data.MaxHealth", maxHealth);
            config.set("Data.Mana", mana);
            config.set("Data.MaxMana", maxMana);
            config.set("Data.Combat.Level", combatLevel);
            config.set("Data.Combat.Experience", combatExperience);
            config.set("Data.Farming.Level", farmingLevel);
            config.set("Data.Farming.Experience", farmingExperience);
            config.set("Data.Mining.Level", miningLevel);
            config.set("Data.Mining.Experience", miningExperience);
    
            config.set("Data.Kills", kills);
            config.set("Data.Deaths", deaths);
    
            try {
                config.save(file);
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    
        public FileConfiguration getPlayerData(UUID uuid) {
            createPlayerFolder();
            File file = new File(playerFolder, uuid.toString() + ".yml");
    
            return YamlConfiguration.loadConfiguration(file);
        }
    
        public void savePlayerData(UUID uuid) {
            createPlayerFolder();
            File file = new File(playerFolder, uuid.toString() + ".yml");
    
            try {
                getPlayerData(uuid).save(file);
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    
        public boolean playerDataExists(UUID uuid) {
            boolean exists = false;
    
            createPlayerFolder();
            File file = new File(playerFolder, uuid + ".yml");
    
            if(file.exists()) {
                exists = true;
            }
    
            return exists;
        }
    
        public static DataHandler getInstance() {
            return dataHandler;
        }
    
    }
    
     
  2. Offline

    KarimAKL

    @FoxinatorDev A few questions:
    1. Which variable is updating?
    2. Where are you calling this method?
    3. I don't see you doing anything with the config in that method.
     
  3. Offline

    FoxinatorDev

    1. The "mana" variable is being updated. The variable can be found in MagicPlayer.java and the removeMana() method is used in the useAbility() method.
    2. setMana() is the main method, called by removeMana(), which is called in useAbility()
    3. In the setMana() method, I'm trying to set the value of "Data.Mana" in the player's data file.
     
  4. Offline

    KarimAKL

    @FoxinatorDev You are setting "Data.Mana" in the "MagicPlayer" class' field "data", and then saving the file from a new FileConfiguration that has no change.

    Solution: You need to set and save the same FileConfiguration instance.
     
  5. Offline

    FoxinatorDev

    Sorry for the late response, but it works now, thanks! :D
     
    KarimAKL likes this.
Thread Status:
Not open for further replies.

Share This Page