From db3f0f719bc84c792f0063b3e64c58e601e1b203 Mon Sep 17 00:00:00 2001 From: Liyan Zhao Date: Sun, 14 Jul 2024 00:29:33 +0800 Subject: [PATCH] fix --- .../enclosure/command/AboutSubcommand.kt | 55 ++++ .../enclosure/command/CreateSubcommand.kt | 142 ++++++++ .../enclosure/command/EnclosureCommand.kt | 311 +----------------- .../enclosure/command/ListSubcommand.kt | 50 +++ .../enclosure/command/TeleportSubcommand.kt | 89 +++++ 5 files changed, 353 insertions(+), 294 deletions(-) create mode 100644 src/main/kotlin/com/github/zly2006/enclosure/command/AboutSubcommand.kt create mode 100644 src/main/kotlin/com/github/zly2006/enclosure/command/CreateSubcommand.kt create mode 100644 src/main/kotlin/com/github/zly2006/enclosure/command/ListSubcommand.kt create mode 100644 src/main/kotlin/com/github/zly2006/enclosure/command/TeleportSubcommand.kt diff --git a/src/main/kotlin/com/github/zly2006/enclosure/command/AboutSubcommand.kt b/src/main/kotlin/com/github/zly2006/enclosure/command/AboutSubcommand.kt new file mode 100644 index 0000000..7213f76 --- /dev/null +++ b/src/main/kotlin/com/github/zly2006/enclosure/command/AboutSubcommand.kt @@ -0,0 +1,55 @@ +package com.github.zly2006.enclosure.command + +import com.github.zly2006.enclosure.MOD_VERSION +import com.github.zly2006.enclosure.network.config.EnclosureInstalledC2SPacket +import com.github.zly2006.enclosure.utils.Permission +import com.github.zly2006.enclosure.utils.Serializable2Text.SerializationSettings +import com.github.zly2006.enclosure.utils.TrT +import com.github.zly2006.enclosure.utils.gold +import net.minecraft.text.ClickEvent +import net.minecraft.text.Text + +fun BuilderScope<*>.registerAbout() { + literal("about") { + permission("enclosure.command.about", BuilderScope.Companion.DefaultPermission.TRUE) + executes { + val player = source.player + source.sendMessage(TrT.of("enclosure.about.author")) + source.sendMessage(TrT.of("enclosure.about.source").styled { + it.withClickEvent(ClickEvent(ClickEvent.Action.OPEN_URL, "https://github.com/zly2006/Enclosure")) + }) + source.sendMessage(TrT.of("enclosure.about.team_page")) + source.sendMessage( + TrT.of("enclosure.about.version.server").append(MOD_VERSION.friendlyString) + ) + if (player != null && EnclosureInstalledC2SPacket.isInstalled(player)) { + val version = EnclosureInstalledC2SPacket.clientVersion(player) + source.sendMessage(TrT.of("enclosure.about.version.client").append(version!!.friendlyString)) + } + source.sendMessage(TrT.of("enclosure.about.copyright")) + } + } + literal("flags") { + permission("enclosure.command.flags", BuilderScope.Companion.DefaultPermission.TRUE) + paged({ "/enclosure flags" }) { + Permission.PERMISSIONS.values.map { + it.serialize(SerializationSettings.Full, null) + } + } + } + literal("limits") { + permission("enclosure.command.limits", BuilderScope.Companion.DefaultPermission.TRUE) + executes { + val limits = getLimits(this) + val translatable = TrT.of("enclosure.message.limit.header") + limits.javaClass.fields.mapNotNull { field -> + runCatching { + Text.literal("\n") + .append(TrT.limit(field).append(": ").gold()) + .append(field[limits].toString()) + }.getOrNull() + }.forEach { text -> translatable.append(text) } + source.sendMessage(translatable) + } + } +} diff --git a/src/main/kotlin/com/github/zly2006/enclosure/command/CreateSubcommand.kt b/src/main/kotlin/com/github/zly2006/enclosure/command/CreateSubcommand.kt new file mode 100644 index 0000000..fb831a4 --- /dev/null +++ b/src/main/kotlin/com/github/zly2006/enclosure/command/CreateSubcommand.kt @@ -0,0 +1,142 @@ +package com.github.zly2006.enclosure.command + +import com.github.zly2006.enclosure.Enclosure +import com.github.zly2006.enclosure.EnclosureArea +import com.github.zly2006.enclosure.LOGGER +import com.github.zly2006.enclosure.ServerMain +import com.github.zly2006.enclosure.utils.Serializable2Text.SerializationSettings +import com.github.zly2006.enclosure.utils.TrT +import com.github.zly2006.enclosure.utils.plus +import com.mojang.brigadier.arguments.StringArgumentType +import com.mojang.brigadier.context.CommandContext +import net.minecraft.server.command.ServerCommandSource +import net.minecraft.text.Text +import net.minecraft.util.math.BlockPos + +fun BuilderScope<*>.registerCreate() { + literal("create") { + permission("enclosure.command.create", BuilderScope.Companion.DefaultPermission.TRUE) + argument("name", StringArgumentType.word()) { + executes { + createEnclosure(this) + } + } + } + literal("auto") { + permission("enclosure.command.create", BuilderScope.Companion.DefaultPermission.TRUE) + argument("name", StringArgumentType.word()) { + executes { + val pos = BlockPos.ofFloored(source.position) + val limits = getLimits(this) + val session = sessionOf(source) + val expandX = (limits.maxXRange - 1) / 2 + val expandZ = (limits.maxZRange - 1) / 2 + session.enabled = true + session.world = source.world + session.pos1 = BlockPos(pos.x - expandX, limits.minY, pos.z - expandZ) + session.pos2 = BlockPos( + pos.x + expandX, + limits.maxY.coerceAtMost(limits.maxHeight + limits.minY - 1), + pos.z + expandZ + ) + session.owner = source.uuid + createEnclosure(this) + } + } + } + literal("subzone") { + permission("enclosure.command.subzone", BuilderScope.Companion.DefaultPermission.TRUE) + argument("name", StringArgumentType.string()) { + executes { + val name = StringArgumentType.getString(this, "name") + if (name.length > ServerMain.commonConfig.maxEnclosureNameLength) { + error(TrT.of("enclosure.message.res_name_too_long"), this) + } + if (name.chars().anyMatch { c: Int -> !Character.isLetterOrDigit(c) && c != '_'.code }) { + error(TrT.of("enclosure.message.res_name_invalid"), this) + } + val session = sessionOf(source) + val list = ServerMain.getAllEnclosures(sessionOf(source).world) + val area: EnclosureArea = Enclosure(session, name) + val enclosure = list.areas.firstOrNull { res -> + res is Enclosure && res.includesArea(area) + } as Enclosure? ?: error(TrT.of("enclosure.message.no_father_enclosure"), this) + if (enclosure.subEnclosures.areas.any { it.name.equals(name, ignoreCase = true) }) { + error(TrT.of("enclosure.message.name_in_use"), this) + } + if (!enclosure.isOwner(source)) { + error(TrT.of("enclosure.message.not_owner"), this) + } + val intersectArea = sessionOf(source).intersect(enclosure.subEnclosures) + if (intersectArea != null) { + error( + TrT.of("enclosure.message.intersected") + .append(intersectArea.serialize(SerializationSettings.Name, source.player)), this + ) + } + val limits = getLimits(this) + if (!source.hasPermissionLevel(4)) { + checkSessionSize(session, this) + val count = enclosure.subEnclosures.areas.size.toLong() + if (count > limits.maxSubLands) { + error( + TrT.of("enclosure.message.scle").append(Text.literal(limits.maxSubLands.toString())), + this + ) + } + } + area.changeWorld(session.world) + enclosure.addChild(area) + source.sendMessage( + TrT.of("enclosure.message.created") + .append(area.serialize(SerializationSettings.Name, source.player)) + ) + LOGGER.info("Created subzone {} by {}", area.fullName, source.name) + } + } + } +} + +private fun createEnclosure(context: CommandContext) { + val name = StringArgumentType.getString(context, "name") + if (ServerMain.getEnclosure(name) != null) { + error(TrT.of("enclosure.message.name_in_use"), context) + } + if (!context.source.hasPermissionLevel(4)) { + if (name.length > ServerMain.commonConfig.maxEnclosureNameLength) { + error(TrT.of("enclosure.message.res_name_too_long"), context) + } + } + if (name.chars().anyMatch { c: Int -> !Character.isLetterOrDigit(c) && c != '_'.code }) { + error(TrT.of("enclosure.message.res_name_invalid"), context) + } + val session = sessionOf(context.source) + checkSession(context) + val list = ServerMain.getAllEnclosures(session.world) + val intersectArea = sessionOf(context.source).intersect(list) + if (intersectArea != null) { + error(TrT.of("enclosure.message.intersected") + intersectArea.serialize(SerializationSettings.Name, context.source.player), context) + } + val enclosure = Enclosure(session, name) + val limits = getLimits(context) + if (!context.source.hasPermissionLevel(4)) { + checkSessionSize(session, context) + if (context.source.player != null) { + val count = ServerMain.getAllEnclosuresForSuggestion(context.source.uuid).size.toLong() + if (count >= limits.maxLands) { + error( + TrT.of("enclosure.message.rcle.self") + .append(Text.literal(limits.maxLands.toString())), context + ) + } + } + } + enclosure.changeWorld(session.world) + session.reset(session.world) + list.addArea(enclosure) + context.source.sendMessage( + TrT.of("enclosure.message.created") + .append(enclosure.serialize(SerializationSettings.Name, context.source.player)) + ) + LOGGER.info(context.source.name + " created enclosure " + enclosure.name) +} diff --git a/src/main/kotlin/com/github/zly2006/enclosure/command/EnclosureCommand.kt b/src/main/kotlin/com/github/zly2006/enclosure/command/EnclosureCommand.kt index 17f7ce3..e67870f 100644 --- a/src/main/kotlin/com/github/zly2006/enclosure/command/EnclosureCommand.kt +++ b/src/main/kotlin/com/github/zly2006/enclosure/command/EnclosureCommand.kt @@ -1,8 +1,10 @@ package com.github.zly2006.enclosure.command -import com.github.zly2006.enclosure.* -import com.github.zly2006.enclosure.access.PlayerAccess +import com.github.zly2006.enclosure.Enclosure +import com.github.zly2006.enclosure.EnclosureArea +import com.github.zly2006.enclosure.ServerMain import com.github.zly2006.enclosure.gui.EnclosureScreenHandler +import com.github.zly2006.enclosure.minecraftServer import com.github.zly2006.enclosure.network.config.EnclosureInstalledC2SPacket import com.github.zly2006.enclosure.utils.* import com.github.zly2006.enclosure.utils.Serializable2Text.SerializationSettings @@ -17,21 +19,18 @@ import com.mojang.brigadier.suggestion.SuggestionsBuilder import com.mojang.brigadier.tree.LiteralCommandNode import net.minecraft.command.CommandRegistryAccess import net.minecraft.command.CommandSource -import net.minecraft.command.argument.DimensionArgumentType import net.minecraft.command.argument.EntityArgumentType import net.minecraft.server.command.CommandManager import net.minecraft.server.command.ServerCommandSource +import net.minecraft.server.network.ServerPlayerEntity import net.minecraft.server.world.ServerWorld -import net.minecraft.text.ClickEvent import net.minecraft.text.Style import net.minecraft.text.Text import net.minecraft.util.Formatting -import net.minecraft.util.Identifier import net.minecraft.util.math.BlockPos import net.minecraft.util.math.Box import net.minecraft.util.math.Vec3d import java.util.* -import java.util.function.Consumer typealias argT = ArgumentBuilder @@ -98,8 +97,8 @@ fun optionalBooleanArgument(): RequiredArgumentBuilder) { val session = sessionOf(context.source) @@ -121,50 +120,6 @@ fun checkSessionSize(session: Session, context: CommandContext) { - val name = StringArgumentType.getString(context, "name") - if (ServerMain.getEnclosure(name) != null) { - error(TrT.of("enclosure.message.name_in_use"), context) - } - if (!context.source.hasPermissionLevel(4)) { - if (name.length > ServerMain.commonConfig.maxEnclosureNameLength) { - error(TrT.of("enclosure.message.res_name_too_long"), context) - } - } - if (name.chars().anyMatch { c: Int -> !Character.isLetterOrDigit(c) && c != '_'.code }) { - error(TrT.of("enclosure.message.res_name_invalid"), context) - } - val session = sessionOf(context.source) - checkSession(context) - val list = ServerMain.getAllEnclosures(session.world) - val intersectArea = sessionOf(context.source).intersect(list) - if (intersectArea != null) { - error(TrT.of("enclosure.message.intersected") + intersectArea.serialize(SerializationSettings.Name, context.source.player), context) - } - val enclosure = Enclosure(session, name) - val limits = getLimits(context) - if (!context.source.hasPermissionLevel(4)) { - checkSessionSize(session, context) - if (context.source.player != null) { - val count = ServerMain.getAllEnclosuresForSuggestion(context.source.uuid).size.toLong() - if (count >= limits.maxLands) { - error( - TrT.of("enclosure.message.rcle.self") - .append(Text.literal(limits.maxLands.toString())), context - ) - } - } - } - enclosure.changeWorld(session.world) - session.reset(session.world) - list.addArea(enclosure) - context.source.sendMessage( - TrT.of("enclosure.message.created") - .append(enclosure.serialize(SerializationSettings.Name, context.source.player)) - ) - LOGGER.info(context.source.name + " created enclosure " + enclosure.name) -} - fun getOfflineUUID(context: CommandContext): UUID { try { return UUID.fromString(StringArgumentType.getString(context, "player")) @@ -185,89 +140,6 @@ fun BuilderScope<*>.registerConfirmCommand() { fun register(dispatcher: CommandDispatcher, access: CommandRegistryAccess): LiteralCommandNode? { val node = BuilderScope(CommandManager.literal("enclosure")).apply { - registerConfirmCommand() - registerAdmin() - literal("about") { - permission("enclosure.command.about", BuilderScope.Companion.DefaultPermission.TRUE) - executes { - val player = source.player - source.sendMessage(TrT.of("enclosure.about.author")) - source.sendMessage(TrT.of("enclosure.about.source").styled { - it.withClickEvent(ClickEvent(ClickEvent.Action.OPEN_URL, "https://github.com/zly2006/Enclosure")) - }) - source.sendMessage(TrT.of("enclosure.about.team_page")) - source.sendMessage( - TrT.of("enclosure.about.version.server").append(MOD_VERSION.friendlyString) - ) - if (player != null && EnclosureInstalledC2SPacket.isInstalled(player)) { - val version = EnclosureInstalledC2SPacket.clientVersion(player) - source.sendMessage(TrT.of("enclosure.about.version.client").append(version!!.friendlyString)) - } - source.sendMessage(TrT.of("enclosure.about.copyright")) - } - } - literal("flags") { - permission("enclosure.command.flags", BuilderScope.Companion.DefaultPermission.TRUE) - paged({ "/enclosure flags" }) { - Permission.PERMISSIONS.values.map { - it.serialize(SerializationSettings.Full, null) - } - } - } - literal("limits") { - permission("enclosure.command.limits", BuilderScope.Companion.DefaultPermission.TRUE) - executes { - val limits = getLimits(this) - val translatable = TrT.of("enclosure.message.limit.header") - limits.javaClass.fields.mapNotNull { field -> - runCatching { - Text.literal("\n") - .append(TrT.limit(field).append(": ").gold()) - .append(field[limits].toString()) - }.getOrNull() - }.forEach { text -> translatable.append(text) } - source.sendMessage(translatable) - } - } - literal("list") { - permission("enclosure.command.list", BuilderScope.Companion.DefaultPermission.TRUE) - paged({ "/enclosure list" }) { - ServerMain.getAllEnclosures().map { - it.serialize(SerializationSettings.Summarize, null) - } - } - literal("world") { - argument("world", DimensionArgumentType.dimension()) { - paged({ - val world = getArgument("world", Identifier::class.java) - "/enclosure list world $world" - }, { - val world = DimensionArgumentType.getDimensionArgument(this, "world") - ServerMain.getAllEnclosures(world).areas.map { - it.serialize(SerializationSettings.Summarize, null) - } - }) - } - } - literal("user") { - argument(offlinePlayerArgument()) { - executes { - val uuid = getOfflineUUID(this) - val list = ServerMain.getAllEnclosuresForSuggestion(uuid) - val ret = TrT.of("enclosure.message.list.user", Utils.getDisplayNameByUUID(uuid), list.size) - list.forEach(Consumer { e: Enclosure -> - ret.append("\n").append( - e.serialize( - SerializationSettings.Summarize, - source.player - ) - ) - }) - source.sendMessage(ret) - } - } - } - } literal("help") { permission("enclosure.command.help", BuilderScope.Companion.DefaultPermission.TRUE) executes { @@ -295,34 +167,12 @@ fun register(dispatcher: CommandDispatcher, access: Command } } } - literal("auto") { - permission("enclosure.command.auto", BuilderScope.Companion.DefaultPermission.TRUE) - argument("name", StringArgumentType.word()) { - executes { - val pos = BlockPos.ofFloored(source.position) - val limits = getLimits(this) - val session = sessionOf(source) - val expandX = (limits.maxXRange - 1) / 2 - val expandZ = (limits.maxZRange - 1) / 2 - session.enabled = true - session.world = source.world - session.pos1 = BlockPos(pos.x - expandX, limits.minY, pos.z - expandZ) - session.pos2 = BlockPos( - pos.x + expandX, - limits.maxY.coerceAtMost(limits.maxHeight + limits.minY - 1), - pos.z + expandZ - ) - session.owner = source.uuid - createEnclosure(this) - } - } - } literal("gui") { // Note: this register block only runs once BuilderScope.map["enclosure.gui"] = BuilderScope.Companion.DefaultPermission.TRUE parent.requires { it.isExecutedByPlayer && - checkPermission(it, "enclosure.gui") + checkPermission(it, "enclosure.gui") } optionalEnclosure { val player = source.player!! @@ -347,144 +197,17 @@ fun register(dispatcher: CommandDispatcher, access: Command source.sendMessage(text) } } - literal("tp") { - permission("enclosure.command.tp", BuilderScope.Companion.DefaultPermission.TRUE) - argument(landArgument()) { - executes { - val player = source.player!! - val lastTeleportTimeSpan = - System.currentTimeMillis() - (player as PlayerAccess).permissionDeniedMsgTime - val cd = ServerMain.commonConfig.teleportCooldown - val area = getEnclosure(this) + registerConfirmCommand() + registerAdmin() + registerAbout() + registerList() - if (!area.hasPerm(player, Permission.COMMAND_TP)) { - player.sendMessage(Permission.COMMAND_TP.getNoPermissionMsg(player)) - return@executes - } - if (!source.hasPermissionLevel(4) && cd > 0 && lastTeleportTimeSpan < cd) { - error( - TrT.of( - "enclosure.message.teleport_too_fast", - String.format("%.1f", (cd - lastTeleportTimeSpan) / 1000.0) - ), this - ) - } - if (!source.hasPermissionLevel(4) && ServerMain.commonConfig.restrictEnclosureTp) { - if (source.player is PlayerAccess) { - if (!(source.player as PlayerAccess).visitedEnclosures.contains(area.uuid)) { - error(TrT.of("enclosure.message.unvisited_enclosure"), this) - } - } - } - (player as PlayerAccess).permissionDeniedMsgTime = System.currentTimeMillis() - if (ServerMain.commonConfig.showTeleportWarning) { - if (!isPositionSafe(area.world, area.teleportPos!!)) { - source.sendMessage( - TrT.of("enclosure.message.teleport_warning").formatted(Formatting.YELLOW) - ) - ConfirmManager.confirm(null, source.player) { area.teleport(player) } - } else { - area.teleport(player) - } - } else { - area.teleport(player) - } - } - } - } - literal("settp") { - permission("enclosure.command.settp", BuilderScope.Companion.DefaultPermission.TRUE) - optionalEnclosure { area -> - if (!area.hasPerm(source.player!!, Permission.ADMIN)) { - error(Permission.ADMIN.getNoPermissionMsg(source.player), this) - } - if (!area.contains(BlockPos.ofFloored(source.position))) { - error(TrT.of("enclosure.message.res_settp_pos_error"), this) - } - area.setTeleportPos(source.position, source.rotation.y, source.rotation.x) - if (ServerMain.commonConfig.showTeleportWarning && !isPositionSafe(area.world, area.teleportPos!!)) { - source.sendMessage( - TrT.of("enclosure.message.teleport_warning.on_set").formatted(Formatting.YELLOW) - ) - } - source.sendMessage( - TrT.of("enclosure.message.change_teleport_position.0") - .append(area.serialize(SerializationSettings.Name, source.player)) - .append(TrT.of("enclosure.message.change_teleport_position.1")) - .append( - String.format( - "[%.2f, %.2f, %.2f](yaw: %.2f, pitch: %.2f)", - source.position.x, - source.position.y, - source.position.z, - source.rotation.y, - source.rotation.x - ) - ) - ) - } - } - literal("create") { - permission("enclosure.command.create", BuilderScope.Companion.DefaultPermission.TRUE) - argument("name", StringArgumentType.word()) { - executes { - createEnclosure(this) - } - } - } - literal("subzone") { - permission("enclosure.command.subzone", BuilderScope.Companion.DefaultPermission.TRUE) - argument("name", StringArgumentType.string()) { - executes { - val name = StringArgumentType.getString(this, "name") - if (name.length > ServerMain.commonConfig.maxEnclosureNameLength) { - error(TrT.of("enclosure.message.res_name_too_long"), this) - } - if (name.chars().anyMatch { c: Int -> !Character.isLetterOrDigit(c) && c != '_'.code }) { - error(TrT.of("enclosure.message.res_name_invalid"), this) - } - val session = sessionOf(source) - val list = ServerMain.getAllEnclosures(sessionOf(source).world) - val area: EnclosureArea = Enclosure(session, name) - val enclosure = list.areas.firstOrNull { res -> - res is Enclosure && res.includesArea(area) - } as Enclosure? ?: error(TrT.of("enclosure.message.no_father_enclosure"), this) - if (enclosure.subEnclosures.areas.any { it.name.equals(name, ignoreCase = true) }) { - error(TrT.of("enclosure.message.name_in_use"), this) - } - if (!enclosure.isOwner(source)) { - error(TrT.of("enclosure.message.not_owner"), this) - } - val intersectArea = sessionOf(source).intersect(enclosure.subEnclosures) - if (intersectArea != null) { - error( - TrT.of("enclosure.message.intersected") - .append(intersectArea.serialize(SerializationSettings.Name, source.player)), this - ) - } - val limits = getLimits(this) - if (!source.hasPermissionLevel(4)) { - checkSessionSize(session, this) - val count = enclosure.subEnclosures.areas.size.toLong() - if (count > limits.maxSubLands) { - error( - TrT.of("enclosure.message.scle").append(Text.literal(limits.maxSubLands.toString())), - this - ) - } - } - area.changeWorld(session.world) - enclosure.addChild(area) - source.sendMessage( - TrT.of("enclosure.message.created") - .append(area.serialize(SerializationSettings.Name, source.player)) - ) - LOGGER.info("Created subzone {} by {}", area.fullName, source.name) - } - } - } + registerCreate() registerRename() registerRemove() + + registerTeleport() + registerSelection() registerPermissions() registerMessages(access) } @@ -492,7 +215,7 @@ fun register(dispatcher: CommandDispatcher, access: Command } @Suppress("ReplaceSizeCheckWithIsNotEmpty") -fun isPositionSafe(world: ServerWorld, teleportPos: Vec3d): Boolean { +fun isPositionSafe(world: ServerWorld, teleportPos: Vec3d, player: ServerPlayerEntity): Boolean { // load chunks val pos = BlockPos.ofFloored(teleportPos) world.getChunk(pos) diff --git a/src/main/kotlin/com/github/zly2006/enclosure/command/ListSubcommand.kt b/src/main/kotlin/com/github/zly2006/enclosure/command/ListSubcommand.kt new file mode 100644 index 0000000..9e5a420 --- /dev/null +++ b/src/main/kotlin/com/github/zly2006/enclosure/command/ListSubcommand.kt @@ -0,0 +1,50 @@ +package com.github.zly2006.enclosure.command + +import com.github.zly2006.enclosure.ServerMain +import com.github.zly2006.enclosure.utils.Serializable2Text.SerializationSettings +import com.github.zly2006.enclosure.utils.TrT +import com.github.zly2006.enclosure.utils.Utils +import net.minecraft.command.argument.DimensionArgumentType +import net.minecraft.util.Identifier + +fun BuilderScope<*>.registerList() { + literal("list") { + permission("enclosure.command.list", BuilderScope.Companion.DefaultPermission.TRUE) + paged({ "/enclosure list" }) { + ServerMain.getAllEnclosures().map { + it.serialize(SerializationSettings.Summarize, null) + } + } + literal("world") { + argument("world", DimensionArgumentType.dimension()) { + paged({ + val world = getArgument("world", Identifier::class.java) + "/enclosure list world $world" + }, { + val world = DimensionArgumentType.getDimensionArgument(this, "world") + ServerMain.getAllEnclosures(world).areas.map { + it.serialize(SerializationSettings.Summarize, null) + } + }) + } + } + literal("user") { + argument(offlinePlayerArgument()) { + executes { + val uuid = getOfflineUUID(this) + val list = ServerMain.getAllEnclosuresForSuggestion(uuid) + val ret = TrT.of("enclosure.message.list.user", Utils.getDisplayNameByUUID(uuid), list.size) + list.forEach { + ret.append("\n").append( + it.serialize( + SerializationSettings.Summarize, + source.player + ) + ) + } + source.sendMessage(ret) + } + } + } + } +} diff --git a/src/main/kotlin/com/github/zly2006/enclosure/command/TeleportSubcommand.kt b/src/main/kotlin/com/github/zly2006/enclosure/command/TeleportSubcommand.kt new file mode 100644 index 0000000..ec69344 --- /dev/null +++ b/src/main/kotlin/com/github/zly2006/enclosure/command/TeleportSubcommand.kt @@ -0,0 +1,89 @@ +package com.github.zly2006.enclosure.command + +import com.github.zly2006.enclosure.ServerMain +import com.github.zly2006.enclosure.access.PlayerAccess +import com.github.zly2006.enclosure.utils.Permission +import com.github.zly2006.enclosure.utils.Serializable2Text.SerializationSettings +import com.github.zly2006.enclosure.utils.TrT +import net.minecraft.util.Formatting +import net.minecraft.util.math.BlockPos + +fun BuilderScope<*>.registerTeleport() { + literal("tp") { + permission("enclosure.command.tp", BuilderScope.Companion.DefaultPermission.TRUE) + argument(landArgument()) { + executes { + val player = source.player!! + val lastTeleportTimeSpan = + System.currentTimeMillis() - (player as PlayerAccess).permissionDeniedMsgTime + val cd = ServerMain.commonConfig.teleportCooldown + val area = getEnclosure(this) + + if (!area.hasPerm(player, Permission.COMMAND_TP)) { + player.sendMessage(Permission.COMMAND_TP.getNoPermissionMsg(player)) + return@executes + } + if (!source.hasPermissionLevel(4) && cd > 0 && lastTeleportTimeSpan < cd) { + error( + TrT.of( + "enclosure.message.teleport_too_fast", + String.format("%.1f", (cd - lastTeleportTimeSpan) / 1000.0) + ), this + ) + } + if (!source.hasPermissionLevel(4) && ServerMain.commonConfig.restrictEnclosureTp) { + if (source.player is PlayerAccess) { + if (!(source.player as PlayerAccess).visitedEnclosures.contains(area.uuid)) { + error(TrT.of("enclosure.message.unvisited_enclosure"), this) + } + } + } + (player as PlayerAccess).permissionDeniedMsgTime = System.currentTimeMillis() + if (ServerMain.commonConfig.showTeleportWarning) { + if (!isPositionSafe(area.world, area.teleportPos!!, player)) { + source.sendMessage( + TrT.of("enclosure.message.teleport_warning").formatted(Formatting.YELLOW) + ) + ConfirmManager.confirm(null, source.player) { area.teleport(player) } + } else { + area.teleport(player) + } + } else { + area.teleport(player) + } + } + } + } + literal("settp") { + permission("enclosure.command.settp", BuilderScope.Companion.DefaultPermission.TRUE) + optionalEnclosure { area -> + if (!area.hasPerm(source.player!!, Permission.ADMIN)) { + error(Permission.ADMIN.getNoPermissionMsg(source.player), this) + } + if (!area.contains(BlockPos.ofFloored(source.position))) { + error(TrT.of("enclosure.message.res_settp_pos_error"), this) + } + area.setTeleportPos(source.position, source.rotation.y, source.rotation.x) + if (ServerMain.commonConfig.showTeleportWarning && !isPositionSafe(area.world, area.teleportPos!!, source.player!!)) { + source.sendMessage( + TrT.of("enclosure.message.teleport_warning.on_set").formatted(Formatting.YELLOW) + ) + } + source.sendMessage( + TrT.of("enclosure.message.change_teleport_position.0") + .append(area.serialize(SerializationSettings.Name, source.player)) + .append(TrT.of("enclosure.message.change_teleport_position.1")) + .append( + String.format( + "[%.2f, %.2f, %.2f](yaw: %.2f, pitch: %.2f)", + source.position.x, + source.position.y, + source.position.z, + source.rotation.y, + source.rotation.x + ) + ) + ) + } + } +}