From 25c23e144c1cb48fa0a1d0cc6336d66ee1213979 Mon Sep 17 00:00:00 2001 From: DNx5 Date: Tue, 24 Nov 2015 23:13:51 +0700 Subject: [PATCH] - send blank inventory on logout if protect inventory is enabled. - added reload support for protect inventory option. --- src/main/java/fr/xephi/authme/AuthMe.java | 17 +++++++++---- .../AuthMeInventoryPacketAdapter.java | 25 ++++++++++++++++--- .../login/ProcessSyncronousPlayerLogin.java | 11 +++----- .../process/logout/AsynchronousLogout.java | 7 +++--- .../logout/ProcessSyncronousPlayerLogout.java | 14 +++++++---- .../fr/xephi/authme/settings/Settings.java | 6 +++++ 6 files changed, 56 insertions(+), 24 deletions(-) diff --git a/src/main/java/fr/xephi/authme/AuthMe.java b/src/main/java/fr/xephi/authme/AuthMe.java index f8e3f28e0..37d574440 100644 --- a/src/main/java/fr/xephi/authme/AuthMe.java +++ b/src/main/java/fr/xephi/authme/AuthMe.java @@ -1,5 +1,6 @@ package fr.xephi.authme; +import com.comphenix.protocol.ProtocolLibrary; import com.earth2me.essentials.Essentials; import com.onarandombox.MultiverseCore.MultiverseCore; import fr.xephi.authme.api.API; @@ -687,14 +688,20 @@ public class AuthMe extends JavaPlugin { // Check the presence of the ProtocolLib plugin public void checkProtocolLib() { - if (Settings.protectInventoryBeforeLogInEnabled) { - if (server.getPluginManager().isPluginEnabled("ProtocolLib")) { - inventoryProtector = new AuthMeInventoryPacketAdapter(this); - inventoryProtector.register(); - } else { + if (!server.getPluginManager().isPluginEnabled("ProtocolLib")) { + if (Settings.protectInventoryBeforeLogInEnabled) { ConsoleLogger.showError("WARNING!!! The protectInventory feature requires ProtocolLib! Disabling it..."); Settings.protectInventoryBeforeLogInEnabled = false; } + return; + } + + if (Settings.protectInventoryBeforeLogInEnabled) { + inventoryProtector = new AuthMeInventoryPacketAdapter(this); + inventoryProtector.register(); + } else if (inventoryProtector != null) { + ProtocolLibrary.getProtocolManager().removePacketListener(inventoryProtector); + inventoryProtector = null; } } diff --git a/src/main/java/fr/xephi/authme/listener/AuthMeInventoryPacketAdapter.java b/src/main/java/fr/xephi/authme/listener/AuthMeInventoryPacketAdapter.java index 96495fdb1..388f4049c 100644 --- a/src/main/java/fr/xephi/authme/listener/AuthMeInventoryPacketAdapter.java +++ b/src/main/java/fr/xephi/authme/listener/AuthMeInventoryPacketAdapter.java @@ -39,9 +39,11 @@ import java.util.logging.Level; public class AuthMeInventoryPacketAdapter extends PacketAdapter { private static final int PLAYER_INVENTORY = 0; - //http://wiki.vg/Inventory#Inventory (0-4 crafting, 5-8 armor, 9-35 main inventory, 36-44 inventory) - //+1 because an index starts with 0 - private static final int PLAYER_CRAFTING_SIZE = 5; + // http://wiki.vg/Inventory#Inventory (0-4 crafting, 5-8 armor, 9-35 main inventory, 36-44 hotbar) + // +1 because an index starts with 0 + private static final int CRAFTING_SIZE = 5; + private static final int ARMOR_SIZE = 4; + private static final int MAIN_SIZE = 27; private static final int HOTBAR_SIZE = 9; /** @@ -88,7 +90,7 @@ public class AuthMeInventoryPacketAdapter extends PacketAdapter { //we are sending our own inventory inventoryPacket.getIntegers().write(0, PLAYER_INVENTORY); - ItemStack[] playerCrafting = new ItemStack[PLAYER_CRAFTING_SIZE]; + ItemStack[] playerCrafting = new ItemStack[CRAFTING_SIZE]; Arrays.fill(playerCrafting, new ItemStack(Material.AIR)); ItemStack[] armorContents = player.getInventory().getArmorContents(); ItemStack[] mainInventory = player.getInventory().getContents(); @@ -120,4 +122,19 @@ public class AuthMeInventoryPacketAdapter extends PacketAdapter { plugin.getLogger().log(Level.WARNING, "Error during inventory recovery", invocationExc); } } + + public void sendBlankInventoryPacket(Player player) { + ProtocolManager protocolManager = ProtocolLibrary.getProtocolManager(); + PacketContainer inventoryPacket = protocolManager.createPacket(PacketType.Play.Server.WINDOW_ITEMS); + inventoryPacket.getIntegers().write(0, PLAYER_INVENTORY); + int inventorySize = CRAFTING_SIZE + ARMOR_SIZE + MAIN_SIZE; + ItemStack[] blankInventory = new ItemStack[inventorySize]; + Arrays.fill(blankInventory, new ItemStack(Material.AIR)); + inventoryPacket.getItemArrayModifier().write(0, blankInventory); + try { + protocolManager.sendServerPacket(player, inventoryPacket, false); + } catch (InvocationTargetException invocationExc) { + plugin.getLogger().log(Level.WARNING, "Error during sending blank inventory", invocationExc); + } + } } diff --git a/src/main/java/fr/xephi/authme/process/login/ProcessSyncronousPlayerLogin.java b/src/main/java/fr/xephi/authme/process/login/ProcessSyncronousPlayerLogin.java index 1b705d8aa..96f65ddc0 100644 --- a/src/main/java/fr/xephi/authme/process/login/ProcessSyncronousPlayerLogin.java +++ b/src/main/java/fr/xephi/authme/process/login/ProcessSyncronousPlayerLogin.java @@ -98,18 +98,15 @@ public class ProcessSyncronousPlayerLogin implements Runnable { protected void restoreInventory() { RestoreInventoryEvent event = new RestoreInventoryEvent(player); - Bukkit.getServer().getPluginManager().callEvent(event); - if (!event.isCancelled()) { + pm.callEvent(event); + if (!event.isCancelled() && plugin.inventoryProtector != null) { plugin.inventoryProtector.sendInventoryPacket(player); } } protected void forceCommands() { for (String command : Settings.forceCommands) { - try { - player.performCommand(command.replace("%p", player.getName())); - } catch (Exception ignored) { - } + player.performCommand(command.replace("%p", player.getName())); } for (String command : Settings.forceCommandsAsConsole) { Bukkit.getServer().dispatchCommand(Bukkit.getServer().getConsoleSender(), command.replace("%p", player.getName())); @@ -154,7 +151,7 @@ public class ProcessSyncronousPlayerLogin implements Runnable { restoreFlyghtState(); - if (Settings.protectInventoryBeforeLogInEnabled && plugin.inventoryProtector != null) { + if (Settings.protectInventoryBeforeLogInEnabled) { restoreInventory(); } diff --git a/src/main/java/fr/xephi/authme/process/logout/AsynchronousLogout.java b/src/main/java/fr/xephi/authme/process/logout/AsynchronousLogout.java index d4f35331d..d6642f0ee 100644 --- a/src/main/java/fr/xephi/authme/process/logout/AsynchronousLogout.java +++ b/src/main/java/fr/xephi/authme/process/logout/AsynchronousLogout.java @@ -46,8 +46,9 @@ public class AsynchronousLogout { public void process() { preLogout(); - if (!canLogout) + if (!canLogout) { return; + } final Player p = player; BukkitScheduler scheduler = p.getServer().getScheduler(); PlayerAuth auth = PlayerCache.getInstance().getAuth(name); @@ -66,11 +67,11 @@ public class AsynchronousLogout { Utils.teleportToSpawn(p); } }); - if (LimboCache.getInstance().hasLimboPlayer(name)) + if (LimboCache.getInstance().hasLimboPlayer(name)) { LimboCache.getInstance().deleteLimboPlayer(name); + } LimboCache.getInstance().addLimboPlayer(player); Utils.setGroup(player, GroupType.NOTLOGGEDIN); - scheduler.scheduleSyncDelayedTask(plugin, new ProcessSyncronousPlayerLogout(p, plugin)); } } diff --git a/src/main/java/fr/xephi/authme/process/logout/ProcessSyncronousPlayerLogout.java b/src/main/java/fr/xephi/authme/process/logout/ProcessSyncronousPlayerLogout.java index b23ce6e82..9bf98aab6 100644 --- a/src/main/java/fr/xephi/authme/process/logout/ProcessSyncronousPlayerLogout.java +++ b/src/main/java/fr/xephi/authme/process/logout/ProcessSyncronousPlayerLogout.java @@ -43,14 +43,18 @@ public class ProcessSyncronousPlayerLogout implements Runnable { */ @Override public void run() { - if (plugin.sessions.containsKey(name)) + if (plugin.sessions.containsKey(name)) { plugin.sessions.get(name).cancel(); - plugin.sessions.remove(name); - int delay = Settings.getRegistrationTimeout * 20; + plugin.sessions.remove(name); + } + if (Settings.protectInventoryBeforeLogInEnabled) { + plugin.inventoryProtector.sendBlankInventoryPacket(player); + } + int timeOut = Settings.getRegistrationTimeout * 20; int interval = Settings.getWarnMessageInterval; BukkitScheduler sched = player.getServer().getScheduler(); - if (delay != 0) { - BukkitTask id = sched.runTaskLaterAsynchronously(plugin, new TimeoutTask(plugin, name, player), delay); + if (timeOut != 0) { + BukkitTask id = sched.runTaskLaterAsynchronously(plugin, new TimeoutTask(plugin, name, player), timeOut); LimboCache.getInstance().getLimboPlayer(name).setTimeoutTaskId(id); } BukkitTask msgT = sched.runTaskAsynchronously(plugin, new MessageTask(plugin, name, m.send("login_msg"), interval)); diff --git a/src/main/java/fr/xephi/authme/settings/Settings.java b/src/main/java/fr/xephi/authme/settings/Settings.java index 503c217eb..ad501994a 100644 --- a/src/main/java/fr/xephi/authme/settings/Settings.java +++ b/src/main/java/fr/xephi/authme/settings/Settings.java @@ -4,6 +4,7 @@ import fr.xephi.authme.AuthMe; import fr.xephi.authme.ConsoleLogger; import fr.xephi.authme.datasource.DataSource; import fr.xephi.authme.datasource.DataSource.DataSourceType; +import fr.xephi.authme.listener.AuthMeInventoryPacketAdapter; import fr.xephi.authme.security.HashAlgorithm; import org.bukkit.configuration.file.YamlConfiguration; @@ -187,7 +188,10 @@ public final class Settings extends YamlConfiguration { getUnrestrictedName = configFile.getStringList("settings.unrestrictions.UnrestrictedName"); getRegisteredGroup = configFile.getString("GroupOptions.RegisteredPlayerGroup", ""); getEnablePasswordVerifier = configFile.getBoolean("settings.restrictions.enablePasswordVerifier", true); + protectInventoryBeforeLogInEnabled = configFile.getBoolean("settings.restrictions.ProtectInventoryBeforeLogIn", true); + plugin.checkProtocolLib(); + passwordMaxLength = configFile.getInt("settings.security.passwordMaxLength", 20); isBackupActivated = configFile.getBoolean("BackupSystem.ActivateBackup", false); isBackupOnStart = configFile.getBoolean("BackupSystem.OnServerStart", false); @@ -195,6 +199,7 @@ public final class Settings extends YamlConfiguration { backupWindowsPath = configFile.getString("BackupSystem.MysqlWindowsPath", "C:\\Program Files\\MySQL\\MySQL Server 5.1\\"); isStopEnabled = configFile.getBoolean("Security.SQLProblem.stopServer", true); reloadSupport = configFile.getBoolean("Security.ReloadCommand.useReloadCommandSupport", true); + allowCommands = new ArrayList<>(); allowCommands.addAll(Arrays.asList("/login", "/l", "/register", "/reg", "/email", "/captcha")); for (String cmd : configFile.getStringList("settings.restrictions.allowCommands")) { @@ -203,6 +208,7 @@ public final class Settings extends YamlConfiguration { allowCommands.add(cmd); } } + rakamakUsers = configFile.getString("Converter.Rakamak.fileName", "users.rak"); rakamakUsersIp = configFile.getString("Converter.Rakamak.ipFileName", "UsersIp.rak"); rakamakUseIp = configFile.getBoolean("Converter.Rakamak.useIp", false);