Rename DataFileCache to PlayerData

This commit is contained in:
DNx5 2016-02-20 09:32:08 +07:00
parent 2f75e03275
commit 27642dd82c
3 changed files with 27 additions and 56 deletions

View File

@ -19,8 +19,6 @@ import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.lang.reflect.Type; import java.lang.reflect.Type;
/**
*/
public class JsonCache { public class JsonCache {
private final Gson gson; private final Gson gson;
@ -32,13 +30,13 @@ public class JsonCache {
ConsoleLogger.showError("Failed to create cache directory."); ConsoleLogger.showError("Failed to create cache directory.");
} }
gson = new GsonBuilder() gson = new GsonBuilder()
.registerTypeAdapter(DataFileCache.class, new PlayerDataSerializer()) .registerTypeAdapter(PlayerData.class, new PlayerDataSerializer())
.registerTypeAdapter(DataFileCache.class, new PlayerDataDeserializer()) .registerTypeAdapter(PlayerData.class, new PlayerDataDeserializer())
.setPrettyPrinting() .setPrettyPrinting()
.create(); .create();
} }
public void createCache(Player player, DataFileCache playerData) { public void createCache(Player player, PlayerData playerData) {
if (player == null) { if (player == null) {
return; return;
} }
@ -67,7 +65,7 @@ public class JsonCache {
} }
} }
public DataFileCache readCache(Player player) { public PlayerData readCache(Player player) {
String path; String path;
try { try {
path = player.getUniqueId().toString(); path = player.getUniqueId().toString();
@ -82,7 +80,7 @@ public class JsonCache {
try { try {
String str = Files.toString(file, Charsets.UTF_8); String str = Files.toString(file, Charsets.UTF_8);
return gson.fromJson(str, DataFileCache.class); return gson.fromJson(str, PlayerData.class);
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
return null; return null;
@ -116,9 +114,9 @@ public class JsonCache {
return file.exists(); return file.exists();
} }
private class PlayerDataDeserializer implements JsonDeserializer<DataFileCache> { private class PlayerDataDeserializer implements JsonDeserializer<PlayerData> {
@Override @Override
public DataFileCache deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext) throws JsonParseException { public PlayerData deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext) throws JsonParseException {
JsonObject jsonObject = jsonElement.getAsJsonObject(); JsonObject jsonObject = jsonElement.getAsJsonObject();
if (jsonObject == null) { if (jsonObject == null) {
return null; return null;
@ -138,17 +136,18 @@ public class JsonCache {
fly = e.getAsBoolean(); fly = e.getAsBoolean();
} }
return new DataFileCache(group, operator, fly); return new PlayerData(group, operator, fly);
} }
} }
private class PlayerDataSerializer implements JsonSerializer<DataFileCache> { private class PlayerDataSerializer implements JsonSerializer<PlayerData> {
@Override @Override
public JsonElement serialize(DataFileCache dataFileCache, Type type, JsonSerializationContext jsonSerializationContext) { public JsonElement serialize(PlayerData playerData, Type type,
JsonSerializationContext jsonSerializationContext) {
JsonObject jsonObject = new JsonObject(); JsonObject jsonObject = new JsonObject();
jsonObject.addProperty("group", dataFileCache.getGroup()); jsonObject.addProperty("group", playerData.getGroup());
jsonObject.addProperty("operator", dataFileCache.getOperator()); jsonObject.addProperty("operator", playerData.getOperator());
jsonObject.addProperty("fly", dataFileCache.isFlyEnabled()); jsonObject.addProperty("fly", playerData.isFlyEnabled());
return jsonObject; return jsonObject;
} }
} }

View File

@ -1,39 +1,21 @@
package fr.xephi.authme.cache.backup; package fr.xephi.authme.cache.backup;
/** public class PlayerData {
*/
public class DataFileCache {
private final String group; private final String group;
private final boolean operator; private final boolean operator;
private final boolean flyEnabled; private final boolean flyEnabled;
/** public PlayerData(String group, boolean operator, boolean flyEnabled) {
* Constructor for DataFileCache.
*
* @param group String
* @param operator boolean
*/
public DataFileCache(String group, boolean operator, boolean flyEnabled) {
this.group = group; this.group = group;
this.operator = operator; this.operator = operator;
this.flyEnabled = flyEnabled; this.flyEnabled = flyEnabled;
} }
/**
* Method getGroup.
*
* @return String
*/
public String getGroup() { public String getGroup() {
return group; return group;
} }
/**
* Method getOperator.
*
* @return boolean
*/
public boolean getOperator() { public boolean getOperator() {
return operator; return operator;
} }

View File

@ -1,9 +1,8 @@
package fr.xephi.authme.cache.limbo; package fr.xephi.authme.cache.limbo;
import fr.xephi.authme.AuthMe; import fr.xephi.authme.AuthMe;
import fr.xephi.authme.ConsoleLogger;
import fr.xephi.authme.cache.backup.DataFileCache;
import fr.xephi.authme.cache.backup.JsonCache; import fr.xephi.authme.cache.backup.JsonCache;
import fr.xephi.authme.cache.backup.PlayerData;
import fr.xephi.authme.permission.PermissionsManager; import fr.xephi.authme.permission.PermissionsManager;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
@ -19,7 +18,7 @@ public class LimboCache {
private volatile static LimboCache singleton; private volatile static LimboCache singleton;
public final ConcurrentHashMap<String, LimboPlayer> cache; public final ConcurrentHashMap<String, LimboPlayer> cache;
public final AuthMe plugin; public final AuthMe plugin;
private final JsonCache playerData; private final JsonCache jsonCache;
/** /**
* Constructor for LimboCache. * Constructor for LimboCache.
@ -29,7 +28,7 @@ public class LimboCache {
private LimboCache(AuthMe plugin) { private LimboCache(AuthMe plugin) {
this.plugin = plugin; this.plugin = plugin;
this.cache = new ConcurrentHashMap<>(); this.cache = new ConcurrentHashMap<>();
this.playerData = new JsonCache(); this.jsonCache = new JsonCache();
} }
/** /**
@ -52,30 +51,21 @@ public class LimboCache {
public void addLimboPlayer(Player player) { public void addLimboPlayer(Player player) {
String name = player.getName().toLowerCase(); String name = player.getName().toLowerCase();
Location loc = player.getLocation(); Location loc = player.getLocation();
boolean operator = false; boolean operator = player.isOp();
boolean flyEnabled = false; boolean flyEnabled = player.getAllowFlight();
String playerGroup = ""; String playerGroup = "";
PermissionsManager permsMan = plugin.getPermissionsManager();
if (permsMan.hasGroupSupport()) {
playerGroup = permsMan.getPrimaryGroup(player);
}
// Get the permissions manager, and make sure it's valid if (jsonCache.doesCacheExist(player)) {
PermissionsManager permsMan = this.plugin.getPermissionsManager(); PlayerData cache = jsonCache.readCache(player);
if (permsMan == null)
ConsoleLogger.showError("Unable to access permissions manager!");
assert permsMan != null;
if (playerData.doesCacheExist(player)) {
DataFileCache cache = playerData.readCache(player);
if (cache != null) { if (cache != null) {
playerGroup = cache.getGroup(); playerGroup = cache.getGroup();
operator = cache.getOperator(); operator = cache.getOperator();
flyEnabled = cache.isFlyEnabled(); flyEnabled = cache.isFlyEnabled();
} }
} else {
operator = player.isOp();
// Check whether groups are supported
if (permsMan.hasGroupSupport()) {
playerGroup = permsMan.getPrimaryGroup(player);
}
} }
if (player.isDead()) { if (player.isDead()) {