From 097755892487c28be796d68e639f1370c1397f7a Mon Sep 17 00:00:00 2001 From: ljacqu Date: Tue, 31 May 2016 11:13:42 +0200 Subject: [PATCH] #736 Remove use of service getters and deprecate them --- src/main/java/fr/xephi/authme/AuthMe.java | 46 +++++++++++++------ src/main/java/fr/xephi/authme/api/NewAPI.java | 19 ++------ .../changepassword/ChangePasswordCommand.java | 8 +++- .../authme/converter/RakamakConverter.java | 9 ++-- .../xephi/authme/hooks/BungeeCordMessage.java | 35 +++++++------- .../process/login/AsynchronousLogin.java | 8 +++- .../process/register/AsyncRegister.java | 12 +++-- .../fr/xephi/authme/settings/SpawnLoader.java | 8 +++- .../xephi/authme/task/ChangePasswordTask.java | 7 +-- .../authme/settings/SpawnLoaderTest.java | 7 ++- 10 files changed, 96 insertions(+), 63 deletions(-) diff --git a/src/main/java/fr/xephi/authme/AuthMe.java b/src/main/java/fr/xephi/authme/AuthMe.java index 9c2124d7a..eaefea8d9 100644 --- a/src/main/java/fr/xephi/authme/AuthMe.java +++ b/src/main/java/fr/xephi/authme/AuthMe.java @@ -297,7 +297,7 @@ public class AuthMe extends JavaPlugin { // Set up the BungeeCord hook - setupBungeeCordHook(newSettings); + setupBungeeCordHook(newSettings, initializer); // Reload support hook reloadSupportHook(); @@ -396,11 +396,11 @@ public class AuthMe extends JavaPlugin { /** * Set up the BungeeCord hook. */ - private void setupBungeeCordHook(NewSetting settings) { + private void setupBungeeCordHook(NewSetting settings, AuthMeServiceInitializer initializer) { if (settings.getProperty(HooksSettings.BUNGEECORD)) { Bukkit.getMessenger().registerOutgoingPluginChannel(this, "BungeeCord"); Bukkit.getMessenger().registerIncomingPluginChannel( - this, "BungeeCord", new BungeeCordMessage(this)); + this, "BungeeCord", initializer.get(BungeeCordMessage.class)); } } @@ -750,37 +750,57 @@ public class AuthMe extends JavaPlugin { return commandHandler.processCommand(sender, commandLabel, args); } + public void notifyAutoPurgeEnd() { + this.autoPurging = false; + } + + + // ------------- + // Service getters (deprecated) + // Use @Inject fields instead + // ------------- /** - * Get the permissions manager instance. - * - * @return Permissions Manager instance. + * @return permission manager + * @deprecated should be used in API classes only (temporarily) */ + @Deprecated public PermissionsManager getPermissionsManager() { return this.permsMan; } /** - * Return the management instance. - * - * @return management The Management + * @return process manager + * @deprecated should be used in API classes only (temporarily) */ + @Deprecated public Management getManagement() { return management; } + /** + * @return the datasource + * @deprecated should be used in API classes only (temporarily) + */ + @Deprecated public DataSource getDataSource() { return database; } + /** + * @return password manager + * @deprecated should be used in API classes only (temporarily) + */ + @Deprecated public PasswordSecurity getPasswordSecurity() { return passwordSecurity; } + /** + * @return plugin hooks + * @deprecated should be used in API classes only (temporarily) + */ + @Deprecated public PluginHooks getPluginHooks() { return pluginHooks; } - - public void notifyAutoPurgeEnd() { - this.autoPurging = false; - } } diff --git a/src/main/java/fr/xephi/authme/api/NewAPI.java b/src/main/java/fr/xephi/authme/api/NewAPI.java index a0def6391..5e0063e97 100644 --- a/src/main/java/fr/xephi/authme/api/NewAPI.java +++ b/src/main/java/fr/xephi/authme/api/NewAPI.java @@ -1,16 +1,14 @@ package fr.xephi.authme.api; -import org.bukkit.Bukkit; -import org.bukkit.Location; -import org.bukkit.Server; -import org.bukkit.entity.Player; -import org.bukkit.plugin.Plugin; - import fr.xephi.authme.AuthMe; import fr.xephi.authme.cache.auth.PlayerAuth; import fr.xephi.authme.cache.auth.PlayerCache; import fr.xephi.authme.security.crypts.HashedPassword; import fr.xephi.authme.util.Utils; +import org.bukkit.Bukkit; +import org.bukkit.Location; +import org.bukkit.entity.Player; +import org.bukkit.plugin.Plugin; import javax.inject.Inject; @@ -35,15 +33,6 @@ public class NewAPI { this.plugin = plugin; } - /** - * Constructor for NewAPI. - * - * @param server The server instance - */ - public NewAPI(Server server) { - this.plugin = (AuthMe) server.getPluginManager().getPlugin("AuthMe"); - } - /** * Get the API object for AuthMe. * diff --git a/src/main/java/fr/xephi/authme/command/executable/changepassword/ChangePasswordCommand.java b/src/main/java/fr/xephi/authme/command/executable/changepassword/ChangePasswordCommand.java index 310976fc6..01aea8625 100644 --- a/src/main/java/fr/xephi/authme/command/executable/changepassword/ChangePasswordCommand.java +++ b/src/main/java/fr/xephi/authme/command/executable/changepassword/ChangePasswordCommand.java @@ -5,6 +5,7 @@ import fr.xephi.authme.cache.auth.PlayerCache; import fr.xephi.authme.command.CommandService; import fr.xephi.authme.command.PlayerCommand; import fr.xephi.authme.output.MessageKey; +import fr.xephi.authme.security.PasswordSecurity; import fr.xephi.authme.task.ChangePasswordTask; import fr.xephi.authme.util.BukkitService; import org.bukkit.entity.Player; @@ -23,6 +24,10 @@ public class ChangePasswordCommand extends PlayerCommand { @Inject private BukkitService bukkitService; + @Inject + // TODO ljacqu 20160531: Remove this once change password task runs as a process (via Management) + private PasswordSecurity passwordSecurity; + @Override public void runCommand(Player player, List arguments, CommandService commandService) { String oldPassword = arguments.get(0); @@ -43,6 +48,7 @@ public class ChangePasswordCommand extends PlayerCommand { AuthMe plugin = AuthMe.getInstance(); // TODO ljacqu 20160117: Call async task via Management - bukkitService.runTaskAsynchronously(new ChangePasswordTask(plugin, player, oldPassword, newPassword)); + bukkitService.runTaskAsynchronously( + new ChangePasswordTask(plugin, player, oldPassword, newPassword, passwordSecurity)); } } diff --git a/src/main/java/fr/xephi/authme/converter/RakamakConverter.java b/src/main/java/fr/xephi/authme/converter/RakamakConverter.java index de6ea77b5..e568bb8b4 100644 --- a/src/main/java/fr/xephi/authme/converter/RakamakConverter.java +++ b/src/main/java/fr/xephi/authme/converter/RakamakConverter.java @@ -1,6 +1,5 @@ package fr.xephi.authme.converter; -import fr.xephi.authme.AuthMe; import fr.xephi.authme.ConsoleLogger; import fr.xephi.authme.cache.auth.PlayerAuth; import fr.xephi.authme.datasource.DataSource; @@ -24,17 +23,18 @@ import java.util.Map.Entry; */ public class RakamakConverter implements Converter { - private final AuthMe instance; private final DataSource database; private final NewSetting settings; private final File pluginFolder; + private final PasswordSecurity passwordSecurity; @Inject - RakamakConverter(@DataFolder File dataFolder, AuthMe instance, DataSource dataSource, NewSetting settings) { - this.instance = instance; + RakamakConverter(@DataFolder File dataFolder, DataSource dataSource, NewSetting settings, + PasswordSecurity passwordSecurity) { this.database = dataSource; this.settings = settings; this.pluginFolder = dataFolder; + this.passwordSecurity = passwordSecurity; } @Override @@ -64,7 +64,6 @@ public class RakamakConverter implements Converter { ipFile.close(); users = new BufferedReader(new FileReader(source)); - PasswordSecurity passwordSecurity = instance.getPasswordSecurity(); while ((line = users.readLine()) != null) { if (line.contains("=")) { String[] arguments = line.split("="); diff --git a/src/main/java/fr/xephi/authme/hooks/BungeeCordMessage.java b/src/main/java/fr/xephi/authme/hooks/BungeeCordMessage.java index ed089b8a8..26d5847ae 100644 --- a/src/main/java/fr/xephi/authme/hooks/BungeeCordMessage.java +++ b/src/main/java/fr/xephi/authme/hooks/BungeeCordMessage.java @@ -2,29 +2,31 @@ package fr.xephi.authme.hooks; import com.google.common.io.ByteArrayDataInput; import com.google.common.io.ByteStreams; -import fr.xephi.authme.AuthMe; import fr.xephi.authme.ConsoleLogger; import fr.xephi.authme.cache.auth.PlayerAuth; import fr.xephi.authme.cache.auth.PlayerCache; import fr.xephi.authme.datasource.DataSource; import fr.xephi.authme.security.crypts.HashedPassword; +import fr.xephi.authme.util.BukkitService; import org.bukkit.entity.Player; import org.bukkit.plugin.messaging.PluginMessageListener; -/** - */ +import javax.inject.Inject; + + public class BungeeCordMessage implements PluginMessageListener { - private final AuthMe plugin; + @Inject + private DataSource dataSource; + + @Inject + private BukkitService bukkitService; + + @Inject + private PlayerCache playerCache; + + BungeeCordMessage() { } - /** - * Constructor for BungeeCordMessage. - * - * @param plugin The plugin instance - */ - public BungeeCordMessage(AuthMe plugin) { - this.plugin = plugin; - } @Override public void onPluginMessageReceived(String channel, Player player, byte[] message) { @@ -38,8 +40,7 @@ public class BungeeCordMessage implements PluginMessageListener { final String[] args = str.split(";"); final String act = args[0]; final String name = args[1]; - final DataSource dataSource = plugin.getDataSource(); - plugin.getServer().getScheduler().runTaskAsynchronously(plugin, new Runnable() { + bukkitService.runTaskAsynchronously(new Runnable() { @Override public void run() { PlayerAuth auth = dataSource.getAuth(name); @@ -47,12 +48,12 @@ public class BungeeCordMessage implements PluginMessageListener { return; } if ("login".equals(act)) { - PlayerCache.getInstance().updatePlayer(auth); + playerCache.updatePlayer(auth); dataSource.setLogged(name); ConsoleLogger.info("Player " + auth.getNickname() + " has logged in from one of your server!"); } else if ("logout".equals(act)) { - PlayerCache.getInstance().removePlayer(name); + playerCache.removePlayer(name); dataSource.setUnlogged(name); ConsoleLogger.info("Player " + auth.getNickname() + " has logged out from one of your server!"); @@ -63,7 +64,7 @@ public class BungeeCordMessage implements PluginMessageListener { final String password = args[2]; final String salt = args.length >= 4 ? args[3] : null; auth.setPassword(new HashedPassword(password, salt)); - PlayerCache.getInstance().updatePlayer(auth); + playerCache.updatePlayer(auth); dataSource.updatePassword(auth); } diff --git a/src/main/java/fr/xephi/authme/process/login/AsynchronousLogin.java b/src/main/java/fr/xephi/authme/process/login/AsynchronousLogin.java index a86ac93e2..473c8142f 100644 --- a/src/main/java/fr/xephi/authme/process/login/AsynchronousLogin.java +++ b/src/main/java/fr/xephi/authme/process/login/AsynchronousLogin.java @@ -16,6 +16,7 @@ import fr.xephi.authme.permission.PlayerStatePermission; import fr.xephi.authme.process.AsynchronousProcess; import fr.xephi.authme.process.ProcessService; import fr.xephi.authme.process.SyncProcessManager; +import fr.xephi.authme.security.PasswordSecurity; import fr.xephi.authme.security.RandomString; import fr.xephi.authme.settings.Settings; import fr.xephi.authme.settings.properties.DatabaseSettings; @@ -62,6 +63,9 @@ public class AsynchronousLogin implements AsynchronousProcess { @Inject private BukkitService bukkitService; + @Inject + private PasswordSecurity passwordSecurity; + AsynchronousLogin() { } @@ -150,8 +154,8 @@ public class AsynchronousLogin implements AsynchronousProcess { } String email = pAuth.getEmail(); - boolean passwordVerified = forceLogin || plugin.getPasswordSecurity() - .comparePassword(password, pAuth.getPassword(), player.getName()); + boolean passwordVerified = forceLogin || passwordSecurity.comparePassword( + password, pAuth.getPassword(), player.getName()); final String name = player.getName().toLowerCase(); if (passwordVerified && player.isOnline()) { diff --git a/src/main/java/fr/xephi/authme/process/register/AsyncRegister.java b/src/main/java/fr/xephi/authme/process/register/AsyncRegister.java index fbc6c86dd..2d47b5bad 100644 --- a/src/main/java/fr/xephi/authme/process/register/AsyncRegister.java +++ b/src/main/java/fr/xephi/authme/process/register/AsyncRegister.java @@ -5,7 +5,7 @@ import fr.xephi.authme.cache.auth.PlayerAuth; import fr.xephi.authme.cache.auth.PlayerCache; import fr.xephi.authme.datasource.DataSource; import fr.xephi.authme.output.MessageKey; -import fr.xephi.authme.permission.PlayerStatePermission; +import fr.xephi.authme.permission.PermissionsManager; import fr.xephi.authme.process.AsynchronousProcess; import fr.xephi.authme.process.ProcessService; import fr.xephi.authme.process.SyncProcessManager; @@ -26,6 +26,8 @@ import org.bukkit.entity.Player; import javax.inject.Inject; import java.util.List; +import static fr.xephi.authme.permission.PlayerStatePermission.ALLOW_MULTIPLE_ACCOUNTS; + public class AsyncRegister implements AsynchronousProcess { @Inject @@ -46,6 +48,9 @@ public class AsyncRegister implements AsynchronousProcess { @Inject private SyncProcessManager syncProcessManager; + @Inject + private PermissionsManager permissionsManager; + AsyncRegister() { } private boolean preRegisterCheck(Player player, String password) { @@ -78,7 +83,7 @@ public class AsyncRegister implements AsynchronousProcess { if (maxRegPerIp > 0 && !"127.0.0.1".equalsIgnoreCase(ip) && !"localhost".equalsIgnoreCase(ip) - && !plugin.getPermissionsManager().hasPermission(player, PlayerStatePermission.ALLOW_MULTIPLE_ACCOUNTS)) { + && !permissionsManager.hasPermission(player, ALLOW_MULTIPLE_ACCOUNTS)) { List otherAccounts = database.getAllAuthsByIp(ip); if (otherAccounts.size() >= maxRegPerIp) { service.send(player, MessageKey.MAX_REGISTER_EXCEEDED, Integer.toString(maxRegPerIp), @@ -102,8 +107,7 @@ public class AsyncRegister implements AsynchronousProcess { private void emailRegister(Player player, String password, String email) { final String name = player.getName().toLowerCase(); final int maxRegPerEmail = service.getProperty(EmailSettings.MAX_REG_PER_EMAIL); - if (maxRegPerEmail > 0 - && !plugin.getPermissionsManager().hasPermission(player, PlayerStatePermission.ALLOW_MULTIPLE_ACCOUNTS)) { + if (maxRegPerEmail > 0 && !permissionsManager.hasPermission(player, ALLOW_MULTIPLE_ACCOUNTS)) { int otherAccounts = database.countAuthsByEmail(email); if (otherAccounts >= maxRegPerEmail) { service.send(player, MessageKey.MAX_REGISTER_EXCEEDED, Integer.toString(maxRegPerEmail), diff --git a/src/main/java/fr/xephi/authme/settings/SpawnLoader.java b/src/main/java/fr/xephi/authme/settings/SpawnLoader.java index 012eb0533..f2a4cc5c1 100644 --- a/src/main/java/fr/xephi/authme/settings/SpawnLoader.java +++ b/src/main/java/fr/xephi/authme/settings/SpawnLoader.java @@ -3,6 +3,7 @@ package fr.xephi.authme.settings; import fr.xephi.authme.AuthMe; import fr.xephi.authme.ConsoleLogger; import fr.xephi.authme.cache.auth.PlayerCache; +import fr.xephi.authme.datasource.DataSource; import fr.xephi.authme.hooks.PluginHooks; import fr.xephi.authme.initialization.DataFolder; import fr.xephi.authme.initialization.SettingsDependent; @@ -32,6 +33,7 @@ public class SpawnLoader implements SettingsDependent { private final File authMeConfigurationFile; private final PluginHooks pluginHooks; + private final DataSource dataSource; private FileConfiguration authMeConfiguration; private String[] spawnPriority; private Location essentialsSpawn; @@ -44,12 +46,14 @@ public class SpawnLoader implements SettingsDependent { * @param pluginHooks The plugin hooks instance */ @Inject - public SpawnLoader(@DataFolder File pluginFolder, NewSetting settings, PluginHooks pluginHooks) { + public SpawnLoader(@DataFolder File pluginFolder, NewSetting settings, PluginHooks pluginHooks, + DataSource dataSource) { File spawnFile = new File(pluginFolder, "spawn.yml"); // TODO ljacqu 20160312: Check if resource could be copied and handle the case if not FileUtils.copyFileFromResource(spawnFile, "spawn.yml"); this.authMeConfigurationFile = new File(pluginFolder, "spawn.yml"); this.pluginHooks = pluginHooks; + this.dataSource = dataSource; loadSettings(settings); } @@ -166,7 +170,7 @@ public class SpawnLoader implements SettingsDependent { if (PlayerCache.getInstance().isAuthenticated(playerNameLower)) { spawnLoc = getSpawn(); } else if (getFirstSpawn() != null && (!player.hasPlayedBefore() || - !plugin.getDataSource().isAuthAvailable(playerNameLower))) { + !dataSource.isAuthAvailable(playerNameLower))) { spawnLoc = getFirstSpawn(); } else { spawnLoc = getSpawn(); diff --git a/src/main/java/fr/xephi/authme/task/ChangePasswordTask.java b/src/main/java/fr/xephi/authme/task/ChangePasswordTask.java index c5ff5b95b..1fbd8c148 100644 --- a/src/main/java/fr/xephi/authme/task/ChangePasswordTask.java +++ b/src/main/java/fr/xephi/authme/task/ChangePasswordTask.java @@ -19,19 +19,20 @@ public class ChangePasswordTask implements Runnable { private final Player player; private final String oldPassword; private final String newPassword; + private final PasswordSecurity passwordSecurity; - public ChangePasswordTask(AuthMe plugin, Player player, String oldPassword, String newPassword) { + public ChangePasswordTask(AuthMe plugin, Player player, String oldPassword, String newPassword, + PasswordSecurity passwordSecurity) { this.plugin = plugin; this.player = player; this.oldPassword = oldPassword; this.newPassword = newPassword; + this.passwordSecurity = passwordSecurity; } @Override public void run() { Messages m = plugin.getMessages(); - PasswordSecurity passwordSecurity = plugin.getPasswordSecurity(); - final String name = player.getName().toLowerCase(); PlayerAuth auth = PlayerCache.getInstance().getAuth(name); if (passwordSecurity.comparePassword(oldPassword, auth.getPassword(), player.getName())) { diff --git a/src/test/java/fr/xephi/authme/settings/SpawnLoaderTest.java b/src/test/java/fr/xephi/authme/settings/SpawnLoaderTest.java index 8bd9da4bc..7bd0d40ec 100644 --- a/src/test/java/fr/xephi/authme/settings/SpawnLoaderTest.java +++ b/src/test/java/fr/xephi/authme/settings/SpawnLoaderTest.java @@ -2,6 +2,7 @@ package fr.xephi.authme.settings; import com.google.common.io.Files; import fr.xephi.authme.TestHelper; +import fr.xephi.authme.datasource.DataSource; import fr.xephi.authme.hooks.PluginHooks; import fr.xephi.authme.settings.properties.RestrictionSettings; import org.bukkit.Location; @@ -11,6 +12,8 @@ import org.junit.Before; import org.junit.Rule; import org.junit.Test; import org.junit.rules.TemporaryFolder; +import org.junit.runner.RunWith; +import org.mockito.runners.MockitoJUnitRunner; import java.io.File; import java.io.IOException; @@ -23,6 +26,7 @@ import static org.mockito.Mockito.mock; /** * Test for {@link SpawnLoader}. */ +@RunWith(MockitoJUnitRunner.class) public class SpawnLoaderTest { @Rule @@ -48,7 +52,8 @@ public class SpawnLoaderTest { @Test public void shouldSetSpawn() { // given - SpawnLoader spawnLoader = new SpawnLoader(testFolder, settings, mock(PluginHooks.class)); + SpawnLoader spawnLoader = + new SpawnLoader(testFolder, settings, mock(PluginHooks.class), mock(DataSource.class)); World world = mock(World.class); given(world.getName()).willReturn("new_world"); Location newSpawn = new Location(world, 123, 45.0, -67.89);