Skip to content

Commit

Permalink
新增领地tp功能
Browse files Browse the repository at this point in the history
  • Loading branch information
ColdeZhang committed Apr 21, 2024
1 parent 0023048 commit 28735ef
Show file tree
Hide file tree
Showing 20 changed files with 365 additions and 32 deletions.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
<img src="https://ssl.lunadeer.cn:14437/i/2024/03/28/6604f0cec0f0e.png" alt="" width="70%">

### [开源地址](https://ssl.lunadeer.cn:14446/zhangyuheng/Dominion) | [文档地址](https://ssl.lunadeer.cn:14448/doc/23/)

### [下载页面](https://ssl.lunadeer.cn:14446/zhangyuheng/Dominion/releases)

### [统计页面](https://bstats.org/plugin/bukkit/Dominion/21445) | [Hangar](https://hangar.papermc.io/zhangyuheng/Dominion)

</div>
Expand Down Expand Up @@ -147,6 +149,8 @@
| 缩小领地 | `/dominion contract [大小] [领地名称]` |
| 设置进入领地的提示语 | `/dominion set_enter_msg <提示语> [领地名称]` |
| 设置离开领地的提示语 | `/dominion set_leave_msg <提示语> [领地名称]` |
| 设置领地传送点 | `/dominion set_tp_location [领地名称]` |
| 传送到领地传送点 | `/dominion tp <领地名称>` |
| 重命名领地 | `/dominion rename <原领地名称> <新领地名称>` |
| 转让领地 | `/dominion give <领地名称> <玩家名称>` |
| 删除领地 | `/dominion delete <领地名称>` |
Expand Down Expand Up @@ -198,6 +202,13 @@ AutoCleanAfterDays: 180

BlueMap: true

Teleport:
Enable: true
# 传送延迟 秒
Delay: 0
# 冷却时间 秒
CoolDown: 0

CheckUpdate: true

Debug: false
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.17.2-beta</version>
<version>1.18.3-beta</version>
<packaging>jar</packaging>

<name>Dominion</name>
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/cn/lunadeer/dominion/Cache.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.bukkit.entity.Player;

import javax.annotation.Nullable;
import java.time.LocalDateTime;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;
Expand Down Expand Up @@ -270,4 +271,6 @@ public List<DominionDTO> getDominions() {
private final AtomicLong _last_update_privilege = new AtomicLong(0);
private final AtomicBoolean _update_privilege_is_scheduled = new AtomicBoolean(false);
private static final long UPDATE_INTERVAL = 1000 * 4;

public final Map<UUID, LocalDateTime> NextTimeAllowTeleport = new java.util.HashMap<>();
}
11 changes: 11 additions & 0 deletions src/main/java/cn/lunadeer/dominion/Commands.java
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,12 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command
case "set_leave_msg":
DominionOperate.setLeaveMessage(sender, args);
break;
case "set_tp_location":
DominionOperate.setTpLocation(sender, args);
break;
case "tp":
DominionOperate.teleportToDominion(sender, args);
break;
case "rename":
DominionOperate.renameDominion(sender, args);
break;
Expand Down Expand Up @@ -167,6 +173,8 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command
"create_privilege", "set_privilege", "clear_privilege", "list", "privilege_info",
"set_enter_msg",
"set_leave_msg",
"set_tp_location",
"tp",
"rename",
"give",
"reload_cache",
Expand All @@ -189,7 +197,10 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command
case "privilege_list":
case "rename":
case "give":
case "set_tp_location":
return playerDominions(sender);
case "tp":
return allDominions();
case "set":
return dominionFlags();
case "create_privilege":
Expand Down
1 change: 1 addition & 0 deletions src/main/java/cn/lunadeer/dominion/commands/Apis.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public static void sizeInfo(CommandSender sender, DominionDTO dominionDTO) {
Notification.info(sender, " 大小为" + (x2 - x1) + " x" + (y2 - y1) + " x" + (z2 - z1));
Notification.info(sender, " 中心坐标为 " + (x1 + (x2 - x1) / 2) + " " + (y1 + (y2 - y1) / 2) + " " + (z1 + (z2 - z1) / 2));
Notification.info(sender, " 高度为 " + (y2 - y1));
Notification.info(sender, " Y1=" + y1 + " Y2=" + y2);
Notification.info(sender, " 体积为 " + (x2 - x1) * (y2 - y1) * (z2 - z1));
Notification.info(sender, " 领地的世界为 " + dominionDTO.getWorld());
Notification.info(sender, " 领地的对角点坐标为 x1=" + x1 + " y1=" + y1 + " z1=" + z1 + " x2=" + x2 + " y2=" + y2 + " z2=" + z2);
Expand Down
98 changes: 98 additions & 0 deletions src/main/java/cn/lunadeer/dominion/commands/DominionOperate.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
package cn.lunadeer.dominion.commands;

import cn.lunadeer.dominion.Cache;
import cn.lunadeer.dominion.Dominion;
import cn.lunadeer.dominion.controllers.DominionController;
import cn.lunadeer.dominion.dtos.DominionDTO;
import cn.lunadeer.dominion.dtos.PlayerPrivilegeDTO;
import cn.lunadeer.dominion.utils.Notification;
import cn.lunadeer.dominion.utils.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.commands.Apis.*;
Expand Down Expand Up @@ -272,6 +277,27 @@ public static void setLeaveMessage(CommandSender sender, String[] args) {
Notification.error(sender, "用法: /dominion set_leave_msg <提示语> [领地名称]");
}

/**
* 设置领地传送点
* /dominion set_tp_location [领地名称]
*
* @param sender 命令发送者
* @param args 命令参数
*/
public static void setTpLocation(CommandSender sender, String[] args) {
Player player = playerOnly(sender);
if (player == null) return;
if (args.length == 1) {
DominionController.setTpLocation(player);
return;
}
if (args.length == 2) {
DominionController.setTpLocation(player, args[1]);
return;
}
Notification.error(sender, "用法: /dominion set_tp_location [领地名称]");
}

/**
* 重命名领地
* /dominion rename <原领地名称> <新领地名称>
Expand Down Expand Up @@ -315,4 +341,76 @@ public static void giveDominion(CommandSender sender, String[] args) {
}
Notification.error(sender, "用法: /dominion give <领地名称> <玩家名称>");
}

/**
* 传送到领地
* /dominion tp <领地名称>
*
* @param sender 命令发送者
* @param args 命令参数
*/
public static void teleportToDominion(CommandSender sender, String[] args) {
Player player = playerOnly(sender);
if (player == null) return;
if (args.length != 2) {
Notification.error(sender, "用法: /dominion tp <领地名称>");
return;
}
if (!Dominion.config.getTpEnable()) {
Notification.error(sender, "管理员没有开启领地传送功能");
return;
}
DominionDTO dominionDTO = DominionDTO.select(args[1]);
if (dominionDTO == null) {
Notification.error(sender, "领地不存在");
return;
}
PlayerPrivilegeDTO privilegeDTO = PlayerPrivilegeDTO.select(player.getUniqueId(), dominionDTO.getId());
if (privilegeDTO == null) {
if (!dominionDTO.getTeleport()) {
Notification.error(sender, "此领地禁止传送");
return;
}
} else {
if (!privilegeDTO.getTeleport()) {
Notification.error(sender, "你不被允许传送到这个领地");
return;
}
}

Location location = dominionDTO.getTpLocation();
if (location == null) {
Notification.error(sender, "此领地没有设置传送点");
return;
// int center_x = (dominionDTO.getX1() + dominionDTO.getX2()) / 2;
// int center_z = (dominionDTO.getZ1() + dominionDTO.getZ2()) / 2;
// // find safe location
// World world = Dominion.instance.getServer().getWorld(dominionDTO.getWorld());
// if (world == null) {
// Notification.error(sender, "此领地所在世界不存在");
// XLogger.warn("领地 " + dominionDTO.getName() + " 所在世界不存在");
// return;
// }
// location = world.getHighestBlockAt(center_x, center_z).getLocation();
}

LocalDateTime now = LocalDateTime.now();
LocalDateTime next_time = Cache.instance.NextTimeAllowTeleport.get(player.getUniqueId());
if (next_time != null) {
if (now.isBefore(next_time)) {
long secs_until_next = now.until(next_time, java.time.temporal.ChronoUnit.SECONDS);
Notification.error(player, "请等待 " + secs_until_next + " 秒后再次传送");
return;
}
}

Notification.info(player, "传送将在 " + Dominion.config.getTpDelay() + " 秒后执行");
Cache.instance.NextTimeAllowTeleport.put(player.getUniqueId(), now.plusSeconds(Dominion.config.getTpCoolDown()));
Dominion.scheduler.region.runDelayed(Dominion.instance, (instance) -> {
if (player.isOnline()) {
player.teleportAsync(location);
Notification.info(player, "已将你传送到 " + dominionDTO.getName());
}
}, Dominion.config.getTpDelay() == 0 ? 1 : 20L * Dominion.config.getTpDelay());
}
}
8 changes: 8 additions & 0 deletions src/main/java/cn/lunadeer/dominion/commands/Helper.java
Original file line number Diff line number Diff line change
Expand Up @@ -97,5 +97,13 @@ public static List<String> playerAdminDominions(CommandSender sender) {
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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ public static List<DominionDTO> all(Player owner) {
return DominionDTO.selectAll(owner.getUniqueId());
}

public static List<DominionDTO> all() {
return DominionDTO.selectAll();
}

/**
* 创建领地
*
Expand Down Expand Up @@ -427,6 +431,55 @@ public static void setLeaveMessage(Player operator, String message, String domin
Notification.info(operator, "成功设置领地 " + dominion_name + " 的离开消息");
}

/**
* 设置领地的传送点
*
* @param operator 操作者
*/
public static void setTpLocation(Player operator) {
DominionDTO dominion = getPlayerCurrentDominion(operator);
if (dominion == null) {
return;
}
setTpLocation(operator, dominion.getName());
}

/**
* 设置领地的传送点
*
* @param operator 操作者
* @param dominion_name 领地名称
*/
public static void setTpLocation(Player operator, String dominion_name) {
DominionDTO dominion = DominionDTO.select(dominion_name);
if (dominion == null) {
Notification.error(operator, "领地 " + dominion_name + " 不存在");
return;
}
if (notOwner(operator, dominion)) {
Notification.error(operator, "你不是领地 " + dominion_name + " 的拥有者,无法执行此操作");
return;
}
// 检查是否在领地内
if (operator.getWorld().getName().equals(dominion.getWorld()) &&
operator.getLocation().getBlockX() >= dominion.getX1() &&
operator.getLocation().getBlockX() <= dominion.getX2() &&
operator.getLocation().getBlockY() >= dominion.getY1() &&
operator.getLocation().getBlockY() <= dominion.getY2() &&
operator.getLocation().getBlockZ() >= dominion.getZ1() &&
operator.getLocation().getBlockZ() <= dominion.getZ2()) {
Location loc = operator.getLocation();
loc.setY(loc.getY() + 1.5);
dominion.setTpLocation(loc);
Notification.info(operator, "成功设置领地 " + dominion_name + " 的传送点," +
"当前位置为 " + operator.getLocation().getBlockX() + " " +
operator.getLocation().getBlockY() + 1 + " " +
operator.getLocation().getBlockZ());
} else {
Notification.error(operator, "你不在领地 " + dominion_name + " 内,无法设置传送点");
}
}

/**
* 重命名领地
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ public static DominionDTO setFlag(Player operator, String flag, boolean value, S
case "repeater": return dominion.setRepeater(value);
case "shear": return dominion.setShear(value);
case "shoot": return dominion.setShoot(value);
case "teleport": return dominion.setTeleport(value);
case "tnt_explode": return dominion.setTntExplode(value);
case "trade": return dominion.setTrade(value);
case "trample": return dominion.setTrample(value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,9 @@ public static boolean setPrivilege(Player operator, String player_name, String f
case "shoot":
privilege.setShoot(value);
break;
case "teleport":
privilege.setTeleport(value);
break;
case "trade":
privilege.setTrade(value);
break;
Expand Down Expand Up @@ -266,7 +269,7 @@ private static PlayerPrivilegeDTO createPlayerPrivilege(Player operator, UUID pl
dom.getPlace(), dom.getPressure(),
dom.getRiding(), dom.getRepeater(),
dom.getShear(), dom.getShoot(),
dom.getTrade(),
dom.getTeleport(), dom.getTrade(),
dom.getVehicleDestroy(),
dom.getVehicleSpawn());
privilege = PlayerPrivilegeDTO.insert(privilege);
Expand Down
Loading

0 comments on commit 28735ef

Please sign in to comment.