CustomYML

Discussion in 'Spigot Plugin Development' started by _xXProDudeXx_, Jun 29, 2016.

  1. Hello guys,

    I created a metrics class with a custom yml file, and I do not really get what I should add in my main class to edit the custom yml file in the onEnable(){

    this is my Metrics source:

    Code (Text):
    package net.ZeusReksYou_.ZBans.Utilities;

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

    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.configuration.file.YamlConfiguration;

    import net.ZeusReksYou_.ZBans.ZBans;

    public class Metrics {

        public ZBans plugin;

        public Metrics(ZBans plugin) {
            this.plugin = plugin;
        }

        File customYml = new File(plugin.getDataFolder() + "/bans.yml");
        FileConfiguration customConfig = YamlConfiguration.loadConfiguration(customYml);

        public void saveCustomYml(FileConfiguration ymlConfig, File ymlFile) {
            try {
                customConfig.save(customYml);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
       
        public void notExist(){
            if(!customYml.exists()){
                customConfig.set("path.to.string", "hi");
                customConfig.set("path.to.int", "1");
                saveCustomYml(customConfig, customYml);
            }
        }

    }
    What do I have to do?
     
  2. Make the File and FileConfiguration objects private and then make getters for them, then use "metricsInstance.getConfig().set..."
     
    • Winner Winner x 1
  3. Make a getter for the FileConfiguration variable, when you do that you just use it as you would getConfig()
     
  4. I don't get it. I have tried to find out looking up for other threads but those threads mostly supported a custom configuration in the same class as the code it is needed in. I want it as an externall class and couldn't find that.

    Can anyone copy my code, and write a short example of the getter + how I would edit my configuration in the other classes?

    Thanks!
     
  5. Code (Java):

    public FileConfiguration getConfig() { return customYml; }
     
    Code (Java):

    Metrics metrics = new Metrics();
    metrics.getConfig().set("forks", "a");
     
     
    • Winner Winner x 1
  6. Okay so if I paste this on my main class it should be working? Let's try it out !

    EDIT: If I type in

    return customYml;

    It gives an error. So I should register the file like:

    File customYml = new File(this.getDataFolder(), "custom.yml");

    ?

    EDIT: I think I was wrong with saying: 'Okay so if I paste this on my main class it should be working?'

    I have written down that code in my Metrics class, and no errors given. So do I also have to register the file and fileconfiguration in my main class or am I doing this totally wrong?
     
    #6 _xXProDudeXx_, Jun 30, 2016
    Last edited: Jun 30, 2016
  7. Okay so this is what I tried to do, which failed pretty hard :(

    Metrics class:

    Code (Text):
    package net.ZeusReksYou_.ZBans.Utilities;

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

    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.configuration.file.YamlConfiguration;

    import net.ZeusReksYou_.ZBans.ZBans;

    public class Metrics {

        public ZBans plugin;
       
        public FileConfiguration getConfigFile(){
            return customConfig;
        }
       
        Metrics metrics = new Metrics(plugin);
       

        public Metrics(ZBans plugin) {
            this.plugin = plugin;
        }

        File customYml = new File(plugin.getDataFolder() + "/bans.yml");
        FileConfiguration customConfig = YamlConfiguration.loadConfiguration(customYml);

        public void saveCustomYml(FileConfiguration ymlConfig, File ymlFile) {
            try {
                customConfig.save(customYml);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
       
        public void notExist(){
            if(!customYml.exists()){
                customConfig.set("path.to.string", "hi");
                customConfig.set("path.to.int", "1");
                saveCustomYml(customConfig, customYml);
            }
        }

    }
    Main class:

    Code (Text):
    File f;

        public Metrics plugin;

        public ZBans(Metrics plugin) {
            this.plugin = plugin;

            f = new File(this.getDataFolder(), "bans.yml");
        }

    public void onEnable(){

    if (!f.exists()) {
                plugin.getConfigFile().set("path.to.string", "hi");
                plugin.getConfigFile().addDefault("path.to.int", "69");
            }

    plugin.getConfigFile().addDefault("path.to.string", "hi");
            plugin.saveCustomYml(plugin.getConfigFile(), f);

    }
    I know this is wrong as it also shows an error in the console (Abnormal Class Type)
    @Trophonix What to do?
     
  8. That would probably be because you never made the FileConfiguration private.

    Not sure why you made your metrics instance inside metrics...
     
    • Winner Winner x 1
  9. Dose it come up any errors on the console ?
     
  10. Yea I thought already that was wrong. So that metrics parts gives an error on the return customYml. So register the file in the main class as well?

    Yea, take a look at this ! http://prntscr.com/bmx55a
     
  11. I mean, it would be better and save time and work to simply make the config file inside your main class, but if you're going with this Metrics thing obviously you don't need to remake the File object in both classes. You just need to make a getter for the FileConfiguration object and then use it..
     
    • Winner Winner x 1
  12. You are right, I will create the file into the main class, however I would really like a Metrics file :(
     
  13. Code (Text):
    package net.ZeusReksYou_.ZBans;

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

    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.command.ConsoleCommandSender;
    import org.bukkit.configuration.InvalidConfigurationException;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.configuration.file.YamlConfiguration;
    import org.bukkit.plugin.java.JavaPlugin;

    import net.ZeusReksYou_.ZBans.Commands.BanCMD;
    import net.ZeusReksYou_.ZBans.Commands.BanIPCMD;
    import net.ZeusReksYou_.ZBans.Commands.BanListCMD;
    import net.ZeusReksYou_.ZBans.Commands.BanWarnCMD;
    import net.ZeusReksYou_.ZBans.Commands.CheckCMD;
    import net.ZeusReksYou_.ZBans.Commands.IpCMD;
    import net.ZeusReksYou_.ZBans.Commands.KickCMD;
    import net.ZeusReksYou_.ZBans.Commands.UnbanCMD;
    import net.ZeusReksYou_.ZBans.Commands.UnbanIPCMD;
    import net.ZeusReksYou_.ZBans.Commands.ZBansCMD;
    import net.ZeusReksYou_.ZBans.Listeners.AntiSwearLST;
    import net.ZeusReksYou_.ZBans.Listeners.PlayerJoinLST;

    public class ZBans extends JavaPlugin {

        ConsoleCommandSender c;
        String Prefix;

        File BansFile = new File(this.getDataFolder(), "bans.yml");
        private FileConfiguration BansConfigFile = YamlConfiguration.loadConfiguration(BansFile);

        private FileConfiguration getBanConfigFile() {
            return BansConfigFile;
        }

        public void saveBanFile() {
            try {
                this.getBanConfigFile().save(BansFile);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        public void DoesNotExist() {
            if (!BansFile.exists()) {
                this.getBanConfigFile().set("path.to.string", "hi");
                this.getBanConfigFile().set("path.to.int", "69");
                System.out.println("Bans.yml succesfully created!");
            } else {
                System.out.println("Bans.yml succesfully loaded!");
                try {
                    try {
                        BansConfigFile.load(BansFile);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                } catch (InvalidConfigurationException e) {
                    e.printStackTrace();
                }

            }
        }

        public void onEnable() {

            c = Bukkit.getConsoleSender();
            Prefix = format(this.getConfig().getString("Global Prefix"));

            if (Bukkit.getPluginManager().getPlugins().equals("MaxBans")) {
                c.sendMessage(Prefix + ChatColor.RED
                        + "MaxBans is installed as well. This might give bugs. Remove either MaxBans or ZBans in order to remove any possible bugs!");
            }
            if (Bukkit.getPluginManager().getPlugins().equals("LiteBans")) {
                c.sendMessage(Prefix + ChatColor.RED
                        + "MaxBans is installed as well. This might give bugs. Remove either LiteBans or ZBans in order to remove any possible bugs!");
            }
            if (Bukkit.getPluginManager().getPlugins().equals("AdvancedBans")) {
                c.sendMessage(Prefix + ChatColor.RED
                        + "MaxBans is installed as well. This might give bugs. Remove either AdvancedBans or ZBans in order to remove any possible bugs!");
            }

            if (!this.getConfig().getString("op_bans").contains(Boolean.valueOf(true).toString())
                    && !this.getConfig().getString("op_bans").contains(Boolean.valueOf(false).toString())) {
                c.sendMessage(Prefix + "§6=========================");
                c.sendMessage(Prefix + "§9Loading plugin...");
                c.sendMessage(Prefix + "§9Loaded Commands!");
                c.sendMessage(Prefix + "§9Loaded Events!");
                c.sendMessage("");
                c.sendMessage(Prefix + "§4Configuration is broken on line: 'op_bans'");
                c.sendMessage(Prefix + "§4You can only fill in 'true' or 'false' !");
                c.sendMessage("");
                c.sendMessage(Prefix + "§9Loaded ZPBans!");
                c.sendMessage(Prefix + "§cCoded by: §3ZeusReksYou");
                c.sendMessage(Prefix + "§6=========================");
            }
           
            try {
                try {
                    this.getBanConfigFile().load(BansFile);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            } catch (InvalidConfigurationException e) {
                e.printStackTrace();
            }

            this.getCommand("check").setExecutor(new CheckCMD(this));
            this.getCommand("banwarn").setExecutor(new BanWarnCMD(this));
            this.getCommand("kick").setExecutor(new KickCMD(this));
            this.getCommand("unban").setExecutor(new UnbanCMD(this));
            this.getCommand("ban").setExecutor(new BanCMD(this));
            this.getCommand("banlist").setExecutor(new BanListCMD(this));
            this.getCommand("banip").setExecutor(new BanIPCMD(this));
            this.getCommand("unbanip").setExecutor(new UnbanIPCMD(this));
            this.getCommand("getip").setExecutor(new IpCMD(this));
            this.getCommand("zbans").setExecutor(new ZBansCMD(this));

            this.saveDefaultConfig();

            Bukkit.getPluginManager().registerEvents(new PlayerJoinLST(this), this);
            Bukkit.getPluginManager().registerEvents(new AntiSwearLST(this), this);
            // Bukkit.getPluginManager().registerEvents(new MuteLST(this), this);

            c.sendMessage(Prefix + "§6=========================");
            c.sendMessage(Prefix + "§9Loading plugin...");
            c.sendMessage(Prefix + "§9Loaded 10 Commands!");
            c.sendMessage(Prefix + "§9Loaded 3 Events!");
            c.sendMessage(Prefix + "§9Loaded Configuration");
            c.sendMessage(Prefix + "§9Loaded ZPBans!");
            c.sendMessage(Prefix + "§cCoded by: §3ZeusReksYou");
            c.sendMessage(Prefix + "§6=========================");

        }

        public String format(String format) {
            return ChatColor.translateAlternateColorCodes('&', format);
        }

    }
     
    Still gives a little error as it says it can't find the file (error is on the try / catch where I load the file)
    What am I doing wrong?
     
  14. You don't need a getter if the object is inside the same class you're using it in. Make it private. Right now it is package-local.
    Code (Text):
    private FileConfiguration ....
    That isn't how loading works. You're already loading it elsewhere:
    Code (Java):
    private FileConfiguration BansConfigFile = YamlConfiguration.loadConfiguration(BansFile);
     
    • Winner Winner x 1
  15. But the plugin doesn't generate a bans.yml. Is that because of I don't have private?
     
  16. No, it not being private is just bad.

    It doesn't generate a bans.yml because of the error you caused by trying to use that load thing, and you're never doing anything to save it.

    Use File#createNewFile or FIleConfiguration#save to save the file physically.
     
    • Like Like x 1
    • Winner Winner x 1
  17. Great. It worked. Last question:

    I have created a License.yml and I followed the steps you wrote down to do to load the License.yml.
    Once I reload the server a custom.yml file does popup, but it is empty. How do I make sure an existing .yml file does also becomes physical in a plugins folder with the text written down in it?
     
  18. You can either compile the yml file in your jar in the same directory as the plugin.yml and use this.saveResource("<filename>.yml", false) or you can set the values manually with config.set
     
    • Winner Winner x 1
  19. @Trophonix Thanks so much for your help. Everything worked perfectly! I really appreciate this, thanks man !
     
  20. @Trophonix one very very very last question. If I want to add a name + reason to the configuration, what things have to be written down to acces the file in the externall class like for example the Ban class?