feat: 优化结构
This commit is contained in:
@@ -38,11 +38,11 @@ public class PlaceholderHook extends PlaceholderExpansion {
|
|||||||
return BallAPI.getInstance().getLocalServerInfo().getName();
|
return BallAPI.getInstance().getLocalServerInfo().getName();
|
||||||
}
|
}
|
||||||
case "proxy_id": {
|
case "proxy_id": {
|
||||||
return BallAPI.getInstance().getPlayerInfo(player.getUniqueId()).getProxyServer();
|
return BallAPI.getInstance().getAllPlayerInfo(player.getUniqueId()).getProxyServer();
|
||||||
}
|
}
|
||||||
case "proxy_name": {
|
case "proxy_name": {
|
||||||
String id = BallAPI.getInstance().getPlayerInfo(player.getUniqueId()).getProxyServer();
|
String id = BallAPI.getInstance().getAllPlayerInfo(player.getUniqueId()).getProxyServer();
|
||||||
return BallAPI.getInstance().getServerInfo(id).getName();
|
return BallAPI.getInstance().getAllServerInfo(id).getName();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
|
@@ -56,7 +56,7 @@ public class HamsterBallPlugin extends Plugin {
|
|||||||
new ServerOnlineEvent(BallAPI.getInstance().getLocalServerInfo())
|
new ServerOnlineEvent(BallAPI.getInstance().getLocalServerInfo())
|
||||||
);
|
);
|
||||||
// 移除失效的在线玩家
|
// 移除失效的在线玩家
|
||||||
BallAPI.getInstance().getPlayerInfo().values()
|
BallAPI.getInstance().getAllPlayerInfo().values()
|
||||||
.stream()
|
.stream()
|
||||||
.filter(BallPlayerInfo::isOnline)
|
.filter(BallPlayerInfo::isOnline)
|
||||||
.filter(o -> BallAPI.getInstance().isLocalServer(o.getProxyServer()))
|
.filter(o -> BallAPI.getInstance().isLocalServer(o.getProxyServer()))
|
||||||
|
@@ -42,9 +42,9 @@ public abstract class BallAPI {
|
|||||||
@Getter
|
@Getter
|
||||||
protected static BallAPI instance;
|
protected static BallAPI instance;
|
||||||
@NotNull
|
@NotNull
|
||||||
protected final Map<String, BallServerInfo> serverInfo;
|
protected final Map<String, BallServerInfo> allServerInfo;
|
||||||
@NotNull
|
@NotNull
|
||||||
protected final Map<UUID, BallPlayerInfo> playerInfo;
|
protected final Map<UUID, BallPlayerInfo> allPlayerInfo;
|
||||||
@NotNull
|
@NotNull
|
||||||
private final BallServerInfo localServerInfo;
|
private final BallServerInfo localServerInfo;
|
||||||
@Nullable
|
@Nullable
|
||||||
@@ -64,8 +64,8 @@ public abstract class BallAPI {
|
|||||||
this.redisClient = redisClient;
|
this.redisClient = redisClient;
|
||||||
subConnection = redisClient.connectPubSub(BallMessage.CODEC);
|
subConnection = redisClient.connectPubSub(BallMessage.CODEC);
|
||||||
pubConnection = redisClient.connectPubSub(BallMessage.CODEC);
|
pubConnection = redisClient.connectPubSub(BallMessage.CODEC);
|
||||||
serverInfo = new ConcurrentHashMap<>();
|
allServerInfo = new ConcurrentHashMap<>();
|
||||||
playerInfo = new ConcurrentHashMap<>();
|
allPlayerInfo = new ConcurrentHashMap<>();
|
||||||
eventBus = new AsyncEventBus("HamsterBall - EventBus", CoreAPI.getInstance().getExecutorService());
|
eventBus = new AsyncEventBus("HamsterBall - EventBus", CoreAPI.getInstance().getExecutorService());
|
||||||
eventBus.register(BallCommonListener.INSTANCE);
|
eventBus.register(BallCommonListener.INSTANCE);
|
||||||
if (debug) {
|
if (debug) {
|
||||||
@@ -114,7 +114,7 @@ public abstract class BallAPI {
|
|||||||
try (ResultSet set = statement.executeQuery()) {
|
try (ResultSet set = statement.executeQuery()) {
|
||||||
while (set.next()) {
|
while (set.next()) {
|
||||||
String serverID = set.getString("id");
|
String serverID = set.getString("id");
|
||||||
serverInfo.put(serverID, new BallServerInfo(
|
allServerInfo.put(serverID, new BallServerInfo(
|
||||||
serverID,
|
serverID,
|
||||||
set.getString("name"),
|
set.getString("name"),
|
||||||
BallServerType.valueOf(set.getString("type")),
|
BallServerType.valueOf(set.getString("type")),
|
||||||
@@ -130,7 +130,7 @@ public abstract class BallAPI {
|
|||||||
try (ResultSet set = statement.executeQuery()) {
|
try (ResultSet set = statement.executeQuery()) {
|
||||||
while (set.next()) {
|
while (set.next()) {
|
||||||
UUID uuid = UUID.fromString(set.getString("uuid"));
|
UUID uuid = UUID.fromString(set.getString("uuid"));
|
||||||
playerInfo.put(uuid, new BallPlayerInfo(uuid,
|
allPlayerInfo.put(uuid, new BallPlayerInfo(uuid,
|
||||||
set.getString("name"),
|
set.getString("name"),
|
||||||
set.getString("game_server"),
|
set.getString("game_server"),
|
||||||
set.getString("proxy_server"),
|
set.getString("proxy_server"),
|
||||||
@@ -293,7 +293,7 @@ public abstract class BallAPI {
|
|||||||
public void sendMessageToPlayer(@NotNull Collection<UUID> receivers, @NotNull DisplayMessage message, boolean cache) {
|
public void sendMessageToPlayer(@NotNull Collection<UUID> receivers, @NotNull DisplayMessage message, boolean cache) {
|
||||||
if (cache) {
|
if (cache) {
|
||||||
for (UUID receiver : receivers) {
|
for (UUID receiver : receivers) {
|
||||||
BallPlayerInfo info = getPlayerInfo(receiver);
|
BallPlayerInfo info = getAllPlayerInfo(receiver);
|
||||||
if (info != null && info.isOnline()) {
|
if (info != null && info.isOnline()) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@@ -470,8 +470,8 @@ public abstract class BallAPI {
|
|||||||
* @param serverID 服务器ID
|
* @param serverID 服务器ID
|
||||||
* @return 可能为 null
|
* @return 可能为 null
|
||||||
*/
|
*/
|
||||||
public BallServerInfo getServerInfo(@NotNull String serverID) {
|
public BallServerInfo getAllServerInfo(@NotNull String serverID) {
|
||||||
return serverInfo.get(serverID);
|
return allServerInfo.get(serverID);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -480,8 +480,8 @@ public abstract class BallAPI {
|
|||||||
* @param uuid 玩家的 UUID
|
* @param uuid 玩家的 UUID
|
||||||
* @return 玩家信息
|
* @return 玩家信息
|
||||||
*/
|
*/
|
||||||
public BallPlayerInfo getPlayerInfo(@NotNull UUID uuid) {
|
public BallPlayerInfo getAllPlayerInfo(@NotNull UUID uuid) {
|
||||||
return playerInfo.get(uuid);
|
return allPlayerInfo.get(uuid);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -490,8 +490,8 @@ public abstract class BallAPI {
|
|||||||
* @param playerName 玩家名称
|
* @param playerName 玩家名称
|
||||||
* @return 玩家信息
|
* @return 玩家信息
|
||||||
*/
|
*/
|
||||||
public BallPlayerInfo getPlayerInfo(@NotNull String playerName) {
|
public BallPlayerInfo getAllPlayerInfo(@NotNull String playerName) {
|
||||||
for (BallPlayerInfo info : playerInfo.values()) {
|
for (BallPlayerInfo info : allPlayerInfo.values()) {
|
||||||
if (info.getName().equalsIgnoreCase(playerName)) {
|
if (info.getName().equalsIgnoreCase(playerName)) {
|
||||||
return info;
|
return info;
|
||||||
}
|
}
|
||||||
@@ -506,7 +506,7 @@ public abstract class BallAPI {
|
|||||||
* @return 玩家信息
|
* @return 玩家信息
|
||||||
*/
|
*/
|
||||||
public BallPlayerInfo getPlayerInfoExact(@NotNull String playerName) {
|
public BallPlayerInfo getPlayerInfoExact(@NotNull String playerName) {
|
||||||
for (BallPlayerInfo info : playerInfo.values()) {
|
for (BallPlayerInfo info : allPlayerInfo.values()) {
|
||||||
if (info.getName().equals(playerName)) {
|
if (info.getName().equals(playerName)) {
|
||||||
return info;
|
return info;
|
||||||
}
|
}
|
||||||
@@ -522,7 +522,7 @@ public abstract class BallAPI {
|
|||||||
*/
|
*/
|
||||||
@Nullable
|
@Nullable
|
||||||
public UUID getPlayerUUID(String playerName) {
|
public UUID getPlayerUUID(String playerName) {
|
||||||
BallPlayerInfo info = getPlayerInfo(playerName);
|
BallPlayerInfo info = getAllPlayerInfo(playerName);
|
||||||
if (info == null) {
|
if (info == null) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
@@ -538,7 +538,7 @@ public abstract class BallAPI {
|
|||||||
*/
|
*/
|
||||||
@NotNull
|
@NotNull
|
||||||
public UUID getPlayerUUID(String playerName, @NotNull UUID defaultValue) {
|
public UUID getPlayerUUID(String playerName, @NotNull UUID defaultValue) {
|
||||||
BallPlayerInfo info = getPlayerInfo(playerName);
|
BallPlayerInfo info = getAllPlayerInfo(playerName);
|
||||||
if (info == null) {
|
if (info == null) {
|
||||||
return defaultValue;
|
return defaultValue;
|
||||||
}
|
}
|
||||||
@@ -553,7 +553,7 @@ public abstract class BallAPI {
|
|||||||
*/
|
*/
|
||||||
@Nullable
|
@Nullable
|
||||||
public String getPlayerName(@NotNull UUID uuid) {
|
public String getPlayerName(@NotNull UUID uuid) {
|
||||||
BallPlayerInfo info = getPlayerInfo(uuid);
|
BallPlayerInfo info = getAllPlayerInfo(uuid);
|
||||||
if (info == null) {
|
if (info == null) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
@@ -569,7 +569,7 @@ public abstract class BallAPI {
|
|||||||
*/
|
*/
|
||||||
@NotNull
|
@NotNull
|
||||||
public String getPlayerName(@NotNull UUID uuid, @NotNull String defaultValue) {
|
public String getPlayerName(@NotNull UUID uuid, @NotNull String defaultValue) {
|
||||||
BallPlayerInfo info = getPlayerInfo(uuid);
|
BallPlayerInfo info = getAllPlayerInfo(uuid);
|
||||||
if (info == null) {
|
if (info == null) {
|
||||||
return defaultValue;
|
return defaultValue;
|
||||||
}
|
}
|
||||||
|
@@ -19,22 +19,22 @@ public class BallCommonListener {
|
|||||||
@Subscribe
|
@Subscribe
|
||||||
public void onBallPlayerConnectServer(@NotNull BallPlayerConnectServerEvent event) {
|
public void onBallPlayerConnectServer(@NotNull BallPlayerConnectServerEvent event) {
|
||||||
BallPlayerInfo info = event.getPlayerInfo();
|
BallPlayerInfo info = event.getPlayerInfo();
|
||||||
BallAPI.getInstance().getPlayerInfo().put(info.getUuid(), info);
|
BallAPI.getInstance().getAllPlayerInfo().put(info.getUuid(), info);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Subscribe
|
@Subscribe
|
||||||
public void onBallPlayerInfoUpdate(@NotNull BallPlayerInfoUpdateEvent event) {
|
public void onBallPlayerInfoUpdate(@NotNull BallPlayerInfoUpdateEvent event) {
|
||||||
BallPlayerInfo info = event.getPlayerInfo();
|
BallPlayerInfo info = event.getPlayerInfo();
|
||||||
BallAPI.getInstance().getPlayerInfo().put(info.getUuid(), info);
|
BallAPI.getInstance().getAllPlayerInfo().put(info.getUuid(), info);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Subscribe
|
@Subscribe
|
||||||
public void onServerOnline(@NotNull ServerOnlineEvent event) {
|
public void onServerOnline(@NotNull ServerOnlineEvent event) {
|
||||||
BallServerInfo info = event.getServerInfo();
|
BallServerInfo info = event.getServerInfo();
|
||||||
BallAPI.getInstance().getServerInfo().put(info.getId(), info);
|
BallAPI.getInstance().getAllServerInfo().put(info.getId(), info);
|
||||||
switch (info.getType()) {
|
switch (info.getType()) {
|
||||||
case GAME: {
|
case GAME: {
|
||||||
BallAPI.getInstance().getPlayerInfo().forEach((uuid, playerInfo) -> {
|
BallAPI.getInstance().getAllPlayerInfo().forEach((uuid, playerInfo) -> {
|
||||||
if (playerInfo.getGameServer().equals(info.getId())) {
|
if (playerInfo.getGameServer().equals(info.getId())) {
|
||||||
playerInfo.setOnline(false);
|
playerInfo.setOnline(false);
|
||||||
}
|
}
|
||||||
@@ -42,7 +42,7 @@ public class BallCommonListener {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case PROXY: {
|
case PROXY: {
|
||||||
BallAPI.getInstance().getPlayerInfo().forEach((uuid, playerInfo) -> {
|
BallAPI.getInstance().getAllPlayerInfo().forEach((uuid, playerInfo) -> {
|
||||||
if (playerInfo.getProxyServer().equals(info.getId())) {
|
if (playerInfo.getProxyServer().equals(info.getId())) {
|
||||||
playerInfo.setOnline(false);
|
playerInfo.setOnline(false);
|
||||||
}
|
}
|
||||||
@@ -55,13 +55,13 @@ public class BallCommonListener {
|
|||||||
@Subscribe
|
@Subscribe
|
||||||
public void onServerOffline(@NotNull ServerOfflineEvent event) {
|
public void onServerOffline(@NotNull ServerOfflineEvent event) {
|
||||||
String serverID = event.getServerID();
|
String serverID = event.getServerID();
|
||||||
BallServerInfo info = BallAPI.getInstance().getServerInfo().remove(serverID);
|
BallServerInfo info = BallAPI.getInstance().getAllServerInfo().remove(serverID);
|
||||||
if (info == null) {
|
if (info == null) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
switch (info.getType()) {
|
switch (info.getType()) {
|
||||||
case GAME: {
|
case GAME: {
|
||||||
BallAPI.getInstance().getPlayerInfo().forEach((uuid, playerInfo) -> {
|
BallAPI.getInstance().getAllPlayerInfo().forEach((uuid, playerInfo) -> {
|
||||||
if (playerInfo.getGameServer().equals(info.getId())) {
|
if (playerInfo.getGameServer().equals(info.getId())) {
|
||||||
playerInfo.setOnline(false);
|
playerInfo.setOnline(false);
|
||||||
}
|
}
|
||||||
@@ -69,7 +69,7 @@ public class BallCommonListener {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case PROXY: {
|
case PROXY: {
|
||||||
BallAPI.getInstance().getPlayerInfo().forEach((uuid, playerInfo) -> {
|
BallAPI.getInstance().getAllPlayerInfo().forEach((uuid, playerInfo) -> {
|
||||||
if (playerInfo.getProxyServer().equals(info.getId())) {
|
if (playerInfo.getProxyServer().equals(info.getId())) {
|
||||||
playerInfo.setOnline(false);
|
playerInfo.setOnline(false);
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user