Some Configuration Generation Problems

Discussion in 'Plugin Development' started by Mr Washington, Jul 19, 2012.

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

    Mr Washington

    Hello community,

    Now I am not one to normally have problems with generating configs, I can usually get them to work on the first try...But im at a stalemate with this one! For some reason the plugin just does not want to generate the config.yml and write defaults to it. It will generate an empty config.yml in the plugins data folder but it doesn't seem to write to it. My plugin is a private warping plugin to be used alongside of Essentials on my server (Private use) and all the warp data is saved in the config (Coordinates, name, list of players that can access it....) but when I try to set the warp i get Internal Errors with no stack trace from the console. Below is the source code of both the main class and the class that handles settings in the config:

    Main Class:
    Code:
    package me.itidez.plugins.iwarp;
     
    import java.util.EventListener;
    import java.util.HashMap;
    import java.util.List;
    import me.itidez.plugins.iwarp.commands.pWarp;
    import me.itidez.plugins.iwarp.commands.pWarpAddFriend;
    import me.itidez.plugins.iwarp.commands.pWarpRemFriend;
    import me.itidez.plugins.iwarp.commands.setpWarp;
    import net.milkbowl.vault.permission.Permission;
    import org.bukkit.ChatColor;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.plugin.RegisteredServiceProvider;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class Iwarp extends JavaPlugin implements Listener {
       
        public ErrorHandler eh;
        public Permission permission;
        public FileConfiguration config;
       
        @Override
        public void onDisable() {
            // TODO: Place any custom disable code here.
        }
     
        @Override
        public void onEnable() {
            config = this.getConfig();
            eh = new ErrorHandler(this);
            setupPermissions();
            getCommand("pwarp").setExecutor(new pWarp(this));
            getCommand("pwarpaf").setExecutor(new pWarpAddFriend(this));
            getCommand("pwarprf").setExecutor(new pWarpRemFriend(this));
            getCommand("setpwarp").setExecutor(new setpWarp(this));
            config.addDefault("settings.version", this.getDescription().getVersion());
            config.options().copyDefaults(true);
            saveConfig();
            reloadConfig();
        }
       
        private boolean setupPermissions()
        {
            RegisteredServiceProvider<Permission> permissionProvider = getServer().getServicesManager().getRegistration(net.milkbowl.vault.permission.Permission.class);
            if (permissionProvider != null) {
                permission = permissionProvider.getProvider();
            }
            return (permission != null);
        }
       
        @EventHandler
        public void onPrivateWarp(PrivateWarpEvent event) {
            Player p = event.getPlayer();
            List allowed = config.getList("warp."+event.getWarpName()+".players");
            if (allowed.contains(p.toString())) {
                p.teleport(event.getLoc());
                p.sendMessage(ChatColor.GRAY+"Teleported to "+event.getWarpName());
            } else if (!allowed.contains(p.toString())) {
                eh._(Error.PLAYERACCESS, p);
            }
        }
    }
    
    Config changing class (If it looks sketchy its because it used to run a custom config but it didnt work either)
    Code:
    /*
    * To change this template, choose Tools | Templates
    * and open the template in the editor.
    */
    package me.itidez.plugins.iwarp;
     
    import java.io.File;
    import java.io.IOException;
    import java.util.List;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import org.bukkit.Location;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.configuration.file.YamlConfiguration;
    import org.bukkit.entity.Player;
     
    /**
    *
    * @author tjs238
    */
    public class WarpHolder {
        private Iwarp plugin;
       
        public WarpHolder(String name, Iwarp plugin) {
            this.plugin = plugin;
        }
       
        public void setWarp(String name, Location loc, List players) {
            plugin.config.set("warp."+name+".name", name);
            plugin.config.set("warp."+name+".location.world", loc.getWorld().toString());
            plugin.config.set("warp."+name+".location.x", loc.getX());
            plugin.config.set("warp."+name+".location.y", loc.getY());
            plugin.config.set("warp."+name+".location.z", loc.getZ());
            plugin.config.set("warp."+name+".location.pitch", loc.getPitch());
            plugin.config.set("warp."+name+".location.yaw", loc.getYaw());
            plugin.config.set("warp."+name+".players", players);
            plugin.saveConfig();
        }
       
        public void addMembers(String name, String player) throws IOException {
            List owners = plugin.config.getList("warp."+name+".players");
            owners.add(player);
            plugin.config.set("warp."+name+".players", owners);
            plugin.saveConfig();
        }
       
        public void remMembers(String name, String player) throws IOException {
            List owners = plugin.config.getList("warp."+name+".players");
            owners.remove(player);
            plugin.config.set("warp."+name+".players", owners);
            plugin.saveConfig();
        }
       
    }
    
    any ideas would be greatly appreciated because I can not get this thing to work at all and its starting to annoy me since its such a simple task!

    Thanks!
     
  2. Offline

    Jogy34

    I think it's the reload config. In the javadocs it says "Discards any data in getConfig() and reloads from disk." which sounds to me like it erases everything in the config basically. I do my config files in a more non-conventional way because I like it better so I'm not 100% sure it that is right because I have never used the reloadConfig method.
     
  3. Offline

    Mr Washington

    Yes but I call the saveConfig() thread first which saves it to the flatfile doesn't it? Also this works in my other plugin in the same way so I dont know why its not working!
     
  4. you saying htread, mayby your not doing it on a thread safe way, or did you just used the wrong word?
     
  5. Offline

    Mr Washington

    I belive I used the wrong word here. Basically I have another plugin setup much the same way with configurations regarding saveConfig() and reloadConfig(). If saveConfig() saves it back to the flat file I called it before reloadConfig() which wipes the cached version of the variable config and restores it to the flat file version. But if I save to the flat file first, wouldn't that allow me to continue use of config if it saves it first?
     
  6. Why do you want to reload after saving? You're loading what you just saved, so nothing changes... Or do you expect he config file to get changed between saveConfig() and reloadConfig()? :confused:
    Anyway, if you (for whatever reason) really need to reload the config from disk make sure you are resetting your config variable, as it's no longer pointing to the real config. That means you need to do this:
    config = getConfig();
    after every reloadConfig();
     
Thread Status:
Not open for further replies.

Share This Page