diff --git a/src/main/java/fr/xephi/authme/AuthMe.java b/src/main/java/fr/xephi/authme/AuthMe.java index bd9b58d4..181e0da3 100644 --- a/src/main/java/fr/xephi/authme/AuthMe.java +++ b/src/main/java/fr/xephi/authme/AuthMe.java @@ -21,13 +21,10 @@ import fr.xephi.authme.initialization.DataFolder; import fr.xephi.authme.initialization.MetricsStarter; import fr.xephi.authme.listener.AuthMeBlockListener; import fr.xephi.authme.listener.AuthMeEntityListener; -import fr.xephi.authme.listener.AuthMeInventoryPacketAdapter; import fr.xephi.authme.listener.AuthMePlayerListener; import fr.xephi.authme.listener.AuthMePlayerListener16; import fr.xephi.authme.listener.AuthMePlayerListener18; import fr.xephi.authme.listener.AuthMeServerListener; -import fr.xephi.authme.listener.AuthMeTabCompletePacketAdapter; -import fr.xephi.authme.listener.AuthMeTablistPacketAdapter; import fr.xephi.authme.mail.SendMailSSL; import fr.xephi.authme.output.ConsoleFilter; import fr.xephi.authme.output.Log4JFilter; @@ -121,9 +118,6 @@ public class AuthMe extends JavaPlugin { * Private instances (mail and ProtocolLib) */ private SendMailSSL mail; - private AuthMeInventoryPacketAdapter inventoryProtector; - private AuthMeTabCompletePacketAdapter tabComplete; - private AuthMeTablistPacketAdapter tablistHider; /** * Constructor. @@ -260,9 +254,6 @@ public class AuthMe extends JavaPlugin { // Set up the mail API setupMailApi(); - // Check if the ProtocolLib is available - checkProtocolLib(); - // Do a backup on start // TODO: maybe create a backup manager? new PerformBackup(this, newSettings).doBackup(PerformBackup.BackupCause.START); @@ -571,41 +562,6 @@ public class AuthMe extends JavaPlugin { logger.addFilter(new Log4JFilter()); } - // Check the presence of the ProtocolLib plugin - public void checkProtocolLib() { - if (!getServer().getPluginManager().isPluginEnabled("ProtocolLib")) { - if (newSettings.getProperty(RestrictionSettings.PROTECT_INVENTORY_BEFORE_LOGIN)) { - ConsoleLogger.showError("WARNING! The protectInventory feature requires ProtocolLib! Disabling it..."); - Settings.protectInventoryBeforeLogInEnabled = false; - newSettings.setProperty(RestrictionSettings.PROTECT_INVENTORY_BEFORE_LOGIN, false); - newSettings.save(); - } - return; - } - - if (newSettings.getProperty(RestrictionSettings.PROTECT_INVENTORY_BEFORE_LOGIN) && inventoryProtector == null) { - inventoryProtector = new AuthMeInventoryPacketAdapter(this); - inventoryProtector.register(); - } else if (inventoryProtector != null) { - inventoryProtector.unregister(); - inventoryProtector = null; - } - if (newSettings.getProperty(RestrictionSettings.DENY_TABCOMPLETE_BEFORE_LOGIN) && tabComplete == null) { - tabComplete = new AuthMeTabCompletePacketAdapter(this); - tabComplete.register(); - } else if (tabComplete != null) { - tabComplete.unregister(); - tabComplete = null; - } - if (newSettings.getProperty(RestrictionSettings.HIDE_TABLIST_BEFORE_LOGIN) && tablistHider == null) { - tablistHider = new AuthMeTablistPacketAdapter(this, bukkitService); - tablistHider.register(); - } else if (tablistHider != null) { - tablistHider.unregister(); - tablistHider = null; - } - } - // Save Player Data private void savePlayer(Player player, LimboCache limboCache) { if (safeIsNpc(player) || Utils.isUnrestricted(player)) { @@ -714,42 +670,6 @@ public class AuthMe extends JavaPlugin { return this.mail; } - /** - * Get the ProtocolLib inventory packet adapter. - * - * @return The inventory packet adapter. - */ - public AuthMeInventoryPacketAdapter getInventoryProtector() { - return inventoryProtector; - } - - /** - * Get the ProtocolLib tab complete packet adapter. - * - * @return The tab complete packet adapter. - */ - public AuthMeTabCompletePacketAdapter getTabComplete() { - return tabComplete; - } - - /** - * Get the ProtocolLib tab list packet adapter. - * - * @return The tab list packet adapter. - */ - public AuthMeTablistPacketAdapter getTablistHider() { - return tablistHider; - } - - /** - * Disables instances should the ProtocolLib plugin be disabled on the server. - */ - public void disableProtocolLib() { - this.inventoryProtector = null; - this.tablistHider = null; - this.tabComplete = null; - } - // ------------- // Service getters (deprecated) // Use @Inject fields instead diff --git a/src/main/java/fr/xephi/authme/listener/AuthMeServerListener.java b/src/main/java/fr/xephi/authme/listener/AuthMeServerListener.java index d545ef10..a851b5e9 100644 --- a/src/main/java/fr/xephi/authme/listener/AuthMeServerListener.java +++ b/src/main/java/fr/xephi/authme/listener/AuthMeServerListener.java @@ -3,6 +3,7 @@ package fr.xephi.authme.listener; import fr.xephi.authme.AuthMe; import fr.xephi.authme.ConsoleLogger; import fr.xephi.authme.hooks.PluginHooks; +import fr.xephi.authme.listener.protocollib.ProtocolLibService; import fr.xephi.authme.output.MessageKey; import fr.xephi.authme.output.Messages; import fr.xephi.authme.permission.PermissionsManager; @@ -34,6 +35,8 @@ public class AuthMeServerListener implements Listener { @Inject private SpawnLoader spawnLoader; @Inject + private ProtocolLibService protocolLibService; + @Inject private ValidationService validationService; @Inject private PermissionsManager permissionsManager; @@ -75,8 +78,8 @@ public class AuthMeServerListener implements Listener { } if (pluginName.equalsIgnoreCase("ProtocolLib")) { - plugin.disableProtocolLib(); - ConsoleLogger.showError("ProtocolLib has been disabled, unhook packet inventory protection!"); + protocolLibService.disable(); + ConsoleLogger.showError("ProtocolLib has been disabled, unhooking packet adapters!"); } } @@ -103,7 +106,7 @@ public class AuthMeServerListener implements Listener { } if (pluginName.equalsIgnoreCase("ProtocolLib")) { - plugin.checkProtocolLib(); + protocolLibService.setup(); } } } diff --git a/src/main/java/fr/xephi/authme/listener/AuthMeInventoryPacketAdapter.java b/src/main/java/fr/xephi/authme/listener/protocollib/AuthMeInventoryPacketAdapter.java similarity index 99% rename from src/main/java/fr/xephi/authme/listener/AuthMeInventoryPacketAdapter.java rename to src/main/java/fr/xephi/authme/listener/protocollib/AuthMeInventoryPacketAdapter.java index c6bb0967..a8179b74 100644 --- a/src/main/java/fr/xephi/authme/listener/AuthMeInventoryPacketAdapter.java +++ b/src/main/java/fr/xephi/authme/listener/protocollib/AuthMeInventoryPacketAdapter.java @@ -14,7 +14,7 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ -package fr.xephi.authme.listener; +package fr.xephi.authme.listener.protocollib; import com.comphenix.protocol.PacketType; import com.comphenix.protocol.ProtocolLibrary; @@ -22,22 +22,19 @@ import com.comphenix.protocol.ProtocolManager; import com.comphenix.protocol.events.PacketAdapter; import com.comphenix.protocol.events.PacketContainer; import com.comphenix.protocol.events.PacketEvent; - import fr.xephi.authme.AuthMe; import fr.xephi.authme.cache.auth.PlayerCache; import fr.xephi.authme.settings.Settings; - -import java.lang.reflect.InvocationTargetException; -import java.util.Arrays; -import java.util.Collections; -import java.util.logging.Level; - +import org.apache.commons.lang.reflect.MethodUtils; import org.bukkit.Material; import org.bukkit.entity.Player; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.PlayerInventory; -import org.apache.commons.lang.reflect.MethodUtils; +import java.lang.reflect.InvocationTargetException; +import java.util.Arrays; +import java.util.Collections; +import java.util.logging.Level; public class AuthMeInventoryPacketAdapter extends PacketAdapter { diff --git a/src/main/java/fr/xephi/authme/listener/AuthMeTabCompletePacketAdapter.java b/src/main/java/fr/xephi/authme/listener/protocollib/AuthMeTabCompletePacketAdapter.java similarity index 96% rename from src/main/java/fr/xephi/authme/listener/AuthMeTabCompletePacketAdapter.java rename to src/main/java/fr/xephi/authme/listener/protocollib/AuthMeTabCompletePacketAdapter.java index ae6c47bc..ad4d1e0b 100644 --- a/src/main/java/fr/xephi/authme/listener/AuthMeTabCompletePacketAdapter.java +++ b/src/main/java/fr/xephi/authme/listener/protocollib/AuthMeTabCompletePacketAdapter.java @@ -1,4 +1,4 @@ -package fr.xephi.authme.listener; +package fr.xephi.authme.listener.protocollib; import com.comphenix.protocol.PacketType; import com.comphenix.protocol.ProtocolLibrary; diff --git a/src/main/java/fr/xephi/authme/listener/AuthMeTablistPacketAdapter.java b/src/main/java/fr/xephi/authme/listener/protocollib/AuthMeTablistPacketAdapter.java similarity index 99% rename from src/main/java/fr/xephi/authme/listener/AuthMeTablistPacketAdapter.java rename to src/main/java/fr/xephi/authme/listener/protocollib/AuthMeTablistPacketAdapter.java index 8775959e..dd24bdb1 100644 --- a/src/main/java/fr/xephi/authme/listener/AuthMeTablistPacketAdapter.java +++ b/src/main/java/fr/xephi/authme/listener/protocollib/AuthMeTablistPacketAdapter.java @@ -1,4 +1,4 @@ -package fr.xephi.authme.listener; +package fr.xephi.authme.listener.protocollib; import com.comphenix.protocol.PacketType; import com.comphenix.protocol.ProtocolLibrary; @@ -14,12 +14,14 @@ import com.comphenix.protocol.wrappers.PlayerInfoData; import com.comphenix.protocol.wrappers.WrappedChatComponent; import com.comphenix.protocol.wrappers.WrappedGameProfile; import com.google.common.collect.Lists; - import fr.xephi.authme.AuthMe; import fr.xephi.authme.ConsoleLogger; import fr.xephi.authme.cache.auth.PlayerCache; import fr.xephi.authme.util.BukkitService; +import org.bukkit.Bukkit; +import org.bukkit.entity.Player; +import javax.inject.Inject; import java.lang.reflect.InvocationTargetException; import java.util.Arrays; import java.util.Iterator; @@ -27,11 +29,6 @@ import java.util.List; import java.util.UUID; import java.util.logging.Level; -import javax.inject.Inject; - -import org.bukkit.Bukkit; -import org.bukkit.entity.Player; - public class AuthMeTablistPacketAdapter extends PacketAdapter { private final BukkitService bukkitService; diff --git a/src/main/java/fr/xephi/authme/listener/protocollib/ProtocolLibService.java b/src/main/java/fr/xephi/authme/listener/protocollib/ProtocolLibService.java new file mode 100644 index 00000000..b1f5573a --- /dev/null +++ b/src/main/java/fr/xephi/authme/listener/protocollib/ProtocolLibService.java @@ -0,0 +1,146 @@ +package fr.xephi.authme.listener.protocollib; + +import fr.xephi.authme.AuthMe; +import fr.xephi.authme.ConsoleLogger; +import fr.xephi.authme.initialization.SettingsDependent; +import fr.xephi.authme.settings.NewSetting; +import fr.xephi.authme.settings.properties.RestrictionSettings; +import fr.xephi.authme.util.BukkitService; +import org.bukkit.entity.Player; + +import javax.inject.Inject; + +public class ProtocolLibService implements SettingsDependent { + + /* Packet Adapters */ + private AuthMeInventoryPacketAdapter inventoryPacketAdapter; + private AuthMeTabCompletePacketAdapter tabCompletePacketAdapter; + private AuthMeTablistPacketAdapter tablistPacketAdapter; + + /* Settings */ + private boolean protectInvBeforeLogin; + private boolean denyTabCompleteBeforeLogin; + private boolean hideTablistBeforeLogin; + + /* Service */ + private boolean isEnabled; + private AuthMe plugin; + private BukkitService bukkitService; + + @Inject + ProtocolLibService(AuthMe plugin, BukkitService bukkitService, NewSetting settings) { + this.plugin = plugin; + this.bukkitService = bukkitService; + loadSettings(settings); + setup(); + } + + /** + * Set up the ProtocolLib packet adapters. + */ + public void setup() { + // Check if ProtocolLib is enabled on the server. + if (!plugin.getServer().getPluginManager().isPluginEnabled("ProtocolLib")) { + if (protectInvBeforeLogin) { + ConsoleLogger.showError("WARNING! The protectInventory feature requires ProtocolLib! Disabling it..."); + } + if (denyTabCompleteBeforeLogin) { + ConsoleLogger.showError("WARNING! The denyTabComplete feature requires ProtocolLib! Disabling it..."); + } + if (hideTablistBeforeLogin) { + ConsoleLogger.showError("WARNING! The hideTablist feature requires ProtocolLib! Disabling it..."); + } + + this.isEnabled = false; + return; + } + + // Set up packet adapters + if (protectInvBeforeLogin && inventoryPacketAdapter == null) { + inventoryPacketAdapter = new AuthMeInventoryPacketAdapter(plugin); + inventoryPacketAdapter.register(); + } else if (inventoryPacketAdapter != null) { + inventoryPacketAdapter.unregister(); + inventoryPacketAdapter = null; + } + if (denyTabCompleteBeforeLogin && tabCompletePacketAdapter == null) { + tabCompletePacketAdapter = new AuthMeTabCompletePacketAdapter(plugin); + tabCompletePacketAdapter.register(); + } else if (tabCompletePacketAdapter != null) { + tabCompletePacketAdapter.unregister(); + tabCompletePacketAdapter = null; + } + if (hideTablistBeforeLogin && tablistPacketAdapter == null) { + tablistPacketAdapter = new AuthMeTablistPacketAdapter(plugin, bukkitService); + tablistPacketAdapter.register(); + } else if (tablistPacketAdapter != null) { + tablistPacketAdapter.unregister(); + tablistPacketAdapter = null; + } + + this.isEnabled = true; + } + + public void disable() { + isEnabled = false; + + if (inventoryPacketAdapter != null) { + inventoryPacketAdapter.unregister(); + inventoryPacketAdapter = null; + } + if (tabCompletePacketAdapter != null) { + tabCompletePacketAdapter.unregister(); + tabCompletePacketAdapter = null; + } + if (tablistPacketAdapter != null) { + tablistPacketAdapter.unregister(); + tablistPacketAdapter = null; + } + } + + /** + * Send a packet to the player to give them an inventory. + * + * @param player The player to send the packet to. + */ + public void sendInventoryPacket(Player player) { + if (!isEnabled || inventoryPacketAdapter == null) { + return; + } + + inventoryPacketAdapter.sendInventoryPacket(player); + } + + /** + * Send a packet to the player to give them a blank inventory. + * + * @param player The player to send the packet to. + */ + public void sendBlankInventoryPacket(Player player) { + if (!isEnabled || inventoryPacketAdapter == null) { + return; + } + + inventoryPacketAdapter.sendBlankInventoryPacket(player); + } + + /** + * Send a tab list packet to a player. + * + * @param player The player to send the packet to. + */ + public void sendTabList(Player player) { + if (!isEnabled || tablistPacketAdapter == null) { + return; + } + + tablistPacketAdapter.sendTablist(player); + } + + @Override + public void loadSettings(NewSetting settings) { + this.protectInvBeforeLogin = settings.getProperty(RestrictionSettings.PROTECT_INVENTORY_BEFORE_LOGIN); + this.denyTabCompleteBeforeLogin = settings.getProperty(RestrictionSettings.DENY_TABCOMPLETE_BEFORE_LOGIN); + this.hideTablistBeforeLogin = settings.getProperty(RestrictionSettings.HIDE_TABLIST_BEFORE_LOGIN); + } +} diff --git a/src/main/java/fr/xephi/authme/process/join/AsynchronousJoin.java b/src/main/java/fr/xephi/authme/process/join/AsynchronousJoin.java index 709a9dac..5551b9b8 100644 --- a/src/main/java/fr/xephi/authme/process/join/AsynchronousJoin.java +++ b/src/main/java/fr/xephi/authme/process/join/AsynchronousJoin.java @@ -9,6 +9,7 @@ import fr.xephi.authme.cache.limbo.LimboCache; import fr.xephi.authme.datasource.DataSource; import fr.xephi.authme.events.ProtectInventoryEvent; import fr.xephi.authme.hooks.PluginHooks; +import fr.xephi.authme.listener.protocollib.ProtocolLibService; import fr.xephi.authme.output.MessageKey; import fr.xephi.authme.permission.AuthGroupType; import fr.xephi.authme.permission.PlayerStatePermission; @@ -68,6 +69,9 @@ public class AsynchronousJoin implements AsynchronousProcess { @Inject private BukkitService bukkitService; + @Inject + private ProtocolLibService protocolLibService; + @Inject private LimboPlayerTaskManager limboPlayerTaskManager; @@ -126,11 +130,11 @@ public class AsynchronousJoin implements AsynchronousProcess { limboCache.updateLimboPlayer(player); // Protect inventory - if (service.getProperty(PROTECT_INVENTORY_BEFORE_LOGIN) && plugin.getInventoryProtector() != null) { + if (service.getProperty(PROTECT_INVENTORY_BEFORE_LOGIN)) { ProtectInventoryEvent ev = new ProtectInventoryEvent(player); bukkitService.callEvent(ev); if (ev.isCancelled()) { - plugin.getInventoryProtector().sendInventoryPacket(player); + protocolLibService.sendInventoryPacket(player); if (!service.getProperty(SecuritySettings.REMOVE_SPAM_FROM_CONSOLE)) { ConsoleLogger.info("ProtectInventoryEvent has been cancelled for " + player.getName() + "..."); } diff --git a/src/main/java/fr/xephi/authme/process/login/ProcessSyncPlayerLogin.java b/src/main/java/fr/xephi/authme/process/login/ProcessSyncPlayerLogin.java index e4ded1de..14a4f8e0 100644 --- a/src/main/java/fr/xephi/authme/process/login/ProcessSyncPlayerLogin.java +++ b/src/main/java/fr/xephi/authme/process/login/ProcessSyncPlayerLogin.java @@ -10,6 +10,7 @@ import fr.xephi.authme.datasource.DataSource; import fr.xephi.authme.events.LoginEvent; import fr.xephi.authme.events.RestoreInventoryEvent; import fr.xephi.authme.listener.AuthMePlayerListener; +import fr.xephi.authme.listener.protocollib.ProtocolLibService; import fr.xephi.authme.permission.AuthGroupType; import fr.xephi.authme.process.ProcessService; import fr.xephi.authme.process.SynchronousProcess; @@ -50,6 +51,9 @@ public class ProcessSyncPlayerLogin implements SynchronousProcess { @Inject private BukkitService bukkitService; + @Inject + private ProtocolLibService protocolLibService; + @Inject private PluginManager pluginManager; @@ -106,8 +110,8 @@ public class ProcessSyncPlayerLogin implements SynchronousProcess { restoreInventory(player); } - if (service.getProperty(RestrictionSettings.HIDE_TABLIST_BEFORE_LOGIN) && plugin.getTablistHider() != null) { - plugin.getTablistHider().sendTablist(player); + if (service.getProperty(RestrictionSettings.HIDE_TABLIST_BEFORE_LOGIN)) { + protocolLibService.sendTabList(player); } // Clean up no longer used temporary data diff --git a/src/main/java/fr/xephi/authme/process/logout/ProcessSynchronousPlayerLogout.java b/src/main/java/fr/xephi/authme/process/logout/ProcessSynchronousPlayerLogout.java index 38514567..6a36ab44 100644 --- a/src/main/java/fr/xephi/authme/process/logout/ProcessSynchronousPlayerLogout.java +++ b/src/main/java/fr/xephi/authme/process/logout/ProcessSynchronousPlayerLogout.java @@ -6,6 +6,7 @@ import fr.xephi.authme.AuthMe; import fr.xephi.authme.ConsoleLogger; import fr.xephi.authme.cache.SessionManager; import fr.xephi.authme.events.LogoutEvent; +import fr.xephi.authme.listener.protocollib.ProtocolLibService; import fr.xephi.authme.output.MessageKey; import fr.xephi.authme.process.ProcessService; import fr.xephi.authme.process.SynchronousProcess; @@ -34,6 +35,9 @@ public class ProcessSynchronousPlayerLogout implements SynchronousProcess { @Inject private BukkitService bukkitService; + @Inject + private ProtocolLibService protocolLibService; + @Inject private LimboPlayerTaskManager limboPlayerTaskManager; @@ -66,7 +70,7 @@ public class ProcessSynchronousPlayerLogout implements SynchronousProcess { sessionManager.cancelSession(name); } if (service.getProperty(RestrictionSettings.PROTECT_INVENTORY_BEFORE_LOGIN)) { - plugin.getInventoryProtector().sendBlankInventoryPacket(player); + protocolLibService.sendBlankInventoryPacket(player); } limboPlayerTaskManager.registerTimeoutTask(player); diff --git a/src/main/java/fr/xephi/authme/process/register/ProcessSyncPasswordRegister.java b/src/main/java/fr/xephi/authme/process/register/ProcessSyncPasswordRegister.java index 2ac8d027..524d6b1c 100644 --- a/src/main/java/fr/xephi/authme/process/register/ProcessSyncPasswordRegister.java +++ b/src/main/java/fr/xephi/authme/process/register/ProcessSyncPasswordRegister.java @@ -8,6 +8,7 @@ import fr.xephi.authme.cache.limbo.LimboCache; import fr.xephi.authme.cache.limbo.LimboPlayer; import fr.xephi.authme.events.LoginEvent; import fr.xephi.authme.events.RestoreInventoryEvent; +import fr.xephi.authme.listener.protocollib.ProtocolLibService; import fr.xephi.authme.output.MessageKey; import fr.xephi.authme.permission.AuthGroupType; import fr.xephi.authme.process.ProcessService; @@ -42,6 +43,9 @@ public class ProcessSyncPasswordRegister implements SynchronousProcess { @Inject private BukkitService bukkitService; + @Inject + private ProtocolLibService protocolLibService; + @Inject private LimboCache limboCache; @@ -92,17 +96,17 @@ public class ProcessSyncPasswordRegister implements SynchronousProcess { final String name = player.getName().toLowerCase(); LimboPlayer limbo = limboCache.getLimboPlayer(name); if (limbo != null) { - if (service.getProperty(RestrictionSettings.HIDE_TABLIST_BEFORE_LOGIN) && plugin.getTablistHider() != null) { - plugin.getTablistHider().sendTablist(player); + if (service.getProperty(RestrictionSettings.HIDE_TABLIST_BEFORE_LOGIN)) { + protocolLibService.sendTabList(player); } Utils.teleportToSpawn(player); - if (service.getProperty(PROTECT_INVENTORY_BEFORE_LOGIN) && plugin.getInventoryProtector() != null) { + if (service.getProperty(PROTECT_INVENTORY_BEFORE_LOGIN)) { RestoreInventoryEvent event = new RestoreInventoryEvent(player); bukkitService.callEvent(event); if (!event.isCancelled()) { - plugin.getInventoryProtector().sendInventoryPacket(player); + protocolLibService.sendInventoryPacket(player); } }