Other yml files like config.yml

Discussion in 'Plugin Development' started by JarFile, Feb 7, 2015.

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

    JarFile

    My config.yml is getting way too long, I notice that other plugins, such as essentials has multiple files. rules.txt, custom.txt, rules.txt, items.cvs. How would I create other files like config.yml and grab variables from it still?
     
  2. Offline

    Skionz

    @JarFile You can write your own file format by parsing string and using File streams. To create another YAML file you can use YamlConfiguration.load(File) which is essentially the same as JavaPlugin#getConfig()
     
  3. Offline

    TehHypnoz

    Quick example, made without IDE so might contain errors:

    Code:
    File f = new File(insert_location_here);
    YamlConfiguration config = new YamlConfiguration();
    try {
    config.load(f);
    } catch (Exception ex) {
    ex.printStackTrace();
    }
    
     
  4. Offline

    JarFile

    @TehHypnoz So I have this
    Code:
                File crates = new File(getDataFolder(), "crates.yml");
    
    Here is what I use to generate it.
    Code:
                if(!crates.exists())
                {
                    getLogger().info("Crates.yml not found, Creating");
                    crates.createNewFile();
                }
                else
                {
                    getLogger().info("Crates.yml Found, Loading");
                }
    
    This obviously creates a brand new file, I have a crates.yml in my Java IDE, how would I use that file? How would I grab variables from it also.
     
  5. Offline

    SuperOriginal

     
  6. Offline

    Konato_K

  7. @JarFile
    Setting variables:
    Code:
    crates.set("A.Thing", "Goes here");
    Getting variables:
    Code:
    crates.getString/Boolean/Int/Etc.("A.Thing");
    Example (from one of my current private plugins):
    Code:
    public class Methods {
    
        File hubConfig;
        FileConfiguration fileConfig;
    
        public Methods() {
            hubConfig = new File("plugins/TheRealmCore/hub.yml");
            fileConfig = YamlConfiguration.loadConfiguration(hubConfig);
        }
    
        public void createConfig() {
            if (!hubConfig.exists()) {
                try {
                    hubConfig.createNewFile();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                System.out.println("[TheRealmCore] File Created: hub.yml");
            }
        }
    
        public void createDefaults() {
            if(hubConfig.length() <= 0) {
            fileConfig.set("Hub Spawn.X", 0);
            fileConfig.set("Hub Spawn.Y", 0);
            fileConfig.set("Hub Spawn.Z", 0);
            fileConfig.set("Hub Spawn.Pitch", 0);
            fileConfig.set("Hub Spawn.Yaw", 0);
            fileConfig.set("Hub Spawn.World", "world");
            } else {
                return;
            }
        }
    
        public void setX(double x) {
            fileConfig.set("Hub Spawn.X", x);
        }
    
        public void setY(double y) {
            fileConfig.set("Hub Spawn.Y", y);
        }
    
        public void setZ(double z) {
            fileConfig.set("Hub Spawn.Z", z);
        }
    
        public void setPitch(float pitch) {
            fileConfig.set("Hub Spawn.Pitch", pitch);
        }
    
        public void setYaw(float yaw) {
            fileConfig.set("Hub Spawn.Yaw", yaw);
        }
    
        public void setWorld(String world) {
            fileConfig.set("Hub Spawn.World", world);
        }
    
        public double getX() {
            return fileConfig.getDouble("Hub Spawn.X");
        }
    
        public double getY() {
            return fileConfig.getDouble("Hub Spawn.Y");
        }
    
        public double getZ() {
            return fileConfig.getDouble("Hub Spawn.Z");
        }
    
        public float getPitch() {
            return fileConfig.getInt("Hub Spawn.Pitch");
        }
    
        public float getYaw() {
            return fileConfig.getInt("Hub Spawn.Yaw");
        }
    
        public String getWorld() {
            return fileConfig.getString("Hub Spawn.World");
        }
    
        public void saveConfig() {
            try {
                fileConfig.save(hubConfig);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    You don't need all of the methods to set and get variables, but I think you get the jest of it.
     
Thread Status:
Not open for further replies.

Share This Page