Use JsonCache correctly, couldn't list all changes.

This commit is contained in:
DNx5 2016-06-28 21:36:58 +07:00
parent 874869cef8
commit 145747505f
17 changed files with 185 additions and 152 deletions

View File

@ -580,11 +580,12 @@ public class AuthMe extends JavaPlugin {
if (!Settings.noTeleport) {
player.teleport(limbo.getLoc());
}
Utils.addNormal(player, limbo.getGroup());
player.setOp(limbo.isOperator());
limbo.getTimeoutTask().cancel();
limboCache.deleteLimboPlayer(name);
player.setAllowFlight(limbo.isCanFly());
player.setWalkSpeed(limbo.getWalkSpeed());
limbo.clearTasks();
limboCache.deleteLimboPlayer(player);
}
PlayerCache.getInstance().removePlayer(name);
}

View File

@ -12,8 +12,17 @@ import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;
import fr.xephi.authme.AuthMe;
import fr.xephi.authme.ConsoleLogger;
import fr.xephi.authme.cache.limbo.LimboPlayer;
import fr.xephi.authme.permission.PermissionsManager;
import fr.xephi.authme.settings.SpawnLoader;
import fr.xephi.authme.util.BukkitService;
import fr.xephi.authme.util.FileUtils;
import fr.xephi.authme.util.Utils;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.entity.Player;
import javax.inject.Inject;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Type;
@ -22,110 +31,145 @@ public class JsonCache {
private final Gson gson;
private final File cacheDir;
@Inject
private AuthMe plugin;
@Inject
private PermissionsManager permissionsManager;
@Inject
private SpawnLoader spawnLoader;
@Inject
private BukkitService bukkitService;
public JsonCache() {
cacheDir = new File(AuthMe.getInstance().getDataFolder(), "cache");
cacheDir = new File(plugin.getDataFolder(), "cache");
if (!cacheDir.exists() && !cacheDir.isDirectory() && !cacheDir.mkdir()) {
ConsoleLogger.showError("Failed to create cache directory.");
}
gson = new GsonBuilder()
.registerTypeAdapter(PlayerData.class, new PlayerDataSerializer())
.registerTypeAdapter(PlayerData.class, new PlayerDataDeserializer())
.registerTypeAdapter(LimboPlayer.class, new LimboPlayerSerializer())
.registerTypeAdapter(LimboPlayer.class, new LimboPlayerDeserializer())
.setPrettyPrinting()
.create();
}
public PlayerData readCache(Player player) {
String name = player.getName().toLowerCase();
File file = new File(cacheDir, name + File.separator + "cache.json");
public LimboPlayer readCache(Player player) {
String id = Utils.getUUIDorName(player);
File file = new File(cacheDir, id + File.separator + "cache.json");
if (!file.exists()) {
return null;
}
try {
String str = Files.toString(file, Charsets.UTF_8);
return gson.fromJson(str, PlayerData.class);
return gson.fromJson(str, LimboPlayer.class);
} catch (IOException e) {
ConsoleLogger.writeStackTrace(e);
return null;
}
}
public void removeCache(Player player) {
public void writeCache(Player player) {
String id = Utils.getUUIDorName(player);
String name = player.getName().toLowerCase();
File file = new File(cacheDir, name);
Location location = player.isDead() ? spawnLoader.getSpawnLocation(player) : player.getLocation();
String group = permissionsManager.getPrimaryGroup(player);
boolean operator = player.isOp();
boolean canFly = player.getAllowFlight();
float walkSpeed = player.getWalkSpeed();
LimboPlayer limboPlayer = new LimboPlayer(name, location, operator, group, canFly, walkSpeed);
try {
File file = new File(cacheDir, id + File.separator + "cache.json");
Files.write(gson.toJson(limboPlayer), file, Charsets.UTF_8);
} catch (IOException e) {
ConsoleLogger.logException("Failed to write " + player.getName() + " cache.", e);
}
}
public void removeCache(Player player) {
String id = Utils.getUUIDorName(player);
File file = new File(cacheDir, id);
if (file.exists()) {
purgeDirectory(file);
FileUtils.purgeDirectory(file);
if (!file.delete()) {
ConsoleLogger.showError("Failed to remove" + player.getName() + "cache.");
ConsoleLogger.showError("Failed to remove " + player.getName() + " cache.");
}
}
}
public boolean doesCacheExist(Player player) {
String name = player.getName().toLowerCase();
File file = new File(cacheDir, name + File.separator + "cache.json");
String id = Utils.getUUIDorName(player);
File file = new File(cacheDir, id + File.separator + "cache.json");
return file.exists();
}
private class PlayerDataDeserializer implements JsonDeserializer<PlayerData> {
private class LimboPlayerDeserializer implements JsonDeserializer<LimboPlayer> {
@Override
public PlayerData deserialize(JsonElement jsonElement, Type type,
JsonDeserializationContext context) {
public LimboPlayer deserialize(JsonElement jsonElement, Type type,
JsonDeserializationContext context) {
JsonObject jsonObject = jsonElement.getAsJsonObject();
if (jsonObject == null) {
return null;
}
String group = null;
Location loc = null;
String group = "";
boolean operator = false;
boolean fly = false;
boolean canFly = false;
float walkSpeed = 0.2f;
JsonElement e;
if ((e = jsonObject.getAsJsonObject("location")) != null) {
JsonObject obj = e.getAsJsonObject();
World world = bukkitService.getWorld(obj.get("world").getAsString());
if (world != null) {
double x = obj.get("x").getAsDouble();
double y = obj.get("y").getAsDouble();
double z = obj.get("z").getAsDouble();
float yaw = obj.get("yaw").getAsFloat();
float pitch = obj.get("pitch").getAsFloat();
loc = new Location(world, x, y, z, yaw, pitch);
}
}
if ((e = jsonObject.get("group")) != null) {
group = e.getAsString();
}
if ((e = jsonObject.get("operator")) != null) {
operator = e.getAsBoolean();
}
if ((e = jsonObject.get("fly")) != null) {
fly = e.getAsBoolean();
if ((e = jsonObject.get("can-fly")) != null) {
canFly = e.getAsBoolean();
}
if ((e = jsonObject.get("walk-speed")) != null) {
walkSpeed = e.getAsFloat();
}
return new PlayerData(group, operator, fly);
return new LimboPlayer("", loc, operator, group, canFly, walkSpeed);
}
}
private class PlayerDataSerializer implements JsonSerializer<PlayerData> {
private class LimboPlayerSerializer implements JsonSerializer<LimboPlayer> {
@Override
public JsonElement serialize(PlayerData playerData, Type type,
public JsonElement serialize(LimboPlayer limboPlayer, Type type,
JsonSerializationContext context) {
JsonObject jsonObject = new JsonObject();
jsonObject.addProperty("group", playerData.getGroup());
jsonObject.addProperty("operator", playerData.getOperator());
jsonObject.addProperty("fly", playerData.isFlyEnabled());
return jsonObject;
JsonObject obj = new JsonObject();
obj.addProperty("group", limboPlayer.getGroup());
Location loc = limboPlayer.getLoc();
JsonObject obj2 = new JsonObject();
obj2.addProperty("world", loc.getWorld().getName());
obj2.addProperty("x", loc.getX());
obj2.addProperty("y", loc.getY());
obj2.addProperty("z", loc.getZ());
obj2.addProperty("yaw", loc.getYaw());
obj2.addProperty("pitch", loc.getPitch());
obj.add("location", obj2);
obj.addProperty("operator", limboPlayer.isOperator());
obj.addProperty("can-fly", limboPlayer.isCanFly());
obj.addProperty("walk-speed", limboPlayer.getWalkSpeed());
return obj;
}
}
/**
* Delete a given directory and all its content.
*
* @param directory The directory to remove
*/
private static void purgeDirectory(File directory) {
if (!directory.isDirectory()) {
return;
}
File[] files = directory.listFiles();
if (files == null) {
return;
}
for (File target : files) {
if (target.isDirectory()) {
purgeDirectory(target);
}
target.delete();
}
}
}

View File

@ -1,26 +0,0 @@
package fr.xephi.authme.cache.backup;
public class PlayerData {
private final String group;
private final boolean operator;
private final boolean flyEnabled;
public PlayerData(String group, boolean operator, boolean flyEnabled) {
this.group = group;
this.operator = operator;
this.flyEnabled = flyEnabled;
}
public String getGroup() {
return group;
}
public boolean getOperator() {
return operator;
}
public boolean isFlyEnabled() {
return flyEnabled;
}
}

View File

@ -1,7 +1,6 @@
package fr.xephi.authme.cache.limbo;
import fr.xephi.authme.cache.backup.JsonCache;
import fr.xephi.authme.cache.backup.PlayerData;
import fr.xephi.authme.permission.PermissionsManager;
import fr.xephi.authme.settings.SpawnLoader;
import org.bukkit.Location;
@ -41,36 +40,40 @@ public class LimboCache {
Location location = player.isDead() ? spawnLoader.getSpawnLocation(player) : player.getLocation();
boolean operator = player.isOp();
boolean flyEnabled = player.getAllowFlight();
float walkSpeed = player.getWalkSpeed();
String playerGroup = "";
if (permissionsManager.hasGroupSupport()) {
playerGroup = permissionsManager.getPrimaryGroup(player);
}
if (jsonCache.doesCacheExist(player)) {
PlayerData cache = jsonCache.readCache(player);
LimboPlayer cache = jsonCache.readCache(player);
if (cache != null) {
location = cache.getLoc();
playerGroup = cache.getGroup();
operator = cache.getOperator();
flyEnabled = cache.isFlyEnabled();
operator = cache.isOperator();
flyEnabled = cache.isCanFly();
walkSpeed = cache.getWalkSpeed();
}
} else {
jsonCache.writeCache(player);
}
cache.put(name, new LimboPlayer(name, location, operator, playerGroup, flyEnabled));
cache.put(name, new LimboPlayer(name, location, operator, playerGroup, flyEnabled, walkSpeed));
}
/**
* Method deleteLimboPlayer.
*
* @param name String
* @param player Player player to remove.
*/
public void deleteLimboPlayer(String name) {
checkNotNull(name);
name = name.toLowerCase();
public void deleteLimboPlayer(Player player) {
String name = player.getName().toLowerCase();
LimboPlayer cachedPlayer = cache.remove(name);
if (cachedPlayer != null) {
cachedPlayer.clearTasks();
}
jsonCache.removeCache(player);
}
/**
@ -104,7 +107,7 @@ public class LimboCache {
*/
public void updateLimboPlayer(Player player) {
checkNotNull(player);
deleteLimboPlayer(player.getName().toLowerCase());
deleteLimboPlayer(player);
addLimboPlayer(player);
}

View File

@ -10,20 +10,22 @@ import org.bukkit.scheduler.BukkitTask;
public class LimboPlayer {
private final String name;
private final boolean fly;
private final boolean canFly;
private final boolean operator;
private final String group;
private final Location loc;
private final float walkSpeed;
private BukkitTask timeoutTask = null;
private BukkitTask messageTask = null;
public LimboPlayer(String name, Location loc, boolean operator,
String group, boolean fly) {
String group, boolean fly, float walkSpeed) {
this.name = name;
this.loc = loc;
this.operator = operator;
this.group = group;
this.fly = fly;
this.canFly = fly;
this.walkSpeed = walkSpeed;
}
/**
@ -62,8 +64,12 @@ public class LimboPlayer {
return group;
}
public boolean isFly() {
return fly;
public boolean isCanFly() {
return canFly;
}
public float getWalkSpeed() {
return walkSpeed;
}
/**

View File

@ -73,10 +73,6 @@ public class AuthMeInventoryPacketAdapter extends PacketAdapter {
ProtocolLibrary.getProtocolManager().removePacketListener(this);
}
public void sendInventoryPacket(Player player) {
player.updateInventory();
}
public void sendBlankInventoryPacket(Player player) {
ProtocolManager protocolManager = ProtocolLibrary.getProtocolManager();
PacketContainer inventoryPacket = protocolManager.createPacket(PacketType.Play.Server.WINDOW_ITEMS);

View File

@ -98,19 +98,6 @@ public class ProtocolLibService implements SettingsDependent {
}
}
/**
* Send a packet to the player to give them an inventory.
*
* @param player The player to send the packet to.
*/
public void sendInventoryPacket(Player player) {
if (!isEnabled || inventoryPacketAdapter == null) {
return;
}
inventoryPacketAdapter.sendInventoryPacket(player);
}
/**
* Send a packet to the player to give them a blank inventory.
*

View File

@ -67,14 +67,17 @@ public class AuthGroupHandler {
case LOGGED_IN:
// Get the limbo player data
LimboPlayer limbo = limboCache.getLimboPlayer(player.getName().toLowerCase());
if (limbo == null)
if (limbo == null) {
return false;
}
// Get the players group
String realGroup = limbo.getGroup();
// Remove the other group types groups, set the real group
permissionsManager.removeGroups(player, Arrays.asList(Settings.unRegisteredGroup, Settings.getRegisteredGroup, Settings.getUnloggedinGroup));
permissionsManager.removeGroups(player,
Arrays.asList(Settings.unRegisteredGroup, Settings.getRegisteredGroup, Settings.getUnloggedinGroup)
);
return permissionsManager.addGroup(player, realGroup);
default:
return false;

View File

@ -71,11 +71,11 @@ public class SyncProcessManager {
});
}
public void processSyncPlayerQuit(final Player player, final boolean isOp, final boolean needToChange) {
public void processSyncPlayerQuit(final Player player) {
runTask(new Runnable() {
@Override
public void run() {
processSyncronousPlayerQuit.processSyncQuit(player, isOp, needToChange);
processSyncronousPlayerQuit.processSyncQuit(player);
}
});
}

View File

@ -134,7 +134,7 @@ public class AsynchronousJoin implements AsynchronousProcess {
ProtectInventoryEvent ev = new ProtectInventoryEvent(player);
bukkitService.callEvent(ev);
if (ev.isCancelled()) {
protocolLibService.sendInventoryPacket(player);
player.updateInventory();
if (!service.getProperty(SecuritySettings.REMOVE_SPAM_FROM_CONSOLE)) {
ConsoleLogger.info("ProtectInventoryEvent has been cancelled for " + player.getName() + "...");
}

View File

@ -199,12 +199,7 @@ public class AsynchronousLogin implements AsynchronousProcess {
// processed in other order.
LimboPlayer limboPlayer = limboCache.getLimboPlayer(name);
if (limboPlayer != null) {
if (limboPlayer.getTimeoutTask() != null) {
limboPlayer.getTimeoutTask().cancel();
}
if (limboPlayer.getMessageTask() != null) {
limboPlayer.getMessageTask().cancel();
}
limboPlayer.clearTasks();
}
syncProcessManager.processSyncPlayerLogin(player);
} else if (player.isOnline()) {

View File

@ -2,7 +2,6 @@ package fr.xephi.authme.process.login;
import com.google.common.io.ByteArrayDataOutput;
import com.google.common.io.ByteStreams;
import fr.xephi.authme.AuthMe;
import fr.xephi.authme.cache.auth.PlayerAuth;
import fr.xephi.authme.cache.limbo.LimboCache;
@ -20,7 +19,6 @@ import fr.xephi.authme.settings.properties.RegistrationSettings;
import fr.xephi.authme.settings.properties.RestrictionSettings;
import fr.xephi.authme.util.BukkitService;
import fr.xephi.authme.util.TeleportationService;
import org.apache.commons.lang.reflect.MethodUtils;
import org.bukkit.Bukkit;
import org.bukkit.entity.LivingEntity;
@ -77,7 +75,7 @@ public class ProcessSyncPlayerLogin implements SynchronousProcess {
RestoreInventoryEvent event = new RestoreInventoryEvent(player);
pluginManager.callEvent(event);
if (!event.isCancelled()) {
protocolLibService.sendInventoryPacket(player);
player.updateInventory();
}
}
@ -99,8 +97,13 @@ public class ProcessSyncPlayerLogin implements SynchronousProcess {
if (limbo != null) {
// Restore Op state and Permission Group
restoreOpState(player, limbo);
player.setOp(limbo.isOperator());
// Restore primary group
service.setGroup(player, AuthGroupType.LOGGED_IN);
// Restore can-fly state
player.setAllowFlight(limbo.isCanFly());
// Restore walk speed
player.setWalkSpeed(limbo.getWalkSpeed());
teleportationService.teleportOnLogin(player, auth, limbo);
@ -117,7 +120,7 @@ public class ProcessSyncPlayerLogin implements SynchronousProcess {
}
// Clean up no longer used temporary data
limboCache.deleteLimboPlayer(name);
limboCache.deleteLimboPlayer(player);
}
// We can now display the join message (if delayed)
@ -142,6 +145,7 @@ public class ProcessSyncPlayerLogin implements SynchronousProcess {
bukkitService.callEvent(new LoginEvent(player));
player.saveData();
sendBungeeMessage(player);
// Login is done, display welcome message
if (service.getProperty(RegistrationSettings.USE_WELCOME_MESSAGE)) {
if (service.getProperty(RegistrationSettings.BROADCAST_WELCOME_MESSAGE)) {
@ -161,10 +165,6 @@ public class ProcessSyncPlayerLogin implements SynchronousProcess {
sendTo(player);
}
private void restoreOpState(Player player, LimboPlayer limboPlayer) {
player.setOp(limboPlayer.isOperator());
}
private void sendTo(Player player) {
if(!service.getProperty(HooksSettings.BUNGEECORD)) {
return;

View File

@ -60,7 +60,7 @@ public class AsynchronousLogout implements AsynchronousProcess {
}
});
if (limboCache.hasLimboPlayer(name)) {
limboCache.deleteLimboPlayer(name);
limboCache.deleteLimboPlayer(player);
}
limboCache.addLimboPlayer(player);
service.setGroup(player, AuthGroupType.NOT_LOGGED_IN);

View File

@ -5,7 +5,6 @@ import fr.xephi.authme.cache.SessionManager;
import fr.xephi.authme.cache.auth.PlayerAuth;
import fr.xephi.authme.cache.auth.PlayerCache;
import fr.xephi.authme.cache.limbo.LimboCache;
import fr.xephi.authme.cache.limbo.LimboPlayer;
import fr.xephi.authme.datasource.CacheDataSource;
import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.process.AsynchronousProcess;
@ -13,7 +12,6 @@ import fr.xephi.authme.process.ProcessService;
import fr.xephi.authme.process.SyncProcessManager;
import fr.xephi.authme.settings.Settings;
import fr.xephi.authme.settings.properties.RestrictionSettings;
import fr.xephi.authme.util.StringUtils;
import fr.xephi.authme.util.Utils;
import org.bukkit.Location;
import org.bukkit.entity.Player;
@ -74,18 +72,6 @@ public class AsynchronousQuit implements AsynchronousProcess {
database.updateSession(auth);
}
boolean needToChange = false;
boolean isOp = false;
LimboPlayer limbo = limboCache.getLimboPlayer(name);
if (limbo != null) {
if (!StringUtils.isEmpty(limbo.getGroup())) {
Utils.addNormal(player, limbo.getGroup());
}
needToChange = true;
isOp = limbo.isOperator();
limboCache.deleteLimboPlayer(name);
}
if (!isKick) {
if (plugin.isEnabled()) {
BukkitTask task = plugin.getServer().getScheduler().runTaskLaterAsynchronously(plugin, new Runnable() {
@ -108,7 +94,7 @@ public class AsynchronousQuit implements AsynchronousProcess {
}
if (plugin.isEnabled()) {
syncProcessManager.processSyncPlayerQuit(player, isOp, needToChange);
syncProcessManager.processSyncPlayerQuit(player);
}
// remove player from cache
if (database instanceof CacheDataSource) {

View File

@ -1,14 +1,30 @@
package fr.xephi.authme.process.quit;
import fr.xephi.authme.cache.limbo.LimboCache;
import fr.xephi.authme.cache.limbo.LimboPlayer;
import fr.xephi.authme.process.SynchronousProcess;
import fr.xephi.authme.util.StringUtils;
import fr.xephi.authme.util.Utils;
import org.bukkit.entity.Player;
import javax.inject.Inject;
public class ProcessSyncronousPlayerQuit implements SynchronousProcess {
public void processSyncQuit(Player player, boolean isOp, boolean needToChange) {
if (needToChange) {
player.setOp(isOp);
@Inject
private LimboCache limboCache;
public void processSyncQuit(Player player) {
LimboPlayer limbo = limboCache.getLimboPlayer(player.getName().toLowerCase());
if (limbo != null) {
if (!StringUtils.isEmpty(limbo.getGroup())) {
Utils.addNormal(player, limbo.getGroup());
}
player.setOp(limbo.isOperator());
player.setAllowFlight(limbo.isCanFly());
player.setWalkSpeed(limbo.getWalkSpeed());
limboCache.deleteLimboPlayer(player);
}
player.leaveVehicle();
}

View File

@ -106,11 +106,11 @@ public class ProcessSyncPasswordRegister implements SynchronousProcess {
RestoreInventoryEvent event = new RestoreInventoryEvent(player);
bukkitService.callEvent(event);
if (!event.isCancelled()) {
protocolLibService.sendInventoryPacket(player);
player.updateInventory();
}
}
limboCache.deleteLimboPlayer(name);
limboCache.deleteLimboPlayer(player);
}
if (!Settings.getRegisteredGroup.isEmpty()) {

View File

@ -22,7 +22,8 @@ public class FileUtils {
* Copy a resource file (from the JAR) to the given file if it doesn't exist.
*
* @param destinationFile The file to check and copy to (outside of JAR)
* @param resourcePath Absolute path to the resource file (path to file within JAR)
* @param resourcePath Absolute path to the resource file (path to file within JAR)
*
* @return False if the file does not exist and could not be copied, true otherwise
*/
public static boolean copyFileFromResource(File destinationFile, String resourcePath) {
@ -49,4 +50,25 @@ public class FileUtils {
}
return false;
}
/**
* Delete a given directory and all its content.
*
* @param directory The directory to remove
*/
public static void purgeDirectory(File directory) {
if (!directory.isDirectory()) {
return;
}
File[] files = directory.listFiles();
if (files == null) {
return;
}
for (File target : files) {
if (target.isDirectory()) {
purgeDirectory(target);
}
target.delete();
}
}
}