unregister asynchronously

This commit is contained in:
DNx5 2015-09-19 12:51:59 +07:00
parent b5546c07b4
commit a00deb0e0f

View File

@ -1,18 +1,5 @@
package fr.xephi.authme.commands;
import java.security.NoSuchAlgorithmException;
import org.bukkit.Location;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;
import org.bukkit.scheduler.BukkitScheduler;
import org.bukkit.scheduler.BukkitTask;
import fr.xephi.authme.AuthMe;
import fr.xephi.authme.ConsoleLogger;
import fr.xephi.authme.Utils;
@ -26,6 +13,18 @@ import fr.xephi.authme.settings.Messages;
import fr.xephi.authme.settings.Settings;
import fr.xephi.authme.task.MessageTask;
import fr.xephi.authme.task.TimeoutTask;
import org.bukkit.Location;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;
import org.bukkit.scheduler.BukkitScheduler;
import org.bukkit.scheduler.BukkitTask;
import java.security.NoSuchAlgorithmException;
public class UnregisterCommand implements CommandExecutor {
@ -39,8 +38,8 @@ public class UnregisterCommand implements CommandExecutor {
}
@Override
public boolean onCommand(CommandSender sender, Command cmnd, String label,
String[] args) {
public boolean onCommand(final CommandSender sender, Command cmnd, String label,
final String[] args) {
if (!(sender instanceof Player)) {
return true;
}
@ -50,8 +49,8 @@ public class UnregisterCommand implements CommandExecutor {
return true;
}
Player player = (Player) sender;
String name = player.getName().toLowerCase();
final Player player = (Player) sender;
final String name = player.getName().toLowerCase();
if (!PlayerCache.getInstance().isAuthenticated(name)) {
m.send(player, "not_logged_in");
@ -62,72 +61,77 @@ public class UnregisterCommand implements CommandExecutor {
m.send(player, "usage_unreg");
return true;
}
try {
if (PasswordSecurity.comparePasswordWithHash(args[0], PlayerCache.getInstance().getAuth(name).getHash(), player.getName())) {
if (!plugin.database.removeAuth(name)) {
player.sendMessage("error");
return true;
}
if (Settings.isForcedRegistrationEnabled) {
if (Settings.isTeleportToSpawnEnabled && !Settings.noTeleport) {
Location spawn = plugin.getSpawnLocation(player);
SpawnTeleportEvent tpEvent = new SpawnTeleportEvent(player, player.getLocation(), spawn, false);
plugin.getServer().getPluginManager().callEvent(tpEvent);
if (!tpEvent.isCancelled()) {
player.teleport(tpEvent.getTo());
plugin.getServer().getScheduler().runTaskAsynchronously(plugin, new Runnable() {
@Override
public void run() {
try {
if (PasswordSecurity.comparePasswordWithHash(args[0], PlayerCache.getInstance().getAuth(name).getHash(), player.getName())) {
if (!plugin.database.removeAuth(name)) {
player.sendMessage("error");
return;
}
}
player.getInventory().setContents(new ItemStack[36]);
player.getInventory().setArmorContents(new ItemStack[4]);
player.saveData();
PlayerCache.getInstance().removePlayer(player.getName().toLowerCase());
if (!Settings.getRegisteredGroup.isEmpty())
Utils.getInstance().setGroup(player, GroupType.UNREGISTERED);
LimboCache.getInstance().addLimboPlayer(player);
int delay = Settings.getRegistrationTimeout * 20;
int interval = Settings.getWarnMessageInterval;
BukkitScheduler sched = sender.getServer().getScheduler();
if (delay != 0) {
BukkitTask id = sched.runTaskLaterAsynchronously(plugin, new TimeoutTask(plugin, name, player), delay);
LimboCache.getInstance().getLimboPlayer(name).setTimeoutTaskId(id);
}
LimboCache.getInstance().getLimboPlayer(name).setMessageTaskId(sched.runTaskAsynchronously(plugin, new MessageTask(plugin, name, m.send("reg_msg"), interval)));
m.send(player, "unregistered");
ConsoleLogger.info(player.getDisplayName() + " unregistered himself");
return true;
}
if (!Settings.unRegisteredGroup.isEmpty()) {
Utils.getInstance().setGroup(player, Utils.GroupType.UNREGISTERED);
}
PlayerCache.getInstance().removePlayer(player.getName().toLowerCase());
// check if Player cache File Exist and delete it, preventing
// duplication of items
if (playerCache.doesCacheExist(player)) {
playerCache.removeCache(player);
}
if (Settings.applyBlindEffect)
player.addPotionEffect(new PotionEffect(PotionEffectType.BLINDNESS, Settings.getRegistrationTimeout * 20, 2));
m.send(player, "unregistered");
ConsoleLogger.info(player.getDisplayName() + " unregistered himself");
if (Settings.isTeleportToSpawnEnabled && !Settings.noTeleport) {
Location spawn = plugin.getSpawnLocation(player);
SpawnTeleportEvent tpEvent = new SpawnTeleportEvent(player, player.getLocation(), spawn, false);
plugin.getServer().getPluginManager().callEvent(tpEvent);
if (!tpEvent.isCancelled()) {
if (!tpEvent.getTo().getWorld().getChunkAt(tpEvent.getTo()).isLoaded()) {
tpEvent.getTo().getWorld().getChunkAt(tpEvent.getTo()).load();
if (Settings.isForcedRegistrationEnabled) {
if (Settings.isTeleportToSpawnEnabled && !Settings.noTeleport) {
Location spawn = plugin.getSpawnLocation(player);
SpawnTeleportEvent tpEvent = new SpawnTeleportEvent(player, player.getLocation(), spawn, false);
plugin.getServer().getPluginManager().callEvent(tpEvent);
if (!tpEvent.isCancelled()) {
player.teleport(tpEvent.getTo());
}
}
player.getInventory().setContents(new ItemStack[36]);
player.getInventory().setArmorContents(new ItemStack[4]);
player.saveData();
PlayerCache.getInstance().removePlayer(player.getName().toLowerCase());
if (!Settings.getRegisteredGroup.isEmpty())
Utils.getInstance().setGroup(player, GroupType.UNREGISTERED);
LimboCache.getInstance().addLimboPlayer(player);
int delay = Settings.getRegistrationTimeout * 20;
int interval = Settings.getWarnMessageInterval;
BukkitScheduler sched = sender.getServer().getScheduler();
if (delay != 0) {
BukkitTask id = sched.runTaskLaterAsynchronously(plugin, new TimeoutTask(plugin, name, player), delay);
LimboCache.getInstance().getLimboPlayer(name).setTimeoutTaskId(id);
}
LimboCache.getInstance().getLimboPlayer(name).setMessageTaskId(sched.runTaskAsynchronously(plugin, new MessageTask(plugin, name, m.send("reg_msg"), interval)));
m.send(player, "unregistered");
ConsoleLogger.info(player.getDisplayName() + " unregistered himself");
return;
}
player.teleport(tpEvent.getTo());
if (!Settings.unRegisteredGroup.isEmpty()) {
Utils.getInstance().setGroup(player, Utils.GroupType.UNREGISTERED);
}
PlayerCache.getInstance().removePlayer(player.getName().toLowerCase());
// check if Player cache File Exist and delete it, preventing
// duplication of items
if (playerCache.doesCacheExist(player)) {
playerCache.removeCache(player);
}
if (Settings.applyBlindEffect)
player.addPotionEffect(new PotionEffect(PotionEffectType.BLINDNESS, Settings.getRegistrationTimeout * 20, 2));
m.send(player, "unregistered");
ConsoleLogger.info(player.getDisplayName() + " unregistered himself");
if (Settings.isTeleportToSpawnEnabled && !Settings.noTeleport) {
Location spawn = plugin.getSpawnLocation(player);
SpawnTeleportEvent tpEvent = new SpawnTeleportEvent(player, player.getLocation(), spawn, false);
plugin.getServer().getPluginManager().callEvent(tpEvent);
if (!tpEvent.isCancelled()) {
if (!tpEvent.getTo().getWorld().getChunkAt(tpEvent.getTo()).isLoaded()) {
tpEvent.getTo().getWorld().getChunkAt(tpEvent.getTo()).load();
}
player.teleport(tpEvent.getTo());
}
}
return;
} else {
m.send(player, "wrong_pwd");
}
} catch (NoSuchAlgorithmException ex) {
ConsoleLogger.showError(ex.getMessage());
sender.sendMessage("Internal Error please read the server log");
}
return true;
} else {
m.send(player, "wrong_pwd");
}
} catch (NoSuchAlgorithmException ex) {
ConsoleLogger.showError(ex.getMessage());
sender.sendMessage("Internal Error please read the server log");
}
});
return true;
}
}