#28 Using TBMCPlayer instead of YamlConfig

This commit is contained in:
Norbi Peti 2017-01-28 21:05:07 +01:00
parent 5a559aa4c3
commit 159cef01ff
2 changed files with 11 additions and 18 deletions

View file

@ -84,7 +84,7 @@ public abstract class ChromaGamerBase implements AutoCloseable {
try { try {
save_(); save_();
} catch (Exception e) { } catch (Exception e) {
TBMCCoreAPI.SendException("Error while saving player data!", e); TBMCCoreAPI.SendException("Error while saving player to " + getFolder() + "/" + getFileName() + ".yml!", e);
} }
} }

View file

@ -1,6 +1,5 @@
package buttondevteam.lib.player; package buttondevteam.lib.player;
import java.io.File;
import java.util.Iterator; import java.util.Iterator;
import java.util.Map.Entry; import java.util.Map.Entry;
import java.util.UUID; import java.util.UUID;
@ -8,7 +7,6 @@ import java.util.concurrent.ConcurrentHashMap;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer; import org.bukkit.OfflinePlayer;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import com.palmergames.bukkit.towny.Towny; import com.palmergames.bukkit.towny.Towny;
@ -66,10 +64,10 @@ public abstract class TBMCPlayerBase extends ChromaGamerBase {
return (T) playermap.get(uuid + "-" + cl.getSimpleName()); return (T) playermap.get(uuid + "-" + cl.getSimpleName());
try { try {
T player; T player;
if (playerfiles.containsKey(uuid)) { if (playermap.containsKey(uuid + "-" + TBMCPlayer.class.getSimpleName())) {
player = cl.newInstance(); player = cl.newInstance();
player.plugindata = playerfiles.get(uuid); player.plugindata = playermap.get(uuid + "-" + TBMCPlayer.class.getSimpleName()).plugindata;
playermap.put(player.uuid + "-" + player.getFolder(), player); // It will get removed on player quit playermap.put(player.uuid + "-" + cl.getSimpleName(), player); // It will get removed on player quit
} else } else
player = ChromaGamerBase.getUser(uuid.toString(), cl); player = ChromaGamerBase.getUser(uuid.toString(), cl);
player.uuid = uuid; player.uuid = uuid;
@ -86,8 +84,6 @@ public abstract class TBMCPlayerBase extends ChromaGamerBase {
*/ */
static final ConcurrentHashMap<String, TBMCPlayerBase> playermap = new ConcurrentHashMap<>(); static final ConcurrentHashMap<String, TBMCPlayerBase> playermap = new ConcurrentHashMap<>();
private static final ConcurrentHashMap<UUID, YamlConfiguration> playerfiles = new ConcurrentHashMap<>();
/** /**
* Gets the TBMCPlayer object as a specific plugin player, keeping it's data<br> * Gets the TBMCPlayer object as a specific plugin player, keeping it's data<br>
* Make sure to use try-with-resources with this to save the data, as it may need to load the file * Make sure to use try-with-resources with this to save the data, as it may need to load the file
@ -124,11 +120,13 @@ public abstract class TBMCPlayerBase extends ChromaGamerBase {
Bukkit.getLogger().info("Renaming " + player.getPlayerName() + " to " + p.getName()); Bukkit.getLogger().info("Renaming " + player.getPlayerName() + " to " + p.getName());
TownyUniverse tu = Towny.getPlugin(Towny.class).getTownyUniverse(); TownyUniverse tu = Towny.getPlugin(Towny.class).getTownyUniverse();
Resident resident = tu.getResidentMap().get(player.getPlayerName()); Resident resident = tu.getResidentMap().get(player.getPlayerName());
if (resident == null) if (resident == null) {
Bukkit.getLogger().warning("Resident not found - couldn't rename in Towny."); Bukkit.getLogger().warning("Resident not found - couldn't rename in Towny.");
else if (tu.getResidentMap().contains(p.getName())) TBMCCoreAPI.sendDebugMessage("Resident not found - couldn't rename in Towny.");
} else if (tu.getResidentMap().contains(p.getName())) {
Bukkit.getLogger().warning("Target resident name is already in use."); // TODO: Handle Bukkit.getLogger().warning("Target resident name is already in use."); // TODO: Handle
else TBMCCoreAPI.sendDebugMessage("Target resident name is already in use.");
} else
try { try {
TownyUniverse.getDataSource().renamePlayer(resident, p.getName()); TownyUniverse.getDataSource().renamePlayer(resident, p.getName());
} catch (AlreadyRegisteredException e) { } catch (AlreadyRegisteredException e) {
@ -158,13 +156,8 @@ public abstract class TBMCPlayerBase extends ChromaGamerBase {
Bukkit.getServer().getPluginManager().callEvent(new TBMCPlayerQuitEvent(player)); Bukkit.getServer().getPluginManager().callEvent(new TBMCPlayerQuitEvent(player));
} }
} }
final YamlConfiguration playerfile = playerfiles.get(p.getUniqueId()); final TBMCPlayerBase player = playermap.get(p.getUniqueId() + "-" + TBMCPlayer.class.getSimpleName());
try { // Only save to file once, not for each plugin player.save();
playerfile.save(p.getUniqueId().toString() + ".yml"); // TODO: Bring this together with the close() method, like a common method or something
} catch (Exception e) {
TBMCCoreAPI.SendException("Error while saving quitting player " + playerfile.getString("playername") + " ("
+ "minecraft/" + p.getUniqueId() + ".yml)!", e);
}
} }
public static void savePlayers() { public static void savePlayers() {