custom config won't load on first start up, JoinEvent isn't triggered unless server reloads

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

  1. Offline

    TerroDoor

    If i simply export my plugin into my server and reload, the config works fine. However, if i remote close the server and start it back up, I have to reload the server for my config and JoinEvent listener to work? Otherwise it stores no information. Thanks!

    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("loading new YAML file...");
    
                } catch(IOException e) {
    
                    e.printStackTrace();
                }
            }
        }
    
        public int getPoints(String p) {
    
            YamlConfiguration stats = YamlConfiguration.loadConfiguration(plugin.pData);
    
            int points = stats.getInt("points." + p);
    
            return points;
    
        }
    
        public void setupPlayerPoints(String p) {
    
            File statsFile = new File(plugin.getDataFolder(), "stats.yml");
            YamlConfiguration stats = YamlConfiguration.loadConfiguration(plugin.pData);
    
            if (getPoints(p) > 0) return;
       
            stats.set("points." + p, 0);
    
            try  {
    
                stats.save(statsFile);
    
            } catch (IOException e) {
    
                e.printStackTrace();
    
            }
        }
    
    Main class:

    Code:
        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);
        pm.registerEvents(new Prot(this), this);
        pm.registerEvents(new Kits(this), this);
        pm.registerEvents(new Deaths(this), this);
       
        cfgm.setupConfig();
       
        }
       
        public void onDisable() {
           
        }
    
    Join listener:

    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();
            String uuid = p.getName().toString();
    
            p.getInventory().setArmorContents(null);
            p.getInventory().clear();
            p.getInventory().addItem(new ItemStack(Material.FEATHER));
    
            plugin.prot.add(p.getName());
            p.teleport(plugin.spawn);
           
            plugin.cfgm.setupPlayerPoints(uuid);
            return;
        }
    }
    
     
  2. Offline

    maxwell71

    add
    Code:
    reloadConfig();
    
    to your onDisable method.

    EDIT:
    Sorry, I thought you are using bukkits default config. You have to make a method yourself that reloads your config.
     
  3. Offline

    TerroDoor

    I've tried using .options.reloadConfig but can't seem to find out how to do it

    Sent from my ZTE BLADE A602 using Tapatalk
     
  4. Offline

    maxwell71

    Just make your own method, or save the config again.
     
  5. Offline

    KarimAKL

    @TerroDoor Use FileConfiguration#save(File file) and FileConfiguration#load(File file) to save and load the files.
     
  6. Offline

    TerroDoor

    @KarimAKL I did some revision and went back to using the default config and played around with the reloadConfig method, which works and updated the config fine, i still get the issue with the server having to be reloaded right after the first start to enable the plugin to work although it registers in my /pl before i reload, quite strange.

    When using custom configs, is it better to have a get() save() and reload() method to do this properly?

    Thanks for your time
     
  7. Offline

    KarimAKL

    @TerroDoor Could you explain exactly what you are doing?

    To reload:
    1. Make change in file manually.
    2. Save the file manually.
    3. Reload

    To save:
    1. Make change in file using code.
    2. Save the file using code.

    I've done this a lot of times and i haven't had any problems, i'm not sure what else could be wrong.
     
  8. Offline

    TerroDoor

    @KarimAKL okay i'll show my full code but basically i just creating a kill counter and saving each players kills to the config, it works but what im trying to solve is this:

    1. I export the plugin and delete the config file
    2. I run the server to generate a new config(works)
    3. I run /pl in the console(plugin works)
    4. Now here's where I'm confused. If i join my localhost without reloading the server in console i wont be added or saved to the config, nothing happens. However, everything else in the join event works fine, so the plugin is working, just not the config.
     

Share This Page