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

View File

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

View File

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