optimize some code.

This commit is contained in:
DNx5 2015-11-17 06:54:24 +07:00
parent f6c6a7c6d4
commit 6bc5bb3cb1
2 changed files with 58 additions and 68 deletions

View File

@ -201,20 +201,22 @@ public class AuthMePlayerListener implements Listener {
return; return;
final Player player = event.getPlayer(); final Player player = event.getPlayer();
String name = player.getName().toLowerCase(); final String name = player.getName().toLowerCase();
final String joinMsg = event.getJoinMessage();
final boolean delay = Settings.delayJoinLeaveMessages && joinMsg != null;
// Remove the join message while the player isn't logging in // Remove the join message while the player isn't logging in
if (Settings.delayJoinLeaveMessages && event.getJoinMessage() != null) { if (Settings.delayJoinLeaveMessages && event.getJoinMessage() != null) {
joinMessage.put(name, event.getJoinMessage());
event.setJoinMessage(null); event.setJoinMessage(null);
} }
// Shedule login task so works after the prelogin // Shedule login task so works after the prelogin
// (Fix found by Koolaid5000) // (Fix found by Koolaid5000)
Bukkit.getScheduler().runTask(plugin, new Runnable() { Bukkit.getScheduler().runTask(plugin, new Runnable() {
@Override @Override
public void run() { public void run() {
if (delay)
joinMessage.put(name, joinMsg);
plugin.management.performJoin(player); plugin.management.performJoin(player);
} }
}); });

View File

@ -4,7 +4,6 @@ import org.bukkit.Bukkit;
import org.bukkit.GameMode; import org.bukkit.GameMode;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.potion.PotionEffect; import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType; import org.bukkit.potion.PotionEffectType;
@ -31,24 +30,26 @@ import fr.xephi.authme.util.Utils.GroupType;
public class AsyncronousJoin { public class AsyncronousJoin {
protected Player player; private final AuthMe plugin;
protected DataSource database; private final Player player;
protected AuthMe plugin; private final DataSource database;
protected String name; private final String name;
private Messages m = Messages.getInstance(); private final Messages m;
private final BukkitScheduler sched;
public AsyncronousJoin(Player player, AuthMe plugin, DataSource database) { public AsyncronousJoin(Player player, AuthMe plugin, DataSource database) {
this.player = player; this.player = player;
this.plugin = plugin; this.plugin = plugin;
this.sched = plugin.getServer().getScheduler();
this.database = database; this.database = database;
this.name = player.getName().toLowerCase(); this.name = player.getName().toLowerCase();
this.m = Messages.getInstance();
} }
public void process() { public void process() {
if (AuthMePlayerListener.gameMode.containsKey(name)) if (AuthMePlayerListener.gameMode.containsKey(name))
AuthMePlayerListener.gameMode.remove(name); AuthMePlayerListener.gameMode.remove(name);
AuthMePlayerListener.gameMode.putIfAbsent(name, player.getGameMode()); AuthMePlayerListener.gameMode.putIfAbsent(name, player.getGameMode());
BukkitScheduler sched = plugin.getServer().getScheduler();
if (Utils.isNPC(player) || Utils.isUnrestricted(player)) { if (Utils.isNPC(player) || Utils.isUnrestricted(player)) {
return; return;
@ -166,7 +167,7 @@ public class AsyncronousJoin {
return; return;
} }
if (!Settings.noTeleport) if (!Settings.noTeleport)
if (!needFirstspawn() && Settings.isTeleportToSpawnEnabled || (Settings.isForceSpawnLocOnJoinEnabled && Settings.getForcedWorlds.contains(player.getWorld().getName()))) { if (!needFirstSpawn() && Settings.isTeleportToSpawnEnabled || (Settings.isForceSpawnLocOnJoinEnabled && Settings.getForcedWorlds.contains(player.getWorld().getName()))) {
sched.scheduleSyncDelayedTask(plugin, new Runnable() { sched.scheduleSyncDelayedTask(plugin, new Runnable() {
@Override @Override
@ -185,42 +186,34 @@ public class AsyncronousJoin {
} }
} }
String[] msg;
if (Settings.emailRegistration) { if (!LimboCache.getInstance().hasLimboPlayer(name)) {
msg = isAuthAvailable ? m.send("login_msg") : m.send("reg_email_msg");
} else {
msg = isAuthAvailable ? m.send("login_msg") : m.send("reg_msg");
}
int time = Settings.getRegistrationTimeout * 20;
int msgInterval = Settings.getWarnMessageInterval;
if (time != 0) {
BukkitTask id = sched.runTaskLaterAsynchronously(plugin, new TimeoutTask(plugin, name, player), time);
if (!LimboCache.getInstance().hasLimboPlayer(name))
LimboCache.getInstance().addLimboPlayer(player); LimboCache.getInstance().addLimboPlayer(player);
}
final int timeOut = Settings.getRegistrationTimeout * 20;
int msgInterval = Settings.getWarnMessageInterval;
if (timeOut > 0) {
BukkitTask id = sched.runTaskLaterAsynchronously(plugin, new TimeoutTask(plugin, name, player), timeOut);
LimboCache.getInstance().getLimboPlayer(name).setTimeoutTaskId(id); LimboCache.getInstance().getLimboPlayer(name).setTimeoutTaskId(id);
} }
if (!LimboCache.getInstance().hasLimboPlayer(name))
LimboCache.getInstance().addLimboPlayer(player);
if (isAuthAvailable) {
Utils.setGroup(player, GroupType.NOTLOGGEDIN);
} else {
Utils.setGroup(player, GroupType.UNREGISTERED);
}
sched.scheduleSyncDelayedTask(plugin, new Runnable() {
Utils.setGroup(player, isAuthAvailable ? GroupType.NOTLOGGEDIN : GroupType.UNREGISTERED);
sched.scheduleSyncDelayedTask(plugin, new Runnable() {
@Override @Override
public void run() { public void run() {
if (player.isOp())
player.setOp(false); player.setOp(false);
if (player.getGameMode() != GameMode.CREATIVE && !Settings.isMovementAllowed) { if (!Settings.isMovementAllowed) {
player.setAllowFlight(true); player.setAllowFlight(true);
player.setFlying(true); player.setFlying(true);
} }
player.setNoDamageTicks(Settings.getRegistrationTimeout * 20); player.setNoDamageTicks(timeOut);
if (Settings.useEssentialsMotd) if (Settings.useEssentialsMotd) {
player.performCommand("motd"); player.performCommand("motd");
if (Settings.applyBlindEffect) }
player.addPotionEffect(new PotionEffect(PotionEffectType.BLINDNESS, Settings.getRegistrationTimeout * 20, 2)); if (Settings.applyBlindEffect) {
player.addPotionEffect(new PotionEffect(PotionEffectType.BLINDNESS, timeOut, 2));
}
if (!Settings.isMovementAllowed && Settings.isRemoveSpeedEnabled) { if (!Settings.isMovementAllowed && Settings.isRemoveSpeedEnabled) {
player.setWalkSpeed(0.0f); player.setWalkSpeed(0.0f);
player.setFlySpeed(0.0f); player.setFlySpeed(0.0f);
@ -228,33 +221,37 @@ public class AsyncronousJoin {
} }
}); });
if (Settings.isSessionsEnabled && isAuthAvailable && (PlayerCache.getInstance().isAuthenticated(name) || database.isLogged(name))) { if (Settings.isSessionsEnabled && isAuthAvailable && (PlayerCache.getInstance().isAuthenticated(name) || database.isLogged(name))) {
if (plugin.sessions.containsKey(name)) if (plugin.sessions.containsKey(name)) {
plugin.sessions.get(name).cancel(); plugin.sessions.get(name).cancel();
plugin.sessions.remove(name); plugin.sessions.remove(name);
}
PlayerAuth auth = database.getAuth(name); PlayerAuth auth = database.getAuth(name);
database.setUnlogged(name);
PlayerCache.getInstance().removePlayer(name);
if (auth != null && auth.getIp().equals(ip)) { if (auth != null && auth.getIp().equals(ip)) {
m.send(player, "valid_session"); m.send(player, "valid_session");
PlayerCache.getInstance().removePlayer(name);
database.setUnlogged(name);
plugin.management.performLogin(player, "dontneed", true); plugin.management.performLogin(player, "dontneed", true);
return;
} else if (Settings.sessionExpireOnIpChange) { } else if (Settings.sessionExpireOnIpChange) {
PlayerCache.getInstance().removePlayer(name);
database.setUnlogged(name);
m.send(player, "invalid_session"); m.send(player, "invalid_session");
} }
return;
}
BukkitTask msgT = sched.runTaskAsynchronously(plugin, new MessageTask(plugin, name, msg, msgInterval));
LimboCache.getInstance().getLimboPlayer(name).setMessageTaskId(msgT);
} }
private boolean needFirstspawn() { String[] msg = isAuthAvailable ? m.send("login_msg") :
m.send("reg_" + (Settings.emailRegistration? "email_" : "") + "msg");
BukkitTask msgTask = sched.runTaskAsynchronously(plugin, new MessageTask(plugin, name, msg, msgInterval));
LimboCache.getInstance().getLimboPlayer(name).setMessageTaskId(msgTask);
}
private boolean needFirstSpawn() {
if (player.hasPlayedBefore()) if (player.hasPlayedBefore())
return false; return false;
if (Spawn.getInstance().getFirstSpawn() == null || Spawn.getInstance().getFirstSpawn().getWorld() == null) Location firstspawn = Spawn.getInstance().getFirstSpawn();
if (firstspawn == null || firstspawn.getWorld() == null)
return false; return false;
FirstSpawnTeleportEvent tpEvent = new FirstSpawnTeleportEvent(player, player.getLocation(), Spawn.getInstance().getFirstSpawn()); FirstSpawnTeleportEvent tpEvent = new FirstSpawnTeleportEvent(player, player.getLocation(), firstspawn);
plugin.getServer().getPluginManager().callEvent(tpEvent); plugin.getServer().getPluginManager().callEvent(tpEvent);
if (!tpEvent.isCancelled()) { if (!tpEvent.isCancelled()) {
if (player.isOnline() && tpEvent.getTo() != null && tpEvent.getTo().getWorld() != null) { if (player.isOnline() && tpEvent.getTo() != null && tpEvent.getTo().getWorld() != null) {
@ -270,11 +267,9 @@ public class AsyncronousJoin {
} }
} }
return true; return true;
} }
private void placePlayerSafely(final Player player, private void placePlayerSafely(final Player player, final Location spawnLoc) {
final Location spawnLoc) {
if (spawnLoc == null) if (spawnLoc == null)
return; return;
if (!Settings.noTeleport) if (!Settings.noTeleport)
@ -283,26 +278,19 @@ public class AsyncronousJoin {
return; return;
if (!player.hasPlayedBefore()) if (!player.hasPlayedBefore())
return; return;
Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() { sched.scheduleSyncDelayedTask(plugin, new Runnable() {
@Override @Override
public void run() { public void run() {
Location loc = null; if (spawnLoc.getWorld() == null) {
Block b = player.getLocation().getBlock(); return;
if (b.getType() == Material.PORTAL || b.getType() == Material.ENDER_PORTAL) {
m.send(player, "unsafe_spawn");
if (spawnLoc.getWorld() != null)
loc = spawnLoc;
} else {
Block c = player.getLocation().add(0D, 1D, 0D).getBlock();
if (c.getType() == Material.PORTAL || c.getType() == Material.ENDER_PORTAL) {
m.send(player, "unsafe_spawn");
if (spawnLoc.getWorld() != null)
loc = spawnLoc;
} }
Material cur = player.getLocation().getBlock().getType();
Material top = player.getLocation().add(0D, 1D, 0D).getBlock().getType();
if (cur == Material.PORTAL || cur == Material.ENDER_PORTAL
|| top == Material.PORTAL || top == Material.ENDER_PORTAL) {
m.send(player, "unsafe_spawn");
player.teleport(spawnLoc);
} }
if (loc != null)
player.teleport(loc);
} }
}); });