Added player data converter and fixes

This commit is contained in:
Norbi Peti 2017-04-13 00:50:05 +02:00
parent 7c8a47e4b2
commit 0160b4146f
3 changed files with 61 additions and 8 deletions

View file

@ -1,13 +1,22 @@
package buttondevteam.core;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.logging.Logger;
import org.bukkit.Bukkit;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.InvalidConfigurationException;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.plugin.PluginDescriptionFile;
import org.bukkit.plugin.RegisteredServiceProvider;
import org.bukkit.plugin.java.JavaPlugin;
import buttondevteam.lib.TBMCCoreAPI;
import buttondevteam.lib.chat.TBMCChatAPI;
import buttondevteam.lib.player.ChromaGamerBase;
import buttondevteam.lib.player.TBMCPlayerBase;
import net.milkbowl.vault.permission.Permission;
@ -18,6 +27,7 @@ public class MainPlugin extends JavaPlugin {
private PluginDescriptionFile pdfFile;
private Logger logger;
private int C = 0, keep = 0;
@Override
public void onEnable() {
@ -33,6 +43,45 @@ public class MainPlugin extends JavaPlugin {
TBMCCoreAPI.RegisterEventsForExceptions(new PlayerListener(), this);
TBMCCoreAPI.RegisterUserClass(TBMCPlayerBase.class);
logger.info(pdfFile.getName() + " has been Enabled (V." + pdfFile.getVersion() + ").");
Arrays.stream(new File(ChromaGamerBase.TBMC_PLAYERS_DIR).listFiles(f -> !f.isDirectory())).map(f -> {
YamlConfiguration yc = new YamlConfiguration();
try {
yc.load(f);
} catch (IOException | InvalidConfigurationException e) {
TBMCCoreAPI.SendException("Error while converting player data!", e);
}
f.delete();
return yc;
}).forEach(yc -> {
try {
int flairtime = yc.getInt("flairtime"), fcount = yc.getInt("fcount"), fdeaths = yc.getInt("fdeaths");
String flairstate = yc.getString("flairstate");
List<String> usernames = yc.getStringList("usernames");
boolean flaircheater = yc.getBoolean("flaircheater");
final String uuid = yc.getString("uuid");
C++;
if ((fcount == 0 || fdeaths == 0)
&& (flairstate == null || "NoComment".equals(flairstate) || flairtime <= 0))
return; // Those who received no Fs yet will also get their stats reset if no flair
final File file = new File(ChromaGamerBase.TBMC_PLAYERS_DIR + "minecraft", uuid + ".yml");
YamlConfiguration targetyc = YamlConfiguration.loadConfiguration(file);
targetyc.set("PlayerName", yc.getString("playername"));
targetyc.set("minecraft_id", uuid);
ConfigurationSection bc = targetyc.createSection("ButtonChat");
bc.set("FlairTime", "NoComment".equals(flairstate) ? -3 : flairtime); // FlairTimeNone: -3
bc.set("FCount", fcount);
bc.set("FDeaths", fdeaths);
bc.set("FlairState", flairstate);
bc.set("UserNames", usernames);
bc.set("FlairCheater", flaircheater);
targetyc.save(file);
keep++;
} catch (Exception e) {
TBMCCoreAPI.SendException("Error while converting player data!", e);
}
});
Bukkit.getScheduler().runTask(this, () -> logger.info("Converted " + keep + " player data from " + C));
//TODO: Remove once ran it at least once
}
@Override

View file

@ -1,7 +1,6 @@
package buttondevteam.lib.player;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map.Entry;
import java.util.function.Consumer;
@ -85,24 +84,21 @@ public abstract class ChromaGamerBase implements AutoCloseable {
*/
@Override
public void close() throws Exception {
save_();
if (plugindata.getKeys(false).size() > 0)
plugindata.save(new File(TBMC_PLAYERS_DIR + getFolder(), getFileName() + ".yml"));
}
/**
* Saves the player. It'll send all exceptions that may happen. To catch the exception, use {@link #close()} instead.
* Saves the player. It'll handle all exceptions that may happen. To catch the exception, use {@link #close()} instead.
*/
public void save() {
try {
save_();
close();
} catch (Exception e) {
TBMCCoreAPI.SendException("Error while saving player to " + getFolder() + "/" + getFileName() + ".yml!", e);
}
}
private void save_() throws IOException {
plugindata.save(new File(TBMC_PLAYERS_DIR + getFolder(), getFileName() + ".yml"));
}
/**
* Connect two accounts. Do not use for connecting two Minecraft accounts or similar. Also make sure you have the "id" tag set
*

View file

@ -2,6 +2,7 @@ package buttondevteam.lib.player;
import java.util.Iterator;
import java.util.Map.Entry;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
@ -209,4 +210,11 @@ public abstract class TBMCPlayerBase extends ChromaGamerBase {
else
return null;
}
@Override
public void close() throws Exception {
Set<String> keys = plugindata.getKeys(false);
if (keys.size() > 1) // PlayerName is always saved, but we don't need a file for just that
super.close();
}
}