feat: 添加调试模式

This commit is contained in:
2022-10-29 08:14:22 +08:00
parent 43b9a6dc5b
commit 6a94fccb42
7 changed files with 162 additions and 8 deletions

View File

@@ -6,11 +6,13 @@ import cn.hamster3.mc.plugin.ball.common.api.BallAPI;
import cn.hamster3.mc.plugin.ball.common.config.BallConfig; import cn.hamster3.mc.plugin.ball.common.config.BallConfig;
import cn.hamster3.mc.plugin.ball.common.entity.BallServerInfo; import cn.hamster3.mc.plugin.ball.common.entity.BallServerInfo;
import cn.hamster3.mc.plugin.ball.common.entity.BallServerType; import cn.hamster3.mc.plugin.ball.common.entity.BallServerType;
import cn.hamster3.mc.plugin.ball.common.listener.BallDebugListener;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.configuration.file.FileConfiguration;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.logging.Logger;
public class BallBukkitAPI extends BallAPI { public class BallBukkitAPI extends BallAPI {
public BallBukkitAPI(@NotNull BallConfig config) { public BallBukkitAPI(@NotNull BallConfig config) {
@@ -45,6 +47,9 @@ public class BallBukkitAPI extends BallAPI {
instance = new BallBukkitAPI(config); instance = new BallBukkitAPI(config);
instance.addListener(BallBukkitListener.INSTANCE); instance.addListener(BallBukkitListener.INSTANCE);
if (pluginConfig.getBoolean("debug", false)) {
instance.addListener(BallDebugListener.INSTANCE);
}
} }
@Override @Override
@@ -56,4 +61,9 @@ public class BallBukkitAPI extends BallAPI {
public void disable() throws SQLException, InterruptedException { public void disable() throws SQLException, InterruptedException {
super.disable(); super.disable();
} }
@Override
public @NotNull Logger getLogger() {
return HamsterBallPlugin.getInstance().getLogger();
}
} }

View File

@@ -1,3 +1,6 @@
# 是否允许在控制台输出调试信息
debug: false
ball-server: ball-server:
host: "ball.hamster3.cn" host: "ball.hamster3.cn"
port: 58888 port: 58888

View File

@@ -4,6 +4,7 @@ import cn.hamster3.mc.plugin.ball.common.api.BallAPI;
import cn.hamster3.mc.plugin.ball.common.config.BallConfig; import cn.hamster3.mc.plugin.ball.common.config.BallConfig;
import cn.hamster3.mc.plugin.ball.common.entity.BallServerInfo; import cn.hamster3.mc.plugin.ball.common.entity.BallServerInfo;
import cn.hamster3.mc.plugin.ball.common.entity.BallServerType; import cn.hamster3.mc.plugin.ball.common.entity.BallServerType;
import cn.hamster3.mc.plugin.ball.common.listener.BallDebugListener;
import cn.hamster3.mc.plugin.core.bungee.HamsterBallPlugin; import cn.hamster3.mc.plugin.core.bungee.HamsterBallPlugin;
import cn.hamster3.mc.plugin.core.bungee.listener.BallBungeeCordListener; import cn.hamster3.mc.plugin.core.bungee.listener.BallBungeeCordListener;
import cn.hamster3.mc.plugin.core.bungee.util.BungeeCordUtils; import cn.hamster3.mc.plugin.core.bungee.util.BungeeCordUtils;
@@ -11,6 +12,7 @@ import net.md_5.bungee.config.Configuration;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.logging.Logger;
public class BallBungeeCordAPI extends BallAPI { public class BallBungeeCordAPI extends BallAPI {
public BallBungeeCordAPI(@NotNull BallConfig config) { public BallBungeeCordAPI(@NotNull BallConfig config) {
@@ -40,7 +42,11 @@ public class BallBungeeCordAPI extends BallAPI {
pluginConfig.getInt("ball-server.nio-thread") pluginConfig.getInt("ball-server.nio-thread")
); );
instance = new BallBungeeCordAPI(config); instance = new BallBungeeCordAPI(config);
instance.addListener(BallBungeeCordListener.INSTANCE); instance.addListener(BallBungeeCordListener.INSTANCE);
if (pluginConfig.getBoolean("debug", false)) {
instance.addListener(BallDebugListener.INSTANCE);
}
} }
@Override @Override
@@ -52,4 +58,9 @@ public class BallBungeeCordAPI extends BallAPI {
public void disable() throws SQLException, InterruptedException { public void disable() throws SQLException, InterruptedException {
super.disable(); super.disable();
} }
@Override
public @NotNull Logger getLogger() {
return HamsterBallPlugin.getInstance().getLogger();
}
} }

View File

@@ -1,3 +1,6 @@
# 是否允许在控制台输出调试信息
debug: false
ball-server: ball-server:
host: "ball.hamster3.cn" host: "ball.hamster3.cn"
port: 58888 port: 58888

View File

@@ -11,6 +11,8 @@ dependencies {
// https://mvnrepository.com/artifact/net.kyori/adventure-api // https://mvnrepository.com/artifact/net.kyori/adventure-api
compileOnly 'net.kyori:adventure-api:4.11.0' compileOnly 'net.kyori:adventure-api:4.11.0'
// https://mvnrepository.com/artifact/net.kyori/adventure-text-serializer-plain
compileOnly 'net.kyori:adventure-text-serializer-plain:4.11.0'
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.9.0' testImplementation 'org.junit.jupiter:junit-jupiter-api:5.9.0'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.9.0' testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.9.0'

View File

@@ -33,6 +33,7 @@ import org.jetbrains.annotations.Nullable;
import java.sql.*; import java.sql.*;
import java.util.*; import java.util.*;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import java.util.logging.Logger;
@SuppressWarnings("unused") @SuppressWarnings("unused")
public abstract class BallAPI { public abstract class BallAPI {
@@ -57,11 +58,12 @@ public abstract class BallAPI {
private final Bootstrap bootstrap; private final Bootstrap bootstrap;
private final NioEventLoopGroup executors; private final NioEventLoopGroup executors;
protected boolean enable; protected boolean enabled;
protected Channel channel; protected Channel channel;
protected BallAPI(@NotNull BallConfig config) { protected BallAPI(@NotNull BallConfig config) {
this.config = config; this.config = config;
this.enabled = false;
executors = new NioEventLoopGroup(config.getNioThread()); executors = new NioEventLoopGroup(config.getNioThread());
serverInfo = new ConcurrentHashMap<>(); serverInfo = new ConcurrentHashMap<>();
@@ -146,10 +148,10 @@ public abstract class BallAPI {
} }
protected void enable() throws SQLException, InterruptedException { protected void enable() throws SQLException, InterruptedException {
if (enable) { if (enabled) {
return; return;
} }
enable = true; enabled = true;
BallServerInfo localInfo = getLocalServerInfo(); BallServerInfo localInfo = getLocalServerInfo();
connect(); connect();
@@ -233,20 +235,25 @@ public abstract class BallAPI {
} }
protected void connect() throws InterruptedException { protected void connect() throws InterruptedException {
if (!enable) { if (!enabled) {
getLogger().info("仓鼠球已关闭,拒绝启动连接!");
return; return;
} }
getLogger().info("准备连接至仓鼠球服务中心!");
ChannelFuture future = bootstrap.connect(config.getHost(), config.getPort()).await(); ChannelFuture future = bootstrap.connect(config.getHost(), config.getPort()).await();
if (future.isSuccess()) { if (future.isSuccess()) {
channel = future.channel(); channel = future.channel();
for (BallListener listener : listeners) { for (BallListener listener : listeners) {
listener.onConnectActive(); listener.onConnectActive();
} }
getLogger().info("已连接至仓鼠球服务中心!");
} else {
getLogger().warning("连接至仓鼠球服务中心失败!");
} }
} }
protected void reconnect(int ttl) { protected void reconnect(int ttl) {
if (!enable) { if (!enabled) {
return; return;
} }
if (channel != null && channel.isOpen() && channel.isRegistered() && channel.isActive() && channel.isWritable()) { if (channel != null && channel.isOpen() && channel.isRegistered() && channel.isActive() && channel.isWritable()) {
@@ -275,10 +282,10 @@ public abstract class BallAPI {
} }
protected void disable() throws SQLException, InterruptedException { protected void disable() throws SQLException, InterruptedException {
if (!enable) { if (!enabled) {
return; return;
} }
enable = false; enabled = false;
sendBallMessage( sendBallMessage(
BALL_CHANNEL, BALL_CHANNEL,
@@ -620,9 +627,10 @@ public abstract class BallAPI {
*/ */
public void sendBallMessage(@NotNull BallMessageInfo messageInfo, boolean block) { public void sendBallMessage(@NotNull BallMessageInfo messageInfo, boolean block) {
if (channel == null || !channel.isWritable()) { if (channel == null || !channel.isWritable()) {
getLogger().warning("由于服务不可用,有一条消息发送失败了: " + messageInfo);
return; return;
} }
ChannelFuture future = channel.write(CoreConstantObjects.GSON.toJsonTree(messageInfo)); ChannelFuture future = channel.writeAndFlush(CoreConstantObjects.GSON.toJsonTree(messageInfo));
if (block) { if (block) {
try { try {
future.await(); future.await();
@@ -752,4 +760,7 @@ public abstract class BallAPI {
public List<BallListener> getListeners() { public List<BallListener> getListeners() {
return listeners; return listeners;
} }
@NotNull
public abstract Logger getLogger();
} }

View File

@@ -0,0 +1,114 @@
package cn.hamster3.mc.plugin.ball.common.listener;
import cn.hamster3.mc.plugin.ball.common.api.BallAPI;
import cn.hamster3.mc.plugin.ball.common.data.BallMessageInfo;
import cn.hamster3.mc.plugin.ball.common.event.player.*;
import cn.hamster3.mc.plugin.ball.common.event.server.ServerOfflineEvent;
import cn.hamster3.mc.plugin.ball.common.event.server.ServerOnlineEvent;
import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer;
import org.jetbrains.annotations.NotNull;
import java.util.logging.Level;
public final class BallDebugListener extends BallListener {
public static final BallDebugListener INSTANCE = new BallDebugListener();
private BallDebugListener() {
}
@Override
public ListenerPriority getPriority() {
return ListenerPriority.MONITOR;
}
@Override
public void onConnectActive() {
BallAPI.getInstance().getLogger().info("连接已可用。");
}
@Override
public void onConnectInactive() {
BallAPI.getInstance().getLogger().info("连接已中断!");
}
@Override
public void onConnectException(Throwable throwable) {
BallAPI.getInstance().getLogger().log(Level.INFO, "连接出现错误!", throwable);
}
@Override
public void onReconnectFailed() {
BallAPI.getInstance().getLogger().info("重新连接失败!");
}
@Override
public void onBallPlayerPreLogin(@NotNull BallPlayerPreLoginEvent event) {
BallAPI.getInstance().getLogger().info("BallPlayerPreLoginEvent: " + event.getPlayerName());
}
@Override
public void onBallPlayerLogin(@NotNull BallPlayerLoginEvent event) {
BallAPI.getInstance().getLogger().info("BallPlayerLoginEvent: " + event.getPlayerInfo().getName());
}
@Override
public void onBallPlayerPostLogin(@NotNull BallPlayerPostLoginEvent event) {
BallAPI.getInstance().getLogger().info("BallPlayerPostLoginEvent: " + event.getPlayerInfo().getName());
}
@Override
public void onBallPlayerPreConnectServer(@NotNull BallPlayerPreConnectServerEvent event) {
BallAPI.getInstance().getLogger().info("BallPlayerPreConnectServerEvent: ");
BallAPI.getInstance().getLogger().info("player: " + event.getPlayerInfo().getName());
BallAPI.getInstance().getLogger().info("from: " + event.getFrom());
BallAPI.getInstance().getLogger().info("to: " + event.getTo());
}
@Override
public void onBallPlayerConnectServer(@NotNull BallPlayerConnectServerEvent event) {
BallAPI.getInstance().getLogger().info("BallPlayerConnectServerEvent: ");
BallAPI.getInstance().getLogger().info("player: " + event.getPlayerInfo().getName());
BallAPI.getInstance().getLogger().info("from: " + event.getFrom());
BallAPI.getInstance().getLogger().info("to: " + event.getTo());
}
@Override
public void onBallPlayerPostConnectServer(@NotNull BallPlayerPostConnectServerEvent event) {
BallAPI.getInstance().getLogger().info("BallPlayerPostConnectServerEvent: ");
BallAPI.getInstance().getLogger().info("player: " + event.getPlayerInfo().getName());
}
@Override
public void onBallPlayerLogout(@NotNull BallPlayerLogoutEvent event) {
BallAPI.getInstance().getLogger().info("BallPlayerLogoutEvent: ");
BallAPI.getInstance().getLogger().info("player: " + event.getPlayerInfo().getName());
}
@Override
public void onBallPlayerChat(@NotNull BallPlayerChatEvent event) {
BallAPI.getInstance().getLogger().info("BallPlayerChatEvent: ");
BallAPI.getInstance().getLogger().info("displayName: " + event.getDisplayName());
BallAPI.getInstance().getLogger().info("playerUUID: " + event.getPlayerUUID());
BallAPI.getInstance().getLogger().info("message: " + PlainTextComponentSerializer.plainText().serialize(event.getMessage()));
}
@Override
public void onServerOffline(@NotNull ServerOfflineEvent event) {
BallAPI.getInstance().getLogger().info("ServerOfflineEvent: " + event.getServerID());
}
@Override
public void onServerOnline(@NotNull ServerOnlineEvent event) {
BallAPI.getInstance().getLogger().info("ServerOnlineEvent: " + event.getServerInfo().getId());
}
@Override
public void onMessageSend(@NotNull BallMessageInfo event) {
BallAPI.getInstance().getLogger().info("发送了一条消息: " + event);
}
@Override
public void onMessageReceived(@NotNull BallMessageInfo event) {
BallAPI.getInstance().getLogger().info("收到了一条消息: " + event);
}
}