Search in sources :

Example 26 with YamlConfiguration

use of org.bukkit.configuration.file.YamlConfiguration in project Bukkit by Bukkit.

the class JavaPlugin method reloadConfig.

@SuppressWarnings("deprecation")
@Override
public void reloadConfig() {
    newConfig = YamlConfiguration.loadConfiguration(configFile);
    final InputStream defConfigStream = getResource("config.yml");
    if (defConfigStream == null) {
        return;
    }
    final YamlConfiguration defConfig;
    if (isStrictlyUTF8() || FileConfiguration.UTF8_OVERRIDE) {
        defConfig = YamlConfiguration.loadConfiguration(new InputStreamReader(defConfigStream, Charsets.UTF_8));
    } else {
        final byte[] contents;
        defConfig = new YamlConfiguration();
        try {
            contents = ByteStreams.toByteArray(defConfigStream);
        } catch (final IOException e) {
            getLogger().log(Level.SEVERE, "Unexpected failure reading config.yml", e);
            return;
        }
        final String text = new String(contents, Charset.defaultCharset());
        if (!text.equals(new String(contents, Charsets.UTF_8))) {
            getLogger().warning("Default system encoding may have misread config.yml from plugin jar");
        }
        try {
            defConfig.loadFromString(text);
        } catch (final InvalidConfigurationException e) {
            getLogger().log(Level.SEVERE, "Cannot load configuration from jar", e);
        }
    }
    newConfig.setDefaults(defConfig);
}
Also used : InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) IOException(java.io.IOException) YamlConfiguration(org.bukkit.configuration.file.YamlConfiguration) InvalidConfigurationException(org.bukkit.configuration.InvalidConfigurationException)

Example 27 with YamlConfiguration

use of org.bukkit.configuration.file.YamlConfiguration in project AuthMeReloaded by AuthMe.

the class SpawnLoaderTest method shouldSetSpawn.

@Test
public void shouldSetSpawn() {
    // given
    World world = mock(World.class);
    given(world.getName()).willReturn("new_world");
    Location newSpawn = new Location(world, 123, 45.0, -67.89);
    // when
    boolean result = spawnLoader.setSpawn(newSpawn);
    // then
    assertThat(result, equalTo(true));
    YamlConfiguration configuration = YamlConfiguration.loadConfiguration(new File(testFolder, "spawn.yml"));
    assertThat(configuration.getDouble("spawn.x"), equalTo(123.0));
    assertThat(configuration.getDouble("spawn.y"), equalTo(45.0));
    assertThat(configuration.getDouble("spawn.z"), equalTo(-67.89));
    assertThat(configuration.getString("spawn.world"), equalTo("new_world"));
}
Also used : World(org.bukkit.World) YamlConfiguration(org.bukkit.configuration.file.YamlConfiguration) File(java.io.File) Location(org.bukkit.Location) Test(org.junit.Test)

Example 28 with YamlConfiguration

use of org.bukkit.configuration.file.YamlConfiguration in project Glowstone by GlowstoneMC.

the class ServerConfig method getConfigFile.

////////////////////////////////////////////////////////////////////////////
// Fancy stuff
public ConfigurationSection getConfigFile(Key key) {
    String filename = getString(key);
    if (extraConfig.containsKey(filename)) {
        return extraConfig.get(filename);
    }
    YamlConfiguration conf = new YamlConfiguration();
    File file = getFile(filename), migrateFrom = new File(key.def.toString());
    // create file if it doesn't exist
    if (!file.exists()) {
        if (migrateFrom.exists()) {
            FileUtil.copy(migrateFrom, file);
        } else {
            copyDefaults(key.def.toString(), file);
        }
    }
    // read in config
    try {
        conf.load(file);
    } catch (IOException e) {
        GlowServer.logger.log(Level.SEVERE, "Failed to read config: " + file, e);
    } catch (InvalidConfigurationException e) {
        report(file, e);
    }
    extraConfig.put(filename, conf);
    return conf;
}
Also used : YamlConfiguration(org.bukkit.configuration.file.YamlConfiguration) InvalidConfigurationException(org.bukkit.configuration.InvalidConfigurationException)

Example 29 with YamlConfiguration

use of org.bukkit.configuration.file.YamlConfiguration in project InfernalMobs by NyaaCat.

the class LootConfig method dump.

public void dump(File f) {
    YamlConfiguration cfg = new YamlConfiguration();
    ConfigurationSection items = cfg.createSection("lootItems");
    ConfigurationSection dropMap = cfg.createSection("dropMap");
    for (String name : lootItems.keySet()) {
        LootItem item = lootItems.get(name);
        ConfigurationSection s = items.createSection(name);
        s.set("item", item.item);
        if (item.amountRange != null)
            s.set("amountRange", item.amountRange.toString());
        if (item.damageRange != null)
            s.set("damageRange", item.damageRange.toString());
        if (item.commands != null)
            s.set("commands", item.commands);
        if (item.extraEnchants != null) {
            ConfigurationSection sec = s.createSection("extraEnchants");
            for (Enchantment e : item.extraEnchants.keySet()) {
                sec.set(e.getName(), item.extraEnchants.get(e).toString());
            }
        }
    }
    for (Integer level : this.dropMap.keySet()) {
        dropMap.createSection("level-" + level.toString(), this.dropMap.get(level));
    }
    try {
        cfg.save(f);
    } catch (Exception ex) {
        ex.printStackTrace();
        System.out.print(cfg.saveToString());
    }
}
Also used : YamlConfiguration(org.bukkit.configuration.file.YamlConfiguration) Enchantment(org.bukkit.enchantments.Enchantment) ConfigurationSection(org.bukkit.configuration.ConfigurationSection)

Example 30 with YamlConfiguration

use of org.bukkit.configuration.file.YamlConfiguration in project InfernalMobs by NyaaCat.

the class LootConfig method parse.

public static LootConfig parse(File f) {
    LootConfig l = new LootConfig();
    l.lootItems = new HashMap<>();
    l.dropMap = new HashMap<>();
    YamlConfiguration cfg = YamlConfiguration.loadConfiguration(f);
    ConfigurationSection items = cfg.getConfigurationSection("lootItems");
    ConfigurationSection dropMap = cfg.getConfigurationSection("dropMap");
    for (String itemName : items.getKeys(false)) {
        ConfigurationSection s = items.getConfigurationSection(itemName);
        LootItem i = new LootItem();
        i.item = s.getItemStack("item");
        if (s.isString("amountRange"))
            i.amountRange = RangePair.parse(s.getString("amountRange"));
        if (s.isString("damageRange"))
            i.damageRange = RangePair.parse(s.getString("damageRange"));
        if (s.isList("commands"))
            i.commands = s.getStringList("commands");
        if (s.isConfigurationSection("extraEnchants")) {
            i.extraEnchants = new HashMap<>();
            ConfigurationSection sec = s.getConfigurationSection("extraEnchants");
            for (String ench : sec.getKeys(false)) {
                i.extraEnchants.put(Enchantment.getByName(ench), RangePair.parse(sec.getString(ench)));
            }
        }
        if (s.isString("amountRange"))
            i.amountRange = RangePair.parse(s.getString("amountRange"));
        l.lootItems.put(itemName, i);
    }
    for (String levelKey : dropMap.getKeys(false)) {
        if (!levelKey.startsWith("level-"))
            continue;
        Integer level = Integer.parseInt(levelKey.substring(6));
        ConfigurationSection levelMap = dropMap.getConfigurationSection(levelKey);
        Map<String, Double> map = new HashMap<>();
        for (String dropName : levelMap.getKeys(false)) {
            map.put(dropName, levelMap.getDouble(dropName));
        }
        l.dropMap.put(level, map);
    }
    return l;
}
Also used : HashMap(java.util.HashMap) YamlConfiguration(org.bukkit.configuration.file.YamlConfiguration) ConfigurationSection(org.bukkit.configuration.ConfigurationSection)

Aggregations

YamlConfiguration (org.bukkit.configuration.file.YamlConfiguration)36 File (java.io.File)24 IOException (java.io.IOException)21 InvalidConfigurationException (org.bukkit.configuration.InvalidConfigurationException)13 ConfigurationSection (org.bukkit.configuration.ConfigurationSection)6 UUID (java.util.UUID)5 ArrayList (java.util.ArrayList)4 Location (org.bukkit.Location)4 FileConfiguration (org.bukkit.configuration.file.FileConfiguration)4 EntityType (org.bukkit.entity.EntityType)4 SettingsFlag (com.wasteofplastic.acidisland.Island.SettingsFlag)3 InputStream (java.io.InputStream)3 HashMap (java.util.HashMap)3 Test (org.junit.Test)3 ChatColor (com.gamingmesh.jobs.util.ChatColor)2 DefaultInfo (de.Keyle.MyPet.api.entity.DefaultInfo)2 MyPetType (de.Keyle.MyPet.api.entity.MyPetType)2 InputStreamReader (java.io.InputStreamReader)2 TreeMap (java.util.TreeMap)2 Material (org.bukkit.Material)2