Compare commits
15 Commits
master
...
global-tel
Author | SHA1 | Date | |
---|---|---|---|
cf41e40596 | |||
4213d14fd6 | |||
59ccbbdd5c | |||
a1a1ef7990 | |||
929b91fc2b | |||
3f8309e127 | |||
4184816569 | |||
5f95067637 | |||
bce9c700a0 | |||
53cf8f351d | |||
485edbc4fa | |||
d514984e56 | |||
b1d76a8cff | |||
3da1e60e6a | |||
8675d15e25 |
2
pom.xml
2
pom.xml
@ -6,7 +6,7 @@
|
||||
|
||||
<groupId>cn.lunadeer</groupId>
|
||||
<artifactId>Dominion</artifactId>
|
||||
<version>1.36.1-beta</version>
|
||||
<version>1.36.1-global-tp-beta</version>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>Dominion</name>
|
||||
|
@ -1,6 +1,7 @@
|
||||
package cn.lunadeer.dominion;
|
||||
|
||||
import cn.lunadeer.dominion.dtos.*;
|
||||
import cn.lunadeer.dominion.managers.GlobalTeleport;
|
||||
import cn.lunadeer.dominion.utils.ResMigration;
|
||||
import cn.lunadeer.minecraftpluginutils.Notification;
|
||||
import cn.lunadeer.minecraftpluginutils.ParticleRender;
|
||||
@ -64,7 +65,7 @@ public class Cache {
|
||||
id_dominions = new ConcurrentHashMap<>();
|
||||
world_dominion_tree = new ConcurrentHashMap<>();
|
||||
dominion_children = new ConcurrentHashMap<>();
|
||||
List<DominionDTO> dominions = DominionDTO.selectAll();
|
||||
List<DominionDTO> dominions = DominionDTO.selectAllOfServer(GlobalTeleport.instance.getThisServerId());
|
||||
count = dominions.size();
|
||||
Map<String, List<DominionDTO>> world_dominions = new HashMap<>();
|
||||
for (DominionDTO d : dominions) {
|
||||
|
@ -3,6 +3,7 @@ package cn.lunadeer.dominion;
|
||||
import cn.lunadeer.dominion.commands.*;
|
||||
import cn.lunadeer.dominion.controllers.PlayerController;
|
||||
import cn.lunadeer.dominion.cuis.*;
|
||||
import cn.lunadeer.dominion.dtos.DominionDTO;
|
||||
import cn.lunadeer.dominion.dtos.PlayerDTO;
|
||||
import cn.lunadeer.dominion.tuis.AllDominion;
|
||||
import cn.lunadeer.dominion.tuis.Menu;
|
||||
@ -242,7 +243,7 @@ public class Commands implements TabExecutor {
|
||||
case "set_tp_location":
|
||||
return playerDominions(sender);
|
||||
case "tp":
|
||||
return allDominions();
|
||||
return DominionDTO.selectAllNames();
|
||||
case "set":
|
||||
return dominionFlags();
|
||||
case "expand":
|
||||
|
@ -10,19 +10,17 @@ import cn.lunadeer.dominion.dtos.GroupDTO;
|
||||
import cn.lunadeer.dominion.dtos.MemberDTO;
|
||||
import cn.lunadeer.minecraftpluginutils.Notification;
|
||||
import cn.lunadeer.minecraftpluginutils.Scheduler;
|
||||
import cn.lunadeer.minecraftpluginutils.Teleport;
|
||||
import cn.lunadeer.minecraftpluginutils.XLogger;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.World;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.Map;
|
||||
|
||||
import static cn.lunadeer.dominion.DominionNode.isInDominion;
|
||||
import static cn.lunadeer.dominion.commands.Apis.autoPoints;
|
||||
import static cn.lunadeer.dominion.commands.Apis.playerOnly;
|
||||
import static cn.lunadeer.dominion.managers.GlobalTeleport.doTp;
|
||||
|
||||
public class DominionOperate {
|
||||
/**
|
||||
@ -351,23 +349,18 @@ public class DominionOperate {
|
||||
Notification.error(sender, "用法: /dominion tp <领地名称>");
|
||||
return;
|
||||
}
|
||||
DominionDTO dominionDTO = DominionDTO.select(args[1]);
|
||||
String name = args[1];
|
||||
if (name.contains(".")) {
|
||||
name = name.split("\\.")[1];
|
||||
}
|
||||
DominionDTO dominionDTO = DominionDTO.select(name);
|
||||
if (dominionDTO == null) {
|
||||
Notification.error(sender, "领地不存在");
|
||||
return;
|
||||
}
|
||||
if (player.isOp() && Dominion.config.getLimitOpBypass()) {
|
||||
Notification.warn(sender, "你是OP,将忽略领地传送限制");
|
||||
Location location = dominionDTO.getTpLocation();
|
||||
if (location == null) {
|
||||
int x = (dominionDTO.getX1() + dominionDTO.getX2()) / 2;
|
||||
int z = (dominionDTO.getZ1() + dominionDTO.getZ2()) / 2;
|
||||
World world = Dominion.instance.getServer().getWorld(dominionDTO.getWorld());
|
||||
location = new Location(world, x, player.getLocation().getY(), z);
|
||||
XLogger.warn("领地 %s 没有设置传送点,将尝试传送到中心点", dominionDTO.getName());
|
||||
}
|
||||
Teleport.doTeleportSafely(player, location);
|
||||
Notification.info(player, "已将你传送到 " + dominionDTO.getName());
|
||||
doTp(player, dominionDTO);
|
||||
return;
|
||||
}
|
||||
if (!Dominion.config.getTpEnable()) {
|
||||
@ -427,26 +420,7 @@ public class DominionOperate {
|
||||
}
|
||||
Cache.instance.NextTimeAllowTeleport.put(player.getUniqueId(), now.plusSeconds(Dominion.config.getTpCoolDown()));
|
||||
Scheduler.runTaskLater(() -> {
|
||||
Location location = dominionDTO.getTpLocation();
|
||||
int center_x = (dominionDTO.getX1() + dominionDTO.getX2()) / 2;
|
||||
int center_z = (dominionDTO.getZ1() + dominionDTO.getZ2()) / 2;
|
||||
World world = Dominion.instance.getServer().getWorld(dominionDTO.getWorld());
|
||||
if (location == null) {
|
||||
location = new Location(world, center_x, player.getLocation().getY(), center_z);
|
||||
Notification.warn(player, "领地 %s 没有设置传送点,将尝试传送到中心点", dominionDTO.getName());
|
||||
} else if (!isInDominion(dominionDTO, location)) {
|
||||
location = new Location(world, center_x, player.getLocation().getY(), center_z);
|
||||
Notification.warn(player, "领地 %s 传送点不在领地内,将尝试传送到中心点", dominionDTO.getName());
|
||||
}
|
||||
if (player.isOnline()) {
|
||||
Teleport.doTeleportSafely(player, location).thenAccept(b -> {
|
||||
if (b) {
|
||||
Notification.info(player, "已将你传送到 " + dominionDTO.getName());
|
||||
} else {
|
||||
Notification.error(player, "传送失败,请重试");
|
||||
}
|
||||
});
|
||||
}
|
||||
doTp(player, dominionDTO);
|
||||
}, 20L * Dominion.config.getTpDelay());
|
||||
}
|
||||
|
||||
|
@ -90,15 +90,6 @@ public class Helper {
|
||||
return dominions_name;
|
||||
}
|
||||
|
||||
public static List<String> allDominions() {
|
||||
List<String> dominions_name = new ArrayList<>();
|
||||
List<DominionDTO> dominions = DominionController.all();
|
||||
for (DominionDTO dominion : dominions) {
|
||||
dominions_name.add(dominion.getName());
|
||||
}
|
||||
return dominions_name;
|
||||
}
|
||||
|
||||
public static List<String> allTemplates(CommandSender sender) {
|
||||
List<String> templates_name = new ArrayList<>();
|
||||
Player player = playerOnly(sender);
|
||||
|
@ -4,6 +4,7 @@ import cn.lunadeer.dominion.Cache;
|
||||
import cn.lunadeer.dominion.Dominion;
|
||||
import cn.lunadeer.dominion.dtos.DominionDTO;
|
||||
import cn.lunadeer.dominion.dtos.PlayerDTO;
|
||||
import cn.lunadeer.dominion.managers.GlobalTeleport;
|
||||
import cn.lunadeer.minecraftpluginutils.Notification;
|
||||
import cn.lunadeer.minecraftpluginutils.ParticleRender;
|
||||
import cn.lunadeer.minecraftpluginutils.VaultConnect;
|
||||
@ -29,10 +30,6 @@ public class DominionController {
|
||||
return DominionDTO.selectAll(owner.getUniqueId());
|
||||
}
|
||||
|
||||
public static List<DominionDTO> all() {
|
||||
return DominionDTO.selectAll();
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建领地
|
||||
*
|
||||
@ -321,6 +318,10 @@ public class DominionController {
|
||||
if (dominion == null) {
|
||||
return;
|
||||
}
|
||||
if (dominion.getServerId() != GlobalTeleport.instance.getThisServerId()) {
|
||||
operator.setResponse(FAIL.addMessage("禁止跨服务器操作"));
|
||||
return;
|
||||
}
|
||||
List<DominionDTO> sub_dominions = getSubDominionsRecursive(dominion);
|
||||
if (!force) {
|
||||
AbstractOperator.Result WARNING = new AbstractOperator.Result(AbstractOperator.Result.WARNING, "删除领地 %s 会同时删除其所有子领地,是否继续?", dominion_name);
|
||||
@ -433,6 +434,10 @@ public class DominionController {
|
||||
operator.setResponse(new AbstractOperator.Result(AbstractOperator.Result.FAILURE, "领地 %s 不存在", dominion_name));
|
||||
return;
|
||||
}
|
||||
if (dominion.getServerId() != GlobalTeleport.instance.getThisServerId()) {
|
||||
operator.setResponse(FAIL.addMessage("禁止跨服务器操作"));
|
||||
return;
|
||||
}
|
||||
World world = Dominion.instance.getServer().getWorld(dominion.getWorld());
|
||||
if (world == null) {
|
||||
operator.setResponse(new AbstractOperator.Result(AbstractOperator.Result.FAILURE, "世界 %s 不存在", dominion.getWorld()));
|
||||
|
@ -2,6 +2,7 @@ package cn.lunadeer.dominion.dtos;
|
||||
|
||||
import cn.lunadeer.dominion.Cache;
|
||||
import cn.lunadeer.dominion.Dominion;
|
||||
import cn.lunadeer.dominion.managers.GlobalTeleport;
|
||||
import cn.lunadeer.minecraftpluginutils.XLogger;
|
||||
import cn.lunadeer.minecraftpluginutils.databse.DatabaseManager;
|
||||
import cn.lunadeer.minecraftpluginutils.databse.Field;
|
||||
@ -47,13 +48,15 @@ public class DominionDTO {
|
||||
flags.put(f, rs.getBoolean(f.getFlagName()));
|
||||
}
|
||||
String color = rs.getString("color");
|
||||
Integer serverId = rs.getInt("server_id");
|
||||
|
||||
DominionDTO dominion = new DominionDTO(id, owner, name, world, x1, y1, z1, x2, y2, z2, parentDomId,
|
||||
rs.getString("join_message"),
|
||||
rs.getString("leave_message"),
|
||||
flags,
|
||||
tp_location,
|
||||
color
|
||||
color,
|
||||
serverId
|
||||
);
|
||||
dominions.add(dominion);
|
||||
}
|
||||
@ -65,6 +68,44 @@ public class DominionDTO {
|
||||
return query(sql);
|
||||
}
|
||||
|
||||
public static List<String> selectAllNamesOfServer(Integer serverId, UUID owner) {
|
||||
String sql = "SELECT name FROM dominion WHERE server_id = ? AND id > 0 AND owner = ?;";
|
||||
List<String> names = new ArrayList<>();
|
||||
try (ResultSet rs = DatabaseManager.instance.query(sql, serverId, owner.toString())) {
|
||||
if (rs == null) return names;
|
||||
while (rs.next()) {
|
||||
names.add(rs.getString("name"));
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
DatabaseManager.handleDatabaseError("数据库操作失败: ", e, sql);
|
||||
}
|
||||
return names;
|
||||
}
|
||||
|
||||
public static List<String> selectAllNames() {
|
||||
String sql = "SELECT name, server_id FROM dominion WHERE id > 0;";
|
||||
List<String> names = new ArrayList<>();
|
||||
try (ResultSet rs = DatabaseManager.instance.query(sql)) {
|
||||
if (rs == null) return names;
|
||||
while (rs.next()) {
|
||||
int serverId = rs.getInt("server_id");
|
||||
String name = rs.getString("name");
|
||||
if (serverId != GlobalTeleport.instance.getThisServerId()) {
|
||||
name = GlobalTeleport.instance.getServerName(serverId) + "." + name;
|
||||
}
|
||||
names.add(name);
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
DatabaseManager.handleDatabaseError("数据库操作失败: ", e, sql);
|
||||
}
|
||||
return names;
|
||||
}
|
||||
|
||||
public static List<DominionDTO> selectAllOfServer(Integer id) {
|
||||
String sql = "SELECT * FROM dominion WHERE id > 0 AND server_id = ?;";
|
||||
return query(sql, id);
|
||||
}
|
||||
|
||||
public static List<DominionDTO> selectAll(String world) {
|
||||
String sql = "SELECT * FROM dominion WHERE world = ? AND id > 0;";
|
||||
return query(sql, world);
|
||||
@ -123,7 +164,7 @@ public class DominionDTO {
|
||||
.field(dominion.x2).field(dominion.y2).field(dominion.z2)
|
||||
.field(dominion.parentDomId)
|
||||
.field(dominion.joinMessage).field(dominion.leaveMessage)
|
||||
.field(dominion.tp_location);
|
||||
.field(dominion.tp_location).field(dominion.serverId);
|
||||
for (Flag f : Flag.getDominionFlagsEnabled()) {
|
||||
insert.field(new Field(f.getFlagName(), f.getDefaultValue()));
|
||||
}
|
||||
@ -150,7 +191,7 @@ public class DominionDTO {
|
||||
String joinMessage, String leaveMessage,
|
||||
Map<Flag, Boolean> flags,
|
||||
String tp_location,
|
||||
String color) {
|
||||
String color, Integer serverId) {
|
||||
this.id.value = id;
|
||||
this.owner.value = owner.toString();
|
||||
this.name.value = name;
|
||||
@ -167,6 +208,7 @@ public class DominionDTO {
|
||||
this.flags.putAll(flags);
|
||||
this.tp_location.value = tp_location;
|
||||
this.color.value = color;
|
||||
this.serverId.value = serverId;
|
||||
}
|
||||
|
||||
|
||||
@ -212,7 +254,7 @@ public class DominionDTO {
|
||||
private final Map<Flag, Boolean> flags = new HashMap<>();
|
||||
private final Field tp_location = new Field("tp_location", "default");
|
||||
private final Field color = new Field("color", "#00BFFF");
|
||||
|
||||
private final Field serverId = new Field("server_id", GlobalTeleport.instance.getThisServerId());
|
||||
|
||||
// getters and setters
|
||||
public Integer getId() {
|
||||
@ -382,7 +424,6 @@ public class DominionDTO {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public Location getTpLocation() {
|
||||
if (Objects.equals(tp_location.value, "default")) {
|
||||
return null;
|
||||
@ -433,4 +474,12 @@ public class DominionDTO {
|
||||
public String getColor() {
|
||||
return (String) color.value;
|
||||
}
|
||||
|
||||
public Integer getServerId() {
|
||||
return (Integer) serverId.value;
|
||||
}
|
||||
|
||||
public String getServerName() {
|
||||
return GlobalTeleport.instance.getServerName(getServerId());
|
||||
}
|
||||
}
|
||||
|
90
src/main/java/cn/lunadeer/dominion/dtos/ServerInfoDTO.java
Normal file
90
src/main/java/cn/lunadeer/dominion/dtos/ServerInfoDTO.java
Normal file
@ -0,0 +1,90 @@
|
||||
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.syntax.InsertRow;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
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);
|
||||
|
||||
public Integer getId() {
|
||||
return (Integer) id.value;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return (String) name.value;
|
||||
}
|
||||
|
||||
|
||||
public static ServerInfoDTO initServerInfo(JavaPlugin plugin, File se) {
|
||||
ServerInfoDTO serverInfoDTO = new ServerInfoDTO();
|
||||
serverInfoDTO.name.value = plugin.getServer().getName();
|
||||
InsertRow insertRow = new InsertRow();
|
||||
insertRow.returningAll()
|
||||
.onConflictDoNothing(new Field("id", null))
|
||||
.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;
|
||||
}
|
||||
|
||||
}
|
@ -1,5 +1,6 @@
|
||||
package cn.lunadeer.dominion.managers;
|
||||
|
||||
import cn.lunadeer.dominion.Dominion;
|
||||
import cn.lunadeer.dominion.dtos.Flag;
|
||||
import cn.lunadeer.minecraftpluginutils.databse.*;
|
||||
import cn.lunadeer.minecraftpluginutils.databse.syntax.AddColumn;
|
||||
@ -231,5 +232,25 @@ public class DatabaseTables {
|
||||
DatabaseManager.handleDatabaseError("迁移 player_privilege 到 dominion_member 失败", e, sql);
|
||||
}
|
||||
}
|
||||
|
||||
// global teleport
|
||||
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, false, "'server'");
|
||||
CreateTable server_info = new CreateTable().ifNotExists();
|
||||
server_info.table("server_info")
|
||||
.field(server_info_id)
|
||||
.field(server_info_name);
|
||||
server_info.execute();
|
||||
new GlobalTeleport(Dominion.instance); // init server info
|
||||
TableColumn server_id = new TableColumn("server_id", FieldType.INT, false, false, true, false, GlobalTeleport.instance.getThisServerId());
|
||||
new AddColumn(server_id).table("dominion").ifNotExists().execute();
|
||||
|
||||
TableColumn bc_tp_cache_player_uuid = new TableColumn("player_uuid", FieldType.STRING, false, false, true, true, "''");
|
||||
TableColumn bc_tp_cache_dom_id = new TableColumn("dom_id", FieldType.INT, false, false, true, false, -1);
|
||||
CreateTable bc_tp_cache = new CreateTable().ifNotExists();
|
||||
bc_tp_cache.table("bc_tp_cache")
|
||||
.field(bc_tp_cache_player_uuid)
|
||||
.field(bc_tp_cache_dom_id);
|
||||
bc_tp_cache.execute();
|
||||
}
|
||||
}
|
||||
|
158
src/main/java/cn/lunadeer/dominion/managers/GlobalTeleport.java
Normal file
158
src/main/java/cn/lunadeer/dominion/managers/GlobalTeleport.java
Normal file
@ -0,0 +1,158 @@
|
||||
package cn.lunadeer.dominion.managers;
|
||||
|
||||
import cn.lunadeer.dominion.Dominion;
|
||||
import cn.lunadeer.dominion.dtos.DominionDTO;
|
||||
import cn.lunadeer.dominion.dtos.ServerInfoDTO;
|
||||
import cn.lunadeer.minecraftpluginutils.Notification;
|
||||
import cn.lunadeer.minecraftpluginutils.Teleport;
|
||||
import cn.lunadeer.minecraftpluginutils.XLogger;
|
||||
import cn.lunadeer.minecraftpluginutils.databse.DatabaseManager;
|
||||
import cn.lunadeer.minecraftpluginutils.databse.Field;
|
||||
import cn.lunadeer.minecraftpluginutils.databse.syntax.InsertRow;
|
||||
import com.google.common.io.ByteArrayDataInput;
|
||||
import com.google.common.io.ByteArrayDataOutput;
|
||||
import com.google.common.io.ByteStreams;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.World;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.player.PlayerJoinEvent;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
import org.bukkit.plugin.messaging.PluginMessageListener;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.io.File;
|
||||
import java.sql.ResultSet;
|
||||
import java.util.Map;
|
||||
|
||||
import static cn.lunadeer.dominion.DominionNode.isInDominion;
|
||||
|
||||
public class GlobalTeleport implements PluginMessageListener, Listener {
|
||||
|
||||
public static GlobalTeleport instance;
|
||||
|
||||
private final JavaPlugin plugin;
|
||||
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.plugin.getServer().getPluginManager().registerEvents(this, this.plugin);
|
||||
instance = this;
|
||||
|
||||
if (!infoFile.exists()) {
|
||||
thisServerInfo = ServerInfoDTO.initServerInfo(plugin, infoFile);
|
||||
} else {
|
||||
thisServerInfo = ServerInfoDTO.updateServerInfo(plugin, infoFile);
|
||||
}
|
||||
allServerInfo = ServerInfoDTO.getAllServerInfo();
|
||||
}
|
||||
|
||||
public String getThisServerName() {
|
||||
return thisServerInfo.getName();
|
||||
}
|
||||
|
||||
public int getThisServerId() {
|
||||
return thisServerInfo.getId();
|
||||
}
|
||||
|
||||
public String getServerName(int id) {
|
||||
return allServerInfo.get(id);
|
||||
}
|
||||
|
||||
public Map<Integer, String> getAllServerInfo() {
|
||||
return allServerInfo;
|
||||
}
|
||||
|
||||
/**
|
||||
* A method that will be thrown when a PluginMessageSource sends a plugin
|
||||
* message on a registered channel.
|
||||
*
|
||||
* @param channel Channel that the message was sent through.
|
||||
* @param player Source of the message.
|
||||
* @param message The raw message that was sent.
|
||||
*/
|
||||
@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();
|
||||
}
|
||||
|
||||
private void teleportToServer(Player player, DominionDTO dominionDTO) {
|
||||
Field player_uuid = new Field("player_uuid", player.getUniqueId().toString());
|
||||
Field dom_id = new Field("dom_id", dominionDTO.getId());
|
||||
InsertRow addCache = new InsertRow();
|
||||
addCache.field(player_uuid)
|
||||
.field(dom_id)
|
||||
.onConflictOverwrite(player_uuid)
|
||||
.table("bc_tp_cache");
|
||||
addCache.execute();
|
||||
ByteArrayDataOutput out = ByteStreams.newDataOutput();
|
||||
out.writeUTF("Connect");
|
||||
out.writeUTF(dominionDTO.getServerName());
|
||||
player.sendPluginMessage(plugin, "BungeeCord", out.toByteArray());
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onJoin(PlayerJoinEvent event) {
|
||||
Player player = event.getPlayer();
|
||||
String sql = "SELECT dom_id FROM bc_tp_cache WHERE player_uuid = ?";
|
||||
int dominionId;
|
||||
try (ResultSet res = DatabaseManager.instance.query(sql, player.getUniqueId().toString())) {
|
||||
if (res.next()) {
|
||||
dominionId = res.getInt("dom_id");
|
||||
} else {
|
||||
XLogger.debug("玩家 %s 没有传送缓存", player.getName());
|
||||
return;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
DatabaseManager.handleDatabaseError("获取玩家的传送缓存失败", e, sql);
|
||||
return;
|
||||
}
|
||||
DominionDTO dominionDTO = DominionDTO.select(dominionId);
|
||||
if (dominionDTO == null) {
|
||||
Notification.error(player, "无法获取目标领地信息");
|
||||
} else {
|
||||
if (dominionDTO.getServerId() == getThisServerId()) {
|
||||
doTp(player, dominionDTO);
|
||||
}
|
||||
}
|
||||
sql = "DELETE FROM bc_tp_cache WHERE player_uuid = ?";
|
||||
DatabaseManager.instance.query(sql, player.getUniqueId().toString());
|
||||
}
|
||||
|
||||
public static void doTp(@NotNull Player player, @NotNull DominionDTO dominionDTO) {
|
||||
if (dominionDTO.getServerId() != GlobalTeleport.instance.getThisServerId()) {
|
||||
GlobalTeleport.instance.teleportToServer(player, dominionDTO);
|
||||
return;
|
||||
}
|
||||
|
||||
Location location = dominionDTO.getTpLocation();
|
||||
int center_x = (dominionDTO.getX1() + dominionDTO.getX2()) / 2;
|
||||
int center_z = (dominionDTO.getZ1() + dominionDTO.getZ2()) / 2;
|
||||
World world = Dominion.instance.getServer().getWorld(dominionDTO.getWorld());
|
||||
if (location == null) {
|
||||
location = new Location(world, center_x, player.getLocation().getY(), center_z);
|
||||
Notification.warn(player, "领地 %s 没有设置传送点,将尝试传送到中心点", dominionDTO.getName());
|
||||
} else if (!isInDominion(dominionDTO, location)) {
|
||||
location = new Location(world, center_x, player.getLocation().getY(), center_z);
|
||||
Notification.warn(player, "领地 %s 传送点不在领地内,将尝试传送到中心点", dominionDTO.getName());
|
||||
}
|
||||
if (player.isOnline()) {
|
||||
Teleport.doTeleportSafely(player, location).thenAccept(b -> {
|
||||
if (b) {
|
||||
Notification.info(player, "已将你传送到 " + dominionDTO.getName());
|
||||
} else {
|
||||
Notification.error(player, "传送失败,请重试");
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
@ -2,6 +2,8 @@ package cn.lunadeer.dominion.tuis.dominion;
|
||||
|
||||
import cn.lunadeer.dominion.Cache;
|
||||
import cn.lunadeer.dominion.DominionNode;
|
||||
import cn.lunadeer.dominion.dtos.DominionDTO;
|
||||
import cn.lunadeer.dominion.managers.GlobalTeleport;
|
||||
import cn.lunadeer.minecraftpluginutils.stui.ListView;
|
||||
import cn.lunadeer.minecraftpluginutils.stui.ViewStyles;
|
||||
import cn.lunadeer.minecraftpluginutils.stui.components.Button;
|
||||
@ -37,6 +39,20 @@ public class DominionList {
|
||||
TextComponent manage = Button.createGreen("管理").setExecuteCommand("/dominion manage " + dominion).build();
|
||||
view.add(Line.create().append(manage).append(dominion));
|
||||
}
|
||||
for (Integer serverId : GlobalTeleport.instance.getAllServerInfo().keySet()) {
|
||||
if (serverId == GlobalTeleport.instance.getThisServerId()) {
|
||||
continue;
|
||||
}
|
||||
List<String> names = DominionDTO.selectAllNamesOfServer(serverId, player.getUniqueId());
|
||||
if (names.size() == 0) continue;
|
||||
view.add(Line.create().append(""));
|
||||
view.add(Line.create().append(Component.text("--- 以下为你在 " + GlobalTeleport.instance.getAllServerInfo().get(serverId) + " 的领地 ---", ViewStyles.main_color)));
|
||||
for (String name : names) {
|
||||
TextComponent manage = Button.createGreen("管理").setExecuteCommand("/dominion manage " + name).build();
|
||||
TextComponent delete = Button.createRed("删除").setExecuteCommand("/dominion delete " + name).build();
|
||||
view.add(Line.create().append(delete).append(manage).append(name));
|
||||
}
|
||||
}
|
||||
view.showOn(player, page);
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user