import org.bukkit.configuration.file.FileConfiguration; //導入方法依賴的package包/類
public BukkitMotdConfig loadConfig() {
saveDefaultConfig();
reloadConfig();
FileConfiguration config = getConfig();
// check for ColorMOTD 1.x
if (!config.contains("version")) {
logger.info("ColorMOTD 1.x detected, renaming 'config.yml' to 'config.old.yml' ...");
File oldLocation = new File(getDataFolder(), "config.yml");
File newLocation = new File(getDataFolder(), "config.old.yml");
newLocation.delete();
if (!oldLocation.renameTo(newLocation)) {
throw new RuntimeException("Unable to rename 'config.yml' to 'config.old.yml'!");
}
saveDefaultConfig();
reloadConfig();
}
BukkitMotdConfig r = new BukkitMotdConfig();
for (Object motdObj : config.getList("motds")) {
if (motdObj instanceof String) {
r.addMotd(((String) motdObj).replace("\\n", "\n"));
} else if (motdObj instanceof Map) {
@SuppressWarnings("unchecked")
Map map = (Map) motdObj;
String line1 = map.getOrDefault("line1", "").toString();
String line2 = map.get("line2").toString();
r.addMotd(line1 + (line2 == null ? "" : "\n" + line2));
} else {
throw new RuntimeException("Unknown motd type: " + motdObj.getClass().getCanonicalName());
}
}
File iconFolder = new File(getDataFolder(), "favicons/");
if(!iconFolder.isDirectory()) iconFolder.mkdirs();
//noinspection ConstantConditions
for(File file : iconFolder.listFiles((dir, name) ->
SUPPORTED_IMAGE_FORMAT.stream().anyMatch(name::endsWith))) {
try {
BufferedImage image = ImageIO.read(file);
r.addServerIcon(new BukkitMotdServerIcon(image));
} catch (IOException e) {
throw new UncheckedIOException("Unable to load " + file.getName(), e);
}
}
return r;
}