From 2f7ebc0ecbbfc5799cde2da2c792b7e91f36af11 Mon Sep 17 00:00:00 2001 From: ljacqu Date: Tue, 18 Apr 2017 21:55:41 +0200 Subject: [PATCH] Remove static PlayerCache#getInstance --- src/main/java/fr/xephi/authme/AuthMe.java | 5 -- .../xephi/authme/data/auth/PlayerCache.java | 57 ++++++------------- .../fr/xephi/authme/datasource/FlatFile.java | 3 +- .../protocollib/InventoryPacketAdapter.java | 13 +++-- .../protocollib/ProtocolLibService.java | 4 +- .../protocollib/TabCompletePacketAdapter.java | 9 ++- .../process/login/AsynchronousLogin.java | 2 +- 7 files changed, 33 insertions(+), 60 deletions(-) diff --git a/src/main/java/fr/xephi/authme/AuthMe.java b/src/main/java/fr/xephi/authme/AuthMe.java index 1c9dbdcfd..c381457e6 100644 --- a/src/main/java/fr/xephi/authme/AuthMe.java +++ b/src/main/java/fr/xephi/authme/AuthMe.java @@ -5,7 +5,6 @@ import ch.jalu.injector.InjectorBuilder; import com.google.common.annotations.VisibleForTesting; import fr.xephi.authme.api.NewAPI; import fr.xephi.authme.command.CommandHandler; -import fr.xephi.authme.data.auth.PlayerCache; import fr.xephi.authme.datasource.DataSource; import fr.xephi.authme.initialization.DataFolder; import fr.xephi.authme.initialization.DataSourceProvider; @@ -254,10 +253,6 @@ public class AuthMe extends JavaPlugin { * @param injector the injector */ void instantiateServices(Injector injector) { - // PlayerCache is still injected statically sometimes - PlayerCache playerCache = PlayerCache.getInstance(); - injector.register(PlayerCache.class, playerCache); - database = injector.getSingleton(DataSource.class); permsMan = injector.getSingleton(PermissionsManager.class); bukkitService = injector.getSingleton(BukkitService.class); diff --git a/src/main/java/fr/xephi/authme/data/auth/PlayerCache.java b/src/main/java/fr/xephi/authme/data/auth/PlayerCache.java index 1ae08f8a9..46fafbf77 100644 --- a/src/main/java/fr/xephi/authme/data/auth/PlayerCache.java +++ b/src/main/java/fr/xephi/authme/data/auth/PlayerCache.java @@ -1,6 +1,7 @@ package fr.xephi.authme.data.auth; +import java.util.Map; import java.util.concurrent.ConcurrentHashMap; /** @@ -8,55 +9,31 @@ import java.util.concurrent.ConcurrentHashMap; */ public class PlayerCache { - private volatile static PlayerCache singleton; - private final ConcurrentHashMap cache; + private final Map cache = new ConcurrentHashMap<>(); - private PlayerCache() { - cache = new ConcurrentHashMap<>(); + PlayerCache() { } /** - * Method getInstance. + * Adds the given auth object to the player cache (for the name defined in the PlayerAuth). * - * @return PlayerCache + * @param auth the player auth object to save */ - public static PlayerCache getInstance() { - if (singleton == null) { - singleton = new PlayerCache(); - } - - return singleton; - } - - /** - * Method addPlayer. - * - * @param auth PlayerAuth - */ - public void addPlayer(PlayerAuth auth) { + public void updatePlayer(PlayerAuth auth) { cache.put(auth.getNickname().toLowerCase(), auth); } /** - * Method updatePlayer. + * Removes a player from the player cache. * - * @param auth PlayerAuth - */ - public void updatePlayer(PlayerAuth auth) { - cache.put(auth.getNickname(), auth); - } - - /** - * Method removePlayer. - * - * @param user String + * @param user name of the player to remove */ public void removePlayer(String user) { cache.remove(user.toLowerCase()); } /** - * get player's authenticated status. + * Get whether a player is authenticated (i.e. whether he is present in the player cache). * * @param user player's name * @@ -67,31 +44,29 @@ public class PlayerCache { } /** - * Method getAuth. + * Returns the PlayerAuth associated with the given user, if available. * - * @param user String + * @param user name of the player * - * @return PlayerAuth + * @return the associated auth object, or null if not available */ public PlayerAuth getAuth(String user) { return cache.get(user.toLowerCase()); } /** - * Method getLogged. - * - * @return int + * @return number of logged in players */ public int getLogged() { return cache.size(); } /** - * Method getCache. + * Returns the player cache data. * - * @return ConcurrentHashMap + * @return all player auths inside the player cache */ - public ConcurrentHashMap getCache() { + public Map getCache() { return this.cache; } diff --git a/src/main/java/fr/xephi/authme/datasource/FlatFile.java b/src/main/java/fr/xephi/authme/datasource/FlatFile.java index 95e2689a4..928300642 100644 --- a/src/main/java/fr/xephi/authme/datasource/FlatFile.java +++ b/src/main/java/fr/xephi/authme/datasource/FlatFile.java @@ -2,7 +2,6 @@ package fr.xephi.authme.datasource; import fr.xephi.authme.ConsoleLogger; import fr.xephi.authme.data.auth.PlayerAuth; -import fr.xephi.authme.data.auth.PlayerCache; import fr.xephi.authme.security.crypts.HashedPassword; import java.io.BufferedReader; @@ -358,7 +357,7 @@ public class FlatFile implements DataSource { @Override public boolean isLogged(String user) { - return PlayerCache.getInstance().isAuthenticated(user); + throw new UnsupportedOperationException("Flat file no longer supported"); } @Override diff --git a/src/main/java/fr/xephi/authme/listener/protocollib/InventoryPacketAdapter.java b/src/main/java/fr/xephi/authme/listener/protocollib/InventoryPacketAdapter.java index 051507651..d4f5491b5 100644 --- a/src/main/java/fr/xephi/authme/listener/protocollib/InventoryPacketAdapter.java +++ b/src/main/java/fr/xephi/authme/listener/protocollib/InventoryPacketAdapter.java @@ -24,10 +24,9 @@ import com.comphenix.protocol.events.PacketAdapter; import com.comphenix.protocol.events.PacketContainer; import com.comphenix.protocol.events.PacketEvent; import com.comphenix.protocol.reflect.StructureModifier; - import fr.xephi.authme.AuthMe; +import fr.xephi.authme.ConsoleLogger; import fr.xephi.authme.data.auth.PlayerCache; - import org.bukkit.Material; import org.bukkit.entity.Player; import org.bukkit.inventory.ItemStack; @@ -35,7 +34,6 @@ import org.bukkit.inventory.ItemStack; import java.lang.reflect.InvocationTargetException; import java.util.Arrays; import java.util.List; -import java.util.logging.Level; class InventoryPacketAdapter extends PacketAdapter { @@ -47,8 +45,11 @@ class InventoryPacketAdapter extends PacketAdapter { private static final int MAIN_SIZE = 27; private static final int HOTBAR_SIZE = 9; - InventoryPacketAdapter(AuthMe plugin) { + private final PlayerCache playerCache; + + InventoryPacketAdapter(AuthMe plugin, PlayerCache playerCache) { super(plugin, PacketType.Play.Server.SET_SLOT, PacketType.Play.Server.WINDOW_ITEMS); + this.playerCache = playerCache; } @Override @@ -57,7 +58,7 @@ class InventoryPacketAdapter extends PacketAdapter { PacketContainer packet = packetEvent.getPacket(); byte windowId = packet.getIntegers().read(0).byteValue(); - if (windowId == PLAYER_INVENTORY && !PlayerCache.getInstance().isAuthenticated(player.getName())) { + if (windowId == PLAYER_INVENTORY && !playerCache.isAuthenticated(player.getName())) { packetEvent.setCancelled(true); } } @@ -92,7 +93,7 @@ class InventoryPacketAdapter extends PacketAdapter { try { protocolManager.sendServerPacket(player, inventoryPacket, false); } catch (InvocationTargetException invocationExc) { - plugin.getLogger().log(Level.WARNING, "Error during sending blank inventory", invocationExc); + ConsoleLogger.logException("Error during sending blank inventory", invocationExc); } } } diff --git a/src/main/java/fr/xephi/authme/listener/protocollib/ProtocolLibService.java b/src/main/java/fr/xephi/authme/listener/protocollib/ProtocolLibService.java index 77a75ac04..585a810ab 100644 --- a/src/main/java/fr/xephi/authme/listener/protocollib/ProtocolLibService.java +++ b/src/main/java/fr/xephi/authme/listener/protocollib/ProtocolLibService.java @@ -57,14 +57,14 @@ public class ProtocolLibService implements SettingsDependent { // Set up packet adapters if (protectInvBeforeLogin && inventoryPacketAdapter == null) { - inventoryPacketAdapter = new InventoryPacketAdapter(plugin); + inventoryPacketAdapter = new InventoryPacketAdapter(plugin, playerCache); inventoryPacketAdapter.register(); } else if (inventoryPacketAdapter != null) { inventoryPacketAdapter.unregister(); inventoryPacketAdapter = null; } if (denyTabCompleteBeforeLogin && tabCompletePacketAdapter == null) { - tabCompletePacketAdapter = new TabCompletePacketAdapter(plugin); + tabCompletePacketAdapter = new TabCompletePacketAdapter(plugin, playerCache); tabCompletePacketAdapter.register(); } else if (tabCompletePacketAdapter != null) { tabCompletePacketAdapter.unregister(); diff --git a/src/main/java/fr/xephi/authme/listener/protocollib/TabCompletePacketAdapter.java b/src/main/java/fr/xephi/authme/listener/protocollib/TabCompletePacketAdapter.java index daf686389..4d3a24a81 100644 --- a/src/main/java/fr/xephi/authme/listener/protocollib/TabCompletePacketAdapter.java +++ b/src/main/java/fr/xephi/authme/listener/protocollib/TabCompletePacketAdapter.java @@ -12,19 +12,22 @@ import fr.xephi.authme.data.auth.PlayerCache; class TabCompletePacketAdapter extends PacketAdapter { - TabCompletePacketAdapter(AuthMe plugin) { + private final PlayerCache playerCache; + + TabCompletePacketAdapter(AuthMe plugin, PlayerCache playerCache) { super(plugin, ListenerPriority.NORMAL, PacketType.Play.Client.TAB_COMPLETE); + this.playerCache = playerCache; } @Override public void onPacketReceiving(PacketEvent event) { if (event.getPacketType() == PacketType.Play.Client.TAB_COMPLETE) { try { - if (!PlayerCache.getInstance().isAuthenticated(event.getPlayer().getName().toLowerCase())) { + if (!playerCache.isAuthenticated(event.getPlayer().getName())) { event.setCancelled(true); } } catch (FieldAccessException e) { - ConsoleLogger.warning("Couldn't access field."); + ConsoleLogger.logException("Couldn't access field:", e); } } } 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 70f68480a..e4b805364 100644 --- a/src/main/java/fr/xephi/authme/process/login/AsynchronousLogin.java +++ b/src/main/java/fr/xephi/authme/process/login/AsynchronousLogin.java @@ -235,7 +235,7 @@ public class AsynchronousLogin implements AsynchronousProcess { ConsoleLogger.fine(player.getName() + " logged in!"); // makes player isLoggedin via API - playerCache.addPlayer(auth); + playerCache.updatePlayer(auth); dataSource.setLogged(name); // As the scheduling executes the Task most likely after the current