Skip to content

Commit

Permalink
优化了一些ui
Browse files Browse the repository at this point in the history
新增限制控制
  • Loading branch information
ColdeZhang committed Mar 29, 2024
1 parent 02839f7 commit 0a670b1
Show file tree
Hide file tree
Showing 17 changed files with 283 additions and 90 deletions.
20 changes: 14 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -170,14 +170,22 @@ Database:
User: dominion
Pass: dominion

# -1 表示不开启
AutoCreateRadius: 10
MaxX: 128
MaxY: 64
MaxZ: 128

AutoClean:
Enabled: false
AfterDays: 180
# -1 表示不限制
Limit:
MinY: -64
MaxY: 320
SizeX: 128
SizeY: 64
SizeZ: 128
Amount: 10
Depth: 3 # 子领地深度 0:不允许子领地 -1:不限制
WorldBlackList: []

# -1 表示不开启
AutoCleanAfterDays: 180

BlueMap: true

Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>cn.lunadeer</groupId>
<artifactId>Dominion</artifactId>
<version>1.13.6-beta</version>
<version>1.14.6-beta</version>
<packaging>jar</packaging>

<name>Dominion</name>
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/cn/lunadeer/dominion/AutoClean.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

public class AutoClean {
public static void run() {
if (!Dominion.config.getAutoCleanEnable()) {
if (Dominion.config.getAutoCleanAfterDays() < 0) {
return;
}
XLogger.info("开始自动清理长时间未登录玩家领地数据");
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/cn/lunadeer/dominion/Cache.java
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,18 @@ public DominionDTO getDominion(Integer id) {
return id_dominions.get(id);
}

public int getPlayerDominionCount(Player player) {
UUID player_uuid = player.getUniqueId();
int count = 0;
for (Integer id : world_dominions.get(player.getWorld().getName())) {
DominionDTO dominion = id_dominions.get(id);
if (dominion.getOwner().equals(player_uuid)) {
count++;
}
}
return count;
}

public List<DominionDTO> getDominions() {
return new ArrayList<>(id_dominions.values());
}
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/cn/lunadeer/dominion/Commands.java
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,9 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command
case "reload_cache":
Operator.reloadCache(sender, args);
break;
case "reload_config":
Operator.reloadConfig(sender, args);
break;
case "export_mca":
Operator.exportMca(sender, args);
break;
Expand Down Expand Up @@ -167,6 +170,7 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command
"rename",
"give",
"reload_cache",
"reload_config",
"export_mca"
);
}
Expand Down
5 changes: 1 addition & 4 deletions src/main/java/cn/lunadeer/dominion/Dominion.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public void onEnable() {
dbConnection = Database.createConnection();
Database.migrate();
scheduler = new Scheduler(this);
AutoClean.run();
Cache.instance = new Cache();

Bukkit.getPluginManager().registerEvents(new PlayerEvents(), this);
Expand All @@ -48,10 +49,6 @@ public void onEnable() {
XLogger.info(" |_____/ \\___/|_| |_| |_|_|_| |_|_|\\___/|_| |_|");
XLogger.info(" ");

scheduler.async.runDelayed(this, scheduledTask -> {
AutoClean.run();
}, 30, TimeUnit.SECONDS);

scheduler.async.runDelayed(this, scheduledTask -> {
BlueMapConnect.render();
}, 40, TimeUnit.SECONDS);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ public static void autoCreateDominion(CommandSender sender, String[] args) {
Notification.error(sender, "用法: /dominion auto_create <领地名称>");
return;
}
if (Dominion.config.getAutoCreateRadius() < 0) {
Notification.error(sender, "自动创建领地功能已关闭");
return;
}
autoPoints(player);
createDominion(sender, args);
}
Expand All @@ -107,6 +111,10 @@ public static void autoCreateSubDominion(CommandSender sender, String[] args) {
Notification.error(sender, "用法: /dominion auto_create_sub <子领地名称> [父领地名称]");
return;
}
if (Dominion.config.getAutoCreateRadius() < 0) {
Notification.error(sender, "自动创建领地功能已关闭");
return;
}
autoPoints(player);
createSubDominion(sender, args);
}
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/cn/lunadeer/dominion/commands/Operator.java
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,15 @@ public static void exportMca(CommandSender sender, String[] args) {
});
}

public static void reloadConfig(CommandSender sender, String[] args) {
if (notOpOrConsole(sender)) return;
Dominion.scheduler.async.runNow(Dominion.instance, ScheduledTask -> {
Notification.info(sender, "正在重新加载配置文件...");
Dominion.config.reload();
Notification.info(sender, "配置文件已重新加载");
});
}

private static int convertWorld2Mca(int world) {
return world < 0 ? world / 512 - 1 : world / 512;
}
Expand Down
109 changes: 97 additions & 12 deletions src/main/java/cn/lunadeer/dominion/controllers/DominionController.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package cn.lunadeer.dominion.controllers;

import cn.lunadeer.dominion.Cache;
import cn.lunadeer.dominion.Dominion;
import cn.lunadeer.dominion.dtos.DominionDTO;
import cn.lunadeer.dominion.dtos.PlayerDTO;
Expand Down Expand Up @@ -69,15 +70,20 @@ public static DominionDTO create(Player owner, String name,
Notification.error(owner, "禁止跨世界操作");
return null;
}
int x_length = Math.abs((int) (loc1.getX() - loc2.getX()));
int y_length = Math.abs((int) (loc1.getY() - loc2.getY()));
int z_length = Math.abs((int) (loc1.getZ() - loc2.getZ()));
if (x_length < 4 || y_length < 4 || z_length < 4) {
Notification.error(owner, "领地的任意一边长度不得小于4");
// 检查世界是否可以创建
if (Dominion.config.getWorldBlackList().contains(owner.getWorld().getName())) {
Notification.error(owner, "禁止在世界 " + owner.getWorld().getName() + " 创建领地");
return null;
}
// 检查领地数量是否达到上限
if (Cache.instance.getPlayerDominionCount(owner) >= Dominion.config.getLimitAmount() && Dominion.config.getLimitAmount() > 0) {
Notification.error(owner, "你的领地数量已达上限,当前上限为 " + Dominion.config.getLimitAmount());
return null;
}
if (x_length > Dominion.config.getMaxX() || y_length > Dominion.config.getMaxY() || z_length > Dominion.config.getMaxZ()) {
Notification.error(owner, "领地尺寸不能超过 " + Dominion.config.getMaxX() + " x " + Dominion.config.getMaxY() + " x " + Dominion.config.getMaxZ());
// 检查领地大小是否合法
if (sizeNotValid(owner,
loc1.getBlockX(), loc1.getBlockY(), loc1.getBlockZ(),
loc2.getBlockX(), loc2.getBlockY(), loc2.getBlockZ())) {
return null;
}
DominionDTO dominion = new DominionDTO(owner.getUniqueId(), name, owner.getWorld().getName(),
Expand Down Expand Up @@ -108,6 +114,10 @@ public static DominionDTO create(Player owner, String name,
Notification.error(owner, "禁止跨世界操作");
return null;
}
// 检查深度是否达到上限
if (depthNotValid(owner, parent_dominion)) {
return null;
}
// 检查是否超出父领地范围
if (!isContained(dominion, parent_dominion)) {
Notification.error(owner, "超出父领地 " + parent_dominion_name + " 范围");
Expand Down Expand Up @@ -199,11 +209,7 @@ public static DominionDTO expand(Player operator, Integer size, String dominion_
Notification.error(operator, "无效的方向");
return null;
}
int x_length = x2 - x1;
int y_length = y2 - y1;
int z_length = z2 - z1;
if (x_length > Dominion.config.getMaxX() || y_length > Dominion.config.getMaxY() || z_length > Dominion.config.getMaxZ()) {
Notification.error(operator, "领地尺寸不能超过 " + Dominion.config.getMaxX() + " x " + Dominion.config.getMaxY() + " x " + Dominion.config.getMaxZ());
if (sizeNotValid(operator, x1, y1, z1, x2, y2, z2)) {
return null;
}
// 校验是否超出父领地范围
Expand Down Expand Up @@ -303,6 +309,9 @@ public static DominionDTO contract(Player operator, Integer size, String dominio
Notification.error(operator, "缩小后的领地无效");
return null;
}
if (sizeNotValid(operator, x1, y1, z1, x2, y2, z2)) {
return null;
}
// 获取所有的子领地
List<DominionDTO> sub_dominions = DominionDTO.selectByParentId(dominion.getWorld(), dominion.getId());
for (DominionDTO sub_dominion : sub_dominions) {
Expand Down Expand Up @@ -517,12 +526,18 @@ private static boolean isIntersect(DominionDTO a, Integer x1, Integer y1, Intege
* 判断 sub 是否完全被 parent 包裹
*/
private static boolean isContained(DominionDTO sub, DominionDTO parent) {
if (parent.getId() == -1) {
return true;
}
return sub.getX1() >= parent.getX1() && sub.getX2() <= parent.getX2() &&
sub.getY1() >= parent.getY1() && sub.getY2() <= parent.getY2() &&
sub.getZ1() >= parent.getZ1() && sub.getZ2() <= parent.getZ2();
}

private static boolean isContained(Integer x1, Integer y1, Integer z1, Integer x2, Integer y2, Integer z2, DominionDTO parent) {
if (parent.getId() == -1) {
return true;
}
return x1 >= parent.getX1() && x2 <= parent.getX2() &&
y1 >= parent.getY1() && y2 <= parent.getY2() &&
z1 >= parent.getZ1() && z2 <= parent.getZ2();
Expand All @@ -543,4 +558,74 @@ private static List<DominionDTO> getSubDominionsRecursive(DominionDTO dominion)
sub_dominions.addAll(sub_sub_dominions);
return sub_dominions;
}

private static boolean sizeNotValid(Player operator, int x1, int y1, int z1, int x2, int y2, int z2) {
// 如果 1 > 2 则交换
if (x1 > x2) {
int temp = x1;
x1 = x2;
x2 = temp;
}
if (y1 > y2) {
int temp = y1;
y1 = y2;
y2 = temp;
}
if (z1 > z2) {
int temp = z1;
z1 = z2;
z2 = temp;
}
int x_length = x2 - x1;
int y_length = y2 - y1;
int z_length = z2 - z1;
if (x_length < 4 || y_length < 4 || z_length < 4) {
Notification.error(operator, "领地的任意一边长度不得小于4");
return true;
}
if (x_length > Dominion.config.getLimitSizeX() && Dominion.config.getLimitSizeX() > 0) {
Notification.error(operator, "领地X方向长度不能超过 " + Dominion.config.getLimitSizeX());
return true;
}
if (y_length > Dominion.config.getLimitSizeY() && Dominion.config.getLimitSizeY() > 0) {
Notification.error(operator, "领地Y方向高度不能超过 " + Dominion.config.getLimitSizeY());
return true;
}
if (z_length > Dominion.config.getLimitSizeZ() && Dominion.config.getLimitSizeZ() > 0) {
Notification.error(operator, "领地Z方向长度不能超过 " + Dominion.config.getLimitSizeZ());
return true;
}
if (y2 > Dominion.config.getLimitMaxY() && Dominion.config.getLimitMaxY() > 0) {
Notification.error(operator, "领地Y坐标不能超过 " + Dominion.config.getLimitMaxY());
return true;
}
if (y1 < Dominion.config.getLimitMinY() && Dominion.config.getLimitMinY() > 0) {
Notification.error(operator, "领地Y坐标不能低于 " + Dominion.config.getLimitMinY());
return true;
}
return false;
}

private static boolean depthNotValid(Player operator, DominionDTO parent_dom) {
if (Dominion.config.getLimitDepth() < 0) {
return false;
}
if (parent_dom.getId() != -1 && Dominion.config.getLimitDepth() == 0) {
Notification.error(operator, "不允许创建子领地");
return true;
}
if (parent_dom.getId() == -1) {
return false;
}
int level = 0;
while (parent_dom.getParentDomId() != -1) {
parent_dom = Cache.instance.getDominion(parent_dom.getParentDomId());
level++;
}
if (level >= Dominion.config.getLimitDepth()) {
Notification.error(operator, "子领地嵌套深度不能超过 " + Dominion.config.getLimitDepth());
return true;
}
return false;
}
}
8 changes: 4 additions & 4 deletions src/main/java/cn/lunadeer/dominion/dtos/DominionDTO.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ public class DominionDTO {
private static List<DominionDTO> query(String sql) {
List<DominionDTO> dominions = new ArrayList<>();
try (ResultSet rs = Database.query(sql)) {
if (sql.contains("UPDATE") || sql.contains("DELETE") || sql.contains("INSERT")) {
// 如果是更新操作,重新加载缓存
Cache.instance.loadDominions();
}
if (rs == null) return dominions;
while (rs.next()) {
Integer id = rs.getInt("id");
Expand Down Expand Up @@ -78,10 +82,6 @@ private static List<DominionDTO> query(String sql) {
);
dominions.add(dominion);
}
if (sql.contains("UPDATE") || sql.contains("DELETE") || sql.contains("INSERT")) {
// 如果是更新操作,重新加载缓存
Cache.instance.loadDominions();
}
} catch (SQLException e) {
XLogger.err("Database query failed: " + e.getMessage());
XLogger.err("SQL: " + sql);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -572,6 +572,10 @@ public PlayerPrivilegeDTO(UUID playerUUID, Integer domID,
private static List<PlayerPrivilegeDTO> query(String sql) {
List<PlayerPrivilegeDTO> players = new ArrayList<>();
try (ResultSet rs = Database.query(sql)) {
if (sql.contains("UPDATE") || sql.contains("DELETE") || sql.contains("INSERT")) {
// 如果是更新操作,重新加载缓存
Cache.instance.loadPlayerPrivileges();
}
if (rs == null) return players;
while (rs.next()) {
PlayerPrivilegeDTO player = new PlayerPrivilegeDTO(
Expand Down Expand Up @@ -618,10 +622,6 @@ private static List<PlayerPrivilegeDTO> query(String sql) {
);
players.add(player);
}
if (sql.contains("UPDATE") || sql.contains("DELETE") || sql.contains("INSERT")) {
// 如果是更新操作,重新加载缓存
Cache.instance.loadPlayerPrivileges();
}
} catch (Exception e) {
XLogger.err("Database query failed: " + e.getMessage());
XLogger.err("SQL: " + sql);
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/cn/lunadeer/dominion/tuis/Apis.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public static void printHelp(CommandSender sender, String[] args) {
Player player = playerOnly(sender);
if (player == null) return;
int page = getPage(args);
ListView view = ListView.create(5, "/dominion help");
ListView view = ListView.create(10, "/dominion help");
view.title("领地插件命令帮助 <>表示必填参数 []表示可选参数")
.add(Line.create().append("打开交互菜单").append(Button.create("/dominion menu", "/dominion menu")))
.add(Line.create().append("查看帮助").append(Button.create("/dominion help [页码]", "/dominion help 1")))
Expand Down
11 changes: 6 additions & 5 deletions src/main/java/cn/lunadeer/dominion/tuis/DominionManage.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import cn.lunadeer.dominion.utils.Notification;
import cn.lunadeer.dominion.utils.STUI.Button;
import cn.lunadeer.dominion.utils.STUI.Line;
import cn.lunadeer.dominion.utils.STUI.ListView;
import cn.lunadeer.dominion.utils.STUI.View;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
Expand Down Expand Up @@ -31,15 +32,15 @@ public static void show(CommandSender sender, String[] args) {
Line privilege_list = Line.create()
.append(Button.create("玩家权限", "/dominion privilege_list " + dominion.getName()))
.append("管理玩家特权");
View view = View.create();
ListView view = ListView.create(10, "/dominion manage " + dominion.getName());
view.title("领地 " + dominion.getName() + " 管理界面")
.navigator(Line.create()
.append(Button.create("主菜单", "/dominion menu"))
.append(Button.create("我的领地", "/dominion list"))
.append(dominion.getName()))
.addLine(size_info)
.addLine(flag_info)
.addLine(privilege_list)
.showOn(player);
.add(size_info)
.add(flag_info)
.add(privilege_list)
.showOn(player, 1);
}
}
Loading

0 comments on commit 0a670b1

Please sign in to comment.