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);
}
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"));
}
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;
}
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());
}
}
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;
}
Aggregations