- Add restore method in LimboCache

* Apply it to process that have use of it
- Fix fly & walk speed not get restored
This commit is contained in:
DNx5 2016-07-04 13:21:57 +07:00
parent deffcb3e2b
commit 140275f366
14 changed files with 98 additions and 115 deletions

View File

@ -7,7 +7,6 @@ import fr.xephi.authme.cache.auth.PlayerAuth;
import fr.xephi.authme.cache.auth.PlayerCache;
import fr.xephi.authme.cache.backup.PlayerDataStorage;
import fr.xephi.authme.cache.limbo.LimboCache;
import fr.xephi.authme.cache.limbo.PlayerData;
import fr.xephi.authme.command.CommandHandler;
import fr.xephi.authme.datasource.CacheDataSource;
import fr.xephi.authme.datasource.DataSource;
@ -565,19 +564,8 @@ public class AuthMe extends JavaPlugin {
}
String name = player.getName().toLowerCase();
if (limboCache.hasPlayerData(name)) {
PlayerData limbo = limboCache.getPlayerData(name);
if (!newSettings.getProperty(RestrictionSettings.NO_TELEPORT)) {
player.teleport(limbo.getLoc());
}
Utils.addNormal(player, limbo.getGroup());
player.setOp(limbo.isOperator());
player.setAllowFlight(limbo.isCanFly());
player.setWalkSpeed(limbo.getWalkSpeed());
if (newSettings.getProperty(RestrictionSettings.TELEPORT_UNAUTHED_TO_SPAWN)) {
limboCache.removePlayerData(player);
} else {
limboCache.deletePlayerData(player);
}
limboCache.restoreData(player);
limboCache.removeFromCache(player);
} else {
if (newSettings.getProperty(RestrictionSettings.SAVE_QUIT_LOCATION)) {
Location loc = spawnLoader.getPlayerLocationOrSpawn(player);

View File

@ -189,7 +189,7 @@ public class PlayerDataStorage {
JsonObject obj = new JsonObject();
obj.addProperty("group", playerData.getGroup());
Location loc = playerData.getLoc();
Location loc = playerData.getLocation();
JsonObject obj2 = new JsonObject();
obj2.addProperty("world", loc.getWorld().getName());
obj2.addProperty("x", loc.getX());

View File

@ -2,7 +2,11 @@ package fr.xephi.authme.cache.limbo;
import fr.xephi.authme.cache.backup.PlayerDataStorage;
import fr.xephi.authme.permission.PermissionsManager;
import fr.xephi.authme.settings.NewSetting;
import fr.xephi.authme.settings.SpawnLoader;
import fr.xephi.authme.settings.properties.PluginSettings;
import fr.xephi.authme.settings.properties.RestrictionSettings;
import fr.xephi.authme.util.StringUtils;
import org.bukkit.Location;
import org.bukkit.entity.Player;
@ -19,18 +23,21 @@ public class LimboCache {
private final ConcurrentHashMap<String, PlayerData> cache = new ConcurrentHashMap<>();
private PlayerDataStorage playerDataStorage;
private NewSetting settings;
private PermissionsManager permissionsManager;
private SpawnLoader spawnLoader;
@Inject
LimboCache(PermissionsManager permissionsManager, SpawnLoader spawnLoader, PlayerDataStorage playerDataStorage) {
LimboCache(NewSetting settings, PermissionsManager permissionsManager,
SpawnLoader spawnLoader, PlayerDataStorage playerDataStorage) {
this.settings = settings;
this.permissionsManager = permissionsManager;
this.spawnLoader = spawnLoader;
this.playerDataStorage = playerDataStorage;
}
/**
* Add a limbo player.
* Load player data if exist, otherwise current player's data will be stored.
*
* @param player Player instance to add.
*/
@ -49,7 +56,7 @@ public class LimboCache {
if (playerDataStorage.hasData(player)) {
PlayerData cache = playerDataStorage.readData(player);
if (cache != null) {
location = cache.getLoc();
location = cache.getLocation();
playerGroup = cache.getGroup();
operator = cache.isOperator();
flyEnabled = cache.isCanFly();
@ -64,21 +71,42 @@ public class LimboCache {
}
/**
* Remove PlayerData and delete cache.json from disk.
* Restore player's data to player if exist.
*
* @param player Player instance to restore
*/
public void restoreData(Player player) {
String lowerName = player.getName().toLowerCase();
if (cache.containsKey(lowerName)) {
PlayerData data = cache.get(lowerName);
player.setOp(data.isOperator());
player.setAllowFlight(data.isCanFly());
player.setWalkSpeed(data.getWalkSpeed());
player.setFlySpeed(data.getFlySpeed());
restoreGroup(player, data.getGroup());
if (!settings.getProperty(RestrictionSettings.NO_TELEPORT)) {
player.teleport(data.getLocation());
}
data.clearTasks();
}
}
/**
* Remove PlayerData from cache and disk.
*
* @param player Player player to remove.
*/
public void deletePlayerData(Player player) {
removePlayerData(player);
removeFromCache(player);
playerDataStorage.removeData(player);
}
/**
* Remove PlayerData from cache, without deleting cache.json file.
* Remove PlayerData from cache.
*
* @param player Player player to remove.
* @param player player to remove.
*/
public void removePlayerData(Player player) {
public void removeFromCache(Player player) {
String name = player.getName().toLowerCase();
PlayerData cachedPlayer = cache.remove(name);
if (cachedPlayer != null) {
@ -117,7 +145,15 @@ public class LimboCache {
*/
public void updatePlayerData(Player player) {
checkNotNull(player);
removePlayerData(player);
removeFromCache(player);
addPlayerData(player);
}
private void restoreGroup(Player player, String group) {
if (!settings.getProperty(PluginSettings.ENABLE_PERMISSION_CHECK)
|| !permissionsManager.hasGroupSupport() || StringUtils.isEmpty(group)) {
return;
}
permissionsManager.setGroup(player, group);
}
}

View File

@ -33,7 +33,7 @@ public class PlayerData {
*
* @return The player's location
*/
public Location getLoc() {
public Location getLocation() {
return loc;
}

View File

@ -66,14 +66,14 @@ public class AuthGroupHandler {
return permissionsManager.addGroup(player, settings.getProperty(SecuritySettings.UNLOGGEDIN_GROUP));
case LOGGED_IN:
// Get the limbo player data
PlayerData limbo = limboCache.getPlayerData(player.getName().toLowerCase());
if (limbo == null) {
// Get the player data
PlayerData data = limboCache.getPlayerData(player.getName().toLowerCase());
if (data == null) {
return false;
}
// Get the players group
String realGroup = limbo.getGroup();
String realGroup = data.getGroup();
// Remove the other group types groups, set the real group
permissionsManager.removeGroups(player,

View File

@ -115,11 +115,11 @@ public class AsynchronousJoin implements AsynchronousProcess {
return;
}
limboCache.updatePlayerData(player);
final boolean isAuthAvailable = database.isAuthAvailable(name);
if (isAuthAvailable) {
limboCache.addPlayerData(player);
service.setGroup(player, AuthGroupType.NOT_LOGGED_IN);
// Protect inventory
@ -150,7 +150,9 @@ public class AsynchronousJoin implements AsynchronousProcess {
}
}
} else {
// Not Registered
// Not Registered. Delete old data, load default one.
limboCache.deletePlayerData(player);
limboCache.addPlayerData(player);
// Groups logic
service.setGroup(player, AuthGroupType.UNREGISTERED);

View File

@ -3,20 +3,16 @@ 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;
import fr.xephi.authme.cache.limbo.PlayerData;
import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.events.LoginEvent;
import fr.xephi.authme.events.RestoreInventoryEvent;
import fr.xephi.authme.listener.AuthMePlayerListener;
import fr.xephi.authme.listener.protocollib.ProtocolLibService;
import fr.xephi.authme.permission.AuthGroupType;
import fr.xephi.authme.process.ProcessService;
import fr.xephi.authme.process.SynchronousProcess;
import fr.xephi.authme.settings.properties.HooksSettings;
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;
@ -60,7 +56,8 @@ public class ProcessSyncPlayerLogin implements SynchronousProcess {
@Inject
private TeleportationService teleportationService;
ProcessSyncPlayerLogin() { }
ProcessSyncPlayerLogin() {
}
private void restoreInventory(Player player) {
RestoreInventoryEvent event = new RestoreInventoryEvent(player);
@ -83,26 +80,12 @@ public class ProcessSyncPlayerLogin implements SynchronousProcess {
public void processPlayerLogin(Player player) {
final String name = player.getName().toLowerCase();
// Limbo contains the State of the Player before /login
final PlayerData limbo = limboCache.getPlayerData(name);
final PlayerAuth auth = dataSource.getAuth(name);
if (limbo != null) {
// Restore Op state and Permission Group
player.setOp(limbo.isOperator());
// Restore primary group
service.setGroup(player, AuthGroupType.LOGGED_IN);
// Restore can-fly state
player.setAllowFlight(limbo.isCanFly());
// Restore speed
if (!service.getProperty(RestrictionSettings.ALLOW_UNAUTHED_MOVEMENT)
&& service.getProperty(RestrictionSettings.REMOVE_SPEED)) {
player.setWalkSpeed(limbo.getWalkSpeed());
player.setFlySpeed(0.2F);
}
teleportationService.teleportOnLogin(player, auth, limbo);
if (limboCache.hasPlayerData(name)) {
limboCache.restoreData(player);
limboCache.deletePlayerData(player);
// do we really need to use location from database for now?
// because LimboCache#restoreData teleport player to last location.
//teleportationService.teleportOnLogin(player, auth, limbo);
if (RESTORE_COLLISIONS && !service.getProperty(KEEP_COLLISIONS_DISABLED)) {
player.setCollidable(true);
}
@ -110,9 +93,6 @@ public class ProcessSyncPlayerLogin implements SynchronousProcess {
if (service.getProperty(PROTECT_INVENTORY_BEFORE_LOGIN)) {
restoreInventory(player);
}
// Clean up no longer used temporary data
limboCache.deletePlayerData(player);
}
// We can now display the join message (if delayed)

View File

@ -50,7 +50,7 @@ public class AsynchronousLogout implements AsynchronousProcess {
database.updateQuitLoc(auth);
}
limboCache.updatePlayerData(player);
limboCache.addPlayerData(player);
playerCache.removePlayer(name);
database.setUnlogged(name);
syncProcessManager.processSyncPlayerLogout(player);

View File

@ -45,7 +45,8 @@ public class ProcessSynchronousPlayerLogout implements SynchronousProcess {
@Inject
private SessionManager sessionManager;
ProcessSynchronousPlayerLogout() { }
ProcessSynchronousPlayerLogout() {
}
private void sendBungeeMessage(Player player) {
@ -82,9 +83,7 @@ public class ProcessSynchronousPlayerLogout implements SynchronousProcess {
private void applyLogoutEffect(Player player) {
// dismount player
if (player.isInsideVehicle() && player.getVehicle() != null) {
player.getVehicle().eject();
}
player.leaveVehicle();
// Apply Blindness effect
final int timeout = service.getProperty(RestrictionSettings.TIMEOUT) * TICKS_PER_SECOND;

View File

@ -2,12 +2,8 @@ package fr.xephi.authme.process.quit;
import fr.xephi.authme.cache.backup.PlayerDataStorage;
import fr.xephi.authme.cache.limbo.LimboCache;
import fr.xephi.authme.cache.limbo.PlayerData;
import fr.xephi.authme.process.ProcessService;
import fr.xephi.authme.process.SynchronousProcess;
import fr.xephi.authme.settings.properties.RestrictionSettings;
import fr.xephi.authme.util.StringUtils;
import fr.xephi.authme.util.Utils;
import org.bukkit.entity.Player;
import javax.inject.Inject;
@ -25,29 +21,14 @@ public class ProcessSyncronousPlayerQuit implements SynchronousProcess {
private LimboCache limboCache;
public void processSyncQuit(Player player) {
PlayerData limbo = limboCache.getPlayerData(player.getName().toLowerCase());
if (limbo != null) { // it mean player is not authenticated
// Only delete if we don't need player's last location
if (service.getProperty(RestrictionSettings.TELEPORT_UNAUTHED_TO_SPAWN)) {
limboCache.removePlayerData(player);
if (limboCache.hasPlayerData(player.getName().toLowerCase())) { // it mean player is not authenticated
limboCache.removeFromCache(player);
} else {
// Restore data if its about to delete PlayerData
if (!StringUtils.isEmpty(limbo.getGroup())) {
Utils.addNormal(player, limbo.getGroup());
}
player.setOp(limbo.isOperator());
player.setAllowFlight(limbo.isCanFly());
player.setWalkSpeed(limbo.getWalkSpeed());
limboCache.deletePlayerData(player);
}
} else {
// Write player's location, so we could retrieve it later on player next join
if (service.getProperty(RestrictionSettings.TELEPORT_UNAUTHED_TO_SPAWN)) {
// Save player's data, so we could retrieve it later on player next join
if (!playerDataStorage.hasData(player)) {
playerDataStorage.saveData(player);
}
}
}
player.leaveVehicle();
}

View File

@ -5,7 +5,6 @@ import com.google.common.io.ByteStreams;
import fr.xephi.authme.AuthMe;
import fr.xephi.authme.ConsoleLogger;
import fr.xephi.authme.cache.limbo.LimboCache;
import fr.xephi.authme.cache.limbo.PlayerData;
import fr.xephi.authme.events.LoginEvent;
import fr.xephi.authme.events.RestoreInventoryEvent;
import fr.xephi.authme.listener.protocollib.ProtocolLibService;
@ -20,6 +19,7 @@ import fr.xephi.authme.settings.properties.RegistrationSettings;
import fr.xephi.authme.settings.properties.SecuritySettings;
import fr.xephi.authme.task.PlayerDataTaskManager;
import fr.xephi.authme.util.BukkitService;
import fr.xephi.authme.util.TeleportationService;
import fr.xephi.authme.util.Utils;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
@ -35,23 +35,21 @@ public class ProcessSyncPasswordRegister implements SynchronousProcess {
@Inject
private AuthMe plugin;
@Inject
private ProcessService service;
@Inject
private BukkitService bukkitService;
@Inject
private ProtocolLibService protocolLibService;
@Inject
private LimboCache limboCache;
@Inject
private PlayerDataTaskManager playerDataTaskManager;
@Inject
private TeleportationService teleportationService;
ProcessSyncPasswordRegister() { }
ProcessSyncPasswordRegister() {
}
private void sendBungeeMessage(Player player) {
@ -93,10 +91,8 @@ public class ProcessSyncPasswordRegister implements SynchronousProcess {
public void processPasswordRegister(Player player) {
final String name = player.getName().toLowerCase();
PlayerData limbo = limboCache.getPlayerData(name);
if (limbo != null) {
Utils.teleportToSpawn(player);
if (limboCache.hasPlayerData(name)) {
teleportationService.teleportOnJoin(player);
if (service.getProperty(PROTECT_INVENTORY_BEFORE_LOGIN)) {
RestoreInventoryEvent event = new RestoreInventoryEvent(player);
bukkitService.callEvent(event);
@ -104,7 +100,7 @@ public class ProcessSyncPasswordRegister implements SynchronousProcess {
player.updateInventory();
}
}
limboCache.restoreData(player);
limboCache.deletePlayerData(player);
}

View File

@ -62,6 +62,7 @@ public class AsynchronousUnregister implements AsynchronousProcess {
if (!Settings.getRegisteredGroup.isEmpty()) {
service.setGroup(player, AuthGroupType.UNREGISTERED);
}
limboCache.deletePlayerData(player);
limboCache.addPlayerData(player);
playerDataTaskManager.registerTimeoutTask(player);
playerDataTaskManager.registerMessageTask(name, false);

View File

@ -102,7 +102,7 @@ public class TeleportationService implements Reloadable {
}
// The world in PlayerData is from where the player comes, before any teleportation by AuthMe
String worldName = limbo.getLoc().getWorld().getName();
String worldName = limbo.getLocation().getWorld().getName();
if (mustForceSpawnAfterLogin(worldName)) {
teleportToSpawn(player, true);
} else if (settings.getProperty(TELEPORT_UNAUTHED_TO_SPAWN)) {
@ -110,7 +110,7 @@ public class TeleportationService implements Reloadable {
Location location = buildLocationFromAuth(player, auth);
teleportBackFromSpawn(player, location);
} else {
teleportBackFromSpawn(player, limbo.getLoc());
teleportBackFromSpawn(player, limbo.getLocation());
}
}
}

View File

@ -280,7 +280,7 @@ public class TeleportationServiceTest {
PlayerData limbo = mock(PlayerData.class);
Location limboLocation = mockLocation();
given(limboLocation.getWorld().getName()).willReturn("forced1");
given(limbo.getLoc()).willReturn(limboLocation);
given(limbo.getLocation()).willReturn(limboLocation);
// when
teleportationService.teleportOnLogin(player, auth, limbo);
@ -304,7 +304,7 @@ public class TeleportationServiceTest {
PlayerData limbo = mock(PlayerData.class);
Location limboLocation = mockLocation();
given(limboLocation.getWorld().getName()).willReturn("Forced1"); // different case
given(limbo.getLoc()).willReturn(limboLocation);
given(limbo.getLocation()).willReturn(limboLocation);
// when
teleportationService.teleportOnLogin(player, auth, limbo);
@ -330,7 +330,7 @@ public class TeleportationServiceTest {
given(player.isOnline()).willReturn(true);
PlayerData limbo = mock(PlayerData.class);
Location limboLocation = mockLocation();
given(limbo.getLoc()).willReturn(limboLocation);
given(limbo.getLocation()).willReturn(limboLocation);
// when
teleportationService.teleportOnLogin(player, auth, limbo);
@ -359,7 +359,7 @@ public class TeleportationServiceTest {
given(player.getWorld()).willReturn(world);
PlayerData limbo = mock(PlayerData.class);
Location limboLocation = mockLocation();
given(limbo.getLoc()).willReturn(limboLocation);
given(limbo.getLocation()).willReturn(limboLocation);
// when
teleportationService.teleportOnLogin(player, auth, limbo);
@ -387,7 +387,7 @@ public class TeleportationServiceTest {
given(player.getWorld()).willReturn(world);
PlayerData limbo = mock(PlayerData.class);
Location location = mockLocation();
given(limbo.getLoc()).willReturn(location);
given(limbo.getLocation()).willReturn(location);
// when
teleportationService.teleportOnLogin(player, auth, limbo);
@ -412,7 +412,7 @@ public class TeleportationServiceTest {
given(player.getWorld()).willReturn(world);
PlayerData limbo = mock(PlayerData.class);
Location location = mockLocation();
given(limbo.getLoc()).willReturn(location);
given(limbo.getLocation()).willReturn(location);
// when
teleportationService.teleportOnLogin(player, auth, limbo);