实现了夸服传送服务器信息注册

This commit is contained in:
zhangyuheng 2024-06-26 11:43:48 +08:00
parent 8675d15e25
commit 3da1e60e6a
2 changed files with 92 additions and 29 deletions

View File

@ -1,38 +1,103 @@
package cn.lunadeer.dominion.dtos;
import cn.lunadeer.minecraftpluginutils.JsonFile;
import cn.lunadeer.minecraftpluginutils.XLogger;
import cn.lunadeer.minecraftpluginutils.databse.DatabaseManager;
import cn.lunadeer.minecraftpluginutils.databse.Field;
import cn.lunadeer.minecraftpluginutils.databse.FieldType;
import cn.lunadeer.minecraftpluginutils.databse.TableColumn;
import cn.lunadeer.minecraftpluginutils.databse.syntax.CreateTable;
import cn.lunadeer.minecraftpluginutils.databse.syntax.InsertRow;
import com.alibaba.fastjson.JSONObject;
import com.google.common.io.ByteArrayDataOutput;
import com.google.common.io.ByteStreams;
import org.bukkit.plugin.java.JavaPlugin;
import java.io.File;
import java.sql.ResultSet;
import java.util.Map;
public class ServerInfoDTO {
private final Field id = new Field("id", FieldType.INT);
private final Field name = new Field("name", FieldType.STRING);
private final Field uuid = new Field("uuid", FieldType.STRING);
private void initTable() {
public Integer getId() {
return (Integer) id.value;
}
public String getName() {
return (String) name.value;
}
private static void initTable() {
TableColumn server_info_id = new TableColumn("id", FieldType.INT, true, true, true, true, 0);
TableColumn server_info_name = new TableColumn("name", FieldType.STRING, false, false, true, true, "server");
TableColumn server_info_uuid = new TableColumn("uuid", FieldType.STRING, false, false, true, true, "00000000-0000-0000-0000-000000000000");
TableColumn server_info_name = new TableColumn("name", FieldType.STRING, false, false, true, false, "server");
CreateTable privilege_template = new CreateTable().ifNotExists();
privilege_template.table("server_info")
.field(server_info_id)
.field(server_info_name)
.field(server_info_uuid);
.field(server_info_name);
privilege_template.execute();
}
public static void initServerInfo() {
ByteArrayDataOutput out = ByteStreams.newDataOutput();
out.writeUTF("GetServer");
public static ServerInfoDTO initServerInfo(JavaPlugin plugin, File se) {
initTable();
ServerInfoDTO serverInfoDTO = new ServerInfoDTO();
serverInfoDTO.name.value = plugin.getServer().getName();
InsertRow insertRow = new InsertRow();
insertRow.returningAll()
.field(serverInfoDTO.name)
.table("server_info");
try (ResultSet res = insertRow.execute()) {
if (res.next()) {
serverInfoDTO.id.value = res.getInt("id");
}
} catch (Exception e) {
DatabaseManager.handleDatabaseError("创建服务器信息失败", e, "");
return null;
}
JSONObject json = new JSONObject();
json.put("name", serverInfoDTO.name.value);
json.put("id", serverInfoDTO.id.value);
try {
JsonFile.saveToFile(json, se);
} catch (Exception e) {
XLogger.err("保存服务器信息失败: %s", e.getMessage());
return null;
}
return serverInfoDTO;
}
public static ServerInfoDTO updateServerInfo(JavaPlugin plugin, File se) {
try {
JSONObject json = JsonFile.loadFromFile(se);
ServerInfoDTO serverInfoDTO = new ServerInfoDTO();
serverInfoDTO.id.value = json.getInteger("id");
serverInfoDTO.name.value = json.getString("name");
String sql = "UPDATE server_info SET name = ? WHERE id = ?";
DatabaseManager.instance.query(sql, serverInfoDTO.name.value, serverInfoDTO.id.value);
return serverInfoDTO;
} catch (Exception e) {
XLogger.err("加载服务器信息失败: %s", e.getMessage());
return null;
}
}
public static Map<Integer, String> getAllServerInfo() {
Map<Integer, String> allServerInfo = new java.util.HashMap<>();
String sql = "SELECT * FROM server_info";
try (ResultSet res = DatabaseManager.instance.query(sql)) {
while (res.next()) {
ServerInfoDTO serverInfoDTO = new ServerInfoDTO();
serverInfoDTO.id.value = res.getInt("id");
serverInfoDTO.name.value = res.getString("name");
allServerInfo.put(serverInfoDTO.getId(), serverInfoDTO.getName());
}
} catch (Exception e) {
DatabaseManager.handleDatabaseError("获取服务器信息失败", e, "");
}
return allServerInfo;
}
}

View File

@ -1,15 +1,12 @@
package cn.lunadeer.dominion.managers;
import cn.lunadeer.dominion.dtos.ServerInfoDTO;
import com.google.common.io.ByteArrayDataInput;
import com.google.common.io.ByteStreams;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.plugin.messaging.PluginMessageListener;
import org.jetbrains.annotations.NotNull;
import java.io.File;
import java.util.HashMap;
import java.util.Map;
public class GlobalTeleport implements PluginMessageListener {
@ -17,21 +14,30 @@ public class GlobalTeleport implements PluginMessageListener {
public static GlobalTeleport instance;
private final JavaPlugin plugin;
private final File infoFile;
private ServerInfoDTO thisServerInfo;
private Map<Integer, ServerInfoDTO> allServerInfo = new HashMap<>();
private final ServerInfoDTO thisServerInfo;
private final Map<Integer, String> allServerInfo;
public GlobalTeleport(JavaPlugin plugin) {
this.plugin = plugin;
File infoFile = new File(plugin.getDataFolder(), "server_info.json");
this.plugin.getServer().getMessenger().registerOutgoingPluginChannel(this.plugin, "BungeeCord");
this.plugin.getServer().getMessenger().registerIncomingPluginChannel(this.plugin, "BungeeCord", this);
this.infoFile = new File(plugin.getDataFolder(), "server_info.json");
instance = this;
if (!infoFile.exists()) {
ServerInfoDTO.initServerInfo();
thisServerInfo = ServerInfoDTO.initServerInfo(plugin, infoFile);
} else {
thisServerInfo = ServerInfoDTO.updateServerInfo(plugin, infoFile);
}
// todo load & update info
allServerInfo = ServerInfoDTO.getAllServerInfo();
}
public String getThisServerName() {
return thisServerInfo.getName();
}
public String getServerName(int id) {
return allServerInfo.get(id);
}
/**
@ -44,14 +50,6 @@ public class GlobalTeleport implements PluginMessageListener {
*/
@Override
public void onPluginMessageReceived(@NotNull String channel, @NotNull Player player, @NotNull byte[] message) {
if (!channel.equals("BungeeCord")) {
return;
}
ByteArrayDataInput in = ByteStreams.newDataInput(message);
String subchannel = in.readUTF();
if (subchannel.equals("SomeSubChannel")) {
// 使用下文中的"返回Response"一节的代码进行读取
// 数据处理
}
}
}