Resource Config

Discussion in 'Spigot Plugin Development' started by Angeles, Jun 9, 2016.

  1. Hello spigot it has come to my attention that people dont really have a good way to config and i have had alot of pm's on how i do them

    So You have to code a class called YmlMaker
    Code:
    Code (Text):
    public class YmlMaker {
        Main Plugin;
        public String fileName;
        private JavaPlugin plugin;
        public File ConfigFile;
        private FileConfiguration Configuration;

        public YmlMaker(Main Plugin) {
            this.Plugin = Plugin;
        }

        public YmlMaker(JavaPlugin plugin, String fileName) {
            if (plugin == null) {
                throw new IllegalArgumentException("plugin cannot be null");
            }
            this.plugin = plugin;
            this.fileName = fileName;
            File dataFolder = plugin.getDataFolder();
            if (dataFolder == null) {
                throw new IllegalStateException();
            }
            this.ConfigFile = new File(dataFolder.toString() + File.separatorChar + this.fileName);
        }

        public void reloadConfig() {
            try {
                this.Configuration = YamlConfiguration.loadConfiguration(new InputStreamReader(new FileInputStream(this.ConfigFile), "UTF-8"));
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
            InputStream defConfigStream = this.plugin.getResource(this.fileName);
            if (defConfigStream != null) {
                YamlConfiguration defConfig = YamlConfiguration.loadConfiguration(defConfigStream);
                this.Configuration.setDefaults(defConfig);
            }
        }

        public FileConfiguration getConfig() {
            if (this.Configuration == null) {
                reloadConfig();
            }
            return this.Configuration;
        }

        public void saveConfig() {
            if ((this.Configuration == null) || (this.ConfigFile == null)) {
                return;
            }
            try {
                getConfig().save(this.ConfigFile);
            } catch (IOException ex) {
                this.plugin.getLogger().log(Level.SEVERE, "Could not save config to " + this.ConfigFile, ex);
            }
        }

        public void saveDefaultConfig() {
            if (!this.ConfigFile.exists()) {
                this.plugin.saveResource(this.fileName, false);
            }
        }
     
    Okay so once you have made the Config Class which is YmlMaker in your Main Class you put
    Code (Text):
    public static YmlMaker [CONFIGNAME];
    Then once you have done that you register it in the onEnable():
    Code (Text):
    public void onEnable()
    {

    config = new YmlMaker(this, "config.yml");
    config.saveDefaultConfig();

    }
     
    So know you have made sure it detects for the config.yml

    Know you make a method
    Code (Text):
    public YmlMaker config() {return config;}
    Okay so know you have done thoes steps you can start adding configs to a class using a easy for loop
    Code:

    Code (Text):
                for(String line : Main.config.getConfig().getStringList("CLEARCHAT_MESSAGE"))
                {
                    Bukkit.broadcastMessage(ChatColor.translateAlternateColorCodes('&', line.replaceAll("%PLAYER%", commandSender.getName())));
                }
    So you would put this were you would put the message e.g p.sendMessage(ChatColor.RED + "dahjsdiahd"); you would swap the for loop for that then you go into your config and put

    Code (Text):
    CLEARCHAT_MESSAGE:
    - '&a&l%PLAYER% has cleared chat'
     
    • Optimistic Optimistic x 2
  2. Or just embed config.yml in your jar and call saveDefaultConfig()...

    Sent from my SGH-I747M using Tapatalk
     
    • Agree Agree x 1
  3. nice and can we also create folders with this? like this
    Code (Text):
    config = new YmlMaker(this, "/somefolder/config.yml");
     
  4. Why would you take someone else's class and post it as a resource? I found the same thing here:
    https://github.com/Zrips/Jobs/blob/master/com/gamingmesh/jobs/config/YmlMaker.java
    And I didn't see you anywhere on the contributors list... You just put the opening braces one line above where they were.
     
  5. As i know the owner of it he has given me permission and im allowed to help people considering its an open src
     
  6. Tbh i have never tried i will look into it im also editing some things to make it better atm
     
  7. Wait, you are the genius from yesterday who asked, if its hard to code an API...

    And there you go making a tutorial for others. Boy, you must have a bigger ego than Kayne West to do somthing like this.

    Bad poster, bad tutorial.
     
    • Funny Funny x 1
  8. Don't take credit for work that isn't yours. By all means reference it and offer to help people in using it (this one is self explanatory though) but don't take credit for another developer's work and claim their code as your own.
     
  9. This isnt an api doe
     
  10. Thats it. Congratulations. Thanks to you, I start drinking again. Right now!

    THE WORLD IS BAD.
     
    • Funny Funny x 1
    • Winner Winner x 1
  11. So, instead of reviving your old thread, why did you choose to create another one?
     
  12. You remind me of the retarded black person from The IT Crowd. Except you're worse.
    [​IMG]