Custom Config Save

Discussion in 'Spigot Plugin Development' started by Goofan, May 26, 2016.

  1. Having an issue with my custom config not saving properly... Sitting with this 3 days now :(
    No Errors!

    Let's start with the Config: ( I create 1 Outpost and 3 Capitals (Erion, Celesti, Orion)
    Data.yml
    Code (Text):
    Capitals:
      '2':
        Name: Orion
        Color: ''
        King: ''
        Spawn:
          X: 59.02999756461196
          Y: 72.6255745688449
          Z: 384.70007449709345
    Objects:
      '0':
        Name: '3'
        Spawn:
          X: 59.02999756461196
          Y: 72.6255745688449
          Z: 384.70007449709345
     
    The ID for Erion is 0, Celesti 1, Orion 2
    it is merly saving the last entry which is driving me nuts...
    Can't seem to figure out why it is doing that...


    Alright so with the problem explained here is the code:
    ConfigManager.java
    Code (Text):
    package eu.kingconquest.conquest.utils;

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

    import org.bukkit.Location;
    import org.bukkit.configuration.InvalidConfigurationException;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.configuration.file.YamlConfiguration;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;

    import eu.kingconquest.conquest.Main;

    public class ConfigManager extends YamlConfiguration {  
       
        private File file;
        private String defaults;
        private JavaPlugin plugin;
       
        /**
         * Creates new PluginFile, without defaults
         * @param plugin - Your plugin
         * @param fileName - Name of the file
         */
        public ConfigManager(Main pl, String path, String fileName) {
            this(pl, path, fileName, null);
        }
        /**
         * Creates new PluginFile, with defaults
         * @param plugin - Your plugin
         * @param fileName - Name of the file
         * @param defaultsName - Name of the defaults
         */
        public ConfigManager(Main pl, String path, String fileName, String defaultsName) {
            plugin = pl;
            defaults = defaultsName;
           
            String pathway = (path == null) ? plugin.getDataFolder() + "\\" + fileName : plugin.getDataFolder() + "\\" + path + "\\" + fileName;
            file = new File(pathway);
            reload();
        }
       
        /**
         * Reload configuration
         */
        public void reload() {
            if (!file.exists()) {
                try {
                    file.getParentFile().mkdirs();
                    file.createNewFile();
                }catch (IOException exception){
                    exception.printStackTrace();
                    plugin.getLogger().severe("Error while creating file " + file.getName());
                }
            }
           
            try {
                load(file);
                if (defaults != null){
                    InputStreamReader reader = new InputStreamReader(plugin.getResource(defaults));
                    FileConfiguration defaultsConfig = YamlConfiguration.loadConfiguration(reader);      
                   
                    setDefaults(defaultsConfig);
                    options().copyDefaults(true);
                   
                    reader.close();
                    save();
                }
            }catch (IOException exception){
                exception.printStackTrace();
                plugin.getLogger().severe("Error while loading file " + file.getName());
               
            }catch (InvalidConfigurationException exception){
                exception.printStackTrace();
                plugin.getLogger().severe("Error while loading file " + file.getName());
            }
        }
       
        /**
         * Save configuration
         */
        public void save() {
            try {
                options().indent(2);
                save(file);
            }catch (IOException exception){
                exception.printStackTrace();
                plugin.getLogger().severe("Error while saving file " + file.getName());
            }
        }
       
        /**
         * Set Location to config
         * @param config - Instance of the Config File
         * @param pathway - Path in config
         * @param key - values to setup
         */
          public void setLocation(ConfigManager config, String pathway, Location key){
              if (pathway != null || key != null ){
                  config.set(pathway + ".X", key.getX());
                  config.set(pathway + ".Y", key.getY());
                  config.set(pathway + ".Z", key.getZ());
                  config.save();
                  return;
              }
          }
         
          /**
           * Get location from config
           * @param config - Instance of the Config File
           * @param pathway - Path in config
           * @param key - values to setup
           * @return
           */
          public Location getLocation(ConfigManager config, Player p, String pathway){
              Location loc = new Location(p.getWorld(), config.getDouble(pathway + ".X"), config.getDouble(pathway + ".Y"), config.getDouble(pathway + ".Z"));
              return loc;
          }
    }

    cach.java (Part to worry about is method: save2Config
    Code (Text):
    package eu.kingconquest.conquest.utils;

    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.Set;

    import org.bukkit.Bukkit;
    import org.bukkit.World;
    import org.bukkit.entity.Player;

    import eu.kingconquest.conquest.Main;
    import eu.kingconquest.conquest.io.Capital;
    import eu.kingconquest.conquest.io.Objective;
    import eu.kingconquest.conquest.io.Outpost;

    public class cach{

        //Configs Instances
        private ConfigManager conf;
        private ConfigManager lang;
        private ConfigManager data;
        private ConfigManager user;
       
    //Config File Cach
        //General
        public static HashMap<World, Long> CaptureRate = new HashMap<World, Long>();
        public static HashMap<World, Double> CapDistance = new HashMap<World, Double>();
        public static HashMap<World, Double> CapMaxHeight = new HashMap<World, Double>();
        public static HashMap<World, Double> CapMinHeight = new HashMap<World, Double>();
        public static int SyncTiming = 0;
        public static String Prefix;
        public static ArrayList<World> ActiveWorlds = new ArrayList<World>();
       
    //Dynamic placeholders
        public static String ObjectName = "";
        public static String CapitalName = "";
        public static String CapitalSpawn = "";
        public static String CapitalColor = "";
        public static String CapitalKing = "";
        public static String ListMember = "";
        public static String ListMembers = "";
        public static String ListCapturer = "";
        public static String ListCapturers = "";
        public static String CapColor = "";
       
        public static Player CapMember = null;
        public static Player ObjCapturer = null;
       
        public static Integer CapMembers = 0;
        public static Integer ObjCapturers = 0;
        public static Integer objID = 0;
        public static Integer capID = 0;
       
    //Language File Cach
        //General
        public static String JoinSuccess = "";
        public static String LeaveSuccess = "";

        //Capitals
        public static String CapSuccess = "";
        public static String editCapName = "";
        public static String editCapSpawn = "";
        public static String editCapColor = "";
        public static String editCapKing = "";
        public static String editObjName = "";
        public static String editObjSpawn = "";
       
        //Objects
        public static String ObjectCreated = "";
        public static String ObjectDeleted = "";
        public static String BeaconCreated = "";
        public static String BeaconDeleted = "";
        public static String CapitalCreated = "";
        public static String CapitalDeleted = "";
        public static String IDLookup = "";

        //Errors
        public static String NotFound = "";
        public static String CapFail = "";
        public static String CapNotFound = "";
        public static String editKingFailed = "";
        public static String editColorFailed = "";
        public static String AlreadyBelong = "";
       
        public static int CapsIDs = 0;
        public static int ObjsIDs = 0;
        public static int AllObjs = 0;
       

        //World currently in use
        protected static World WORLD = null;
       
        //Objects Cach
        public static HashMap<Player, Capital> PLAYERCAPITAL = new HashMap<Player, Capital>();
        public static HashMap<Objective, Capital> OBJECTIVECAPITAL = new HashMap<Objective, Capital>();
        public static HashMap<Outpost, Capital> OUTPOSTCAPITAL = new HashMap<Outpost, Capital>();
        public static HashMap<Outpost, Objective> OPOBJ = new HashMap<Outpost, Objective>();
       
        public static ArrayList<Capital> CAPITALS = new ArrayList<Capital>();
        public static ArrayList<Objective> OBJECTS = new ArrayList<Objective>();
        public static ArrayList<Outpost> OUTPOSTS = new ArrayList<Outpost>();
       
        /**
         * Startup Cach
         */
        public cach(Main plugin){
            conf = new ConfigManager(plugin, null, "Config.yml", "Config.yml");
            lang = new ConfigManager(plugin, null, "Language.yml", "Language.yml");
           
            //Config
                //General
            SyncTiming         = conf.getInt("Sync_Timing");
           
            // General
            AlreadyBelong     = lang.getString("Language.General.AlreadyBelong");
            JoinSuccess     = lang.getString("Language.General.JoinSuccess");
            LeaveSuccess     = lang.getString("Language.General.LeaveSuccess");
            Prefix             = lang.getString("Language.General.PluginPrefix");

            //Capitals
            ListMember         = lang.getString("Language.Capitals.Members");
            ListMembers     = lang.getString("Language.Capitals.ListMembers");
            editCapName     = lang.getString("Language.Capitals.editName");
            editCapSpawn     = lang.getString("Language.Capitals.editSpawn");
            editCapColor     = lang.getString("Language.Capitals.editColor");
            editCapKing     = lang.getString("Language.Capitals.editKing");
            editObjSpawn     = lang.getString("Language.Capitals.editSpawn");
           
            //Objectives
            ListCapturer     = lang.getString("Language.Capitals.Members");
            ListCapturers     = lang.getString("Language.Capitals.ListMembers");
            CapSuccess         = lang.getString("Language.Objectives.CapSuccess");
            CapFail         = lang.getString("Language.Objectives.CapFail");
            ObjectCreated     = lang.getString("Language.Objectives.ObjectCreated");
            ObjectDeleted     = lang.getString("Language.Objectives.ObjectDeleted");
            BeaconCreated     = lang.getString("Language.Objectives.BeaconCreated");
            BeaconDeleted     = lang.getString("Language.Objectives.BeaconDeleted");
            CapitalCreated     = lang.getString("Language.Objectives.CapitalCreated");
            CapitalDeleted     = lang.getString("Language.Objectives.CapitalDeleted");
            IDLookup         = lang.getString("Language.Objectives.IDLookup");
            editObjName     = lang.getString("Language.Objectives.editName");
            editObjSpawn     = lang.getString("Language.Objectives.editSpawn");
           
            //Errors
            NotFound         = lang.getString("Language.Errors.NotFound");
            editKingFailed     = lang.getString("Language.Errors.editColorFailed");
            editKingFailed     = lang.getString("Language.Errors.editKingFailed");
            CapNotFound     = lang.getString("Language.Errors.CapNotFound");
            AlreadyBelong     = lang.getString("Language.Errors.AlreadyBelong");

            Set<String> worlds = conf.getConfigurationSection("Active_Worlds").getKeys(false);

            for(World world: Bukkit.getWorlds()){
                if (worlds.contains(world.getName())){
                   
                    CaptureRate.put(world, conf.getLong("Active_Worlds." + world.getName() + ".Combat.CaptureRate"));
                    CapDistance.put(world, conf.getDouble("Active_Worlds." + world.getName() + ".Combat.CaptureDistance"));
                    CapMaxHeight.put(world, conf.getDouble("Active_Worlds." + world.getName() + ".Combat.CaptureAboveHeight"));
                    CapMinHeight.put(world, conf.getDouble("Active_Worlds." + world.getName() + ".Combat.CaptureBelowHeight"));
                   
                    ActiveWorlds.add(world);
                }
            }
        }
       

        public void save2Config(Main plugin){
            data = new ConfigManager(plugin, "Data", "Data.yml", null);
            user = new ConfigManager(plugin, "Data", "Users.yml", null);
           

            Set<String> worlds = conf.getConfigurationSection("Active_Worlds").getKeys(false);
            for(World world: Bukkit.getWorlds()){
                if (worlds.contains(world.getName())){
                    for(Capital cap : CAPITALS){
                        data.set("Capitals", cap.getID());
                        data.set("Capitals." + cap.getID() + ".Name", cap.getName());
                        data.set("Capitals." + cap.getID() + ".Color", cap.getColor());
                        data.set("Capitals." + cap.getID() + ".King", cap.getKing());
                        data.setLocation(data, "Capitals." + cap.getID() + ".Spawn", cap.getSpawn());
                        for (Player target : cap.getMembers()){
                            user.createSection(cap.getName() + "." + target.getUniqueId().toString());
                            user.save();
                        }
                        data.save();
                    }
                    for(Objective obj : OBJECTS){
                        data.set("Objects", obj.getID());
                        data.set("Objects." + obj.getID() + ".Name", obj.getName());
                        if (obj.getOwner() != null){
                            data.set("Objects." + obj.getID() + ".Owner", obj.getOwner().getName());
                        }
                        data.setLocation(data, "Objects." + obj.getID() + ".Spawn", obj.getSpawn());
                        data.save();
                    }
                    for(Outpost op : OUTPOSTS){
                        data.set("Objects", op.getID());
                        data.set("Objects." + op.getID() + ".Name", op.getName());
                        if (op.getOwner() != null){
                            data.set("Objects." + op.getID() + ".Owner", op.getOwner().getName());
                        }
                        data.set("Objects." + op.getID() + ".Parent", op.getParent());
                        data.setLocation(data, "Objects." + op.getID() + ".Spawn", op.getSpawn());
                        data.save();
                    }
                }
            }
        }
    }
     
    This is run on onDisable(); in main java file

    Thanks in Advance,
    Thomas
     
  2. Try to save the data after each for-loop iteration, not once the entire for-loop is done. Not sure if that will fix your issue, but it might.
     
  3. Isn't that exacly what I am doing?

    Code (Text):

    for(Outpost op : OUTPOSTS){
                        data.set("Objects", op.getID());
                        data.set("Objects." + op.getID() + ".Name", op.getName());
                        if (op.getOwner() != null){
                            data.set("Objects." + op.getID() + ".Owner", op.getOwner().getName());
                        }
                        data.set("Objects." + op.getID() + ".Parent", op.getParent());
                        data.setLocation(data, "Objects." + op.getID() + ".Spawn", op.getSpawn());
                        data.save(); <--- Saving it after each for loop iteration..
                }
     
    I have also tried saving it at the end of everything seems to get the same result :/
     
  4. Oh yea you are, sorry. I see that you are storing your OUTPOSTS in an ArrayList, but what/where are the OUTPOST object and how are you creating them?
     
  5. they are being created as I create the Oupost's simply I add the object Outpost into the arraylist OUTPOST.
    there is objects in there, but it is merly saving the last one to the config.
     
  6. Yea, but I'm just curious how you are saving/setting their IDs.
     
  7. in the object by a setter.

    Code (Text):
    public class Outpost{
    private int ID =  0;
        public void setID(int iD){
            ID = iD;
        }  
    }
    that is the basics
     
  8. Ok, where/how are you actually setting their IDs? Are you just setting them manually, if you could just show the part where you are setting the IDs that'd be great. Just so I can get a full picture of what is going on.
     
  9. yes manually...
    I know the ID's are correct as I am displaying them in chat as I create the object. incrementing them by 1 from the size of the arralist for each.
     
  10. I'm still looking for some help, have moved to MySQL meanwhile but I'd still like to figure this problem out