Skip to content

Commit

Permalink
feat: 优化了LoginPlayerHelper类的线程安全性和性能
Browse files Browse the repository at this point in the history
  • Loading branch information
shulng committed Oct 1, 2024
1 parent febe8bc commit 5ebb528
Showing 1 changed file with 74 additions and 124 deletions.
198 changes: 74 additions & 124 deletions src/main/java/cc/baka9/catseedlogin/bukkit/object/LoginPlayerHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
Expand All @@ -28,170 +28,121 @@
import cc.baka9.catseedlogin.bukkit.database.Cache;

public class LoginPlayerHelper {
private static final Set<LoginPlayer> set = new HashSet<>();
private static final Set<LoginPlayer> set = ConcurrentHashMap.newKeySet();
private static final Map<String, Long> playerExitTimes = new ConcurrentHashMap<>();

public static List<LoginPlayer> getList(){
return new ArrayList<>(set);
}

public static void add(LoginPlayer lp){
synchronized (set) {

set.add(lp);
}
set.add(lp);
}

public static void remove(LoginPlayer lp){
synchronized (set) {

set.remove(lp);
}
set.remove(lp);
}

public static void remove(String name){
synchronized (set) {
for (LoginPlayer lp : set) {
if (lp.getName().equals(name)) {
set.remove(lp);
break;
}
Iterator<LoginPlayer> iterator = set.iterator();
while (iterator.hasNext()) {
LoginPlayer lp = iterator.next();
if (lp.getName().equals(name)) {
iterator.remove();
break;
}
}
}

public static boolean isLogin(String name){
synchronized (set) {
if (Config.Settings.BedrockLoginBypass && isFloodgatePlayer(name)){
return true;
}
if (Config.Settings.LoginwiththesameIP && recordCurrentIP(name)){
if (Config.Settings.BedrockLoginBypass && isFloodgatePlayer(name)){
return true;
}
if (Config.Settings.LoginwiththesameIP && recordCurrentIP(name)){
return true;
}
for (LoginPlayer lp : set) {
if (lp.getName().equals(name)) {
return true;
}
for (LoginPlayer lp : set) {
if (lp.getName().equals(name)) {
return true;
}
}
return false;
}
return false;
}

public static boolean isRegister(String name){
if (Config.Settings.BedrockLoginBypass && isFloodgatePlayer(name)){
return true;
}
return Cache.getIgnoreCase(name) != null;

return Config.Settings.BedrockLoginBypass && isFloodgatePlayer(name) || Cache.getIgnoreCase(name) != null;
}

public static boolean recordCurrentIP(String name) {
Player player = Bukkit.getPlayerExact(name);
return player != null && recordCurrentIP(player);
}

public static boolean recordCurrentIP(Player player) {
String playerName = player.getName();
String currentIP = player.getAddress() != null ? player.getAddress().getAddress().getHostAddress() : null;
public static boolean recordCurrentIP(Player player) {
String currentIP = (player.getAddress() != null) ? player.getAddress().getAddress().getHostAddress() : null;

if (currentIP == null) {
return false;
}
if (currentIP == null) {
return false;
}

LoginPlayer storedPlayer = Cache.getIgnoreCase(playerName);
LoginPlayer storedPlayer = Cache.getIgnoreCase(player.getName());

if (storedPlayer != null) {
List<String> storedIPs = getStoredIPs(storedPlayer);
Long exitTime = playerExitTimes.get(playerName);
if (storedPlayer != null) {
List<String> storedIPs = getStoredIPs(storedPlayer);
Long exitTime = playerExitTimes.get(player.getName());

try {
if (InetAddress.getByName(currentIP).isLoopbackAddress()) {
try {
if (InetAddress.getByName(currentIP).isLoopbackAddress()) {
return false;
}
} catch (UnknownHostException e) {
return false;
}
} catch (UnknownHostException e) {
return false;
}

if (Config.Settings.IPTimeout == 0) {
return storedIPs.contains(currentIP);
} else {
return exitTime != null && storedIPs.contains(currentIP) && (System.currentTimeMillis() - exitTime) <= (long) Config.Settings.IPTimeout * 60 * 1000L;
if (Config.Settings.IPTimeout == 0) {
return storedIPs.contains(currentIP);
} else {
return exitTime != null && storedIPs.contains(currentIP) &&
(System.currentTimeMillis() - exitTime) <= (long) Config.Settings.IPTimeout * 60 * 1000L;
}
}
}

return false;
}


public void recordPlayerExitTime(String playerName) {
if (Config.Settings.IPTimeout != 0 && isLogin(playerName)) {
playerExitTimes.put(playerName, System.currentTimeMillis());
return false;
}
}








public void onPlayerQuit(String playerName) {
public void recordPlayerExitTime(String playerName) {
if (Config.Settings.IPTimeout != 0 && isLogin(playerName)) {
playerExitTimes.put(playerName, System.currentTimeMillis());
}
playerExitTimes.put(playerName, System.currentTimeMillis());
}
}



















public static List<String> getStoredIPs(LoginPlayer lp) {
List<String> storedIPs = new ArrayList<>();

String ipsString = lp.getIps();
if (ipsString != null) {
String[] ipsArray = ipsString.split(";");
storedIPs.addAll(Arrays.asList(ipsArray));
public void onPlayerQuit(String playerName) {
recordPlayerExitTime(playerName);
}

return storedIPs;
}





public static List<String> getStoredIPs(LoginPlayer lp) {
List<String> storedIPs = new ArrayList<>();
String ipsString = lp.getIps();
if (ipsString != null) {
String[] ipsArray = ipsString.split(";");
storedIPs.addAll(Arrays.asList(ipsArray));
}
return storedIPs;
}

public static boolean isFloodgatePlayer(String name) {
Player player = Bukkit.getPlayerExact(name);
return player != null && isFloodgatePlayer(player);
}

public static boolean isFloodgatePlayer(Player player) {
return Bukkit.getPluginManager().getPlugin("floodgate") != null && FloodgateApi.getInstance().isFloodgatePlayer(player.getUniqueId());
return Bukkit.getPluginManager().getPlugin("floodgate") != null &&
FloodgateApi.getInstance().isFloodgatePlayer(player.getUniqueId());
}

public static Long getLastLoginTime(String name) {
LoginPlayer loginPlayer = Cache.getIgnoreCase(name);
if (loginPlayer == null) {
return null;
}
return loginPlayer.getLastAction();
return (loginPlayer != null) ? loginPlayer.getLastAction() : null;
}

// 记录登录IP
Expand All @@ -215,25 +166,24 @@ public static void recordCurrentIP(Player player, LoginPlayer lp){

// ProtocolLib发包空背包
public static void sendBlankInventoryPacket(Player player) {
if (!Config.Settings.Emptybackpack) return;

ProtocolManager protocolManager = ProtocolLibrary.getProtocolManager();
PacketContainer inventoryPacket = protocolManager.createPacket(PacketType.Play.Server.WINDOW_ITEMS);
inventoryPacket.getIntegers().write(0, 0);
if (!Config.Settings.Emptybackpack) return;

int inventorySize = 45;
ItemStack[] blankInventory = new ItemStack[inventorySize];
Arrays.fill(blankInventory, new ItemStack(Material.AIR));
ProtocolManager protocolManager = ProtocolLibrary.getProtocolManager();
PacketContainer inventoryPacket = protocolManager.createPacket(PacketType.Play.Server.WINDOW_ITEMS);
inventoryPacket.getIntegers().write(0, 0);

StructureModifier<ItemStack[]> itemArrayModifier = inventoryPacket.getItemArrayModifier();
if (itemArrayModifier.size() > 0) {
itemArrayModifier.write(0, blankInventory);
} else {
StructureModifier<List<ItemStack>> itemListModifier = inventoryPacket.getItemListModifier();
itemListModifier.write(0, Arrays.asList(blankInventory));
}
int inventorySize = 45;
ItemStack[] blankInventory = new ItemStack[inventorySize];
Arrays.fill(blankInventory, new ItemStack(Material.AIR));

protocolManager.sendServerPacket(player, inventoryPacket, false);
}
StructureModifier<ItemStack[]> itemArrayModifier = inventoryPacket.getItemArrayModifier();
if (itemArrayModifier.size() > 0) {
itemArrayModifier.write(0, blankInventory);
} else {
StructureModifier<List<ItemStack>> itemListModifier = inventoryPacket.getItemListModifier();
itemListModifier.write(0, Arrays.asList(blankInventory));
}

protocolManager.sendServerPacket(player, inventoryPacket, false);
}
}

0 comments on commit 5ebb528

Please sign in to comment.