- Renamed JsonCache to PlayerDataStorage

* the methods inside it renamed to fit with class name
  * cache folder changed into playerdata
- Renamed LimboPlayer to PlayerData
- Added fly speed to PlayerData
- Removed player's name from PlayerData object
- Added getPlayerLocationOrSpawn method in spawn loader.
This commit is contained in:
DNx5 2016-07-03 21:52:46 +07:00
parent 7ea0763966
commit deffcb3e2b
26 changed files with 554 additions and 220 deletions

View File

@ -12,9 +12,280 @@
<XML>
<option name="XML_LEGACY_SETTINGS_IMPORTED" value="true" />
</XML>
<codeStyleSettings language="JAVA">
<arrangement>
<rules>
<section>
<rule>
<match>
<AND>
<FIELD>true</FIELD>
<FINAL>true</FINAL>
<PUBLIC>true</PUBLIC>
<STATIC>true</STATIC>
</AND>
</match>
</rule>
</section>
<section>
<rule>
<match>
<AND>
<FIELD>true</FIELD>
<FINAL>true</FINAL>
<PROTECTED>true</PROTECTED>
<STATIC>true</STATIC>
</AND>
</match>
</rule>
</section>
<section>
<rule>
<match>
<AND>
<FIELD>true</FIELD>
<FINAL>true</FINAL>
<PACKAGE_PRIVATE>true</PACKAGE_PRIVATE>
<STATIC>true</STATIC>
</AND>
</match>
</rule>
</section>
<section>
<rule>
<match>
<AND>
<FIELD>true</FIELD>
<FINAL>true</FINAL>
<PRIVATE>true</PRIVATE>
<STATIC>true</STATIC>
</AND>
</match>
</rule>
</section>
<section>
<rule>
<match>
<AND>
<FIELD>true</FIELD>
<PUBLIC>true</PUBLIC>
<STATIC>true</STATIC>
</AND>
</match>
</rule>
</section>
<section>
<rule>
<match>
<AND>
<FIELD>true</FIELD>
<PROTECTED>true</PROTECTED>
<STATIC>true</STATIC>
</AND>
</match>
</rule>
</section>
<section>
<rule>
<match>
<AND>
<FIELD>true</FIELD>
<PACKAGE_PRIVATE>true</PACKAGE_PRIVATE>
<STATIC>true</STATIC>
</AND>
</match>
</rule>
</section>
<section>
<rule>
<match>
<AND>
<FIELD>true</FIELD>
<PRIVATE>true</PRIVATE>
<STATIC>true</STATIC>
</AND>
</match>
</rule>
</section>
<section>
<rule>
<match>
<AND>
<INITIALIZER_BLOCK>true</INITIALIZER_BLOCK>
<STATIC>true</STATIC>
</AND>
</match>
</rule>
</section>
<section>
<rule>
<match>
<AND>
<FIELD>true</FIELD>
<FINAL>true</FINAL>
<PUBLIC>true</PUBLIC>
</AND>
</match>
</rule>
</section>
<section>
<rule>
<match>
<AND>
<FIELD>true</FIELD>
<FINAL>true</FINAL>
<PROTECTED>true</PROTECTED>
</AND>
</match>
</rule>
</section>
<section>
<rule>
<match>
<AND>
<FIELD>true</FIELD>
<FINAL>true</FINAL>
<PACKAGE_PRIVATE>true</PACKAGE_PRIVATE>
</AND>
</match>
</rule>
</section>
<section>
<rule>
<match>
<AND>
<FIELD>true</FIELD>
<FINAL>true</FINAL>
<PRIVATE>true</PRIVATE>
</AND>
</match>
</rule>
</section>
<section>
<rule>
<match>
<AND>
<FIELD>true</FIELD>
<PUBLIC>true</PUBLIC>
</AND>
</match>
</rule>
</section>
<section>
<rule>
<match>
<AND>
<FIELD>true</FIELD>
<PROTECTED>true</PROTECTED>
</AND>
</match>
</rule>
</section>
<section>
<rule>
<match>
<AND>
<FIELD>true</FIELD>
<PACKAGE_PRIVATE>true</PACKAGE_PRIVATE>
</AND>
</match>
</rule>
</section>
<section>
<rule>
<match>
<AND>
<FIELD>true</FIELD>
<PRIVATE>true</PRIVATE>
</AND>
</match>
</rule>
</section>
<section>
<rule>
<match>
<FIELD>true</FIELD>
</match>
</rule>
</section>
<section>
<rule>
<match>
<INITIALIZER_BLOCK>true</INITIALIZER_BLOCK>
</match>
</rule>
</section>
<section>
<rule>
<match>
<CONSTRUCTOR>true</CONSTRUCTOR>
</match>
</rule>
</section>
<section>
<rule>
<match>
<AND>
<METHOD>true</METHOD>
<STATIC>true</STATIC>
</AND>
</match>
</rule>
</section>
<section>
<rule>
<match>
<METHOD>true</METHOD>
</match>
</rule>
</section>
<section>
<rule>
<match>
<AND>
<METHOD>true</METHOD>
<PRIVATE>true</PRIVATE>
<STATIC>true</STATIC>
</AND>
</match>
</rule>
</section>
<section>
<rule>
<match>
<ENUM>true</ENUM>
</match>
</rule>
</section>
<section>
<rule>
<match>
<INTERFACE>true</INTERFACE>
</match>
</rule>
</section>
<section>
<rule>
<match>
<AND>
<CLASS>true</CLASS>
<STATIC>true</STATIC>
</AND>
</match>
</rule>
</section>
<section>
<rule>
<match>
<CLASS>true</CLASS>
</match>
</rule>
</section>
</rules>
</arrangement>
</codeStyleSettings>
</value>
</option>
<option name="USE_PER_PROJECT_SETTINGS" value="true" />
<option name="PREFERRED_PROJECT_CODE_STYLE" value="Project" />
</component>
</project>
</project>

View File

@ -5,9 +5,9 @@ import fr.xephi.authme.api.API;
import fr.xephi.authme.api.NewAPI;
import fr.xephi.authme.cache.auth.PlayerAuth;
import fr.xephi.authme.cache.auth.PlayerCache;
import fr.xephi.authme.cache.backup.JsonCache;
import fr.xephi.authme.cache.backup.PlayerDataStorage;
import fr.xephi.authme.cache.limbo.LimboCache;
import fr.xephi.authme.cache.limbo.LimboPlayer;
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;
@ -564,8 +564,8 @@ public class AuthMe extends JavaPlugin {
return;
}
String name = player.getName().toLowerCase();
if (limboCache.hasLimboPlayer(name)) {
LimboPlayer limbo = limboCache.getLimboPlayer(name);
if (limboCache.hasPlayerData(name)) {
PlayerData limbo = limboCache.getPlayerData(name);
if (!newSettings.getProperty(RestrictionSettings.NO_TELEPORT)) {
player.teleport(limbo.getLoc());
}
@ -574,14 +574,13 @@ public class AuthMe extends JavaPlugin {
player.setAllowFlight(limbo.isCanFly());
player.setWalkSpeed(limbo.getWalkSpeed());
if (newSettings.getProperty(RestrictionSettings.TELEPORT_UNAUTHED_TO_SPAWN)) {
limboCache.removeLimboPlayer(player);
limboCache.removePlayerData(player);
} else {
limboCache.deleteLimboPlayer(player);
limboCache.deletePlayerData(player);
}
} else {
if (newSettings.getProperty(RestrictionSettings.SAVE_QUIT_LOCATION)) {
Location loc =
player.isOnline() && player.isDead() ? spawnLoader.getSpawnLocation(player) : player.getLocation();
Location loc = spawnLoader.getPlayerLocationOrSpawn(player);
final PlayerAuth auth = PlayerAuth.builder()
.name(player.getName().toLowerCase())
.realName(player.getName())
@ -590,9 +589,9 @@ public class AuthMe extends JavaPlugin {
}
if (newSettings.getProperty(RestrictionSettings.TELEPORT_UNAUTHED_TO_SPAWN)
&& !newSettings.getProperty(RestrictionSettings.NO_TELEPORT)) {
JsonCache jsonCache = initializer.getIfAvailable(JsonCache.class);
if (jsonCache != null && !jsonCache.doesCacheExist(player)) {
jsonCache.writeCache(player);
PlayerDataStorage playerDataStorage = initializer.getIfAvailable(PlayerDataStorage.class);
if (playerDataStorage != null && !playerDataStorage.hasData(player)) {
playerDataStorage.saveData(player);
}
}
}

View File

@ -4,6 +4,7 @@ package fr.xephi.authme.cache.auth;
import java.util.concurrent.ConcurrentHashMap;
/**
* Used to manage player's Authenticated status
*/
public class PlayerCache {
@ -55,11 +56,11 @@ public class PlayerCache {
}
/**
* Method isAuthenticated.
* get player's authenticated status.
*
* @param user String
* @param user player's name
*
* @return boolean
* @return true if player is logged in, false otherwise.
*/
public boolean isAuthenticated(String user) {
return cache.containsKey(user.toLowerCase());

View File

@ -11,7 +11,7 @@ import com.google.gson.JsonObject;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;
import fr.xephi.authme.ConsoleLogger;
import fr.xephi.authme.cache.limbo.LimboPlayer;
import fr.xephi.authme.cache.limbo.PlayerData;
import fr.xephi.authme.initialization.DataFolder;
import fr.xephi.authme.permission.PermissionsManager;
import fr.xephi.authme.settings.SpawnLoader;
@ -27,7 +27,10 @@ import java.io.File;
import java.io.IOException;
import java.lang.reflect.Type;
public class JsonCache {
/**
* Class used to store player's data (OP, flying, speed, position) to disk.
*/
public class PlayerDataStorage {
private final Gson gson;
private final File cacheDir;
@ -36,60 +39,77 @@ public class JsonCache {
private BukkitService bukkitService;
@Inject
JsonCache(@DataFolder File dataFolder, PermissionsManager permsMan,
SpawnLoader spawnLoader, BukkitService bukkitService) {
PlayerDataStorage(@DataFolder File dataFolder, PermissionsManager permsMan,
SpawnLoader spawnLoader, BukkitService bukkitService) {
this.permissionsManager = permsMan;
this.spawnLoader = spawnLoader;
this.bukkitService = bukkitService;
cacheDir = new File(dataFolder, "cache");
cacheDir = new File(dataFolder, "playerdata");
if (!cacheDir.exists() && !cacheDir.isDirectory() && !cacheDir.mkdir()) {
ConsoleLogger.showError("Failed to create cache directory.");
ConsoleLogger.showError("Failed to create userdata directory.");
}
gson = new GsonBuilder()
.registerTypeAdapter(LimboPlayer.class, new LimboPlayerSerializer())
.registerTypeAdapter(LimboPlayer.class, new LimboPlayerDeserializer())
.registerTypeAdapter(PlayerData.class, new PlayerDataSerializer())
.registerTypeAdapter(PlayerData.class, new PlayerDataDeserializer())
.setPrettyPrinting()
.create();
}
public LimboPlayer readCache(Player player) {
/**
* Read and construct new PlayerData from existing player data.
*
* @param player player to read
*
* @return PlayerData object if the data is exist, null otherwise.
*/
public PlayerData readData(Player player) {
String id = Utils.getUUIDorName(player);
File file = new File(cacheDir, id + File.separator + "cache.json");
File file = new File(cacheDir, id + File.separator + "data.json");
if (!file.exists()) {
return null;
}
try {
String str = Files.toString(file, Charsets.UTF_8);
return gson.fromJson(str, LimboPlayer.class);
return gson.fromJson(str, PlayerData.class);
} catch (IOException e) {
ConsoleLogger.writeStackTrace(e);
return null;
}
}
public void writeCache(Player player) {
/**
* Save player data (OP, flying, location, etc) to disk.
*
* @param player player to save
*/
public void saveData(Player player) {
String id = Utils.getUUIDorName(player);
String name = player.getName().toLowerCase();
Location location =
player.isOnline() && player.isDead() ? spawnLoader.getSpawnLocation(player) : player.getLocation();
Location location = spawnLoader.getPlayerLocationOrSpawn(player);
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);
float flySpeed = player.getFlySpeed();
PlayerData playerData = new PlayerData(location, operator, group, canFly, walkSpeed, flySpeed);
try {
File file = new File(cacheDir, id + File.separator + "cache.json");
File file = new File(cacheDir, id + File.separator + "data.json");
Files.createParentDirs(file);
Files.touch(file);
Files.write(gson.toJson(limboPlayer), file, Charsets.UTF_8);
Files.write(gson.toJson(playerData), file, Charsets.UTF_8);
} catch (IOException e) {
ConsoleLogger.logException("Failed to write " + player.getName() + " cache.", e);
ConsoleLogger.logException("Failed to write " + player.getName() + " data.", e);
}
}
public void removeCache(Player player) {
/**
* Remove player data, this will delete
* "playerdata/&lt;uuid or name&gt;/" folder from disk.
*
* @param player player to remove
*/
public void removeData(Player player) {
String id = Utils.getUUIDorName(player);
File file = new File(cacheDir, id);
if (file.exists()) {
@ -100,16 +120,23 @@ public class JsonCache {
}
}
public boolean doesCacheExist(Player player) {
/**
* Use to check is player data is exist.
*
* @param player player to check
*
* @return true if data exist, false otherwise.
*/
public boolean hasData(Player player) {
String id = Utils.getUUIDorName(player);
File file = new File(cacheDir, id + File.separator + "cache.json");
File file = new File(cacheDir, id + File.separator + "data.json");
return file.exists();
}
private class LimboPlayerDeserializer implements JsonDeserializer<LimboPlayer> {
private class PlayerDataDeserializer implements JsonDeserializer<PlayerData> {
@Override
public LimboPlayer deserialize(JsonElement jsonElement, Type type,
JsonDeserializationContext context) {
public PlayerData deserialize(JsonElement jsonElement, Type type,
JsonDeserializationContext context) {
JsonObject jsonObject = jsonElement.getAsJsonObject();
if (jsonObject == null) {
return null;
@ -120,6 +147,7 @@ public class JsonCache {
boolean operator = false;
boolean canFly = false;
float walkSpeed = 0.2f;
float flySpeed = 0.2f;
JsonElement e;
if ((e = jsonObject.getAsJsonObject("location")) != null) {
@ -146,19 +174,22 @@ public class JsonCache {
if ((e = jsonObject.get("walk-speed")) != null) {
walkSpeed = e.getAsFloat();
}
if ((e = jsonObject.get("fly-speed")) != null) {
flySpeed = e.getAsFloat();
}
return new LimboPlayer("", loc, operator, group, canFly, walkSpeed);
return new PlayerData(loc, operator, group, canFly, walkSpeed, flySpeed);
}
}
private class LimboPlayerSerializer implements JsonSerializer<LimboPlayer> {
private class PlayerDataSerializer implements JsonSerializer<PlayerData> {
@Override
public JsonElement serialize(LimboPlayer limboPlayer, Type type,
public JsonElement serialize(PlayerData playerData, Type type,
JsonSerializationContext context) {
JsonObject obj = new JsonObject();
obj.addProperty("group", limboPlayer.getGroup());
obj.addProperty("group", playerData.getGroup());
Location loc = limboPlayer.getLoc();
Location loc = playerData.getLoc();
JsonObject obj2 = new JsonObject();
obj2.addProperty("world", loc.getWorld().getName());
obj2.addProperty("x", loc.getX());
@ -168,9 +199,10 @@ public class JsonCache {
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());
obj.addProperty("operator", playerData.isOperator());
obj.addProperty("can-fly", playerData.isCanFly());
obj.addProperty("walk-speed", playerData.getWalkSpeed());
obj.addProperty("fly-speed", playerData.getFlySpeed());
return obj;
}
}

View File

@ -1,6 +1,6 @@
package fr.xephi.authme.cache.limbo;
import fr.xephi.authme.cache.backup.JsonCache;
import fr.xephi.authme.cache.backup.PlayerDataStorage;
import fr.xephi.authme.permission.PermissionsManager;
import fr.xephi.authme.settings.SpawnLoader;
import org.bukkit.Location;
@ -12,21 +12,21 @@ import java.util.concurrent.ConcurrentHashMap;
import static com.google.common.base.Preconditions.checkNotNull;
/**
* Manages all {@link LimboPlayer} instances.
* Manages all {@link PlayerData} instances.
*/
public class LimboCache {
private final ConcurrentHashMap<String, LimboPlayer> cache = new ConcurrentHashMap<>();
private final ConcurrentHashMap<String, PlayerData> cache = new ConcurrentHashMap<>();
private JsonCache jsonCache;
private PlayerDataStorage playerDataStorage;
private PermissionsManager permissionsManager;
private SpawnLoader spawnLoader;
@Inject
LimboCache(PermissionsManager permissionsManager, SpawnLoader spawnLoader, JsonCache jsonCache) {
LimboCache(PermissionsManager permissionsManager, SpawnLoader spawnLoader, PlayerDataStorage playerDataStorage) {
this.permissionsManager = permissionsManager;
this.spawnLoader = spawnLoader;
this.jsonCache = jsonCache;
this.playerDataStorage = playerDataStorage;
}
/**
@ -34,88 +34,90 @@ public class LimboCache {
*
* @param player Player instance to add.
*/
public void addLimboPlayer(Player player) {
public void addPlayerData(Player player) {
String name = player.getName().toLowerCase();
Location location = player.isDead() ? spawnLoader.getSpawnLocation(player) : player.getLocation();
Location location = spawnLoader.getPlayerLocationOrSpawn(player);
boolean operator = player.isOp();
boolean flyEnabled = player.getAllowFlight();
float walkSpeed = player.getWalkSpeed();
float flySpeed = player.getFlySpeed();
String playerGroup = "";
if (permissionsManager.hasGroupSupport()) {
playerGroup = permissionsManager.getPrimaryGroup(player);
}
if (jsonCache.doesCacheExist(player)) {
LimboPlayer cache = jsonCache.readCache(player);
if (playerDataStorage.hasData(player)) {
PlayerData cache = playerDataStorage.readData(player);
if (cache != null) {
location = cache.getLoc();
playerGroup = cache.getGroup();
operator = cache.isOperator();
flyEnabled = cache.isCanFly();
walkSpeed = cache.getWalkSpeed();
flySpeed = cache.getFlySpeed();
}
} else {
jsonCache.writeCache(player);
playerDataStorage.saveData(player);
}
cache.put(name, new LimboPlayer(name, location, operator, playerGroup, flyEnabled, walkSpeed));
cache.put(name, new PlayerData(location, operator, playerGroup, flyEnabled, walkSpeed, flySpeed));
}
/**
* Remove LimboPlayer and delete cache.json from disk.
* Remove PlayerData and delete cache.json from disk.
*
* @param player Player player to remove.
*/
public void deleteLimboPlayer(Player player) {
removeLimboPlayer(player);
jsonCache.removeCache(player);
public void deletePlayerData(Player player) {
removePlayerData(player);
playerDataStorage.removeData(player);
}
/**
* Remove LimboPlayer from cache, without deleting cache.json file.
* Remove PlayerData from cache, without deleting cache.json file.
*
* @param player Player player to remove.
*/
public void removeLimboPlayer(Player player) {
public void removePlayerData(Player player) {
String name = player.getName().toLowerCase();
LimboPlayer cachedPlayer = cache.remove(name);
PlayerData cachedPlayer = cache.remove(name);
if (cachedPlayer != null) {
cachedPlayer.clearTasks();
}
}
/**
* Method getLimboPlayer.
* Method getPlayerData.
*
* @param name String
*
* @return LimboPlayer
* @return PlayerData
*/
public LimboPlayer getLimboPlayer(String name) {
public PlayerData getPlayerData(String name) {
checkNotNull(name);
return cache.get(name.toLowerCase());
}
/**
* Method hasLimboPlayer.
* Method hasPlayerData.
*
* @param name String
*
* @return boolean
*/
public boolean hasLimboPlayer(String name) {
public boolean hasPlayerData(String name) {
checkNotNull(name);
return cache.containsKey(name.toLowerCase());
}
/**
* Method updateLimboPlayer.
* Method updatePlayerData.
*
* @param player Player
*/
public void updateLimboPlayer(Player player) {
public void updatePlayerData(Player player) {
checkNotNull(player);
removeLimboPlayer(player);
addLimboPlayer(player);
removePlayerData(player);
addPlayerData(player);
}
}

View File

@ -7,34 +7,25 @@ import org.bukkit.scheduler.BukkitTask;
* Represents a player which is not logged in and keeps track of certain states (like OP status, flying)
* which may be revoked from the player until he has logged in or registered.
*/
public class LimboPlayer {
public class PlayerData {
private final String name;
private final boolean canFly;
private final boolean operator;
private final String group;
private final Location loc;
private final float walkSpeed;
private final float flySpeed;
private BukkitTask timeoutTask = null;
private BukkitTask messageTask = null;
public LimboPlayer(String name, Location loc, boolean operator,
String group, boolean fly, float walkSpeed) {
this.name = name;
public PlayerData(Location loc, boolean operator,
String group, boolean fly, float walkSpeed, float flySpeed) {
this.loc = loc;
this.operator = operator;
this.group = group;
this.canFly = fly;
this.walkSpeed = walkSpeed;
}
/**
* Return the name of the player.
*
* @return The player's name
*/
public String getName() {
return name;
this.flySpeed = flySpeed;
}
/**
@ -72,6 +63,10 @@ public class LimboPlayer {
return walkSpeed;
}
public float getFlySpeed() {
return flySpeed;
}
/**
* Return the timeout task, which kicks the player if he hasn't registered or logged in
* after a configurable amount of time.

View File

@ -11,7 +11,7 @@ import fr.xephi.authme.permission.AuthGroupHandler;
import fr.xephi.authme.permission.AuthGroupType;
import fr.xephi.authme.settings.properties.RegistrationSettings;
import fr.xephi.authme.settings.properties.RestrictionSettings;
import fr.xephi.authme.task.LimboPlayerTaskManager;
import fr.xephi.authme.task.PlayerDataTaskManager;
import fr.xephi.authme.util.BukkitService;
import fr.xephi.authme.util.Utils;
import org.bukkit.command.CommandSender;
@ -45,7 +45,7 @@ public class UnregisterAdminCommand implements ExecutableCommand {
private LimboCache limboCache;
@Inject
private LimboPlayerTaskManager limboPlayerTaskManager;
private PlayerDataTaskManager playerDataTaskManager;
@Inject
private AuthGroupHandler authGroupHandler;
@ -96,9 +96,9 @@ public class UnregisterAdminCommand implements ExecutableCommand {
// TODO #765: Remove use of Utils method and behave according to settings
Utils.teleportToSpawn(target);
limboCache.addLimboPlayer(target);
limboPlayerTaskManager.registerTimeoutTask(target);
limboPlayerTaskManager.registerMessageTask(target.getName(), false);
limboCache.addPlayerData(target);
playerDataTaskManager.registerTimeoutTask(target);
playerDataTaskManager.registerMessageTask(target.getName(), false);
final int timeout = commandService.getProperty(RestrictionSettings.TIMEOUT) * TICKS_PER_SECOND;
if (commandService.getProperty(RegistrationSettings.APPLY_BLIND_EFFECT)) {

View File

@ -2,12 +2,11 @@ package fr.xephi.authme.permission;
import fr.xephi.authme.ConsoleLogger;
import fr.xephi.authme.cache.limbo.LimboCache;
import fr.xephi.authme.cache.limbo.LimboPlayer;
import fr.xephi.authme.cache.limbo.PlayerData;
import fr.xephi.authme.settings.NewSetting;
import fr.xephi.authme.settings.Settings;
import fr.xephi.authme.settings.properties.PluginSettings;
import fr.xephi.authme.settings.properties.SecuritySettings;
import org.bukkit.entity.Player;
import javax.inject.Inject;
@ -68,7 +67,7 @@ public class AuthGroupHandler {
case LOGGED_IN:
// Get the limbo player data
LimboPlayer limbo = limboCache.getLimboPlayer(player.getName().toLowerCase());
PlayerData limbo = limboCache.getPlayerData(player.getName().toLowerCase());
if (limbo == null) {
return false;
}

View File

@ -19,7 +19,7 @@ import fr.xephi.authme.settings.properties.PluginSettings;
import fr.xephi.authme.settings.properties.RegistrationSettings;
import fr.xephi.authme.settings.properties.RestrictionSettings;
import fr.xephi.authme.settings.properties.SecuritySettings;
import fr.xephi.authme.task.LimboPlayerTaskManager;
import fr.xephi.authme.task.PlayerDataTaskManager;
import fr.xephi.authme.util.BukkitService;
import fr.xephi.authme.util.Utils;
import org.apache.commons.lang.reflect.MethodUtils;
@ -65,7 +65,7 @@ public class AsynchronousJoin implements AsynchronousProcess {
private BukkitService bukkitService;
@Inject
private LimboPlayerTaskManager limboPlayerTaskManager;
private PlayerDataTaskManager playerDataTaskManager;
AsynchronousJoin() {
}
@ -115,7 +115,7 @@ public class AsynchronousJoin implements AsynchronousProcess {
return;
}
limboCache.updateLimboPlayer(player);
limboCache.updatePlayerData(player);
final boolean isAuthAvailable = database.isAuthAvailable(name);
@ -186,8 +186,8 @@ public class AsynchronousJoin implements AsynchronousProcess {
});
// Timeout and message task
limboPlayerTaskManager.registerTimeoutTask(player);
limboPlayerTaskManager.registerMessageTask(name, isAuthAvailable);
playerDataTaskManager.registerTimeoutTask(player);
playerDataTaskManager.registerMessageTask(name, isAuthAvailable);
}
private boolean isPlayerUnrestricted(String name) {

View File

@ -6,7 +6,7 @@ import fr.xephi.authme.cache.TempbanManager;
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.cache.limbo.PlayerData;
import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.events.AuthMeAsyncPreLoginEvent;
import fr.xephi.authme.output.MessageKey;
@ -23,7 +23,7 @@ import fr.xephi.authme.settings.properties.DatabaseSettings;
import fr.xephi.authme.settings.properties.EmailSettings;
import fr.xephi.authme.settings.properties.RestrictionSettings;
import fr.xephi.authme.settings.properties.SecuritySettings;
import fr.xephi.authme.task.LimboPlayerTaskManager;
import fr.xephi.authme.task.PlayerDataTaskManager;
import fr.xephi.authme.util.BukkitService;
import fr.xephi.authme.util.StringUtils;
import fr.xephi.authme.util.Utils;
@ -70,7 +70,7 @@ public class AsynchronousLogin implements AsynchronousProcess {
private TempbanManager tempbanManager;
@Inject
private LimboPlayerTaskManager limboPlayerTaskManager;
private PlayerDataTaskManager playerDataTaskManager;
AsynchronousLogin() { }
@ -106,7 +106,7 @@ public class AsynchronousLogin implements AsynchronousProcess {
service.send(player, MessageKey.USER_NOT_REGISTERED);
// TODO ljacqu 20160612: Why is the message task being canceled and added again here?
limboPlayerTaskManager.registerMessageTask(name, false);
playerDataTaskManager.registerMessageTask(name, false);
return null;
}
@ -197,9 +197,9 @@ public class AsynchronousLogin implements AsynchronousProcess {
// task, we schedule it in the end
// so that we can be sure, and have not to care if it might be
// processed in other order.
LimboPlayer limboPlayer = limboCache.getLimboPlayer(name);
if (limboPlayer != null) {
limboPlayer.clearTasks();
PlayerData playerData = limboCache.getPlayerData(name);
if (playerData != null) {
playerData.clearTasks();
}
syncProcessManager.processSyncPlayerLogin(player);
} else if (player.isOnline()) {

View File

@ -5,7 +5,7 @@ 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.LimboPlayer;
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;
@ -83,7 +83,7 @@ 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 LimboPlayer limbo = limboCache.getLimboPlayer(name);
final PlayerData limbo = limboCache.getPlayerData(name);
final PlayerAuth auth = dataSource.getAuth(name);
if (limbo != null) {
@ -112,7 +112,7 @@ public class ProcessSyncPlayerLogin implements SynchronousProcess {
}
// Clean up no longer used temporary data
limboCache.deleteLimboPlayer(player);
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.updateLimboPlayer(player);
limboCache.updatePlayerData(player);
playerCache.removePlayer(name);
database.setUnlogged(name);
syncProcessManager.processSyncPlayerLogout(player);

View File

@ -14,7 +14,7 @@ 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.task.LimboPlayerTaskManager;
import fr.xephi.authme.task.PlayerDataTaskManager;
import fr.xephi.authme.util.BukkitService;
import org.bukkit.entity.Player;
import org.bukkit.potion.PotionEffect;
@ -40,7 +40,7 @@ public class ProcessSynchronousPlayerLogout implements SynchronousProcess {
private ProtocolLibService protocolLibService;
@Inject
private LimboPlayerTaskManager limboPlayerTaskManager;
private PlayerDataTaskManager playerDataTaskManager;
@Inject
private SessionManager sessionManager;
@ -66,8 +66,8 @@ public class ProcessSynchronousPlayerLogout implements SynchronousProcess {
protocolLibService.sendBlankInventoryPacket(player);
}
limboPlayerTaskManager.registerTimeoutTask(player);
limboPlayerTaskManager.registerMessageTask(name, true);
playerDataTaskManager.registerTimeoutTask(player);
playerDataTaskManager.registerMessageTask(name, true);
applyLogoutEffect(player);

View File

@ -57,7 +57,7 @@ public class AsynchronousQuit implements AsynchronousProcess {
String ip = Utils.getPlayerIp(player);
if (playerCache.isAuthenticated(name)) {
if (service.getProperty(RestrictionSettings.SAVE_QUIT_LOCATION)) {
Location loc = player.isDead() ? spawnLoader.getSpawnLocation(player) : player.getLocation();
Location loc = spawnLoader.getPlayerLocationOrSpawn(player);
PlayerAuth auth = PlayerAuth.builder()
.name(name).location(loc)
.realName(player.getName()).build();

View File

@ -1,8 +1,8 @@
package fr.xephi.authme.process.quit;
import fr.xephi.authme.cache.backup.JsonCache;
import fr.xephi.authme.cache.backup.PlayerDataStorage;
import fr.xephi.authme.cache.limbo.LimboCache;
import fr.xephi.authme.cache.limbo.LimboPlayer;
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;
@ -16,7 +16,7 @@ import javax.inject.Inject;
public class ProcessSyncronousPlayerQuit implements SynchronousProcess {
@Inject
private JsonCache jsonCache;
private PlayerDataStorage playerDataStorage;
@Inject
private ProcessService service;
@ -25,26 +25,26 @@ public class ProcessSyncronousPlayerQuit implements SynchronousProcess {
private LimboCache limboCache;
public void processSyncQuit(Player player) {
LimboPlayer limbo = limboCache.getLimboPlayer(player.getName().toLowerCase());
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.removeLimboPlayer(player);
limboCache.removePlayerData(player);
} else {
// Restore data if its about to delete LimboPlayer
// 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.deleteLimboPlayer(player);
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)) {
if (!jsonCache.doesCacheExist(player)) {
jsonCache.writeCache(player);
if (!playerDataStorage.hasData(player)) {
playerDataStorage.saveData(player);
}
}
}

View File

@ -7,7 +7,7 @@ import fr.xephi.authme.process.ProcessService;
import fr.xephi.authme.process.SynchronousProcess;
import fr.xephi.authme.settings.Settings;
import fr.xephi.authme.settings.properties.SecuritySettings;
import fr.xephi.authme.task.LimboPlayerTaskManager;
import fr.xephi.authme.task.PlayerDataTaskManager;
import fr.xephi.authme.util.Utils;
import org.bukkit.entity.Player;
@ -20,7 +20,7 @@ public class ProcessSyncEmailRegister implements SynchronousProcess {
private ProcessService service;
@Inject
private LimboPlayerTaskManager limboPlayerTaskManager;
private PlayerDataTaskManager playerDataTaskManager;
ProcessSyncEmailRegister() { }
@ -32,8 +32,8 @@ public class ProcessSyncEmailRegister implements SynchronousProcess {
}
service.send(player, MessageKey.ACCOUNT_NOT_ACTIVATED);
limboPlayerTaskManager.registerTimeoutTask(player);
limboPlayerTaskManager.registerMessageTask(name, true);
playerDataTaskManager.registerTimeoutTask(player);
playerDataTaskManager.registerMessageTask(name, true);
player.saveData();
if (!service.getProperty(SecuritySettings.REMOVE_SPAM_FROM_CONSOLE)) {

View File

@ -5,7 +5,7 @@ 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.LimboPlayer;
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;
@ -18,7 +18,7 @@ import fr.xephi.authme.settings.properties.EmailSettings;
import fr.xephi.authme.settings.properties.HooksSettings;
import fr.xephi.authme.settings.properties.RegistrationSettings;
import fr.xephi.authme.settings.properties.SecuritySettings;
import fr.xephi.authme.task.LimboPlayerTaskManager;
import fr.xephi.authme.task.PlayerDataTaskManager;
import fr.xephi.authme.util.BukkitService;
import fr.xephi.authme.util.Utils;
import org.bukkit.Bukkit;
@ -49,7 +49,7 @@ public class ProcessSyncPasswordRegister implements SynchronousProcess {
private LimboCache limboCache;
@Inject
private LimboPlayerTaskManager limboPlayerTaskManager;
private PlayerDataTaskManager playerDataTaskManager;
ProcessSyncPasswordRegister() { }
@ -82,9 +82,9 @@ public class ProcessSyncPasswordRegister implements SynchronousProcess {
final String name = player.getName().toLowerCase();
Utils.teleportToSpawn(player);
limboCache.updateLimboPlayer(player);
limboPlayerTaskManager.registerTimeoutTask(player);
limboPlayerTaskManager.registerMessageTask(name, true);
limboCache.updatePlayerData(player);
playerDataTaskManager.registerTimeoutTask(player);
playerDataTaskManager.registerMessageTask(name, true);
if (player.isInsideVehicle() && player.getVehicle() != null) {
player.getVehicle().eject();
@ -93,7 +93,7 @@ public class ProcessSyncPasswordRegister implements SynchronousProcess {
public void processPasswordRegister(Player player) {
final String name = player.getName().toLowerCase();
LimboPlayer limbo = limboCache.getLimboPlayer(name);
PlayerData limbo = limboCache.getPlayerData(name);
if (limbo != null) {
Utils.teleportToSpawn(player);
@ -105,7 +105,7 @@ public class ProcessSyncPasswordRegister implements SynchronousProcess {
}
}
limboCache.deleteLimboPlayer(player);
limboCache.deletePlayerData(player);
}
if (!Settings.getRegisteredGroup.isEmpty()) {

View File

@ -13,7 +13,7 @@ import fr.xephi.authme.security.PasswordSecurity;
import fr.xephi.authme.settings.Settings;
import fr.xephi.authme.settings.properties.RegistrationSettings;
import fr.xephi.authme.settings.properties.RestrictionSettings;
import fr.xephi.authme.task.LimboPlayerTaskManager;
import fr.xephi.authme.task.PlayerDataTaskManager;
import fr.xephi.authme.util.Utils;
import org.bukkit.entity.Player;
import org.bukkit.potion.PotionEffect;
@ -41,7 +41,7 @@ public class AsynchronousUnregister implements AsynchronousProcess {
private LimboCache limboCache;
@Inject
private LimboPlayerTaskManager limboPlayerTaskManager;
private PlayerDataTaskManager playerDataTaskManager;
AsynchronousUnregister() { }
@ -62,9 +62,9 @@ public class AsynchronousUnregister implements AsynchronousProcess {
if (!Settings.getRegisteredGroup.isEmpty()) {
service.setGroup(player, AuthGroupType.UNREGISTERED);
}
limboCache.addLimboPlayer(player);
limboPlayerTaskManager.registerTimeoutTask(player);
limboPlayerTaskManager.registerMessageTask(name, false);
limboCache.addPlayerData(player);
playerDataTaskManager.registerTimeoutTask(player);
playerDataTaskManager.registerMessageTask(name, false);
service.send(player, MessageKey.UNREGISTERED_SUCCESS);
ConsoleLogger.info(player.getDisplayName() + " unregistered himself");

View File

@ -19,8 +19,6 @@ public final class Settings {
public static boolean isPermissionCheckEnabled;
public static boolean isTeleportToSpawnEnabled;
public static boolean isAllowRestrictedIp;
public static boolean isSaveQuitLocationEnabled;
public static boolean protectInventoryBeforeLogInEnabled;
public static boolean isStopEnabled;
public static boolean reloadSupport;
public static boolean noTeleport;
@ -45,8 +43,6 @@ public final class Settings {
isPermissionCheckEnabled = load(PluginSettings.ENABLE_PERMISSION_CHECK);
isTeleportToSpawnEnabled = load(RestrictionSettings.TELEPORT_UNAUTHED_TO_SPAWN);
isAllowRestrictedIp = load(RestrictionSettings.ENABLE_RESTRICTED_USERS);
isSaveQuitLocationEnabled = load(RestrictionSettings.SAVE_QUIT_LOCATION);
isRemoveSpeedEnabled = load(RestrictionSettings.REMOVE_SPEED);
getUnloggedinGroup = load(SecuritySettings.UNLOGGEDIN_GROUP);
getNonActivatedGroup = configFile.getInt("ExternalBoardOptions.nonActivedUserGroup", -1);
unRegisteredGroup = configFile.getString("GroupOptions.UnregisteredPlayerGroup", "");

View File

@ -43,9 +43,9 @@ public class SpawnLoader implements Reloadable {
* Constructor.
*
* @param pluginFolder The AuthMe data folder
* @param settings The setting instance
* @param pluginHooks The plugin hooks instance
* @param dataSource The plugin auth database instance
* @param settings The setting instance
* @param pluginHooks The plugin hooks instance
* @param dataSource The plugin auth database instance
*/
@Inject
public SpawnLoader(@DataFolder File pluginFolder, NewSetting settings, PluginHooks pluginHooks,
@ -83,6 +83,7 @@ public class SpawnLoader implements Reloadable {
* Set the AuthMe spawn point.
*
* @param location The location to use
*
* @return True upon success, false otherwise
*/
public boolean setSpawn(Location location) {
@ -102,6 +103,7 @@ public class SpawnLoader implements Reloadable {
* Set the AuthMe first spawn location.
*
* @param location The location to use
*
* @return True upon success, false otherwise
*/
public boolean setFirstSpawn(Location location) {
@ -140,7 +142,9 @@ public class SpawnLoader implements Reloadable {
* depending on the spawn priority setting.
*
* @param player The player to retrieve the spawn point for
*
* @return The spawn location, or the default spawn location upon failure
*
* @see RestrictionSettings#SPAWN_PRIORITY
*/
public Location getSpawnLocation(Player player) {
@ -187,8 +191,9 @@ public class SpawnLoader implements Reloadable {
/**
* Save the location under the given prefix.
*
* @param prefix The prefix to save the spawn under
* @param prefix The prefix to save the spawn under
* @param location The location to persist
*
* @return True upon success, false otherwise
*/
private boolean setLocation(String prefix, Location location) {
@ -214,11 +219,26 @@ public class SpawnLoader implements Reloadable {
return false;
}
/**
* Return player's location if player is alive, or player's spawn location if dead.
*
* @param player player to retrieve
*
* @return location of the given player if alive, spawn location if dead.
*/
public Location getPlayerLocationOrSpawn(Player player) {
if (player.isOnline() && player.isDead()) {
return getSpawnLocation(player);
}
return player.getLocation();
}
/**
* Build a {@link Location} object from the given path in the file configuration.
*
* @param configuration The file configuration to read from
* @param pathPrefix The path to get the spawn point from
* @param pathPrefix The path to get the spawn point from
*
* @return Location corresponding to the values in the path
*/
private static Location getLocationFromConfiguration(FileConfiguration configuration, String pathPrefix) {
@ -240,7 +260,8 @@ public class SpawnLoader implements Reloadable {
* under the given path.
*
* @param configuration The file configuration to use
* @param pathPrefix The path to verify
* @param pathPrefix The path to verify
*
* @return True if all spawn fields are present, false otherwise
*/
private static boolean containsAllSpawnFields(FileConfiguration configuration, String pathPrefix) {
@ -257,7 +278,8 @@ public class SpawnLoader implements Reloadable {
* Retrieve a property as a float from the given file configuration.
*
* @param configuration The file configuration to use
* @param path The path of the property to retrieve
* @param path The path of the property to retrieve
*
* @return The float
*/
private static float getFloat(FileConfiguration configuration, String path) {

View File

@ -45,8 +45,8 @@ public class MessageTask implements Runnable {
player.sendMessage(ms);
}
BukkitTask nextTask = bukkitService.runTaskLater(this, interval * TICKS_PER_SECOND);
if (limboCache.hasLimboPlayer(name)) {
limboCache.getLimboPlayer(name).setMessageTask(nextTask);
if (limboCache.hasPlayerData(name)) {
limboCache.getPlayerData(name).setMessageTask(nextTask);
}
return;
}

View File

@ -3,7 +3,7 @@ package fr.xephi.authme.task;
import fr.xephi.authme.ConsoleLogger;
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.cache.limbo.PlayerData;
import fr.xephi.authme.output.MessageKey;
import fr.xephi.authme.output.Messages;
import fr.xephi.authme.settings.NewSetting;
@ -18,9 +18,9 @@ import javax.inject.Inject;
import static fr.xephi.authme.util.BukkitService.TICKS_PER_SECOND;
/**
* Registers tasks associated with a LimboPlayer.
* Registers tasks associated with a PlayerData.
*/
public class LimboPlayerTaskManager {
public class PlayerDataTaskManager {
@Inject
private Messages messages;
@ -37,7 +37,8 @@ public class LimboPlayerTaskManager {
@Inject
private PlayerCache playerCache;
LimboPlayerTaskManager() { }
PlayerDataTaskManager() {
}
/**
@ -51,14 +52,14 @@ public class LimboPlayerTaskManager {
final int interval = settings.getProperty(RegistrationSettings.MESSAGE_INTERVAL);
final MessageKey key = getMessageKey(isRegistered);
if (interval > 0) {
final LimboPlayer limboPlayer = limboCache.getLimboPlayer(name);
if (limboPlayer == null) {
ConsoleLogger.info("LimboPlayer for '" + name + "' is not available");
final PlayerData playerData = limboCache.getPlayerData(name);
if (playerData == null) {
ConsoleLogger.info("PlayerData for '" + name + "' is not available");
} else {
cancelTask(limboPlayer.getMessageTask());
cancelTask(playerData.getMessageTask());
BukkitTask messageTask = bukkitService.runTask(new MessageTask(name, messages.retrieve(key),
interval, bukkitService, limboCache, playerCache));
limboPlayer.setMessageTask(messageTask);
playerData.setMessageTask(messageTask);
}
}
}
@ -71,14 +72,14 @@ public class LimboPlayerTaskManager {
public void registerTimeoutTask(Player player) {
final int timeout = settings.getProperty(RestrictionSettings.TIMEOUT) * TICKS_PER_SECOND;
if (timeout > 0) {
final LimboPlayer limboPlayer = limboCache.getLimboPlayer(player.getName());
if (limboPlayer == null) {
ConsoleLogger.info("LimboPlayer for '" + player.getName() + "' is not available");
final PlayerData playerData = limboCache.getPlayerData(player.getName());
if (playerData == null) {
ConsoleLogger.info("PlayerData for '" + player.getName() + "' is not available");
} else {
cancelTask(limboPlayer.getTimeoutTask());
cancelTask(playerData.getTimeoutTask());
String message = messages.retrieveSingle(MessageKey.LOGIN_TIMEOUT_ERROR);
BukkitTask task = bukkitService.runTaskLater(new TimeoutTask(player, message, playerCache), timeout);
limboPlayer.setTimeoutTask(task);
playerData.setTimeoutTask(task);
}
}
}

View File

@ -2,7 +2,7 @@ package fr.xephi.authme.util;
import fr.xephi.authme.cache.auth.PlayerAuth;
import fr.xephi.authme.cache.auth.PlayerCache;
import fr.xephi.authme.cache.limbo.LimboPlayer;
import fr.xephi.authme.cache.limbo.PlayerData;
import fr.xephi.authme.events.AbstractTeleportEvent;
import fr.xephi.authme.events.AuthMeTeleportEvent;
import fr.xephi.authme.events.FirstSpawnTeleportEvent;
@ -94,14 +94,14 @@ public class TeleportationService implements Reloadable {
*
* @param player the player
* @param auth corresponding PlayerAuth object
* @param limbo corresponding LimboPlayer object
* @param limbo corresponding PlayerData object
*/
public void teleportOnLogin(final Player player, PlayerAuth auth, LimboPlayer limbo) {
public void teleportOnLogin(final Player player, PlayerAuth auth, PlayerData limbo) {
if (settings.getProperty(RestrictionSettings.NO_TELEPORT)) {
return;
}
// The world in LimboPlayer is from where the player comes, before any teleportation by AuthMe
// The world in PlayerData is from where the player comes, before any teleportation by AuthMe
String worldName = limbo.getLoc().getWorld().getName();
if (mustForceSpawnAfterLogin(worldName)) {
teleportToSpawn(player, true);

View File

@ -72,7 +72,16 @@ public final class Utils {
}
}
/**
* Get player's UUID if can, name otherwise.
*
* @param player Player to retrieve
*
* @return player's UUID or Name in String.
*/
public static String getUUIDorName(OfflinePlayer player) {
// We may made this configurable in future
// so we can have uuid support.
try {
return player.getUniqueId().toString();
} catch (Exception ignore) {
@ -80,6 +89,13 @@ public final class Utils {
}
}
/**
* Compile Pattern sneaky without throwing Exception.
*
* @param pattern pattern string to compile
*
* @return the given regex compiled into Pattern object.
*/
public static Pattern safePatternCompile(String pattern) {
try {
return Pattern.compile(pattern);

View File

@ -3,7 +3,7 @@ package fr.xephi.authme.task;
import fr.xephi.authme.TestHelper;
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.cache.limbo.PlayerData;
import fr.xephi.authme.output.MessageKey;
import fr.xephi.authme.output.Messages;
import fr.xephi.authme.settings.NewSetting;
@ -28,13 +28,13 @@ import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyZeroInteractions;
/**
* Test for {@link LimboPlayerTaskManager}.
* Test for {@link PlayerDataTaskManager}.
*/
@RunWith(MockitoJUnitRunner.class)
public class LimboPlayerTaskManagerTest {
public class PlayerDataTaskManagerTest {
@InjectMocks
private LimboPlayerTaskManager limboPlayerTaskManager;
private PlayerDataTaskManager playerDataTaskManager;
@Mock
private Messages messages;
@ -60,8 +60,8 @@ public class LimboPlayerTaskManagerTest {
public void shouldRegisterMessageTask() {
// given
String name = "bobby";
LimboPlayer limboPlayer = mock(LimboPlayer.class);
given(limboCache.getLimboPlayer(name)).willReturn(limboPlayer);
PlayerData playerData = mock(PlayerData.class);
given(limboCache.getPlayerData(name)).willReturn(playerData);
MessageKey key = MessageKey.REGISTER_EMAIL_MESSAGE;
given(messages.retrieve(key)).willReturn(new String[]{"Please register!"});
BukkitTask bukkiTask = mock(BukkitTask.class);
@ -70,10 +70,10 @@ public class LimboPlayerTaskManagerTest {
given(settings.getProperty(RegistrationSettings.USE_EMAIL_REGISTRATION)).willReturn(true);
// when
limboPlayerTaskManager.registerMessageTask(name, false);
playerDataTaskManager.registerMessageTask(name, false);
// then
verify(limboPlayer).setMessageTask(bukkiTask);
verify(playerData).setMessageTask(bukkiTask);
verify(messages).retrieve(key);
}
@ -81,14 +81,14 @@ public class LimboPlayerTaskManagerTest {
public void shouldNotScheduleTaskForMissingLimboPlayer() {
// given
String name = "ghost";
given(limboCache.getLimboPlayer(name)).willReturn(null);
given(limboCache.getPlayerData(name)).willReturn(null);
given(settings.getProperty(RegistrationSettings.MESSAGE_INTERVAL)).willReturn(5);
// when
limboPlayerTaskManager.registerMessageTask(name, true);
playerDataTaskManager.registerMessageTask(name, true);
// then
verify(limboCache).getLimboPlayer(name);
verify(limboCache).getPlayerData(name);
verifyZeroInteractions(bukkitService);
verifyZeroInteractions(messages);
}
@ -97,28 +97,28 @@ public class LimboPlayerTaskManagerTest {
public void shouldNotScheduleTaskForZeroAsInterval() {
// given
String name = "Tester1";
LimboPlayer limboPlayer = mock(LimboPlayer.class);
given(limboCache.getLimboPlayer(name)).willReturn(limboPlayer);
PlayerData playerData = mock(PlayerData.class);
given(limboCache.getPlayerData(name)).willReturn(playerData);
BukkitTask bukkiTask = mock(BukkitTask.class);
given(bukkitService.runTask(any(MessageTask.class))).willReturn(bukkiTask);
given(settings.getProperty(RegistrationSettings.MESSAGE_INTERVAL)).willReturn(0);
// when
limboPlayerTaskManager.registerMessageTask(name, true);
playerDataTaskManager.registerMessageTask(name, true);
// then
verifyZeroInteractions(limboPlayer, bukkitService);
verifyZeroInteractions(playerData, bukkitService);
}
@Test
public void shouldCancelExistingMessageTask() {
// given
LimboPlayer limboPlayer = mock(LimboPlayer.class);
PlayerData playerData = mock(PlayerData.class);
BukkitTask existingMessageTask = mock(BukkitTask.class);
given(limboPlayer.getMessageTask()).willReturn(existingMessageTask);
given(playerData.getMessageTask()).willReturn(existingMessageTask);
String name = "bobby";
given(limboCache.getLimboPlayer(name)).willReturn(limboPlayer);
given(limboCache.getPlayerData(name)).willReturn(playerData);
given(messages.retrieve(MessageKey.REGISTER_EMAIL_MESSAGE))
.willReturn(new String[]{"Please register", "Use /register"});
@ -128,10 +128,10 @@ public class LimboPlayerTaskManagerTest {
given(settings.getProperty(RegistrationSettings.USE_EMAIL_REGISTRATION)).willReturn(true);
// when
limboPlayerTaskManager.registerMessageTask(name, false);
playerDataTaskManager.registerMessageTask(name, false);
// then
verify(limboPlayer).setMessageTask(bukkiTask);
verify(playerData).setMessageTask(bukkiTask);
verify(messages).retrieve(MessageKey.REGISTER_EMAIL_MESSAGE);
verify(existingMessageTask).cancel();
}
@ -142,17 +142,17 @@ public class LimboPlayerTaskManagerTest {
String name = "l33tPlayer";
Player player = mock(Player.class);
given(player.getName()).willReturn(name);
LimboPlayer limboPlayer = mock(LimboPlayer.class);
given(limboCache.getLimboPlayer(name)).willReturn(limboPlayer);
PlayerData playerData = mock(PlayerData.class);
given(limboCache.getPlayerData(name)).willReturn(playerData);
given(settings.getProperty(RestrictionSettings.TIMEOUT)).willReturn(30);
BukkitTask bukkitTask = mock(BukkitTask.class);
given(bukkitService.runTaskLater(any(TimeoutTask.class), anyLong())).willReturn(bukkitTask);
// when
limboPlayerTaskManager.registerTimeoutTask(player);
playerDataTaskManager.registerTimeoutTask(player);
// then
verify(limboPlayer).setTimeoutTask(bukkitTask);
verify(playerData).setTimeoutTask(bukkitTask);
verify(bukkitService).runTaskLater(any(TimeoutTask.class), eq(600L)); // 30 * TICKS_PER_SECOND
verify(messages).retrieveSingle(MessageKey.LOGIN_TIMEOUT_ERROR);
}
@ -163,11 +163,11 @@ public class LimboPlayerTaskManagerTest {
String name = "Phantom_";
Player player = mock(Player.class);
given(player.getName()).willReturn(name);
given(limboCache.getLimboPlayer(name)).willReturn(null);
given(limboCache.getPlayerData(name)).willReturn(null);
given(settings.getProperty(RestrictionSettings.TIMEOUT)).willReturn(27);
// when
limboPlayerTaskManager.registerTimeoutTask(player);
playerDataTaskManager.registerTimeoutTask(player);
// then
verifyZeroInteractions(bukkitService, messages);
@ -179,15 +179,15 @@ public class LimboPlayerTaskManagerTest {
String name = "snail";
Player player = mock(Player.class);
given(player.getName()).willReturn(name);
LimboPlayer limboPlayer = mock(LimboPlayer.class);
given(limboCache.getLimboPlayer(name)).willReturn(limboPlayer);
PlayerData playerData = mock(PlayerData.class);
given(limboCache.getPlayerData(name)).willReturn(playerData);
given(settings.getProperty(RestrictionSettings.TIMEOUT)).willReturn(0);
// when
limboPlayerTaskManager.registerTimeoutTask(player);
playerDataTaskManager.registerTimeoutTask(player);
// then
verifyZeroInteractions(limboPlayer, bukkitService);
verifyZeroInteractions(playerData, bukkitService);
}
@Test
@ -196,20 +196,20 @@ public class LimboPlayerTaskManagerTest {
String name = "l33tPlayer";
Player player = mock(Player.class);
given(player.getName()).willReturn(name);
LimboPlayer limboPlayer = mock(LimboPlayer.class);
PlayerData playerData = mock(PlayerData.class);
BukkitTask existingTask = mock(BukkitTask.class);
given(limboPlayer.getTimeoutTask()).willReturn(existingTask);
given(limboCache.getLimboPlayer(name)).willReturn(limboPlayer);
given(playerData.getTimeoutTask()).willReturn(existingTask);
given(limboCache.getPlayerData(name)).willReturn(playerData);
given(settings.getProperty(RestrictionSettings.TIMEOUT)).willReturn(18);
BukkitTask bukkitTask = mock(BukkitTask.class);
given(bukkitService.runTaskLater(any(TimeoutTask.class), anyLong())).willReturn(bukkitTask);
// when
limboPlayerTaskManager.registerTimeoutTask(player);
playerDataTaskManager.registerTimeoutTask(player);
// then
verify(existingTask).cancel();
verify(limboPlayer).setTimeoutTask(bukkitTask);
verify(playerData).setTimeoutTask(bukkitTask);
verify(bukkitService).runTaskLater(any(TimeoutTask.class), eq(360L)); // 18 * TICKS_PER_SECOND
verify(messages).retrieveSingle(MessageKey.LOGIN_TIMEOUT_ERROR);
}

View File

@ -2,7 +2,7 @@ package fr.xephi.authme.util;
import fr.xephi.authme.cache.auth.PlayerAuth;
import fr.xephi.authme.cache.auth.PlayerCache;
import fr.xephi.authme.cache.limbo.LimboPlayer;
import fr.xephi.authme.cache.limbo.PlayerData;
import fr.xephi.authme.events.FirstSpawnTeleportEvent;
import fr.xephi.authme.events.SpawnTeleportEvent;
import fr.xephi.authme.settings.NewSetting;
@ -259,7 +259,7 @@ public class TeleportationServiceTest {
given(settings.getProperty(RestrictionSettings.NO_TELEPORT)).willReturn(true);
Player player = mock(Player.class);
PlayerAuth auth = mock(PlayerAuth.class);
LimboPlayer limbo = mock(LimboPlayer.class);
PlayerData limbo = mock(PlayerData.class);
// when
teleportationService.teleportOnLogin(player, auth, limbo);
@ -277,7 +277,7 @@ public class TeleportationServiceTest {
Location spawn = mockLocation();
given(spawnLoader.getSpawnLocation(player)).willReturn(spawn);
PlayerAuth auth = mock(PlayerAuth.class);
LimboPlayer limbo = mock(LimboPlayer.class);
PlayerData limbo = mock(PlayerData.class);
Location limboLocation = mockLocation();
given(limboLocation.getWorld().getName()).willReturn("forced1");
given(limbo.getLoc()).willReturn(limboLocation);
@ -301,7 +301,7 @@ public class TeleportationServiceTest {
Location spawn = mockLocation();
given(spawnLoader.getSpawnLocation(player)).willReturn(spawn);
PlayerAuth auth = mock(PlayerAuth.class);
LimboPlayer limbo = mock(LimboPlayer.class);
PlayerData limbo = mock(PlayerData.class);
Location limboLocation = mockLocation();
given(limboLocation.getWorld().getName()).willReturn("Forced1"); // different case
given(limbo.getLoc()).willReturn(limboLocation);
@ -328,7 +328,7 @@ public class TeleportationServiceTest {
Player player = mock(Player.class);
given(player.isOnline()).willReturn(true);
LimboPlayer limbo = mock(LimboPlayer.class);
PlayerData limbo = mock(PlayerData.class);
Location limboLocation = mockLocation();
given(limbo.getLoc()).willReturn(limboLocation);
@ -357,7 +357,7 @@ public class TeleportationServiceTest {
given(player.isOnline()).willReturn(true);
World world = mock(World.class);
given(player.getWorld()).willReturn(world);
LimboPlayer limbo = mock(LimboPlayer.class);
PlayerData limbo = mock(PlayerData.class);
Location limboLocation = mockLocation();
given(limbo.getLoc()).willReturn(limboLocation);
@ -385,7 +385,7 @@ public class TeleportationServiceTest {
given(player.isOnline()).willReturn(true);
World world = mock(World.class);
given(player.getWorld()).willReturn(world);
LimboPlayer limbo = mock(LimboPlayer.class);
PlayerData limbo = mock(PlayerData.class);
Location location = mockLocation();
given(limbo.getLoc()).willReturn(location);
@ -410,7 +410,7 @@ public class TeleportationServiceTest {
given(player.isOnline()).willReturn(true);
World world = mock(World.class);
given(player.getWorld()).willReturn(world);
LimboPlayer limbo = mock(LimboPlayer.class);
PlayerData limbo = mock(PlayerData.class);
Location location = mockLocation();
given(limbo.getLoc()).willReturn(location);