LoginSystem/src/main/java/fr/xephi/authme/process/login/ProcessSyncronousPlayerLogin.java
Xephi b659d8968e Update 3.3.4
//Changes 3.3.4://
* Add an isLogged column in mySQL
* Add a maxLoginPerIp
* Add a maxJoinPerIp
* Add a way to force kick after register
* Add a way to force login after register
* Update session correctly
* Fix Change Email command
* Fix some perm problems
* Fix some problems with email sending
* Remove some dead code
* Add a way to control spawn priority, by default , in order, it is :
authme,essentials,multiverse,default
2014-03-08 00:16:14 +01:00

203 lines
7.9 KiB
Java

package fr.xephi.authme.process.login;
import org.bukkit.Bukkit;
import org.bukkit.GameMode;
import org.bukkit.Location;
import org.bukkit.entity.Player;
import org.bukkit.plugin.PluginManager;
import fr.xephi.authme.AuthMe;
import fr.xephi.authme.Utils;
import fr.xephi.authme.Utils.groupType;
import fr.xephi.authme.api.API;
import fr.xephi.authme.cache.auth.PlayerAuth;
import fr.xephi.authme.cache.backup.FileCache;
import fr.xephi.authme.cache.limbo.LimboCache;
import fr.xephi.authme.cache.limbo.LimboPlayer;
import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.events.AuthMeTeleportEvent;
import fr.xephi.authme.events.LoginEvent;
import fr.xephi.authme.events.RestoreInventoryEvent;
import fr.xephi.authme.events.SpawnTeleportEvent;
import fr.xephi.authme.listener.AuthMePlayerListener;
import fr.xephi.authme.settings.Settings;
public class ProcessSyncronousPlayerLogin implements Runnable {
private LimboPlayer limbo;
private Player player;
private String name;
private PlayerAuth auth;
private AuthMe plugin;
private DataSource database;
private PluginManager pm;
private FileCache playerCache = new FileCache();
public ProcessSyncronousPlayerLogin(Player player, AuthMe plugin, DataSource data) {
this.plugin = plugin;
this.database = data;
this.pm = plugin.getServer().getPluginManager();
this.player = player;
this.name = player.getName().toLowerCase();
this.limbo = LimboCache.getInstance().getLimboPlayer(name);
this.auth = database.getAuth(name);
}
public LimboPlayer getLimbo() {
return limbo;
}
protected void restoreOpState() {
player.setOp(limbo.getOperator());
if (player.getGameMode() != GameMode.CREATIVE && !Settings.isMovementAllowed) {
player.setAllowFlight(limbo.isFlying());
player.setFlying(limbo.isFlying());
}
}
protected void packQuitLocation() {
Utils.getInstance().packCoords(auth.getQuitLocX(), auth.getQuitLocY(), auth.getQuitLocZ(), auth.getWorld(), player);
}
protected void teleportBackFromSpawn() {
AuthMeTeleportEvent tpEvent = new AuthMeTeleportEvent(player, limbo.getLoc());
pm.callEvent(tpEvent);
if (!tpEvent.isCancelled()) {
Location fLoc = tpEvent.getTo();
if (!fLoc.getChunk().isLoaded()) {
fLoc.getChunk().load();
}
player.teleport(fLoc);
}
}
protected void teleportToSpawn() {
Location spawnL = plugin.getSpawnLocation(player, player.getWorld());
SpawnTeleportEvent tpEvent = new SpawnTeleportEvent(player, player.getLocation(), spawnL, true);
pm.callEvent(tpEvent);
if (!tpEvent.isCancelled()) {
Location fLoc = tpEvent.getTo();
if (!fLoc.getChunk().isLoaded()) {
fLoc.getChunk().load();
}
player.teleport(fLoc);
}
}
protected void restoreInventory() {
RestoreInventoryEvent event = new RestoreInventoryEvent(player, limbo.getInventory(), limbo.getArmour());
Bukkit.getServer().getPluginManager().callEvent(event);
if (!event.isCancelled()) {
API.setPlayerInventory(player, event.getInventory(), event.getArmor());
}
}
protected void forceCommands() {
for (String command : Settings.forceCommands) {
try {
player.performCommand(command.replace("%p", player.getName()));
} catch (Exception e) {}
}
}
@Override
public void run() {
// Limbo contains the State of the Player before /login
if (limbo != null) {
// Op & Flying
restoreOpState();
/*
* Restore Inventories and GameMode
* We need to restore them before teleport the player
* Cause in AuthMePlayerListener, we call ProtectInventoryEvent after Teleporting
* Also it's the current world inventory !
*/
if (!Settings.forceOnlyAfterLogin) {
player.setGameMode(limbo.getGameMode());
// Inventory - Make it after restore GameMode , cause we need to restore the
// right inventory in the right gamemode
if (Settings.protectInventoryBeforeLogInEnabled && player.hasPlayedBefore()) {
restoreInventory();
}
}
else {
// Inventory - Make it before force the survival GameMode to cancel all
// inventory problem
if (Settings.protectInventoryBeforeLogInEnabled && player.hasPlayedBefore()) {
restoreInventory();
}
player.setGameMode(GameMode.SURVIVAL);
}
// Teleport the player
if(Settings.isForceSpawnLocOnJoinEnabled && Settings.getForcedWorlds.contains(player.getWorld().getName())) {
// If we have force the spawn location on join
teleportToSpawn();
} else {
if (Settings.isTeleportToSpawnEnabled) {
// If and only if teleport unauthed to spawn is activate
teleportBackFromSpawn();
} else {
if (Settings.isSaveQuitLocationEnabled && auth.getQuitLocY() != 0) {
// Teleport the player on the saved location
packQuitLocation();
} else {
// Do not move the player from his position
}
}
}
// Teleport
if (Settings.isTeleportToSpawnEnabled && !Settings.isForceSpawnLocOnJoinEnabled && Settings.getForcedWorlds.contains(player.getWorld().getName())) {
if (Settings.isSaveQuitLocationEnabled && auth.getQuitLocY() != 0) {
packQuitLocation();
} else {
teleportBackFromSpawn();
}
} else if (Settings.isForceSpawnLocOnJoinEnabled && Settings.getForcedWorlds.contains(player.getWorld().getName())) {
teleportToSpawn();
} else if (Settings.isSaveQuitLocationEnabled && auth.getQuitLocY() != 0) {
packQuitLocation();
} else {
teleportBackFromSpawn();
}
// Re-Force Survival GameMode if we need due to world change specification
if (Settings.isForceSurvivalModeEnabled)
Utils.forceGM(player);
// Restore Permission Group
Utils.getInstance().setGroup(player, groupType.LOGGEDIN);
// Cleanup no longer used temporary data
LimboCache.getInstance().deleteLimboPlayer(name);
if (playerCache.doesCacheExist(name)) {
playerCache.removeCache(name);
}
}
// We can now display the join message
if (AuthMePlayerListener.joinMessage.containsKey(name) && AuthMePlayerListener.joinMessage.get(name) != null && !AuthMePlayerListener.joinMessage.get(name).isEmpty()) {
for (Player p : Bukkit.getServer().getOnlinePlayers()) {
if (p.isOnline())
p.sendMessage(AuthMePlayerListener.joinMessage.get(name));
}
AuthMePlayerListener.joinMessage.remove(name);
}
// The Loginevent now fires (as intended) after everything is processed
Bukkit.getServer().getPluginManager().callEvent(new LoginEvent(player, true));
player.saveData();
// Login is finish, display welcome message
if(Settings.useWelcomeMessage)
if(Settings.broadcastWelcomeMessage) {
for (String s : Settings.welcomeMsg) {
Bukkit.getServer().broadcastMessage(plugin.replaceAllInfos(s, player));
}
} else {
for (String s : Settings.welcomeMsg) {
player.sendMessage(plugin.replaceAllInfos(s, player));
}
}
// Login is now finish , we can force all commands
forceCommands();
}
}