Remove static PlayerCache#getInstance

This commit is contained in:
ljacqu 2017-04-18 21:55:41 +02:00
parent b0c05afaa7
commit 2f7ebc0ecb
7 changed files with 33 additions and 60 deletions

View File

@ -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);

View File

@ -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<String, PlayerAuth> cache;
private final Map<String, PlayerAuth> 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<String, PlayerAuth> getCache() {
public Map<String, PlayerAuth> getCache() {
return this.cache;
}

View File

@ -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

View File

@ -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);
}
}
}

View File

@ -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();

View File

@ -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);
}
}
}

View File

@ -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