Solved Saving and reloading another config

Discussion in 'Spigot Plugin Development' started by --TOMAS--, Jun 13, 2016.

  1. Hi, I would like to help me with something I don't know.
    How to reload, save, etc a another config file? For example messages.yml
    I have this main for configs:
    Code (Text):
           private File configf, specialf, motdf, chatf, lobbyf;
         private FileConfiguration config, special, motd, chat, lobby;

      public FileConfiguration getSpecialConfig() {
            return this.special;
        }
        public FileConfiguration getMotdConfig() {
            return this.motd;
        }
        public FileConfiguration getChatConfig() {
            return this.chat;
        }
        public FileConfiguration getLobbyConfig() {
            return this.lobby;
        }

        private void createFiles() {

            configf = new File(getDataFolder(), "config.yml");
            specialf = new File(getDataFolder(), "messages.yml");
            motdf = new File(getDataFolder(), "motd.yml");
            chatf = new File(getDataFolder(), "chat.yml");
            lobbyf = new File(getDataFolder(), "lobby.yml");

            if (!configf.exists()) {
                configf.getParentFile().mkdirs();
                saveResource("config.yml", false);
            }
            if (!specialf.exists()) {
                specialf.getParentFile().mkdirs();
                saveResource("messages.yml", false);
             }
            if (!motdf.exists()) {
                motdf.getParentFile().mkdirs();
                saveResource("motd.yml", false);
             }
            if (!chatf.exists()) {
                chatf.getParentFile().mkdirs();
                saveResource("chat.yml", false);
             }
            if (!lobbyf.exists()) {
                lobbyf.getParentFile().mkdirs();
                saveResource("lobby.yml", false);
             }

            config = new YamlConfiguration();
            special = new YamlConfiguration();
            motd = new YamlConfiguration();
            chat = new YamlConfiguration();
            lobby = new YamlConfiguration();
            try {
                try {
                    config.load(configf);
                } catch (InvalidConfigurationException e) {
                    e.printStackTrace();
                }
                try {
                    special.load(specialf);
                } catch (InvalidConfigurationException e) {
                    e.printStackTrace();
                }
                try {
                    motd.load(motdf);
                } catch (InvalidConfigurationException e) {
                    e.printStackTrace();
                }
                try {
                    chat.load(chatf);
                } catch (InvalidConfigurationException e) {
                    e.printStackTrace();
                }
                try {
                    lobby.load(lobbyf);
                } catch (InvalidConfigurationException e) {
                    e.printStackTrace();
                }
            } catch (IOException e) {
                e.printStackTrace();
                }
            }

    Code (Text):
            if(cmd.getName().equalsIgnoreCase("setlobby")){
                if(args.length == 0){
                    if(player.hasPermission("ultralobby.setlobby")){
                    plugin.getLobbyConfig().set("Lobby.world", player.getWorld().getName());
                    plugin.getLobbyConfig().set("Lobby.x", Double.valueOf(player.getLocation().getX()));
                    plugin.getLobbyConfig().set("Lobby.y", Double.valueOf(player.getLocation().getY()));
                    plugin.getLobbyConfig().set("Lobby.z", Double.valueOf(player.getLocation().getZ()));
                    plugin.getLobbyConfig().set("Lobby.pitch", Double.valueOf(player.getLocation().getPitch()));
                    plugin.getLobbyConfig().set("Lobby.yaw", Double.valueOf(player.getLocation().getYaw()));
                                 *LINE TO SAVE LOBBY.YML CONFIG*

                    player.sendMessage(ChatColor.translateAlternateColorCodes('&', plugin.getLobbyConfig().getString("lobbySetted")));
                    }else{
                        player.sendMessage(sin_permisos);
                    }
                }
            }
     
  2. when you say reload you mean like the config command that reloads the changes ?
     
  3. Yes, and save changes.
     
  4. this is really easy you just have to take your new config and save it and load it in a same place and then in your command you reload it(saving and loading) i'm not really good with explaining but check this:
    Code (Text):
        private void reloadMarket(){
            try {
                MarketConfig.load(MarketFile);
                MarketConfig.save(MarketFile);
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (InvalidConfigurationException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    in your command you do this.reloadMarket();
     
  5. Thanks, but in place of reloadMarket i have to write reloadLobby, and in place of MarketFile i have to write lobbyf ??
     
  6. I cant understand, your code doesn't work. Please explain more.
    You can write a code using my variables?
     
  7. You can use this Config.
    Code (Java):

    //The config will be loaded from the file if it's exists, if not it will try to load it from a resource in your plugin if it exists
    IConfig config = new IConfig(plugin, "newconfig.yml");

    //If you want to reload the config
    config.reload();
     
     
  8. i can't do your code you just have to replace it with your own variables
    this is what i have and it works
    Code (Text):
        public File MarketFile= new File(getDataFolder(), "market.yml");
        public FileConfiguration MarketConfig= YamlConfiguration.loadConfiguration(MarketFile);
       
     
  9. The "Config" is a new class? and i can create more configs?
     
  10. The "IConfig" is on the link that i sent, so yeah it's a new class, to create a different config just change the path
    Code (Java):

    IConfig anotherConfig = new IConfig(plugin "anotherconfig.yml");
     
     
  11. Thanks, but.. The class "IChat" is the same always?
    EDIT: When server load, i have this error:

    Code (Text):
    [19:03:08] [Server thread/ERROR]: Error occurred while enabling UltraLobby v4.0 (Is it up to date?)
    java.lang.NullPointerException
        at me.FiesteroCraft.ultralobby.utils.IConfig.<init>(IConfig.java:28) ~[?:?]
        at me.FiesteroCraft.ultralobby.commands.lobbySystem.SetLobbyCmd.<init>(SetLobbyCmd.java:29) ~[?:?]
        at me.FiesteroCraft.ultralobby.Main.registerCommands(Main.java:169) ~[?:?]
        at me.FiesteroCraft.ultralobby.Main.onEnable(Main.java:57) ~[?:?]
        at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:321) ~[spigot-1.8.8.jar:git-Spigot-e4d4710-e1ebe52]
        at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:340) [spigot-1.8.8.jar:git-Spigot-e4d4710-e1ebe52]
        at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:405) [spigot-1.8.8.jar:git-Spigot-e4d4710-e1ebe52]
        at org.bukkit.craftbukkit.v1_8_R3.CraftServer.loadPlugin(CraftServer.java:357) [spigot-1.8.8.jar:git-Spigot-e4d4710-e1ebe52]
        at org.bukkit.craftbukkit.v1_8_R3.CraftServer.enablePlugins(CraftServer.java:317) [spigot-1.8.8.jar:git-Spigot-e4d4710-e1ebe52]
        at net.minecraft.server.v1_8_R3.MinecraftServer.s(MinecraftServer.java:414) [spigot-1.8.8.jar:git-Spigot-e4d4710-e1ebe52]
        at net.minecraft.server.v1_8_R3.MinecraftServer.k(MinecraftServer.java:378) [spigot-1.8.8.jar:git-Spigot-e4d4710-e1ebe52]
        at net.minecraft.server.v1_8_R3.MinecraftServer.a(MinecraftServer.java:333) [spigot-1.8.8.jar:git-Spigot-e4d4710-e1ebe52]
        at net.minecraft.server.v1_8_R3.DedicatedServer.init(DedicatedServer.java:263) [spigot-1.8.8.jar:git-Spigot-e4d4710-e1ebe52]
        at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:525) [spigot-1.8.8.jar:git-Spigot-e4d4710-e1ebe52]
        at java.lang.Thread.run(Unknown Source) [?:1.8.0_91]
     
  12. at me.FiesteroCraft.ultralobby.commands.lobbySystem.SetLobbyCmd.<init>(SetLobbyCmd.java:29)
     
  13. ?? Error in: IConfig config = new IConfig(plugin, "lobby.yml"); from class IConfig. But i dont know the solution.
     
  14. Check if they lobby.yml file exists
     
  15. Yes exists, i have this file on my project.
     
  16. Is your plugin variable null ? Also the file doesn't have to be created or exist.
     
  17. Yes, i have created the "lobby.yml", else, how to create the file? I main class? Because this
    Code (Text):
    IConfig lobbyconfig = new IConfig(plugin "plugin.yml");
    doesn't work in Main class
     
  18. Could you show me where do you define the plugin variable ?
     
  19. Here:
    Code (Text):
    package me.FiesteroCraft.ultralobby.lobbySystem;

    import org.bukkit.Location;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;

    import me.FiesteroCraft.ultralobby.IConfig;
    import me.FiesteroCraft.ultralobby.Main;
    import net.md_5.bungee.api.ChatColor;

    public class SetLobbyCmd implements CommandExecutor {

        IConfig lobbyconfig = new IConfig(plugin "plugin.yml");
        private Main plugin;

        public SetLobbyCmd(Main pl){
            plugin = pl;
        }


        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if(!(sender instanceof Player)){
                sender.sendMessage(ChatColor.DARK_RED + "UltraLobby: " + ChatColor.RED + "You cannot use this command form console!");
                return false;
            }
            Player player = (Player) sender;
            Location loc = player.getLocation();
            String sin_permisos = ChatColor.translateAlternateColorCodes('&', plugin.getSpecialConfig().getString("noPermission"));
            if(cmd.getName().equalsIgnoreCase("setlobby")){
                if(args.length == 0){
                    if(player.hasPermission("ultralobby.setlobby")){
                        lobbyconfig.set("Lobby.world", player.getWorld().getName());
                        lobbyconfig.set("Lobby.x", Double.valueOf(player.getLocation().getX()));
                        lobbyconfig.set("Lobby.y", Double.valueOf(player.getLocation().getY()));
                        lobbyconfig.set("Lobby.z", Double.valueOf(player.getLocation().getZ()));
                        lobbyconfig.set("Lobby.pitch", Double.valueOf(player.getLocation().getPitch()));
                        lobbyconfig.set("Lobby.yaw", Double.valueOf(player.getLocation().getYaw()));
                        //save config
                        lobbyconfig.save();
                        player.sendMessage(ChatColor.translateAlternateColorCodes('&', plugin.getLobbyConfig().getString("lobbySetted")));
                    }else{
                        player.sendMessage(sin_permisos);
                    }
                }
            }
            return false;
        }
    }
     
    And i have "lobby.yml" in my Project
     
  20. You can write an example?