How to create a messages.yml

Discussion in 'Plugin Development' started by Der_Udo, Apr 17, 2012.

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

    I know how to create configs, but now I wanna create another file with the name messages.yml.
    This should be like a config: Here you can define the messages for the plugin.
    But how to create something like a config with another name?
     
  2. Offline

    Theodossis

    Take this example :)
    Code:
    import java.io.File;
    import java.util.logging.Logger;
     
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.configuration.file.YamlConfiguration;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class Messages extends JavaPlugin {
        public final Logger logger = Logger.getLogger("Minecraft");
       
        static File message;
        static FileConfiguration messageFile;
       
        @Override
        public void onEnable() {
            logger.info("Enabled!");
            message = new File(getDataFolder(), "messages.yml");
            messageFile = new YamlConfiguration();
        }
        @Override
        public void onDisable() {
            logger.info("Disabled!");
        }
    }
    
    And when you want a command or a event to read it type
    Code:
    messageFile.getString("String here")
    If i helped you press like :p
     
    Ikeetjeop and Der_Udo like this.
  3. How to save the file then?
     
  4. Offline

    Theodossis

    Take again this for example :p
    Code:
    import java.io.File;
    import java.util.logging.Logger;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
     
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.configuration.file.YamlConfiguration;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class Messages extends JavaPlugin {
        public final Logger logger = Logger.getLogger("Minecraft");
     
        static File message;
        static FileConfiguration messageFile;
     
        @Override
        public void onEnable() {
            logger.info("Enabled!");
            message = new File(getDataFolder(), "messages.yml");
            messageFile = new YamlConfiguration();
            saveYamls();
        }
        @Override
        public void onDisable() {
            logger.info("Disabled!");
        }
    public void saveYamls() {
            try {
                messageFile.save(message);
                logger.info("[Messages] "+" Config saved!");
            } catch (IOException e) {
                e.printStackTrace();
            }
    public static void copy(InputStream inputStream, File file) {
            try {
                OutputStream out = new FileOutputStream(file);
                byte[] buf = new byte[1024];
                int len;
                while ((len = inputStream.read(buf)) > 0) {
                    out.write(buf, 0, len);
                }
                out.close();
                inputStream.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
     
    }
     
  5. I used this example, but the messages.yml was always overwritten by the default. I think it needs to check, if there is already a existing file. But how?
     
Thread Status:
Not open for further replies.

Share This Page