add AbstractOperator.java

This commit is contained in:
zhangyuheng 2024-05-29 10:42:25 +08:00
parent da10ce05e2
commit 27a8fff7a2
5 changed files with 127 additions and 51 deletions

View File

@ -2,6 +2,7 @@ package cn.lunadeer.dominion.commands;
import cn.lunadeer.dominion.Cache;
import cn.lunadeer.dominion.Dominion;
import cn.lunadeer.dominion.controllers.BukkitPlayerOperator;
import cn.lunadeer.dominion.controllers.DominionController;
import cn.lunadeer.dominion.dtos.DominionDTO;
import cn.lunadeer.dominion.dtos.Flag;
@ -41,11 +42,15 @@ public class DominionOperate {
return;
}
String name = args[1];
if (DominionController.create(player, name, points.get(0), points.get(1)) == null) {
Notification.error(sender, "创建领地失败");
return;
}
Notification.info(sender, "成功创建: %s", name);
BukkitPlayerOperator operator = new BukkitPlayerOperator(player);
operator.getResponse().thenAccept(result -> {
if (result.getStatus() == BukkitPlayerOperator.Result.SUCCESS) {
Notification.info(sender, "成功创建: %s", name);
} else {
Notification.error(sender, result.getMessage());
}
});
DominionController.create(operator, name, points.get(0), points.get(1));
}
/**
@ -67,18 +72,19 @@ public class DominionOperate {
Notification.error(sender, "请先使用工具选择子领地的对角线两点,或使用 /dominion auto_create_sub <子领地名称> [父领地名称] 创建自动子领地");
return;
}
BukkitPlayerOperator operator = new BukkitPlayerOperator(player);
operator.getResponse().thenAccept(result -> {
if (result.getStatus() == BukkitPlayerOperator.Result.SUCCESS) {
Notification.info(sender, "成功创建子领地: %s", args[1]);
} else {
Notification.error(sender, "创建子领地失败:%s", result.getMessage());
}
});
if (args.length == 2) {
if (DominionController.create(player, args[1], points.get(0), points.get(1)) != null) {
Notification.info(sender, "成功创建子领地: %s", args[1]);
return;
}
DominionController.create(operator, args[1], points.get(0), points.get(1));
} else {
if (DominionController.create(player, args[1], points.get(0), points.get(1), args[2]) != null) {
Notification.info(sender, "成功创建子领地: %s", args[1]);
return;
}
DominionController.create(operator, args[1], points.get(0), points.get(1), args[2]);
}
Notification.error(sender, "创建子领地失败");
}
/**

View File

@ -0,0 +1,36 @@
package cn.lunadeer.dominion.controllers;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
public interface AbstractOperator {
public static class Result {
public static final boolean SUCCESS = true;
public static final boolean FAILURE = false;
private boolean success;
private String message;
public Result(boolean success, String message, Object... args) {
this.success = success;
this.message = String.format(message, args);
}
public boolean getStatus() {
return success;
}
public String getMessage() {
return message;
}
}
public UUID getUniqueId();
public boolean isOp();
public void setResponse(Result result);
public CompletableFuture<Result> getResponse();
}

View File

@ -11,10 +11,10 @@ import java.util.List;
public class Apis {
public static boolean notOwner(Player player, DominionDTO dominion) {
public static boolean notOwner(AbstractOperator player, DominionDTO dominion) throws Exception {
if (player.isOp()) return false;
if (dominion.getOwner().equals(player.getUniqueId())) return false;
Notification.error(player, "你不是领地 %s 的拥有者,无法执行此操作", dominion.getName());
player.setResponse(new AbstractOperator.Result(AbstractOperator.Result.FAILURE, "你不是领地 %s 的拥有者,无法执行此操作", dominion.getName()));
return true;
}

View File

@ -0,0 +1,34 @@
package cn.lunadeer.dominion.controllers;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
public class BukkitPlayerOperator implements AbstractOperator{
private final org.bukkit.entity.Player player;
private final CompletableFuture<Result> response = new CompletableFuture<>();
public BukkitPlayerOperator(org.bukkit.entity.Player player) {
this.player = player;
}
@Override
public UUID getUniqueId() {
return player.getUniqueId();
}
@Override
public boolean isOp() {
return player.isOp();
}
@Override
public void setResponse(Result result) {
response.complete(result);
}
@Override
public CompletableFuture<Result> getResponse() {
return response;
}
}

View File

@ -31,69 +31,69 @@ public class DominionController {
/**
* 创建领地
*
* @param owner 拥有者
* @param name 领地名称
* @param loc1 位置1
* @param loc2 位置2
* @param operator 拥有者
* @param name 领地名称
* @param loc1 位置1
* @param loc2 位置2
* @return 创建的领地
*/
public static DominionDTO create(Player owner, String name, Location loc1, Location loc2) {
DominionDTO parent = getPlayerCurrentDominion(owner, false);
public static void create(AbstractOperator operator, String name, Location loc1, Location loc2) {
DominionDTO parent = getPlayerCurrentDominion(operator, false);
if (parent == null) {
return create(owner, name, loc1, loc2, "");
create(operator, name, loc1, loc2, "");
} else {
return create(owner, name, loc1, loc2, parent.getName());
create(operator, name, loc1, loc2, parent.getName());
}
}
/**
* 创建子领地
*
* @param owner 拥有者
* @param operator 拥有者
* @param name 领地名称
* @param loc1 位置1
* @param loc2 位置2
* @param parent_dominion_name 父领地名称
* @return 创建的领地
*/
public static DominionDTO create(Player owner, String name,
Location loc1, Location loc2,
String parent_dominion_name) {
public static void create(AbstractOperator operator, String name,
Location loc1, Location loc2,
String parent_dominion_name) {
if (name.isEmpty()) {
Notification.error(owner, "领地名称不能为空");
return null;
operator.setResponse(new AbstractOperator.Result(AbstractOperator.Result.FAILURE, "领地名称不能为空"));
return;
}
if (name.contains(" ")) {
Notification.error(owner, "领地名称不能包含空格");
Notification.error(operator, "领地名称不能包含空格");
return null;
}
if (DominionDTO.select(name) != null) {
Notification.error(owner, "已经存在名称为 %s 的领地", name);
Notification.error(operator, "已经存在名称为 %s 的领地", name);
return null;
}
if (!loc1.getWorld().equals(loc2.getWorld())) {
Notification.error(owner, "禁止跨世界操作");
Notification.error(operator, "禁止跨世界操作");
return null;
}
if (!owner.getWorld().equals(loc1.getWorld())) {
Notification.error(owner, "禁止跨世界操作");
if (!operator.getWorld().equals(loc1.getWorld())) {
Notification.error(operator, "禁止跨世界操作");
return null;
}
// 检查世界是否可以创建
if (worldNotValid(owner)) {
if (worldNotValid(operator)) {
return null;
}
// 检查领地数量是否达到上限
if (amountNotValid(owner)) {
if (amountNotValid(operator)) {
return null;
}
// 检查领地大小是否合法
if (sizeNotValid(owner,
if (sizeNotValid(operator,
loc1.getBlockX(), loc1.getBlockY(), loc1.getBlockZ(),
loc2.getBlockX(), loc2.getBlockY(), loc2.getBlockZ())) {
return null;
}
DominionDTO dominion = new DominionDTO(owner.getUniqueId(), name, owner.getWorld().getName(),
DominionDTO dominion = new DominionDTO(operator.getUniqueId(), name, operator.getWorld().getName(),
(int) Math.min(loc1.getX(), loc2.getX()), (int) Math.min(loc1.getY(), loc2.getY()),
(int) Math.min(loc1.getZ(), loc2.getZ()), (int) Math.max(loc1.getX(), loc2.getX()),
(int) Math.max(loc1.getY(), loc2.getY()), (int) Math.max(loc1.getZ(), loc2.getZ()));
@ -104,7 +104,7 @@ public class DominionController {
parent_dominion = DominionDTO.select(parent_dominion_name);
}
if (parent_dominion == null) {
Notification.error(owner, "父领地 %s 不存在", parent_dominion_name);
Notification.error(operator, "父领地 %s 不存在", parent_dominion_name);
if (parent_dominion_name.isEmpty()) {
XLogger.err("根领地丢失!");
}
@ -112,22 +112,22 @@ public class DominionController {
}
// 是否是父领地的拥有者
if (parent_dominion.getId() != -1) {
if (notOwner(owner, parent_dominion)) {
if (notOwner(operator, parent_dominion)) {
return null;
}
}
// 如果parent_dominion不为-1 检查是否在同一世界
if (parent_dominion.getId() != -1 && !parent_dominion.getWorld().equals(dominion.getWorld())) {
Notification.error(owner, "禁止跨世界操作");
Notification.error(operator, "禁止跨世界操作");
return null;
}
// 检查深度是否达到上限
if (depthNotValid(owner, parent_dominion)) {
if (depthNotValid(operator, parent_dominion)) {
return null;
}
// 检查是否超出父领地范围
if (!isContained(dominion, parent_dominion)) {
Notification.error(owner, "超出父领地 %s 范围", parent_dominion.getName());
Notification.error(operator, "超出父领地 %s 范围", parent_dominion.getName());
return null;
}
// 获取此领地的所有同级领地
@ -135,7 +135,7 @@ public class DominionController {
// 检查是否与其他子领地冲突
for (DominionDTO sub_dominion : sub_dominions) {
if (isIntersect(sub_dominion, dominion)) {
Notification.error(owner, "与领地 %s 冲突", sub_dominion.getName());
Notification.error(operator, "与领地 %s 冲突", sub_dominion.getName());
return null;
}
}
@ -148,19 +148,19 @@ public class DominionController {
count = (loc2.getBlockX() - loc1.getBlockX() + 1) * (loc2.getBlockY() - loc1.getBlockY() + 1) * (loc2.getBlockZ() - loc1.getBlockZ() + 1);
}
float price = count * Dominion.config.getEconomyPrice();
if (Dominion.vault.getEconomy().getBalance(owner) < price) {
Notification.error(owner, "你的余额不足,创建此领地需要 %.2f %s", price, Dominion.vault.getEconomy().currencyNamePlural());
if (Dominion.vault.getEconomy().getBalance(operator) < price) {
Notification.error(operator, "你的余额不足,创建此领地需要 %.2f %s", price, Dominion.vault.getEconomy().currencyNamePlural());
return null;
}
Notification.info(owner, "已扣除 %.2f %s", price, Dominion.vault.getEconomy().currencyNamePlural());
Dominion.vault.getEconomy().withdrawPlayer(owner, price);
Notification.info(operator, "已扣除 %.2f %s", price, Dominion.vault.getEconomy().currencyNamePlural());
Dominion.vault.getEconomy().withdrawPlayer(operator, price);
}
dominion = DominionDTO.insert(dominion);
if (dominion == null) {
Notification.error(owner, "创建失败,详细错误请联系管理员查询日志(当前时间:%s", Time.nowStr());
Notification.error(operator, "创建失败,详细错误请联系管理员查询日志(当前时间:%s", Time.nowStr());
return null;
}
ParticleRender.showBoxFace(Dominion.instance, owner, loc1, loc2);
ParticleRender.showBoxFace(Dominion.instance, operator, loc1, loc2);
return dominion.setParentDomId(parent_dominion.getId());
}