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;
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
if (Settings.delayJoinLeaveMessages && event.getJoinMessage() != null) {
joinMessage.put(name, event.getJoinMessage());
event.setJoinMessage(null);
}
// Shedule login task so works after the prelogin
// (Fix found by Koolaid5000)
Bukkit.getScheduler().runTask(plugin, new Runnable() {
@Override
public void run() {
if (delay)
joinMessage.put(name, joinMsg);
plugin.management.performJoin(player);
}
});

View File

@ -4,7 +4,6 @@ import org.bukkit.Bukkit;
import org.bukkit.GameMode;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.entity.Player;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;
@ -31,24 +30,26 @@ import fr.xephi.authme.util.Utils.GroupType;
public class AsyncronousJoin {
protected Player player;
protected DataSource database;
protected AuthMe plugin;
protected String name;
private Messages m = Messages.getInstance();
private final AuthMe plugin;
private final Player player;
private final DataSource database;
private final String name;
private final Messages m;
private final BukkitScheduler sched;
public AsyncronousJoin(Player player, AuthMe plugin, DataSource database) {
this.player = player;
this.plugin = plugin;
this.sched = plugin.getServer().getScheduler();
this.database = database;
this.name = player.getName().toLowerCase();
this.m = Messages.getInstance();
}
public void process() {
if (AuthMePlayerListener.gameMode.containsKey(name))
AuthMePlayerListener.gameMode.remove(name);
AuthMePlayerListener.gameMode.putIfAbsent(name, player.getGameMode());
BukkitScheduler sched = plugin.getServer().getScheduler();
if (Utils.isNPC(player) || Utils.isUnrestricted(player)) {
return;
@ -166,7 +167,7 @@ public class AsyncronousJoin {
return;
}
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() {
@Override
@ -185,42 +186,34 @@ public class AsyncronousJoin {
}
}
String[] msg;
if (Settings.emailRegistration) {
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))
if (!LimboCache.getInstance().hasLimboPlayer(name)) {
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);
}
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
public void run() {
if (player.isOp())
player.setOp(false);
if (player.getGameMode() != GameMode.CREATIVE && !Settings.isMovementAllowed) {
if (!Settings.isMovementAllowed) {
player.setAllowFlight(true);
player.setFlying(true);
}
player.setNoDamageTicks(Settings.getRegistrationTimeout * 20);
if (Settings.useEssentialsMotd)
player.setNoDamageTicks(timeOut);
if (Settings.useEssentialsMotd) {
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) {
player.setWalkSpeed(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 (plugin.sessions.containsKey(name))
if (plugin.sessions.containsKey(name)) {
plugin.sessions.get(name).cancel();
plugin.sessions.remove(name);
}
PlayerAuth auth = database.getAuth(name);
database.setUnlogged(name);
PlayerCache.getInstance().removePlayer(name);
if (auth != null && auth.getIp().equals(ip)) {
m.send(player, "valid_session");
PlayerCache.getInstance().removePlayer(name);
database.setUnlogged(name);
plugin.management.performLogin(player, "dontneed", true);
return;
} else if (Settings.sessionExpireOnIpChange) {
PlayerCache.getInstance().removePlayer(name);
database.setUnlogged(name);
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())
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;
FirstSpawnTeleportEvent tpEvent = new FirstSpawnTeleportEvent(player, player.getLocation(), Spawn.getInstance().getFirstSpawn());
FirstSpawnTeleportEvent tpEvent = new FirstSpawnTeleportEvent(player, player.getLocation(), firstspawn);
plugin.getServer().getPluginManager().callEvent(tpEvent);
if (!tpEvent.isCancelled()) {
if (player.isOnline() && tpEvent.getTo() != null && tpEvent.getTo().getWorld() != null) {
@ -270,11 +267,9 @@ public class AsyncronousJoin {
}
}
return true;
}
private void placePlayerSafely(final Player player,
final Location spawnLoc) {
private void placePlayerSafely(final Player player, final Location spawnLoc) {
if (spawnLoc == null)
return;
if (!Settings.noTeleport)
@ -283,26 +278,19 @@ public class AsyncronousJoin {
return;
if (!player.hasPlayedBefore())
return;
Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
sched.scheduleSyncDelayedTask(plugin, new Runnable() {
@Override
public void run() {
Location loc = null;
Block b = player.getLocation().getBlock();
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;
if (spawnLoc.getWorld() == null) {
return;
}
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);
}
});