Solved Custom config file does not update after changing the values inside the plugin

Discussion in 'Spigot Plugin Development' started by Justin0406, Dec 22, 2019.

  1. (In advance, I'm sorry for the bad programming, I'm a beginner and still trying to improve)

    I am trying to make a factions plugin, but I ran into a small problem when trying to store all info about factions in a file.

    I made a command (/f create [name]) that should write to a file, for example

    Code (Text):
    /f create MyFaction
    makes this file:

    Code (YAML):
    MyFaction:
       Owner
    : (uuid of the owner)
    Disbanding the faction, using:

    Code (Text):
    /f disband
    deletes the faction from the file.

    However, when trying to use /f create again, the faction name and owner were not written to the file.

    Here is the code involved with these commands and the file

    Factions Class (Faction Commands):

    Code (Java):
    package io.github.JustinBerke0406.morecommands;

    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.World;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;

    import io.github.JustinBerke0406.files.CustomConfig;
    import io.github.JustinBerke0406.files.FactionsFile;

    public class Factions
    {
        public static boolean command(String[] args, CommandSender sender)
        {
            if (!(sender instanceof Player))
            {
                sender.sendMessage("You must be a player to use this command");
             
                return true;
            }
         
            Player p = (Player) sender;
         
            if (args.length < 1)
            {
                //List commands
             
                return true;
            }
         
            //f create
            if (args[0].equalsIgnoreCase("create"))
            {
                if (FactionsFile.isOwner(p))
                {
                    p.sendMessage(ChatColor.RED + "You already own a faction! Disband it to make a new one");
                 
                    return true;
                }
             
                if (args.length == 1)
                {
                    p.sendMessage(ChatColor.RED + "You must enter the name of the faction you want to create!");
                 
                    return true;
                }
             
                if (args.length > 2)
                {
                    p.sendMessage(ChatColor.RED + "You cannot have spaces in the name!");
                 
                    return true;
                }
             
                if (FactionsFile.get().contains(args[1]))
                {
                    p.sendMessage(ChatColor.RED + "That faction already exists!");
                 
                    return true;
                }
             
                String name = args[1];
                String world = CustomConfig.get().getString("Factions World");
             
                World factions = Bukkit.getWorld(world);
             
                if (factions == null)
                {
                    p.sendMessage(ChatColor.GREEN + world + ChatColor.RED + " does not exist!");
                 
                    return true;
                }
             
                if (!(p.getWorld().equals(factions)))
                {
                    p.sendMessage(ChatColor.RED + "You are not allowed to make a faction in this world");
                 
                    return true;
                }
             
                FactionsFile.addFaction(name, p.getUniqueId().toString());
             
                p.sendMessage(ChatColor.GOLD + "You have created " + ChatColor.GREEN + name);
             
                return true;
            }
         
            //f disband
            if (args[0].equalsIgnoreCase("disband"))
            {
                String fact;
             
                if (!FactionsFile.isOwner(p))
                {
                    p.sendMessage(ChatColor.RED + "You need to own a faction to disband one!");
                 
                    return true;
                }
             
                fact = FactionsFile.ownsWhatFaction(p);
             
                FactionsFile.removeFaction(fact);
             
                p.sendMessage(ChatColor.GREEN + fact + ChatColor.GOLD +" has been disbanded!");
             
                return true;
            }
         
            return false;
        }
    }
     
    FactionsFile Class (involved with the creation of the file and appending to it)

    Code (Java):
    package io.github.JustinBerke0406.files;

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

    import org.bukkit.Bukkit;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.configuration.file.YamlConfiguration;
    import org.bukkit.entity.Player;

    public class FactionsFile
    {
        private static File file;
        private static FileConfiguration fac;
     
        public static void setup()
        {
            file = new File(Bukkit.getPluginManager().getPlugin("MoreCommands").getDataFolder(), "factions.yml");
         
            if (!file.exists())
            {
                try
                {
                    file.createNewFile();
                }
                catch (IOException e)
                {
                    e.printStackTrace();
                }
            }
         
            fac = YamlConfiguration.loadConfiguration(file);
        }
     
        public static FileConfiguration get()
        {
            return fac;
        }
     
        public static void save()
        {
            try
            {
                fac.save(file);
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
        }
     
        public static void reload()
        {
            fac = YamlConfiguration.loadConfiguration(file);
        }
     
        public static void addFaction(String faction, String owner)
        {
            fac.addDefault(faction + ".Owner", owner);
         
            save();
        }
     
        public static void removeFaction(String faction)
        {
            fac.set(faction, null);
         
            save();
        }
     
        public static String[] allFactions()
        {
            Object[] keyObject = fac.getKeys(false).toArray();
            String[] keys = new String[keyObject.length];
         
            for (int i = 0; i < keyObject.length; i++)
            {
                keys[i] = keyObject[i].toString();
            }
         
            return keys;
        }
     
        public static boolean isOwner(Player p)
        {
            String[] keys = allFactions();
         
            String uuid;
         
            for (int i = 0; i < keys.length; i++)
            {
                uuid = fac.getString(keys[i] + ".Owner");
             
                if (uuid.equals(p.getUniqueId().toString()))
                {
                    return true;
                }
            }
         
            return false;
        }
     
        public static String ownsWhatFaction(Player p)
        {
            String uuid = p.getUniqueId().toString();
         
            String[] keys = allFactions();
         
            for (int i = 0; i < keys.length; i++)
            {
                if (uuid.equals(fac.get(keys[i]+ ".Owner")))
                {
                    return keys[i];
                }
            }
         
            return null;
        }
    }
     
    Main Class

    Code (Java):
    package io.github.JustinBerke0406.morecommands;

    import org.bukkit.plugin.java.JavaPlugin;

    import io.github.JustinBerke0406.files.CustomConfig;
    import io.github.JustinBerke0406.files.FactionsFile;
    import io.github.JustinBerke0406.morecommands.Listeners;

    public class MoreCommands extends JavaPlugin
    {
        private CmdExecutor commandEx = new CmdExecutor();
       
        @Override
        public void onEnable()
        {  
            configSetup();
           
            factionsSetup();
           
            getLogger().info("MoreCommands was enabled successfully!");
           
            //Events
            getServer().getPluginManager().registerEvents(new Listeners(), this);
           
            //Initializes commands
            commands();
        }
       
        @Override
        public void onDisable()
        {
            CustomConfig.save();
            FactionsFile.save();
        }
       
        private void commands()
        {
            this.getCommand("makesay").setExecutor(commandEx);
            this.getCommand("seeinv").setExecutor(commandEx);
            this.getCommand("f").setExecutor(commandEx);
            this.getCommand("mcreload").setExecutor(commandEx);
        }
       
        private void configSetup()
        {
            CustomConfig.setup();
            CustomConfig.get().addDefault("Factions World", "Factions");
            CustomConfig.get().options().copyDefaults(true);
            CustomConfig.save();
        }
       
        private void factionsSetup()
        {
            FactionsFile.setup();
            FactionsFile.get().options().copyDefaults(true);
            FactionsFile.save();
        }
    }
     
     
    #1 Justin0406, Dec 22, 2019
    Last edited: Dec 22, 2019
  2. From what I can see in the code you've given here, you never call the "setup()" function, which means when you go to save a FileConfiguration file into a File, it's saving to a null File, since "file" hasn't been defined yet, which would cause it not to save.
     
  3. Is the player getting any of those error messages from using the "create" argument?
     
  4. Sorry, I did use the setup function in my main class, i just forgot to add it to the post.
     
  5. They do get the error message when they type the command wrong or if the faction already exists. Otherwise, they do not.
     
  6. This is what happens in the server:

    Code (Text):
    /f create Example
    Code (Text):
    Example has been created!
    Code (Text):
    /f create Example
    Code (Text):
    You already own a faction!
    Code (Text):
    /f disband
    Code (Text):
    Example has been disbanded!
    Code (Text):
    /f create Example
    Code (Text):
    Example has been created!
    Code (Text):
    /f create Example
    Code (Text):
    Example has been created!
    Code (Text):
    /f disband
    Code (Text):
    You must own a faction in order to disband one!
     
  7. Why are you doing
    Code (Java):
    fac.addDefault(faction + ".Owner", owner);
    instead of
    Code (Java):
    fac.set(faction + ".Owner", owner);
    in your #addFaction(...) method?
     
  8. That fixed my issue, thank you.
     
    • Like Like x 1