- Remove unnecessary @Inject on field.

- Teleport player to spawn immediately on PlayerLoginEvent.
- Only save authenticated player's location on quit.
- Fix player's last location get reset if fail to login.
This commit is contained in:
DNx5 2016-06-29 20:25:01 +07:00
parent 609b148157
commit 22a4ef93bf
7 changed files with 88 additions and 62 deletions

View File

@ -293,15 +293,15 @@ public class AuthMe extends JavaPlugin {
// Some statically injected things // Some statically injected things
initializer.register(PlayerCache.class, PlayerCache.getInstance()); initializer.register(PlayerCache.class, PlayerCache.getInstance());
messages = initializer.get(Messages.class); messages = initializer.get(Messages.class);
permsMan = initializer.get(PermissionsManager.class); permsMan = initializer.get(PermissionsManager.class);
bukkitService = initializer.get(BukkitService.class); bukkitService = initializer.get(BukkitService.class);
pluginHooks = initializer.get(PluginHooks.class); pluginHooks = initializer.get(PluginHooks.class);
passwordSecurity = initializer.get(PasswordSecurity.class); passwordSecurity = initializer.get(PasswordSecurity.class);
spawnLoader = initializer.get(SpawnLoader.class); spawnLoader = initializer.get(SpawnLoader.class);
commandHandler = initializer.get(CommandHandler.class); commandHandler = initializer.get(CommandHandler.class);
management = initializer.get(Management.class); management = initializer.get(Management.class);
geoLiteApi = initializer.get(GeoLiteAPI.class); geoLiteApi = initializer.get(GeoLiteAPI.class);
initializer.get(NewAPI.class); initializer.get(NewAPI.class);
initializer.get(API.class); initializer.get(API.class);
} }
@ -577,24 +577,26 @@ public class AuthMe extends JavaPlugin {
player.setOp(limbo.isOperator()); player.setOp(limbo.isOperator());
player.setAllowFlight(limbo.isCanFly()); player.setAllowFlight(limbo.isCanFly());
player.setWalkSpeed(limbo.getWalkSpeed()); player.setWalkSpeed(limbo.getWalkSpeed());
limbo.clearTasks(); if (newSettings.getProperty(RestrictionSettings.TELEPORT_UNAUTHED_TO_SPAWN)) {
limboCache.deleteLimboPlayer(player); limboCache.removeLimboPlayer(player);
} } else {
if (PlayerCache.getInstance().isAuthenticated(name) && !player.isDead()) { limboCache.deleteLimboPlayer(player);
if (Settings.isSaveQuitLocationEnabled) { }
} else {
if (newSettings.getProperty(RestrictionSettings.SAVE_QUIT_LOCATION)) {
Location loc =
player.isOnline() && player.isDead() ? spawnLoader.getSpawnLocation(player) : player.getLocation();
final PlayerAuth auth = PlayerAuth.builder() final PlayerAuth auth = PlayerAuth.builder()
.name(player.getName().toLowerCase()) .name(player.getName().toLowerCase())
.realName(player.getName()) .realName(player.getName())
.location(player.getLocation()).build(); .location(loc).build();
database.updateQuitLoc(auth); database.updateQuitLoc(auth);
} }
if (newSettings.getProperty(RestrictionSettings.TELEPORT_UNAUTHED_TO_SPAWN) if (newSettings.getProperty(RestrictionSettings.TELEPORT_UNAUTHED_TO_SPAWN)
&& !newSettings.getProperty(RestrictionSettings.NO_TELEPORT)) { && !newSettings.getProperty(RestrictionSettings.NO_TELEPORT)) {
JsonCache jsonCache = initializer.getIfAvailable(JsonCache.class); JsonCache jsonCache = initializer.getIfAvailable(JsonCache.class);
if (jsonCache != null) { if (jsonCache != null && !jsonCache.doesCacheExist(player)) {
jsonCache.writeCache(player); jsonCache.writeCache(player);
player.teleport(spawnLoader.getSpawnLocation(player));
} }
} }
} }

View File

@ -31,16 +31,13 @@ public class JsonCache {
private final Gson gson; private final Gson gson;
private final File cacheDir; private final File cacheDir;
@Inject
private PermissionsManager permissionsManager; private PermissionsManager permissionsManager;
@Inject
private SpawnLoader spawnLoader; private SpawnLoader spawnLoader;
@Inject
private BukkitService bukkitService; private BukkitService bukkitService;
@Inject @Inject
public JsonCache(@DataFolder File dataFolder, PermissionsManager permsMan, JsonCache(@DataFolder File dataFolder, PermissionsManager permsMan,
SpawnLoader spawnLoader, BukkitService bukkitService) { SpawnLoader spawnLoader, BukkitService bukkitService) {
this.permissionsManager = permsMan; this.permissionsManager = permsMan;
this.spawnLoader = spawnLoader; this.spawnLoader = spawnLoader;
this.bukkitService = bukkitService; this.bukkitService = bukkitService;
@ -75,7 +72,8 @@ public class JsonCache {
public void writeCache(Player player) { public void writeCache(Player player) {
String id = Utils.getUUIDorName(player); String id = Utils.getUUIDorName(player);
String name = player.getName().toLowerCase(); String name = player.getName().toLowerCase();
Location location = player.isDead() ? spawnLoader.getSpawnLocation(player) : player.getLocation(); Location location =
player.isOnline() && player.isDead() ? spawnLoader.getSpawnLocation(player) : player.getLocation();
String group = permissionsManager.getPrimaryGroup(player); String group = permissionsManager.getPrimaryGroup(player);
boolean operator = player.isOp(); boolean operator = player.isOp();
boolean canFly = player.getAllowFlight(); boolean canFly = player.getAllowFlight();

View File

@ -18,13 +18,8 @@ public class LimboCache {
private final ConcurrentHashMap<String, LimboPlayer> cache = new ConcurrentHashMap<>(); private final ConcurrentHashMap<String, LimboPlayer> cache = new ConcurrentHashMap<>();
@Inject
private JsonCache jsonCache; private JsonCache jsonCache;
@Inject
private PermissionsManager permissionsManager; private PermissionsManager permissionsManager;
@Inject
private SpawnLoader spawnLoader; private SpawnLoader spawnLoader;
@Inject @Inject
@ -67,17 +62,26 @@ public class LimboCache {
} }
/** /**
* Method deleteLimboPlayer. * Remove LimboPlayer and delete cache.json from disk.
* *
* @param player Player player to remove. * @param player Player player to remove.
*/ */
public void deleteLimboPlayer(Player player) { public void deleteLimboPlayer(Player player) {
removeLimboPlayer(player);
jsonCache.removeCache(player);
}
/**
* Remove LimboPlayer from cache, without deleting cache.json file.
*
* @param player Player player to remove.
*/
public void removeLimboPlayer(Player player) {
String name = player.getName().toLowerCase(); String name = player.getName().toLowerCase();
LimboPlayer cachedPlayer = cache.remove(name); LimboPlayer cachedPlayer = cache.remove(name);
if (cachedPlayer != null) { if (cachedPlayer != null) {
cachedPlayer.clearTasks(); cachedPlayer.clearTasks();
} }
jsonCache.removeCache(player);
} }
/** /**
@ -111,7 +115,7 @@ public class LimboCache {
*/ */
public void updateLimboPlayer(Player player) { public void updateLimboPlayer(Player player) {
checkNotNull(player); checkNotNull(player);
deleteLimboPlayer(player); removeLimboPlayer(player);
addLimboPlayer(player); addLimboPlayer(player);
} }
} }

View File

@ -12,6 +12,7 @@ import fr.xephi.authme.settings.properties.HooksSettings;
import fr.xephi.authme.settings.properties.RegistrationSettings; import fr.xephi.authme.settings.properties.RegistrationSettings;
import fr.xephi.authme.settings.properties.RestrictionSettings; import fr.xephi.authme.settings.properties.RestrictionSettings;
import fr.xephi.authme.util.BukkitService; import fr.xephi.authme.util.BukkitService;
import fr.xephi.authme.util.TeleportationService;
import fr.xephi.authme.util.Utils; import fr.xephi.authme.util.Utils;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
@ -72,6 +73,8 @@ public class AuthMePlayerListener implements Listener {
private OnJoinVerifier onJoinVerifier; private OnJoinVerifier onJoinVerifier;
@Inject @Inject
private ListenerService listenerService; private ListenerService listenerService;
@Inject
private TeleportationService teleportationService;
@EventHandler(ignoreCancelled = true, priority = EventPriority.LOWEST) @EventHandler(ignoreCancelled = true, priority = EventPriority.LOWEST)
public void onPlayerCommandPreprocess(PlayerCommandPreprocessEvent event) { public void onPlayerCommandPreprocess(PlayerCommandPreprocessEvent event) {
@ -229,6 +232,7 @@ public class AuthMePlayerListener implements Listener {
} }
antiBot.handlePlayerJoin(player); antiBot.handlePlayerJoin(player);
teleportationService.teleportOnJoin(player);
} }
@EventHandler(priority = EventPriority.HIGHEST) @EventHandler(priority = EventPriority.HIGHEST)

View File

@ -22,7 +22,6 @@ import fr.xephi.authme.settings.properties.RestrictionSettings;
import fr.xephi.authme.settings.properties.SecuritySettings; import fr.xephi.authme.settings.properties.SecuritySettings;
import fr.xephi.authme.task.LimboPlayerTaskManager; import fr.xephi.authme.task.LimboPlayerTaskManager;
import fr.xephi.authme.util.BukkitService; import fr.xephi.authme.util.BukkitService;
import fr.xephi.authme.util.TeleportationService;
import fr.xephi.authme.util.Utils; import fr.xephi.authme.util.Utils;
import org.apache.commons.lang.reflect.MethodUtils; import org.apache.commons.lang.reflect.MethodUtils;
import org.bukkit.GameMode; import org.bukkit.GameMode;
@ -63,9 +62,6 @@ public class AsynchronousJoin implements AsynchronousProcess {
@Inject @Inject
private PluginHooks pluginHooks; private PluginHooks pluginHooks;
@Inject
private TeleportationService teleportationService;
@Inject @Inject
private BukkitService bukkitService; private BukkitService bukkitService;
@ -75,7 +71,8 @@ public class AsynchronousJoin implements AsynchronousProcess {
@Inject @Inject
private LimboPlayerTaskManager limboPlayerTaskManager; private LimboPlayerTaskManager limboPlayerTaskManager;
AsynchronousJoin() { } AsynchronousJoin() {
}
public void processJoin(final Player player) { public void processJoin(final Player player) {
@ -122,12 +119,12 @@ public class AsynchronousJoin implements AsynchronousProcess {
return; return;
} }
limboCache.updateLimboPlayer(player);
final boolean isAuthAvailable = database.isAuthAvailable(name); final boolean isAuthAvailable = database.isAuthAvailable(name);
if (isAuthAvailable) { if (isAuthAvailable) {
service.setGroup(player, AuthGroupType.NOT_LOGGED_IN); service.setGroup(player, AuthGroupType.NOT_LOGGED_IN);
teleportationService.teleportOnJoin(player);
limboCache.updateLimboPlayer(player);
// Protect inventory // Protect inventory
if (service.getProperty(PROTECT_INVENTORY_BEFORE_LOGIN)) { if (service.getProperty(PROTECT_INVENTORY_BEFORE_LOGIN)) {
@ -166,13 +163,6 @@ public class AsynchronousJoin implements AsynchronousProcess {
if (!service.getProperty(RegistrationSettings.FORCE)) { if (!service.getProperty(RegistrationSettings.FORCE)) {
return; return;
} }
teleportationService.teleportOnJoin(player);
}
// The user is not logged in
if (!limboCache.hasLimboPlayer(name)) {
limboCache.addLimboPlayer(player);
} }
final int registrationTimeout = service.getProperty(RestrictionSettings.TIMEOUT) * TICKS_PER_SECOND; final int registrationTimeout = service.getProperty(RestrictionSettings.TIMEOUT) * TICKS_PER_SECOND;
@ -211,11 +201,12 @@ public class AsynchronousJoin implements AsynchronousProcess {
/** /**
* Returns whether the name is restricted based on the restriction settings. * Returns whether the name is restricted based on the restriction settings.
* *
* @param name The name to check * @param name The name to check
* @param ip The IP address of the player * @param ip The IP address of the player
* @param domain The hostname of the IP address * @param domain The hostname of the IP address
*
* @return True if the name is restricted (IP/domain is not allowed for the given name), * @return True if the name is restricted (IP/domain is not allowed for the given name),
* false if the restrictions are met or if the name has no restrictions to it * false if the restrictions are met or if the name has no restrictions to it
*/ */
private boolean isNameRestricted(String name, String ip, String domain) { private boolean isNameRestricted(String name, String ip, String domain) {
if (!service.getProperty(RestrictionSettings.ENABLE_RESTRICTED_USERS)) { if (!service.getProperty(RestrictionSettings.ENABLE_RESTRICTED_USERS)) {
@ -242,7 +233,8 @@ public class AsynchronousJoin implements AsynchronousProcess {
* settings and permissions). If this is the case, the player is kicked. * settings and permissions). If this is the case, the player is kicked.
* *
* @param player the player to verify * @param player the player to verify
* @param ip the ip address of the player * @param ip the ip address of the player
*
* @return true if the verification is OK (no infraction), false if player has been kicked * @return true if the verification is OK (no infraction), false if player has been kicked
*/ */
private boolean validatePlayerCountForIp(final Player player, String ip) { private boolean validatePlayerCountForIp(final Player player, String ip) {

View File

@ -4,12 +4,12 @@ import fr.xephi.authme.AuthMe;
import fr.xephi.authme.cache.SessionManager; import fr.xephi.authme.cache.SessionManager;
import fr.xephi.authme.cache.auth.PlayerAuth; import fr.xephi.authme.cache.auth.PlayerAuth;
import fr.xephi.authme.cache.auth.PlayerCache; import fr.xephi.authme.cache.auth.PlayerCache;
import fr.xephi.authme.cache.limbo.LimboCache;
import fr.xephi.authme.datasource.CacheDataSource; import fr.xephi.authme.datasource.CacheDataSource;
import fr.xephi.authme.datasource.DataSource; import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.process.AsynchronousProcess; import fr.xephi.authme.process.AsynchronousProcess;
import fr.xephi.authme.process.ProcessService; import fr.xephi.authme.process.ProcessService;
import fr.xephi.authme.process.SyncProcessManager; import fr.xephi.authme.process.SyncProcessManager;
import fr.xephi.authme.settings.SpawnLoader;
import fr.xephi.authme.settings.properties.PluginSettings; import fr.xephi.authme.settings.properties.PluginSettings;
import fr.xephi.authme.settings.properties.RestrictionSettings; import fr.xephi.authme.settings.properties.RestrictionSettings;
import fr.xephi.authme.util.Utils; import fr.xephi.authme.util.Utils;
@ -35,15 +35,15 @@ public class AsynchronousQuit implements AsynchronousProcess {
@Inject @Inject
private PlayerCache playerCache; private PlayerCache playerCache;
@Inject
private LimboCache limboCache;
@Inject @Inject
private SyncProcessManager syncProcessManager; private SyncProcessManager syncProcessManager;
@Inject @Inject
private SessionManager sessionManager; private SessionManager sessionManager;
@Inject
private SpawnLoader spawnLoader;
AsynchronousQuit() { AsynchronousQuit() {
} }
@ -55,10 +55,9 @@ public class AsynchronousQuit implements AsynchronousProcess {
final String name = player.getName().toLowerCase(); final String name = player.getName().toLowerCase();
String ip = Utils.getPlayerIp(player); String ip = Utils.getPlayerIp(player);
if (playerCache.isAuthenticated(name)) { if (playerCache.isAuthenticated(name)) {
if (service.getProperty(RestrictionSettings.SAVE_QUIT_LOCATION)) { if (service.getProperty(RestrictionSettings.SAVE_QUIT_LOCATION)) {
Location loc = player.getLocation(); Location loc = player.isDead() ? spawnLoader.getSpawnLocation(player) : player.getLocation();
PlayerAuth auth = PlayerAuth.builder() PlayerAuth auth = PlayerAuth.builder()
.name(name).location(loc) .name(name).location(loc)
.realName(player.getName()).build(); .realName(player.getName()).build();

View File

@ -1,8 +1,12 @@
package fr.xephi.authme.process.quit; package fr.xephi.authme.process.quit;
import fr.xephi.authme.cache.backup.JsonCache;
import fr.xephi.authme.cache.limbo.LimboCache; import fr.xephi.authme.cache.limbo.LimboCache;
import fr.xephi.authme.cache.limbo.LimboPlayer; import fr.xephi.authme.cache.limbo.LimboPlayer;
import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.process.ProcessService;
import fr.xephi.authme.process.SynchronousProcess; import fr.xephi.authme.process.SynchronousProcess;
import fr.xephi.authme.settings.properties.RestrictionSettings;
import fr.xephi.authme.util.StringUtils; import fr.xephi.authme.util.StringUtils;
import fr.xephi.authme.util.Utils; import fr.xephi.authme.util.Utils;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
@ -12,20 +16,43 @@ import javax.inject.Inject;
public class ProcessSyncronousPlayerQuit implements SynchronousProcess { public class ProcessSyncronousPlayerQuit implements SynchronousProcess {
@Inject
private JsonCache jsonCache;
@Inject
private DataSource database;
@Inject
private ProcessService service;
@Inject @Inject
private LimboCache limboCache; private LimboCache limboCache;
public void processSyncQuit(Player player) { public void processSyncQuit(Player player) {
LimboPlayer limbo = limboCache.getLimboPlayer(player.getName().toLowerCase()); LimboPlayer limbo = limboCache.getLimboPlayer(player.getName().toLowerCase());
if (limbo != null) { if (limbo != null) { // it mean player is not authenticated
if (!StringUtils.isEmpty(limbo.getGroup())) { // Only delete if we don't need player's last location
Utils.addNormal(player, limbo.getGroup()); if (service.getProperty(RestrictionSettings.TELEPORT_UNAUTHED_TO_SPAWN)) {
limboCache.removeLimboPlayer(player);
} else {
// Restore data if its about to delete LimboPlayer
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);
}
} else {
// Write player's location, so we could retrieve it later on player next join
if (service.getProperty(RestrictionSettings.TELEPORT_UNAUTHED_TO_SPAWN)) {
if (!jsonCache.doesCacheExist(player)) {
jsonCache.writeCache(player);
}
} }
player.setOp(limbo.isOperator());
player.setAllowFlight(limbo.isCanFly());
player.setWalkSpeed(limbo.getWalkSpeed());
limboCache.deleteLimboPlayer(player);
} }
player.leaveVehicle(); player.leaveVehicle();
} }
} }