feat: 优化代码,添加 auto-register-game-server 功能
This commit is contained in:
@@ -73,7 +73,7 @@ public abstract class BallAPI {
|
||||
@Nullable
|
||||
private ScheduledFuture<?> lockUpdater;
|
||||
|
||||
public BallAPI(@NotNull ConfigSection config, BallServerType type) {
|
||||
public BallAPI(@NotNull ConfigSection config, @NotNull BallServerType type) {
|
||||
ConfigSection serverInfoConfig = config.getSection("server-info");
|
||||
if (serverInfoConfig == null) {
|
||||
throw new IllegalArgumentException("配置文件中未找到 server-info 节点");
|
||||
@@ -88,7 +88,7 @@ public abstract class BallAPI {
|
||||
datasource = CoreAPI.getInstance().getDataSource();
|
||||
}
|
||||
ballConfig = new BallConfig(config);
|
||||
eventBus = new AsyncEventBus("HamsterBall - EventBus", CoreAPI.getInstance().getExecutorService());
|
||||
eventBus = new AsyncEventBus("HamsterBall", CoreAPI.getInstance().getExecutorService());
|
||||
eventBus.register(BallCommonListener.INSTANCE);
|
||||
allServerInfo = new ConcurrentHashMap<>();
|
||||
allPlayerInfo = new ConcurrentHashMap<>();
|
||||
|
@@ -43,7 +43,7 @@ public class BallServerInfo {
|
||||
*/
|
||||
private int port;
|
||||
|
||||
public BallServerInfo(@NotNull ConfigSection config, BallServerType type) {
|
||||
public BallServerInfo(@NotNull ConfigSection config, @NotNull BallServerType type) {
|
||||
Map<String, String> env = System.getenv();
|
||||
id = env.getOrDefault("BALL_SERVER_ID", config.getString("id"));
|
||||
name = env.getOrDefault("BALL_SERVER_NAME", config.getString("name"));
|
||||
|
@@ -0,0 +1,18 @@
|
||||
package cn.hamster3.mc.plugin.ball.common.event.server;
|
||||
|
||||
import cn.hamster3.mc.plugin.ball.common.entity.BallServerInfo;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
|
||||
/**
|
||||
* 服务器上线
|
||||
*/
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
public class BallServerEvent extends BallServerInfo {
|
||||
public BallServerEvent(BallServerInfo info) {
|
||||
super(info.getId(), info.getName(), info.getType(), info.getHost(), info.getPort());
|
||||
}
|
||||
}
|
@@ -3,19 +3,16 @@ package cn.hamster3.mc.plugin.ball.common.event.server;
|
||||
import cn.hamster3.mc.plugin.ball.common.entity.BallServerInfo;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import lombok.EqualsAndHashCode;
|
||||
|
||||
/**
|
||||
* 服务器离线
|
||||
*/
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
public class ServerOfflineEvent {
|
||||
@NotNull
|
||||
private final BallServerInfo serverInfo;
|
||||
|
||||
@NotNull
|
||||
public String getServerID() {
|
||||
return serverInfo.getId();
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
public class ServerOfflineEvent extends BallServerEvent {
|
||||
public ServerOfflineEvent(BallServerInfo info) {
|
||||
super(info);
|
||||
}
|
||||
}
|
||||
|
@@ -3,15 +3,16 @@ package cn.hamster3.mc.plugin.ball.common.event.server;
|
||||
import cn.hamster3.mc.plugin.ball.common.entity.BallServerInfo;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import lombok.EqualsAndHashCode;
|
||||
|
||||
/**
|
||||
* 服务器上线
|
||||
*/
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
public class ServerOnlineEvent {
|
||||
@NotNull
|
||||
private final BallServerInfo serverInfo;
|
||||
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
public class ServerOnlineEvent extends BallServerEvent {
|
||||
public ServerOnlineEvent(BallServerInfo info) {
|
||||
super(info);
|
||||
}
|
||||
}
|
||||
|
@@ -128,12 +128,11 @@ public class BallCommonListener {
|
||||
|
||||
@Subscribe
|
||||
public void onServerOnline(ServerOnlineEvent event) {
|
||||
BallServerInfo info = event.getServerInfo();
|
||||
BallAPI.getInstance().getAllServerInfo().put(info.getId(), info);
|
||||
switch (info.getType()) {
|
||||
BallAPI.getInstance().getAllServerInfo().put(event.getId(), event);
|
||||
switch (event.getType()) {
|
||||
case GAME: {
|
||||
BallAPI.getInstance().getAllPlayerInfo().forEach((uuid, playerInfo) -> {
|
||||
if (playerInfo.getGameServer().equals(info.getId())) {
|
||||
if (playerInfo.getGameServer().equals(event.getId())) {
|
||||
playerInfo.setOnline(false);
|
||||
}
|
||||
});
|
||||
@@ -141,7 +140,7 @@ public class BallCommonListener {
|
||||
}
|
||||
case PROXY: {
|
||||
BallAPI.getInstance().getAllPlayerInfo().forEach((uuid, playerInfo) -> {
|
||||
if (playerInfo.getProxyServer().equals(info.getId())) {
|
||||
if (playerInfo.getProxyServer().equals(event.getId())) {
|
||||
playerInfo.setOnline(false);
|
||||
}
|
||||
});
|
||||
@@ -152,7 +151,7 @@ public class BallCommonListener {
|
||||
|
||||
@Subscribe
|
||||
public void onServerOffline(ServerOfflineEvent event) {
|
||||
String serverID = event.getServerID();
|
||||
String serverID = event.getId();
|
||||
BallServerInfo info = BallAPI.getInstance().getAllServerInfo().remove(serverID);
|
||||
if (info == null) {
|
||||
return;
|
||||
|
Reference in New Issue
Block a user