Custom Config not reloading?

Discussion in 'Spigot Plugin Development' started by Waldxn, Jun 11, 2017.

  1. So I tried to create a command that would change a double in my constant config. It is changing it, but I have an event that fires on EntityDamageByEntityEvent. In the event I use a Sysout to print the double from that config. I reload the config after running the command, but for some reason, the Sysout is reading the old double, as if I didn't change it. It does detect it after /reload though.

    Level.java:
    Code (Text):
    package me.waldxn.skills.cmds;

    import me.waldxn.skills.config.PlayerDataConfig;
    import org.bukkit.Bukkit;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;

    import java.io.File;
    import java.io.IOException;

    public class Level implements CommandExecutor {

        private PlayerDataConfig playerDataConfig = new PlayerDataConfig();
        private File playerDataFile = new File(Bukkit.getPluginManager().getPlugin("Skills").getDataFolder().getAbsolutePath() + "/playerdata.yml");

        @Override
        public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {

            if (!(sender.hasPermission("skills.level"))) {
                sender.sendMessage("You don't have permission to do that!");
                return false;
            }

            if (!(sender instanceof Player)) {
                sender.sendMessage("Must be a player to do this!");
                return false;
            }

            Player player = (Player) sender;

            if (args.length != 2) {
                return false;
            }

            try {
                Integer.parseInt(args[1]);
            } catch (NumberFormatException nfe) {
                player.sendMessage("Enter a level number");
                return false;
            }
            int level = Integer.parseInt(args[1]);

            if (args[0].equalsIgnoreCase("melee")) {
                playerDataConfig.getPlayerDataConfig().set("Players." + player.getName() + ".MeleeLevel", level);
                try {
                    playerDataConfig.getPlayerDataConfig().save(playerDataFile);
                } catch (IOException e) {
                    e.printStackTrace();
                    return false;
                }
                playerDataConfig.reloadPlayerDataConfig();
                System.out.println("Fired");
                return true;
            }
            return false;
        }
    }
     
    Melee.java:
    Code (Text):
    package me.waldxn.skills.skillhandlers;

    import me.waldxn.skills.config.PlayerDataConfig;
    import org.bukkit.Bukkit;
    import org.bukkit.configuration.Configuration;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.EntityDamageByEntityEvent;

    public class Melee implements Listener {

        private Configuration defaultConfig = Bukkit.getPluginManager().getPlugin("Skills").getConfig();
        private PlayerDataConfig playerDataConfig = new PlayerDataConfig();

        @EventHandler
        public void onPlayerAttack(EntityDamageByEntityEvent event) {
            /* Damage Modifiers */
            if (event.getDamager() instanceof Player) {
                int playerLevel = playerDataConfig.getPlayerDataConfig().getInt("Players." + event.getDamager().getName() + ".MeleeLevel");
                if (playerLevel <= 31 && playerLevel > 1) {
                    double modifier = defaultConfig.getDouble("Skills.Melee.DamageModifiers.Level " + playerLevel);
                    double newDamage = event.getDamage() * (event.getDamage() + modifier);
                    event.setDamage(newDamage);
                    System.out.println(playerLevel);
                } else if (playerLevel >= 32) {
                    double modifier = defaultConfig.getDouble("Skills.Melee.DamageModifiers.Level 31");
                    double newDamage = event.getDamage() * (event.getDamage() + modifier);
                    event.setDamage(newDamage);
                }
            }
        }
    }

     
    PlayerDataConfig.java:
    Code (Text):
    package me.waldxn.skills.config;

    import org.bukkit.Bukkit;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.plugin.Plugin;

    import java.io.File;

    public class PlayerDataConfig {

        public void PlayerData() {

            Bukkit.getLogger().info(Bukkit.getPluginManager().getPlugin("Skills").getDataFolder().getAbsolutePath() + "/playerdata.yml");

            File file = new File(Bukkit.getPluginManager().getPlugin("Skills").getDataFolder().getAbsolutePath() + "/playerdata.yml");

            if (!file.exists()) {

                CustomConfigConstructor customConfigConstructor = new CustomConfigConstructor("playerdata.yml", "playerdata.yml");

            }

        }
        private CustomConfigLoader customConfigLoader = new CustomConfigLoader();

        public FileConfiguration getPlayerDataConfig() {

            return customConfigLoader.getCustomConfig("playerdata.yml");

        }

        public void reloadPlayerDataConfig() {

            customConfigLoader.reloadCustomConfig("playerdata.yml");

        }

    }
     
    CustomConfigLoader.java:
    Code (Text):
    package me.waldxn.skills.config;

    import org.bukkit.Bukkit;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.configuration.file.YamlConfiguration;
    import org.bukkit.plugin.Plugin;

    import java.io.*;
    import java.util.logging.Level;

    public class CustomConfigLoader {

        private FileConfiguration customConfig = null;
        private File customConfigFile = null;
        private Plugin plugin = Bukkit.getPluginManager().getPlugin("Skills");

        public FileConfiguration getCustomConfig(String configName) {

            if (customConfig == null) {

                reloadCustomConfig(configName);

            }

            return customConfig;
        }

        public void reloadCustomConfig(String configName){

            if (customConfigFile == null) {

                customConfigFile = new File(plugin.getDataFolder(), configName);

            }

            customConfig = YamlConfiguration.loadConfiguration(customConfigFile);

            // Look for defaults in the jar
            Reader defConfigStream = null;

            try {

                defConfigStream = new InputStreamReader(plugin.getResource(configName), "UTF8");

            } catch (UnsupportedEncodingException e) {

                e.printStackTrace();

            }

            if (defConfigStream != null) {

                YamlConfiguration defConfig = YamlConfiguration.loadConfiguration(customConfigFile);
                customConfig.setDefaults(defConfig);

            }

        }

        public void saveCustomDefaultConfig(String configName) {

            if (customConfig == null || customConfigFile == null) {

                return;

            }

            try {

                getCustomConfig(configName).save(customConfigFile);

            } catch (IOException ex) {

                Bukkit.getLogger().log(Level.SEVERE, "Could not save config to " + customConfigFile, ex);

            }

        }

        public void saveDefaultCustomConfig(String configName) {

            if (customConfigFile == null) {

                customConfigFile = new File(plugin.getDataFolder(), configName);

            }

            if (!customConfigFile.exists()) {

                plugin.saveResource(configName, false);

            }

        }

    }
     
     
  2. Try to not cast it and send to a message this playerDataConfig.getPlayerDataConfig() without casting it. Tell me if it works, also if its not working, try to save it and load it in the same place(command)
     
  3. YamlConfiguration[path='', root='YamlConfiguration']

    EDIT: This actually didn't fix it. Thought it did but it didn't. Still need help reloading this config after the command.
     
    #3 Waldxn, Jun 11, 2017
    Last edited: Jun 11, 2017
  4. Changed thread back to unsolved. Thought problem was fixed but it wasn't. Here is current command class.
    Code (Text):
    package me.waldxn.skills.cmds;

    import me.waldxn.skills.config.PlayerDataConfig;
    import org.bukkit.Bukkit;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.configuration.InvalidConfigurationException;
    import org.bukkit.entity.Player;

    import java.io.File;
    import java.io.IOException;

    public class Level implements CommandExecutor {

        private PlayerDataConfig playerDataConfig = new PlayerDataConfig();
        private File playerDataFile = new File(Bukkit.getPluginManager().getPlugin("Skills").getDataFolder().getAbsolutePath() + "/playerdata.yml");

        @Override
        public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {

            if (!(sender.hasPermission("skills.level"))) {
                sender.sendMessage("You don't have permission to do that!");
                return false;
            }

            if (!(sender instanceof Player)) {
                sender.sendMessage("Must be a player to do this!");
                return false;
            }

            Player player = (Player) sender;

            if (args.length != 2) {
                return false;
            }

            try {
                Integer.parseInt(args[1]);
            } catch (NumberFormatException nfe) {
                player.sendMessage("Enter a level number");
                return false;
            }
            int level = Integer.parseInt(args[1]);

            if (args[0].equalsIgnoreCase("melee")) {
                playerDataConfig.getPlayerDataConfig().set("Players." + player.getName() + ".MeleeLevel", level);
                try {
                    playerDataConfig.getPlayerDataConfig().save(playerDataFile);
                    playerDataConfig.getPlayerDataConfig().load(playerDataFile);
                } catch (IOException | InvalidConfigurationException e) {
                    e.printStackTrace();
                    return false;
                }
                return true;
            }
            return false;
        }
    }
     
  5. Tip, should be using player#getUniqueId instead of player#getName.

    If player gains some levels, logs out, changes name, logs back in, they won't have those levels as you're saving just their name.
     
  6. Thanks, will edit this after fixing this issue.