下面列出了org.bukkit.configuration.file.FileConfiguration#save ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private static void updateFile(File file, FileConfiguration storage) {
ArrayList<Integer> placement = new ArrayList<>(Arrays.asList(0, 2, 4, 6, 8, 9, 11, 13, 15, 17, 18, 20, 22, 24, 26, 27, 29, 31, 33, 35,
36, 38, 40, 42, 44, 45, 47, 49, 51, 53));
storage.set("menuSize", 45);
for (int i = 0; i < playerOptions.size(); i++) {
playerOptions.get(i).setPosition(placement.get(i) % 45);
playerOptions.get(i).setPage((Math.floorDiv(placement.get(i), 45))+1);
playerOptions.get(i).setMenuSize(45);
storage.set("sounds." + playerOptions.get(i).getKey() + ".position", playerOptions.get(i).getPosition());
storage.set("sounds." + playerOptions.get(i).getKey() + ".page", playerOptions.get(i).getPage());
}
try {
storage.save(file);
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* Clears the worlds.yml configuration file, then writes all of the groups currently in memory
* to it.
*/
public void saveGroupsToDisk() {
FileConfiguration groupsConfigFile = plugin.getWorldsConfig();
groupsConfigFile.set("groups", null);
for (Group group : groups.values()) {
String groupKey = "groups." + group.getName();
groupsConfigFile.set(groupKey, null);
groupsConfigFile.set(groupKey + ".worlds", group.getWorlds());
// Saving gamemode regardless of management; might be saving after convert
groupsConfigFile.set(groupKey + ".default-gamemode", group.getGameMode().name());
}
try {
groupsConfigFile.save(plugin.getDataFolder() + "/worlds.yml");
} catch (IOException ex) {
ConsoleLogger.warning("Could not save the groups config to disk:", ex);
}
}
private static void updateFile(File file, FileConfiguration storage) {
ArrayList<Integer> placement = new ArrayList<>(Arrays.asList(0, 2, 4, 6, 8, 9, 11, 13, 15, 17, 18, 20, 22, 24, 26, 27, 29, 31, 33, 35,
36, 38, 40, 42, 44, 45, 47, 49, 51, 53));
storage.set("menuSize", 45);
for (int i = 0; i < playerOptions.size(); i++) {
playerOptions.get(i).setPosition(placement.get(i) % 45);
playerOptions.get(i).setPage((Math.floorDiv(placement.get(i), 45))+1);
playerOptions.get(i).setMenuSize(45);
storage.set("taunts." + playerOptions.get(i).getKey() + ".position", playerOptions.get(i).getPosition());
storage.set("taunts." + playerOptions.get(i).getKey() + ".page", playerOptions.get(i).getPage());
}
try {
storage.save(file);
} catch (IOException e) {
e.printStackTrace();
}
}
public void save() {
if (file == null) {
return;
}
FileConfiguration configFile = YamlConfiguration.loadConfiguration(file);
if (getState(GameRule.MESSAGES) != null) {
for (int msgs : getState(GameRule.MESSAGES).keySet()) {
configFile.set("messages." + msgs, getState(GameRule.MESSAGES).get(msgs));
}
}
configFile.set("invitedPlayers", invitedPlayers);
if (worldEnvironment != null) {
configFile.set("worldEnvironment", worldEnvironment.name());
}
try {
configFile.save(file);
} catch (IOException exception) {
exception.printStackTrace();
}
}
private static void updateFile(File file, FileConfiguration storage) {
ArrayList<Integer> placement = new ArrayList<>(Arrays.asList(0, 2, 4, 6, 8, 9, 11, 13, 15, 17, 18, 20, 22, 24, 26, 27, 29, 31, 33, 35,
36, 38, 40, 42, 44, 45, 47, 49, 51, 53));
storage.set("menuSize", 45);
for (int i = 0; i < playerOptions.size(); i++) {
playerOptions.get(i).setPosition(placement.get(i) % 45);
playerOptions.get(i).setPage((Math.floorDiv(placement.get(i), 45))+1);
playerOptions.get(i).setMenuSize(45);
storage.set("effects." + playerOptions.get(i).getKey() + ".position", playerOptions.get(i).getPosition());
storage.set("effects." + playerOptions.get(i).getKey() + ".page", playerOptions.get(i).getPage());
}
try {
storage.save(file);
} catch (IOException e) {
e.printStackTrace();
}
}
public void saveSigns(LeaderType type) {
File leaderboardsFile = new File(SkyWarsReloaded.get().getDataFolder(), "leaderboards.yml");
if (!leaderboardsFile.exists()) {
SkyWarsReloaded.get().saveResource("leaderboards.yml", false);
}
if (leaderboardsFile.exists()) {
FileConfiguration storage = YamlConfiguration.loadConfiguration(leaderboardsFile);
for (int pos: signs.get(type).keySet()) {
if (signs.get(type).get(pos) != null) {
List<String> locs = new ArrayList<String>();
for (Location loc: signs.get(type).get(pos)) {
locs.add(Util.get().locationToString(loc));
}
storage.set(type.toString().toLowerCase() + ".signs." + pos, locs);
}
}
try {
storage.save(leaderboardsFile);
} catch (IOException e) {
}
}
if (loaded(type)) {
updateSigns(type);
}
}
@Override
public void doSetup() {
FileConfiguration config = getOpenerConfig();
if (config.isSet("Chest Sound")) {
chestSound = config.getBoolean("Chest Sound", true);
} else {
config.set("Chest Sound", true);
try {
config.save(getOpenerConfigFile());
} catch (IOException e) {
e.printStackTrace();
}
}
}
private void copyDefaults(File playerFile) {
try {
FileConfiguration playerConfig = YamlConfiguration.loadConfiguration(playerFile);
Reader defConfigStream = new InputStreamReader(Main.getMain().getResource("playerdata.yml"));
YamlConfiguration defConfig = YamlConfiguration.loadConfiguration(defConfigStream);
playerConfig.options().copyDefaults(true);
playerConfig.setDefaults(defConfig);
playerConfig.save(playerFile);
} catch (Exception e) {
}
}
private void copyDefaults(File playerFile) {
FileConfiguration playerConfig = YamlConfiguration.loadConfiguration(playerFile);
Reader defConfigStream = new InputStreamReader(SkyWarsReloaded.get().getResource("messages.yml"));
if (defConfigStream != null) {
YamlConfiguration defConfig = YamlConfiguration.loadConfiguration(defConfigStream);
playerConfig.options().copyDefaults(true);
playerConfig.setDefaults(defConfig);
try {
playerConfig.save(playerFile);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
@Override
public void saveEventData() {
File dataDirectory = SkyWarsReloaded.get().getDataFolder();
File mapDataDirectory = new File(dataDirectory, "mapsData");
if (!mapDataDirectory.exists() && !mapDataDirectory.mkdirs()) {
return;
}
File mapFile = new File(mapDataDirectory, gMap.getName() + ".yml");
if (mapFile.exists()) {
FileConfiguration fc = YamlConfiguration.loadConfiguration(mapFile);
fc.set("events." + eventName + ".enabled", this.enabled);
fc.set("events." + eventName + ".minStart", this.min);
fc.set("events." + eventName + ".maxStart", this.max);
fc.set("events." + eventName + ".length", this.length);
fc.set("events." + eventName + ".chance", this.chance);
fc.set("events." + eventName + ".title", this.title);
fc.set("events." + eventName + ".subtitle", this.subtitle);
fc.set("events." + eventName + ".startMessage", this.startMessage);
fc.set("events." + eventName + ".endMessage", this.endMessage);
fc.set("events." + eventName + ".announceTimer", this.announceEvent);
fc.set("events." + eventName + ".repeatable", this.repeatable);
fc.set("events." + eventName + ".spawnPer5Tick", this.per5Tick);
try {
fc.save(mapFile);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
private void copyDefaults(File playerFile) {
FileConfiguration playerConfig = YamlConfiguration.loadConfiguration(playerFile);
Reader defConfigStream = new InputStreamReader(SkyWarsReloaded.get().getResource("playerFile.yml"));
YamlConfiguration defConfig = YamlConfiguration.loadConfiguration(defConfigStream);
playerConfig.options().copyDefaults(true);
playerConfig.setDefaults(defConfig);
try {
playerConfig.save(playerFile);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public void saveEventData() {
File dataDirectory = SkyWarsReloaded.get().getDataFolder();
File mapDataDirectory = new File(dataDirectory, "mapsData");
if (!mapDataDirectory.exists() && !mapDataDirectory.mkdirs()) {
return;
}
File mapFile = new File(mapDataDirectory, gMap.getName() + ".yml");
if (mapFile.exists()) {
FileConfiguration fc = YamlConfiguration.loadConfiguration(mapFile);
fc.set("events." + eventName + ".enabled", this.enabled);
fc.set("events." + eventName + ".minStart", this.min);
fc.set("events." + eventName + ".maxStart", this.max);
fc.set("events." + eventName + ".length", this.length);
fc.set("events." + eventName + ".chance", this.chance);
fc.set("events." + eventName + ".title", this.title);
fc.set("events." + eventName + ".subtitle", this.subtitle);
fc.set("events." + eventName + ".startMessage", this.startMessage);
fc.set("events." + eventName + ".endMessage", this.endMessage);
fc.set("events." + eventName + ".announceTimer", this.announceEvent);
fc.set("events." + eventName + ".repeatable", this.repeatable);
try {
fc.save(mapFile);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
@Override
public void saveEventData() {
File dataDirectory = SkyWarsReloaded.get().getDataFolder();
File mapDataDirectory = new File(dataDirectory, "mapsData");
if (!mapDataDirectory.exists() && !mapDataDirectory.mkdirs()) {
return;
}
File mapFile = new File(mapDataDirectory, gMap.getName() + ".yml");
if (mapFile.exists()) {
FileConfiguration fc = YamlConfiguration.loadConfiguration(mapFile);
fc.set("events." + eventName + ".enabled", this.enabled);
fc.set("events." + eventName + ".minStart", this.min);
fc.set("events." + eventName + ".maxStart", this.max);
fc.set("events." + eventName + ".length", this.length);
fc.set("events." + eventName + ".chance", this.chance);
fc.set("events." + eventName + ".title", this.title);
fc.set("events." + eventName + ".subtitle", this.subtitle);
fc.set("events." + eventName + ".startMessage", this.startMessage);
fc.set("events." + eventName + ".endMessage", this.endMessage);
fc.set("events." + eventName + ".announceTimer", this.announceEvent);
fc.set("events." + eventName + ".repeatable", this.repeatable);
fc.set("events." + eventName + ".spawnPer2Tick", this.per2Tick);
try {
fc.save(mapFile);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
private void saveSigns(LeaderType type) {
File leaderboardsFile = new File(SkyWarsReloaded.get().getDataFolder(), "leaderboards.yml");
if (!leaderboardsFile.exists()) {
SkyWarsReloaded.get().saveResource("leaderboards.yml", false);
}
if (leaderboardsFile.exists()) {
FileConfiguration storage = YamlConfiguration.loadConfiguration(leaderboardsFile);
for (int pos: signs.get(type).keySet()) {
if (signs.get(type).get(pos) != null) {
List<String> locs = new ArrayList<>();
for (Location loc: signs.get(type).get(pos)) {
locs.add(Util.get().locationToString(loc));
}
storage.set(type.toString().toLowerCase() + ".signs." + pos, locs);
}
}
try {
storage.save(leaderboardsFile);
} catch (IOException e) {
SkyWarsReloaded.get().getLogger().info("[ERROR] Failed to save leaderboards file");
}
}
if (loaded(type)) {
updateSigns(type);
}
}
@Override
public void saveEventData() {
File dataDirectory = SkyWarsReloaded.get().getDataFolder();
File mapDataDirectory = new File(dataDirectory, "mapsData");
if (!mapDataDirectory.exists() && !mapDataDirectory.mkdirs()) {
return;
}
File mapFile = new File(mapDataDirectory, gMap.getName() + ".yml");
if (mapFile.exists()) {
FileConfiguration fc = YamlConfiguration.loadConfiguration(mapFile);
fc.set("events." + eventName + ".enabled", this.enabled);
fc.set("events." + eventName + ".minStart", this.min);
fc.set("events." + eventName + ".maxStart", this.max);
fc.set("events." + eventName + ".length", this.length);
fc.set("events." + eventName + ".chance", this.chance);
fc.set("events." + eventName + ".title", this.title);
fc.set("events." + eventName + ".subtitle", this.subtitle);
fc.set("events." + eventName + ".startMessage", this.startMessage);
fc.set("events." + eventName + ".endMessage", this.endMessage);
fc.set("events." + eventName + ".announceTimer", this.announceEvent);
fc.set("events." + eventName + ".repeatable", this.repeatable);
try {
fc.save(mapFile);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
@Override
public void saveEventData() {
File dataDirectory = SkyWarsReloaded.get().getDataFolder();
File mapDataDirectory = new File(dataDirectory, "mapsData");
if (!mapDataDirectory.exists() && !mapDataDirectory.mkdirs()) {
return;
}
File mapFile = new File(mapDataDirectory, gMap.getName() + ".yml");
if (mapFile.exists()) {
FileConfiguration fc = YamlConfiguration.loadConfiguration(mapFile);
fc.set("events." + eventName + ".enabled", this.enabled);
fc.set("events." + eventName + ".minStart", this.min);
fc.set("events." + eventName + ".maxStart", this.max);
fc.set("events." + eventName + ".length", this.length);
fc.set("events." + eventName + ".chance", this.chance);
fc.set("events." + eventName + ".title", this.title);
fc.set("events." + eventName + ".subtitle", this.subtitle);
fc.set("events." + eventName + ".startMessage", this.startMessage);
fc.set("events." + eventName + ".endMessage", this.endMessage);
fc.set("events." + eventName + ".announceTimer", this.announceEvent);
fc.set("events." + eventName + ".repeatable", this.repeatable);
fc.set("events." + eventName + ".startingBorderSize", this.borderSize);
fc.set("events." + eventName + ".shrinkRepeatDelay", this.delay);
try {
fc.save(mapFile);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static void newKit(Player player, String kitName) {
File dataDirectory = SkyWarsReloaded.get().getDataFolder();
File kitsDirectory = new File(dataDirectory, "kits");
if (!kitsDirectory.exists()) {
if (!kitsDirectory.mkdirs()) {
return;
}
}
File kitFile = new File(kitsDirectory, kitName + ".yml");
FileConfiguration storage = YamlConfiguration.loadConfiguration(kitFile);
ItemStack[] inventory = player.getInventory().getContents();
storage.set("inventory", inventory);
ItemStack[] armor = player.getInventory().getArmorContents();
storage.set("armor", armor);
storage.set("requirePermission", false);
storage.set("icon", new ItemStack(Material.SNOW_BLOCK, 1));
storage.set("lockedIcon", new ItemStack(Material.BARRIER, 1));
storage.set("position", 0);
storage.set("page", 1);
storage.set("name", kitName);
storage.set("enabled", false);
for (int x = 1; x < 17; x++) {
storage.set("lores.line" + x, " ");
}
storage.set("lores.locked", "&CPermission required to unlock this kit!");
storage.set("gameSettings.noRegen", false);
storage.set("gameSettings.noPvp", false);
storage.set("gameSettings.soupPvp", false);
storage.set("gameSettings.noFallDamage", false);
storage.set("filename", kitFile.getName().substring(0, kitFile.getName().lastIndexOf('.')));
try {
storage.save(kitFile);
} catch (IOException e) {
SkyWarsReloaded.get().getLogger().info("Failed to save new kit file!");
}
GameKit.getKits().add(new GameKit(kitFile));
}
public void saveStats(final PlayerStat pData) {
boolean sqlEnabled = SkyWarsReloaded.get().getConfig().getBoolean("sqldatabase.enabled");
if (!sqlEnabled) {
try {
File dataDirectory = SkyWarsReloaded.get().getDataFolder();
File playerDataDirectory = new File(dataDirectory, "player_data");
if (!playerDataDirectory.exists() && !playerDataDirectory.mkdirs()) {
return;
}
File playerFile = new File(playerDataDirectory, pData.getId().toString() + ".yml");
if (!playerFile.exists()) {
SkyWarsReloaded.get().getLogger().info("File doesn't exist!");
return;
}
copyDefaults(playerFile);
FileConfiguration fc = YamlConfiguration.loadConfiguration(playerFile);
fc.set("uuid", pData.getId());
fc.set("wins", pData.getWins());
fc.set("losses", pData.getLosses());
fc.set("kills", pData.getKills());
fc.set("deaths", pData.getDeaths());
fc.set("elo", pData.getElo());
fc.set("xp", pData.getXp());
fc.set("pareffect", pData.getParticleEffect());
fc.set("proeffect", pData.getProjectileEffect());
fc.set("glasscolor", pData.getGlassColor());
fc.set("killsound", pData.getKillSound());
fc.set("winsound", pData.getWinSound());
fc.set("taunt", pData.getTaunt());
fc.save(playerFile);
} catch (IOException ioException) {
System.out.println("Failed to load faction " + pData.getId() + ": " + ioException.getMessage());
}
} else {
Database database = SkyWarsReloaded.getDb();
if (!database.checkConnection()) {
return;
}
Connection connection = database.getConnection();
PreparedStatement preparedStatement = null;
try {
StringBuilder queryBuilder = new StringBuilder();
queryBuilder.append("UPDATE `sw_player` SET ");
queryBuilder.append("`player_name` = ?, `wins` = ?, `losses` = ?, ");
queryBuilder.append("`kills` = ?, `deaths` = ?, `elo` = ?, `xp` = ?, `pareffect` = ?, `proeffect` = ?, `glasscolor` = ?,`killsound` = ?, `winsound` = ?, `taunt` = ? ");
queryBuilder.append("WHERE `uuid` = ?;");
preparedStatement = connection.prepareStatement(queryBuilder.toString());
preparedStatement.setString(1, pData.getPlayerName());
preparedStatement.setInt(2, pData.getWins());
preparedStatement.setInt(3, pData.getLosses());
preparedStatement.setInt(4, pData.getKills());
preparedStatement.setInt(5, pData.getDeaths());
preparedStatement.setInt(6, pData.getElo());
preparedStatement.setInt(7, pData.getXp());
preparedStatement.setString(8, pData.getParticleEffect());
preparedStatement.setString(9, pData.getProjectileEffect());
preparedStatement.setString(10, pData.getGlassColor());
preparedStatement.setString(11, pData.getKillSound());
preparedStatement.setString(12, pData.getWinSound());
preparedStatement.setString(13, pData.getTaunt());
preparedStatement.setString(14, pData.getId());
preparedStatement.executeUpdate();
} catch (final SQLException sqlException) {
sqlException.printStackTrace();
} finally {
if (preparedStatement != null) {
try {
preparedStatement.close();
} catch (final SQLException ignored) {
}
}
}
}
}
public void saveStats(final PlayerStat pData) {
boolean sqlEnabled = SkyWarsReloaded.get().getConfig().getBoolean("sqldatabase.enabled");
if (!sqlEnabled) {
try {
File dataDirectory = SkyWarsReloaded.get().getDataFolder();
File playerDataDirectory = new File(dataDirectory, "player_data");
if (!playerDataDirectory.exists() && !playerDataDirectory.mkdirs()) {
return;
}
File playerFile = new File(playerDataDirectory, pData.getId() + ".yml");
if (!playerFile.exists()) {
SkyWarsReloaded.get().getLogger().info("File doesn't exist!");
return;
}
copyDefaults(playerFile);
FileConfiguration fc = YamlConfiguration.loadConfiguration(playerFile);
fc.set("uuid", pData.getId());
fc.set("wins", pData.getWins());
fc.set("losses", pData.getLosses());
fc.set("kills", pData.getKills());
fc.set("deaths", pData.getDeaths());
fc.set("elo", pData.getElo());
fc.set("xp", pData.getXp());
fc.set("pareffect", pData.getParticleEffect());
fc.set("proeffect", pData.getProjectileEffect());
fc.set("glasscolor", pData.getGlassColor());
fc.set("killsound", pData.getKillSound());
fc.set("winsound", pData.getWinSound());
fc.set("taunt", pData.getTaunt());
fc.save(playerFile);
} catch (IOException ioException) {
System.out.println("Failed to load faction " + pData.getId() + ": " + ioException.getMessage());
}
} else {
Database database = SkyWarsReloaded.getDb();
if (database.checkConnection()) {
return;
}
Connection connection = database.getConnection();
PreparedStatement preparedStatement = null;
try {
String query = "UPDATE `sw_player` SET `player_name` = ?, `wins` = ?, `losses` = ?, `kills` = ?, `deaths` = ?, `elo` = ?, `xp` = ?, `pareffect` = ?, " +
"`proeffect` = ?, `glasscolor` = ?,`killsound` = ?, `winsound` = ?, `taunt` = ? WHERE `uuid` = ?;";
preparedStatement = connection.prepareStatement(query);
preparedStatement.setString(1, pData.getPlayerName());
preparedStatement.setInt(2, pData.getWins());
preparedStatement.setInt(3, pData.getLosses());
preparedStatement.setInt(4, pData.getKills());
preparedStatement.setInt(5, pData.getDeaths());
preparedStatement.setInt(6, pData.getElo());
preparedStatement.setInt(7, pData.getXp());
preparedStatement.setString(8, pData.getParticleEffect());
preparedStatement.setString(9, pData.getProjectileEffect());
preparedStatement.setString(10, pData.getGlassColor());
preparedStatement.setString(11, pData.getKillSound());
preparedStatement.setString(12, pData.getWinSound());
preparedStatement.setString(13, pData.getTaunt());
preparedStatement.setString(14, pData.getId());
preparedStatement.executeUpdate();
} catch (final SQLException sqlException) {
sqlException.printStackTrace();
} finally {
if (preparedStatement != null) {
try {
preparedStatement.close();
} catch (final SQLException ignored) {
}
}
}
}
}
private Boolean importPlayer(File file) {
log.info("Importing " + file);
String name = FileUtil.getBasename(file);
FileConfiguration config = new YamlConfiguration();
FileUtil.readConfig(config, file);
UUID uniqueId;
if (invalidNames.contains(name)) {
uniqueId = UUIDUtil.fromString(config.getString("player.uuid", null));
if (uniqueId != null) {
invalidNames.remove(name);
playerDB.updatePlayer(uniqueId, name, null);
}
} else {
uniqueId = playerDB.getUUIDFromName(name);
}
if (uniqueId == null) {
log.info("No UUID found for " + name);
file.renameTo(new File(playerErrorFolder, file.getName()));
return false;
}
File newConfig = new File(plugin.getDataFolder() + File.separator + "players", uniqueId.toString() + ".yml");
if (file.renameTo(newConfig)) {
FileUtil.readConfig(config, newConfig);
config.set("player.name", name);
config.set("player.uuid", UUIDUtil.asString(uniqueId));
try {
config.save(newConfig);
if (!newConfig.getName().equals(file.getName())) {
if (file.exists() && !file.delete()) {
file.deleteOnExit();
}
}
return true;
} catch (IOException e) {
log.log(Level.SEVERE, "Failed!", e);
return false;
}
} else if (newConfig.exists()) {
log.info("Unable to move " + file + " to " + newConfig + " since it already exists!");
file.renameTo(new File(newConfig.getParent(), newConfig.getName() + ".old"));
}
return false;
}