Skip to content

Commit

Permalink
新增出生点半径保护
Browse files Browse the repository at this point in the history
  • Loading branch information
ColdeZhang committed Jul 24, 2024
1 parent c2caf96 commit d348ee7
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 1 deletion.
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.40.0-beta</version>
<version>1.41.0-beta</version>
<packaging>jar</packaging>

<name>Dominion</name>
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/cn/lunadeer/dominion/commands/SetConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ public static void handler(CommandSender sender, String[] args) {
case "residence_migration":
setResidenceMigration(sender, args);
break;
case "spawn_protection":
setSpawnProtection(sender, args);
break;
default:
Notification.error(sender, "未知参数");
}
Expand Down Expand Up @@ -252,11 +255,23 @@ private static void setEconomyRefund(CommandSender sender, String[] args) {
} else {
Dominion.config.setEconomyRefund(economyRefund);
}
refreshPageOrNot(sender, args);
}

private static void setResidenceMigration(CommandSender sender, String[] args) {
boolean residenceMigration = Boolean.parseBoolean(args[2]);
Dominion.config.setResidenceMigration(residenceMigration);
refreshPageOrNot(sender, args);
}

private static void setSpawnProtection(CommandSender sender, String[] args) {
int spawnProtection = Integer.parseInt(args[2]);
if (spawnProtection != -1 && spawnProtection <= 0) {
Dominion.config.setSpawnProtection(1);
Notification.error(sender, "出生点保护半径不能小于或等于0");
} else {
Dominion.config.setSpawnProtection(spawnProtection);
}
refreshPageOrNot(sender, args);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,11 @@ public static void create(AbstractOperator operator, String name,
}
// 获取此领地的所有同级领地
List<DominionDTO> sub_dominions = DominionDTO.selectByParentId(dominion.getWorld(), parent_dominion.getId());
// 检查是否与出生点保护冲突
if (isIntersectSpawn(dominion)) {
operator.setResponse(FAIL.addMessage("与出生点保护冲突"));
return;
}
// 检查是否与其他子领地冲突
for (DominionDTO sub_dominion : sub_dominions) {
if (isIntersect(sub_dominion, dominion)) {
Expand All @@ -178,6 +183,30 @@ public static void create(AbstractOperator operator, String name,
operator.setResponse(SUCCESS);
}

private static boolean isIntersectSpawn(DominionDTO dominion) {
int radius = Dominion.config.getSpawnProtection();
if (radius == -1) {
return false;
}
World world = Dominion.instance.getServer().getWorld(dominion.getWorld());
if (world == null) {
return false;
}
Location spawn = world.getSpawnLocation();
return isIntersect(dominion, spawn.getBlockX() - radius, spawn.getBlockY() - radius, spawn.getBlockZ() - radius
, spawn.getBlockX() + radius, spawn.getBlockY() + radius, spawn.getBlockZ() + radius);
}

private static boolean isIntersectSpawn(String world, int[] cords) {
int radius = Dominion.config.getSpawnProtection();
if (radius == -1) {
return false;
}
Location spawn = Objects.requireNonNull(Dominion.instance.getServer().getWorld(world)).getSpawnLocation();
return isIntersect(cords, spawn.getBlockX() - radius, spawn.getBlockY() - radius, spawn.getBlockZ() - radius
, spawn.getBlockX() + radius, spawn.getBlockY() + radius, spawn.getBlockZ() + radius);
}

/**
* 向一个方向扩展领地
* 会尝试对操作者当前所在的领地进行操作,当操作者不在一个领地内或者在子领地内时
Expand Down Expand Up @@ -212,6 +241,11 @@ public static void expand(AbstractOperator operator, Integer size, String domini
if (newCords == null) {
return;
}
// 检查是否与出生点保护冲突
if (isIntersectSpawn(dominion.getWorld(), newCords)) {
operator.setResponse(FAIL.addMessage("与出生点保护冲突"));
return;
}
// 校验是否超出父领地范围
DominionDTO parent_dominion = DominionDTO.select(dominion.getParentDomId());
if (parent_dominion == null) {
Expand Down Expand Up @@ -595,6 +629,12 @@ private static boolean isIntersect(DominionDTO a, int[] cord) {
return isIntersect(a, cord[0], cord[1], cord[2], cord[3], cord[4], cord[5]);
}

private static boolean isIntersect(int[] cord, Integer x1, Integer y1, Integer z1, Integer x2, Integer y2, Integer z2) {
return cord[0] < x2 && cord[3] > x1 &&
cord[1] < y2 && cord[4] > y1 &&
cord[2] < z2 && cord[5] > z1;
}

/**
* 判断 sub 是否完全被 parent 包裹
*/
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/cn/lunadeer/dominion/managers/ConfigManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public void reload() {
XLogger.err("AutoCreateRadius 不能等于 0,已重置为 10");
setAutoCreateRadius(10);
}
_spawn_protection = _file.getInt("Limit.SpawnProtection", 10);
_limit_size_x = _file.getInt("Limit.SizeX", 128);
if (_limit_size_x <= 4 && _limit_size_x != -1) {
XLogger.err("Limit.SizeX 尺寸不能小于 4,已重置为 128");
Expand Down Expand Up @@ -95,6 +96,7 @@ public void reload() {

public void saveAll() {
_file.set("AutoCreateRadius", _auto_create_radius);
_file.set("Limit.SpawnProtection", _spawn_protection);
_file.set("Limit.SizeX", _limit_size_x);
_file.set("Limit.SizeY", _limit_size_y);
_file.set("Limit.SizeZ", _limit_size_z);
Expand Down Expand Up @@ -404,6 +406,16 @@ public void setResidenceMigration(Boolean residence_migration) {
_plugin.saveConfig();
}

public Integer getSpawnProtection() {
return _spawn_protection;
}

public void setSpawnProtection(Integer spawn_protection) {
_spawn_protection = spawn_protection;
_file.set("Limit.SpawnProtection", spawn_protection);
_plugin.saveConfig();
}

private final Dominion _plugin;
private FileConfiguration _file;
private Boolean _debug;
Expand Down Expand Up @@ -444,4 +456,5 @@ public void setResidenceMigration(Boolean residence_migration) {
private Float _economy_refund;
private List<String> _fly_permission_nodes;
private Boolean _residence_migration;
private Integer _spawn_protection;
}
11 changes: 11 additions & 0 deletions src/main/java/cn/lunadeer/dominion/tuis/SysConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,17 @@ public static void show(CommandSender sender, String[] args) {
Line limitSize = Line.create()
.append(Component.text("领地尺寸限制"));
view.add(limitSize);

Line spawnProtect = Line.create()
.append(Component.text(" 出生点保护范围"));
if (Dominion.config.getLimitSizeX() == -1) {
spawnProtect.append(Component.text("无限制")).append(Button.create("设置数值").setExecuteCommand("/dominion set_config spawn_protection 10 " + page).build());
} else {
spawnProtect.append(NumChanger.create(Dominion.config.getLimitSizeX(), "/dominion set_config spawn_protection").setPageNumber(page).build());
spawnProtect.append(Button.create("设置无限制").setExecuteCommand("/dominion set_config spawn_protection -1 " + page).build());
}
view.add(spawnProtect);

Line limitSizeX = Line.create()
.append(Component.text(" X轴(东西)"));
if (Dominion.config.getLimitSizeX() == -1) {
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ Database:
AutoCreateRadius: 10 #-1 表示不开启

Limit:
SpawnProtection: 10 # 出生点保护半径
MinY: -64 # 最小Y坐标
MaxY: 320 # 最大Y坐标
SizeX: 128 # X方向最大长度 -1:表示不限制
Expand Down

0 comments on commit d348ee7

Please sign in to comment.