Remove static PlayerCache#getInstance
This commit is contained in:
parent
b0c05afaa7
commit
2f7ebc0ecb
@ -5,7 +5,6 @@ import ch.jalu.injector.InjectorBuilder;
|
|||||||
import com.google.common.annotations.VisibleForTesting;
|
import com.google.common.annotations.VisibleForTesting;
|
||||||
import fr.xephi.authme.api.NewAPI;
|
import fr.xephi.authme.api.NewAPI;
|
||||||
import fr.xephi.authme.command.CommandHandler;
|
import fr.xephi.authme.command.CommandHandler;
|
||||||
import fr.xephi.authme.data.auth.PlayerCache;
|
|
||||||
import fr.xephi.authme.datasource.DataSource;
|
import fr.xephi.authme.datasource.DataSource;
|
||||||
import fr.xephi.authme.initialization.DataFolder;
|
import fr.xephi.authme.initialization.DataFolder;
|
||||||
import fr.xephi.authme.initialization.DataSourceProvider;
|
import fr.xephi.authme.initialization.DataSourceProvider;
|
||||||
@ -254,10 +253,6 @@ public class AuthMe extends JavaPlugin {
|
|||||||
* @param injector the injector
|
* @param injector the injector
|
||||||
*/
|
*/
|
||||||
void instantiateServices(Injector 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);
|
database = injector.getSingleton(DataSource.class);
|
||||||
permsMan = injector.getSingleton(PermissionsManager.class);
|
permsMan = injector.getSingleton(PermissionsManager.class);
|
||||||
bukkitService = injector.getSingleton(BukkitService.class);
|
bukkitService = injector.getSingleton(BukkitService.class);
|
||||||
|
|||||||
@ -1,6 +1,7 @@
|
|||||||
package fr.xephi.authme.data.auth;
|
package fr.xephi.authme.data.auth;
|
||||||
|
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
import java.util.concurrent.ConcurrentHashMap;
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -8,55 +9,31 @@ import java.util.concurrent.ConcurrentHashMap;
|
|||||||
*/
|
*/
|
||||||
public class PlayerCache {
|
public class PlayerCache {
|
||||||
|
|
||||||
private volatile static PlayerCache singleton;
|
private final Map<String, PlayerAuth> cache = new ConcurrentHashMap<>();
|
||||||
private final ConcurrentHashMap<String, PlayerAuth> cache;
|
|
||||||
|
|
||||||
private PlayerCache() {
|
PlayerCache() {
|
||||||
cache = new ConcurrentHashMap<>();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 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() {
|
public void updatePlayer(PlayerAuth auth) {
|
||||||
if (singleton == null) {
|
|
||||||
singleton = new PlayerCache();
|
|
||||||
}
|
|
||||||
|
|
||||||
return singleton;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Method addPlayer.
|
|
||||||
*
|
|
||||||
* @param auth PlayerAuth
|
|
||||||
*/
|
|
||||||
public void addPlayer(PlayerAuth auth) {
|
|
||||||
cache.put(auth.getNickname().toLowerCase(), auth);
|
cache.put(auth.getNickname().toLowerCase(), auth);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Method updatePlayer.
|
* Removes a player from the player cache.
|
||||||
*
|
*
|
||||||
* @param auth PlayerAuth
|
* @param user name of the player to remove
|
||||||
*/
|
|
||||||
public void updatePlayer(PlayerAuth auth) {
|
|
||||||
cache.put(auth.getNickname(), auth);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Method removePlayer.
|
|
||||||
*
|
|
||||||
* @param user String
|
|
||||||
*/
|
*/
|
||||||
public void removePlayer(String user) {
|
public void removePlayer(String user) {
|
||||||
cache.remove(user.toLowerCase());
|
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
|
* @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) {
|
public PlayerAuth getAuth(String user) {
|
||||||
return cache.get(user.toLowerCase());
|
return cache.get(user.toLowerCase());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Method getLogged.
|
* @return number of logged in players
|
||||||
*
|
|
||||||
* @return int
|
|
||||||
*/
|
*/
|
||||||
public int getLogged() {
|
public int getLogged() {
|
||||||
return cache.size();
|
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;
|
return this.cache;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -2,7 +2,6 @@ package fr.xephi.authme.datasource;
|
|||||||
|
|
||||||
import fr.xephi.authme.ConsoleLogger;
|
import fr.xephi.authme.ConsoleLogger;
|
||||||
import fr.xephi.authme.data.auth.PlayerAuth;
|
import fr.xephi.authme.data.auth.PlayerAuth;
|
||||||
import fr.xephi.authme.data.auth.PlayerCache;
|
|
||||||
import fr.xephi.authme.security.crypts.HashedPassword;
|
import fr.xephi.authme.security.crypts.HashedPassword;
|
||||||
|
|
||||||
import java.io.BufferedReader;
|
import java.io.BufferedReader;
|
||||||
@ -358,7 +357,7 @@ public class FlatFile implements DataSource {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean isLogged(String user) {
|
public boolean isLogged(String user) {
|
||||||
return PlayerCache.getInstance().isAuthenticated(user);
|
throw new UnsupportedOperationException("Flat file no longer supported");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@ -24,10 +24,9 @@ import com.comphenix.protocol.events.PacketAdapter;
|
|||||||
import com.comphenix.protocol.events.PacketContainer;
|
import com.comphenix.protocol.events.PacketContainer;
|
||||||
import com.comphenix.protocol.events.PacketEvent;
|
import com.comphenix.protocol.events.PacketEvent;
|
||||||
import com.comphenix.protocol.reflect.StructureModifier;
|
import com.comphenix.protocol.reflect.StructureModifier;
|
||||||
|
|
||||||
import fr.xephi.authme.AuthMe;
|
import fr.xephi.authme.AuthMe;
|
||||||
|
import fr.xephi.authme.ConsoleLogger;
|
||||||
import fr.xephi.authme.data.auth.PlayerCache;
|
import fr.xephi.authme.data.auth.PlayerCache;
|
||||||
|
|
||||||
import org.bukkit.Material;
|
import org.bukkit.Material;
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
import org.bukkit.inventory.ItemStack;
|
import org.bukkit.inventory.ItemStack;
|
||||||
@ -35,7 +34,6 @@ import org.bukkit.inventory.ItemStack;
|
|||||||
import java.lang.reflect.InvocationTargetException;
|
import java.lang.reflect.InvocationTargetException;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.logging.Level;
|
|
||||||
|
|
||||||
class InventoryPacketAdapter extends PacketAdapter {
|
class InventoryPacketAdapter extends PacketAdapter {
|
||||||
|
|
||||||
@ -47,8 +45,11 @@ class InventoryPacketAdapter extends PacketAdapter {
|
|||||||
private static final int MAIN_SIZE = 27;
|
private static final int MAIN_SIZE = 27;
|
||||||
private static final int HOTBAR_SIZE = 9;
|
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);
|
super(plugin, PacketType.Play.Server.SET_SLOT, PacketType.Play.Server.WINDOW_ITEMS);
|
||||||
|
this.playerCache = playerCache;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -57,7 +58,7 @@ class InventoryPacketAdapter extends PacketAdapter {
|
|||||||
PacketContainer packet = packetEvent.getPacket();
|
PacketContainer packet = packetEvent.getPacket();
|
||||||
|
|
||||||
byte windowId = packet.getIntegers().read(0).byteValue();
|
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);
|
packetEvent.setCancelled(true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -92,7 +93,7 @@ class InventoryPacketAdapter extends PacketAdapter {
|
|||||||
try {
|
try {
|
||||||
protocolManager.sendServerPacket(player, inventoryPacket, false);
|
protocolManager.sendServerPacket(player, inventoryPacket, false);
|
||||||
} catch (InvocationTargetException invocationExc) {
|
} catch (InvocationTargetException invocationExc) {
|
||||||
plugin.getLogger().log(Level.WARNING, "Error during sending blank inventory", invocationExc);
|
ConsoleLogger.logException("Error during sending blank inventory", invocationExc);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -57,14 +57,14 @@ public class ProtocolLibService implements SettingsDependent {
|
|||||||
|
|
||||||
// Set up packet adapters
|
// Set up packet adapters
|
||||||
if (protectInvBeforeLogin && inventoryPacketAdapter == null) {
|
if (protectInvBeforeLogin && inventoryPacketAdapter == null) {
|
||||||
inventoryPacketAdapter = new InventoryPacketAdapter(plugin);
|
inventoryPacketAdapter = new InventoryPacketAdapter(plugin, playerCache);
|
||||||
inventoryPacketAdapter.register();
|
inventoryPacketAdapter.register();
|
||||||
} else if (inventoryPacketAdapter != null) {
|
} else if (inventoryPacketAdapter != null) {
|
||||||
inventoryPacketAdapter.unregister();
|
inventoryPacketAdapter.unregister();
|
||||||
inventoryPacketAdapter = null;
|
inventoryPacketAdapter = null;
|
||||||
}
|
}
|
||||||
if (denyTabCompleteBeforeLogin && tabCompletePacketAdapter == null) {
|
if (denyTabCompleteBeforeLogin && tabCompletePacketAdapter == null) {
|
||||||
tabCompletePacketAdapter = new TabCompletePacketAdapter(plugin);
|
tabCompletePacketAdapter = new TabCompletePacketAdapter(plugin, playerCache);
|
||||||
tabCompletePacketAdapter.register();
|
tabCompletePacketAdapter.register();
|
||||||
} else if (tabCompletePacketAdapter != null) {
|
} else if (tabCompletePacketAdapter != null) {
|
||||||
tabCompletePacketAdapter.unregister();
|
tabCompletePacketAdapter.unregister();
|
||||||
|
|||||||
@ -12,19 +12,22 @@ import fr.xephi.authme.data.auth.PlayerCache;
|
|||||||
|
|
||||||
class TabCompletePacketAdapter extends PacketAdapter {
|
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);
|
super(plugin, ListenerPriority.NORMAL, PacketType.Play.Client.TAB_COMPLETE);
|
||||||
|
this.playerCache = playerCache;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onPacketReceiving(PacketEvent event) {
|
public void onPacketReceiving(PacketEvent event) {
|
||||||
if (event.getPacketType() == PacketType.Play.Client.TAB_COMPLETE) {
|
if (event.getPacketType() == PacketType.Play.Client.TAB_COMPLETE) {
|
||||||
try {
|
try {
|
||||||
if (!PlayerCache.getInstance().isAuthenticated(event.getPlayer().getName().toLowerCase())) {
|
if (!playerCache.isAuthenticated(event.getPlayer().getName())) {
|
||||||
event.setCancelled(true);
|
event.setCancelled(true);
|
||||||
}
|
}
|
||||||
} catch (FieldAccessException e) {
|
} catch (FieldAccessException e) {
|
||||||
ConsoleLogger.warning("Couldn't access field.");
|
ConsoleLogger.logException("Couldn't access field:", e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -235,7 +235,7 @@ public class AsynchronousLogin implements AsynchronousProcess {
|
|||||||
ConsoleLogger.fine(player.getName() + " logged in!");
|
ConsoleLogger.fine(player.getName() + " logged in!");
|
||||||
|
|
||||||
// makes player isLoggedin via API
|
// makes player isLoggedin via API
|
||||||
playerCache.addPlayer(auth);
|
playerCache.updatePlayer(auth);
|
||||||
dataSource.setLogged(name);
|
dataSource.setLogged(name);
|
||||||
|
|
||||||
// As the scheduling executes the Task most likely after the current
|
// As the scheduling executes the Task most likely after the current
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user