Generating Stuff Inside Configs

Discussion in 'Plugin Development' started by Epicballzy, Apr 23, 2014.

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

    Epicballzy

    How would you generate stuff in a config? Even if its a custom config...

    Main:
    Code:
    package com.config;
     
    import java.io.File;
    import java.io.IOException;
    import java.util.logging.Logger;
     
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.configuration.file.YamlConfiguration;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.event.player.PlayerQuitEvent;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class Main extends JavaPlugin implements Listener {
       
        public final Logger logger = Logger.getLogger("Minecraft");
     
        File messages;
        FileConfiguration messageConf;
       
        public void onEnable(){
            Bukkit.getServer().getPluginManager().registerEvents(this, this);
            messages = new File(getDataFolder(), "messages.yml");
            messageConf = YamlConfiguration.loadConfiguration(messages);
            saveConf();
            try {
                saveConf();
                setupConfig(messageConf);
                saveConf();
            } catch(Exception e) {
                e.printStackTrace();
            }
        }
       
        public void saveConf(){
            try{
                messageConf.save(messages);
       
            }catch(Exception e){
                e.printStackTrace();
            }
        }
        @EventHandler
        public void onPlayerJoin(PlayerJoinEvent e) {
            e.setJoinMessage(ChatColor.translateAlternateColorCodes(
                    "&".charAt(0), messageConf.getString("Join").replaceAll("%PLAYER",
                            e.getPlayer().getName().toString())));
        }
     
        @EventHandler
        public void onPlayerQuit(PlayerQuitEvent e) {
            e.setQuitMessage(ChatColor.translateAlternateColorCodes(
                    "&".charAt(0), messageConf.getString("Quit").replaceAll("%PLAYER",
                            e.getPlayer().getName().toString())));
        }
       
        private void setupConfig(FileConfiguration config) throws IOException {
            if(!new File(getDataFolder(), "messages.yml").exists()) {
                new File(getDataFolder(), "messages.yml").createNewFile();
                messageConf.set("Join", "&3[&bJoin&3] &9%PLAYER has arrived!");
                messageConf.set("Quit", "&3[&bQuit&3] &9%PLAYER has left!");
                }
            }
       
    }
    
    I've tried the "setupConfig()" thing, but it doesn't work.
     
  2. Offline

    TheMcScavenger

    Try:

    Code:
    File file = new File(getDataFolder() + "/myfile.yml");
    if(!file.exists){
        file.createNewFile();
    }
     
    FileConfiguration fileConfig = YamlConfiguration.loadConfiguration(file);
    fileConfig.set("path", value);
     
    try{
        fileConfig.save(file);
    }catch(IOException e){
        e.printStackTrace();
    }
     
  3. Offline

    Epicballzy

    @TheMcScavanger Ok, I got the config to set it.. However, when I try to set it to something else in the config, it always gets set back to its default state. (EX: Gens "Joined" and I put "Join" And when I reload it, it sets it back to "Joined") Any way to fix this?
    Code:java
    1. File messages = null;
    2. FileConfiguration messageConf = null;
    3.  
    4. public void onEnable(){
    5. Bukkit.getServer().getPluginManager().registerEvents(this, this);
    6. this.messages = new File(getDataFolder(), "messages.yml");
    7. this.messageConf = YamlConfiguration.loadConfiguration(this.messages);
    8. this.messageConf.options().copyDefaults(true);
    9. messageConf.set("Join", "&3[&bJoin&3] &9%PLAYER has arrived!");
    10. messageConf.set("Quit", "&3[&bQuit&3] &9%PLAYER has left!");
    11. saveConf();
    12. }
     
Thread Status:
Not open for further replies.

Share This Page