Solved Config reset?

Discussion in 'Plugin Development' started by TerroDoor, Aug 3, 2019.

  1. Offline

    TerroDoor

    if i join with one account, i can change the amount of points in the yml and it still saves and loads properly. When i join with a second account, both players points get set back to 0, why?

    Code:
    
        @EventHandler
        public void onJoin(PlayerJoinEvent e) {
            Player p = (Player)e.getPlayer();
            if(!plugin.pDataConfig.contains(p.getName())) {       
               
                plugin.pDataConfig.set(p.getName(), 0);
                plugin.cfgm.saveStats();
               
            } else {
               
                p.sendMessage("welcome back");
                return;
            }
        }
    }
    
    
     
  2. Online

    Machine Maker

    Are you saving the configuration somewhere? I'm not sure if your saveStats() method does that.
     
  3. Offline

    TerroDoor

    @Machine Maker

    Yes i created a stats.yml
    this is my save method

    Code:
        public void saveStats() {
          
            try {
              
                plugin.pDataConfig.save(plugin.pData);
              
            }catch(IOException e) {
              
                e.printStackTrace();
            }
      
        }
    
    public File pData;
    public YamlConfiguration pDataConfig;

    plugin.pData = new File(plugin.getDataFolder(), "stats.yml");
    plugin.pDataConfig = YamlConfiguration.loadConfiguration(plugin.pData);
     
  4. Online

    Machine Maker

    Oh wait, you probably shouldn't be using contains to check if the value is already set. Try using isSet
     
  5. Offline

    TerroDoor

    @Machine Maker, thanks i tried that, when player 1 joins he gets set to 0, i changed it to 100 and joined back and it has saved the new amount.

    When player 2 joins, they both get reset to 0, How can i keep player 1 at 100 and player 2 stays 0?

    Here is my Config class:

    Code:
    
    public class ConfigManager {
      
    private Main plugin;
        public ConfigManager(Main instance) {
            plugin = instance;
        }
      
        public void setupConfig() {
            plugin.pData = new File(plugin.getDataFolder(), "stats.yml");
            plugin.pDataConfig = YamlConfiguration.loadConfiguration(plugin.pData);
          
          
            if (!plugin.getDataFolder().exists()) {
              
                plugin.getDataFolder().mkdir();
              
            }
            if (!plugin.pData.exists()) {
          
                try {
                  
                    plugin.pData.createNewFile();
                  
                    Bukkit.getServer().getConsoleSender().sendMessage("creating file...");
                  
                } catch(IOException e) {
                  
                    e.printStackTrace();
                }
            }
        }
      
        public void saveStats() {
          
            try {
              
                plugin.pDataConfig.save(plugin.pData);
              
            }catch(IOException e) {
              
                e.printStackTrace();
            }
      
        }
    }
    
    Here is my Main class:

    Code:
    
    public class Main extends JavaPlugin {
        public Location spawn = new Location(Bukkit.getServer().getWorld("world"), 0.5, 90, -0.5);
       
        public ConfigManager cfgm = new ConfigManager(this);
        public File pData;
        public YamlConfiguration pDataConfig;
       
        PluginManager pm = Bukkit.getServer().getPluginManager();
       
        public void onEnable() {
       
        pm.registerEvents(new Join(this), this);
       
        cfgm.setupConfig();
        cfgm.saveStats();
       
       
        }
       
        public void onDisable() {
           
        }
    }
    
    
    I've been stuck on this for nearly 2 days, please someone help my brain is hurting

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Aug 3, 2019
  6. Offline

    The_Xman249

    I repeated this , including the .isSet(playerName) method recommended and found it worked just fine. I used a fake name to add in the other player value (In the onJoinEvent) :
    Code:
     setPlayer("OtherPlayer");
    Using basically the same method you used to set the data:
    Code:
        private void setPlayer(String name){
        
            if (!plugin.pDataConfig.isSet(name)) {
    
                plugin.pDataConfig.set(name, 0);
                plugin.cfgm.saveStats();
    
            }
        }
    This indicates that the problem is caused by something somewhere else in your plugin . It's also worth storing the players UUID instead of the name. Other than that , i recommend printing the players name (in the PlayerJoinEvent) just before you set there name into the config to see more about what's going on. Hope this helps!
     
    Last edited: Aug 3, 2019
  7. Offline

    TerroDoor

    @The_Xman249 thanks for the help!

    here's what ive got. works well although if i join with player1 and leave then change the value in the config, it saves when i join back although when player2 joins player 1 gets set back to 0, im puzzled.

    listener i have so far

    Code:
    
     
           @EventHandler
            public void onJoin(PlayerJoinEvent e) {
                Player p = (Player)e.getPlayer();
                String uuid = p.getUniqueId().toString();
               
                if (!plugin.pDataConfig.isSet("points." + uuid)) {
                   
                    setPlayer(uuid);
                    plugin.cfgm.saveStats();
                    }
                }
            }
    
    setplayer method:

    Code:
    
        public void setPlayer(String p){
          
                plugin.pDataConfig.set("points." + p, 0);
                plugin.cfgm.saveStats();
              
                return;
            }
    
    
     
  8. Offline

    The_Xman249

    Do you stop the server when you change the value? If you don't that's why you are getting the problem . Basically you change the value in the config but that's not saved on Bukkit . So when you call the save method again (When player2 joins) , Bukkit overwrites the file (With all the data you saved last time you called the save method) and you loose the value you put in.
     
    Last edited: Aug 5, 2019
  9. Offline

    TerroDoor

    @The_Xman249 I've gone back and used Bukkit's default config to get a better understanding, and I have. Thanks for your last response, I understand what you mean.

    I saved in once in the onEnable:

    Code:
    
    public class Main extends JavaPlugin {
       
        //DATA
        public Location spawn = new Location(Bukkit.getServer().getWorld("world"), 0.5, 78, -0.5);
       
        public ArrayList<String> prot = new ArrayList<String>();
       
        public Inventory kits = Bukkit.createInventory(null, 9, "Select a kit!");
       
        PluginManager pm = Bukkit.getServer().getPluginManager();
       
       
        public void onEnable() {
           
           
            getCommand("help").setExecutor(this);
           
            pm.registerEvents(new Join(this), this);
            pm.registerEvents(new Prot(this), this);
            pm.registerEvents(new Kits(this), this);
           
            saveConfig();
        }
    
        public void onDisable() {
           
           
        }
    
    
    I then checked if the path contains the player, if not, set them to 0. And i saved it once:

    Code:
    
    public class Join implements Listener {
    
    
        private Main plugin;
    
        public Join(Main instance) {
            plugin = instance;
    
        }
    
    
        @EventHandler
        public void onJoin(PlayerJoinEvent e) {
    
            Player p = (Player)e.getPlayer();
           
            if (!plugin.getConfig().contains("points." + p.getName())) {
               
                plugin.getConfig().set("points." + p.getName(), 0);
               
                p.teleport(plugin.spawn);
                plugin.prot.add(p.getName());
    
                p.getInventory().addItem(new ItemStack(Material.FEATHER));
               
                plugin.saveConfig();           
               
            } else {
               
                p.teleport(plugin.spawn);
                plugin.prot.add(p.getName());
    
                p.getInventory().addItem(new ItemStack(Material.FEATHER));
               
                return;
               
            }
               
        }
    }
    
    
    And it worked! Looking back at my other file configation I get myself confused, is anyone able to help break down my last code and show me exactly where i went wrong? I understand i was saving over the config when player2 joined but i still dont fully understand where i went wrong. Thanks guys!
     
  10. Offline

    TerroDoor

    @The_Xman249 how do I save it without reloading the server so if I have multiple people joining at once it saves and loads everyones different amounts? I'm stuck again
     
  11. Offline

    The_Xman249

    You are are saving it . The problem you had before was modifying it and not saving it back into memory , so it got overwritten. If you want to modify the config when the server is running , you really want to be doing it in your plugin using the same methods you already are using .
     
    TerroDoor likes this.
  12. Offline

    TerroDoor

    @The_Xman249 Appreciate your response once again, I feel like I'm understanding but where exactly am I going wrong?

    So I need to create a method to update the config while the server is running?


    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited: Aug 7, 2019
  13. Offline

    The_Xman249

    Where you are going wrongs is trying to update config from the file its self , while the server is running . The way you should modify configs (when the server is running), is inside the plugin.

    When the player joins, you have it so each player that hasn't been added , gets added and put with a value of 0. If you want to change this amount e.g player /sells something , you need to set the new amount , and then save the config (just like how you do when the player joins).

    Otherwise , change the values once the server has stopped, so when the plugin gets enabled, you load the config and the config data gets loaded into bukkit.
     
  14. Offline

    TerroDoor

    @The_Xman249 Okay, inside my Join class i've created a getter and setter method for my config, does this look right?

    Code:
    
    public class Join implements Listener {
    
        private Main plugin;
    
        public Join(Main instance) {
            plugin = instance;
    
        }
    
          public int getPoints(String p) {
            
                FileConfiguration stats = YamlConfiguration.loadConfiguration(plugin.pData);
              
                int points = stats.get("points." + p) != null ? stats.getInt("points."+ p) : 0;
                  
                return points;
              
              }
        
          public void setPoints(String p) {
            
                File statsFile = new File(plugin.getDataFolder(), "stats.yml");
                FileConfiguration stats = YamlConfiguration.loadConfiguration(plugin.pData);
              
                stats.set("points." + p, getPoints(p));
              
                try  {
                stats.save(statsFile);
                } catch (IOException ex) {
                  ex.printStackTrace();
                }
              }
    
        @EventHandler
        public void onJoin(PlayerJoinEvent e) {
    
            Player p = (Player)e.getPlayer();
            String uuid = p.getUniqueId().toString();
          
            setPoints(uuid);
            plugin.prot.add(uuid);
            p.teleport(plugin.spawn);
          
            p.getInventory().setArmorContents(null);
            p.getInventory().addItem(new ItemStack(Material.FEATHER));
          
            return;
        }
    }
    
    I followed what you said and used similar method to my savestat method

    Also, where would this go? do I need it? i've seen it recommended in nearly ever config thread, whats it do?

    Code:
    
        pDataConfig.options().copyDefaults(true);
    
    
    @The_Xman249 After a bit of fiddling and reading through your suggestions it finally works how I needed it to! Appreciate your help a ton man here's how the new code is looking!

    Config Class:

    Code:
        public int getPoints(String p) {
    
            FileConfiguration stats = YamlConfiguration.loadConfiguration(plugin.pData);
    
            int points = stats.getInt("points."+ p);
    
            return points;
    
        }
    
        public void setPoints(String p) {
    
            File statsFile = new File(plugin.getDataFolder(), "stats.yml");
            FileConfiguration stats = YamlConfiguration.loadConfiguration(plugin.pData);
    
            stats.set("points." + p, getPoints(p));
           
            try  {
                stats.save(statsFile);
               
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    }
    
    
    Main and Listener:

    Code:
    
        public void onEnable() {
           
           
        pm.registerEvents(new Join(this), this);
       
        cfgm.setupConfig();
        cfgm.saveStats();
       
       
        @EventHandler
        public void onJoin(PlayerJoinEvent e) {
    
            Player p = (Player)e.getPlayer();
            String uuid = p.getUniqueId().toString();
    
            plugin.cfgm.setPoints(uuid);
           
           
            return;
        }
    }
       
    
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Aug 7, 2019

Share This Page