From d2ecf4fdda3368271d0a981485d6223f8f7e9cb9 Mon Sep 17 00:00:00 2001 From: TheWhiteDog9487 Date: Wed, 17 Jun 2026 16:45:12 +0800 Subject: [PATCH] =?UTF-8?q?0.5.4=20=E4=BF=AE=E5=A4=8D=E5=8D=95=E4=BA=BA?= =?UTF-8?q?=E6=B8=B8=E6=88=8F=E6=97=B6=E5=9C=A8=E4=B8=96=E7=95=8C=E5=86=85?= =?UTF-8?q?=E5=88=87=E6=8D=A2=E8=AF=AD=E8=A8=80=E5=90=8E=E5=91=BD=E4=BB=A4?= =?UTF-8?q?=E6=89=A7=E8=A1=8C=E5=A4=B1=E8=B4=A5=E7=9A=84=E9=97=AE=E9=A2=98?= =?UTF-8?q?=20=E5=91=BD=E4=BB=A4=E6=8F=90=E7=A4=BA=E6=96=87=E6=9C=AC?= =?UTF-8?q?=E5=9C=A8=E5=88=87=E6=8D=A2=E8=AF=AD=E8=A8=80=E4=B9=8B=E5=90=8E?= =?UTF-8?q?=E6=94=AF=E6=8C=81=E7=83=AD=E9=87=8D=E8=BD=BD=E4=BA=86=EF=BC=8C?= =?UTF-8?q?=E4=B8=8D=E5=86=8D=E9=9C=80=E8=A6=81=E9=87=8D=E5=90=AF=E9=9B=86?= =?UTF-8?q?=E6=88=90=E6=9C=8D=E5=8A=A1=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- gradle.properties | 2 +- .../xyz/thewhitedog9487/CommandRegister.kt | 354 ++++++++++-------- .../Event/ResourceReloaderListener.kt | 35 ++ .../Event/ServerLifecycleListener.kt | 18 + .../xyz/thewhitedog9487/RandomTeleporter.kt | 14 +- .../ResourceReloaderListener.kt | 45 --- .../ServerLifecycleListener.kt | 8 - 7 files changed, 257 insertions(+), 219 deletions(-) create mode 100644 src/main/kotlin/xyz/thewhitedog9487/Event/ResourceReloaderListener.kt create mode 100644 src/main/kotlin/xyz/thewhitedog9487/Event/ServerLifecycleListener.kt delete mode 100644 src/main/kotlin/xyz/thewhitedog9487/ResourceReloaderListener.kt delete mode 100644 src/main/kotlin/xyz/thewhitedog9487/ServerLifecycleListener.kt diff --git a/gradle.properties b/gradle.properties index e6b527c..8cf8848 100644 --- a/gradle.properties +++ b/gradle.properties @@ -15,7 +15,7 @@ fabric_api_version=0.152.1+26.2 fabric_kotlin_version=1.13.12+kotlin.2.4.0 # Mod Properties -mod_version=0.5.3 +mod_version=0.5.4 maven_group=xyz.thewhitedog9487 # Dependencies diff --git a/src/main/kotlin/xyz/thewhitedog9487/CommandRegister.kt b/src/main/kotlin/xyz/thewhitedog9487/CommandRegister.kt index fd5260f..1e9ef2c 100644 --- a/src/main/kotlin/xyz/thewhitedog9487/CommandRegister.kt +++ b/src/main/kotlin/xyz/thewhitedog9487/CommandRegister.kt @@ -1,7 +1,9 @@ package xyz.thewhitedog9487 import com.mojang.brigadier.Command +import com.mojang.brigadier.CommandDispatcher import com.mojang.brigadier.arguments.IntegerArgumentType +import com.mojang.brigadier.tree.LiteralCommandNode import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback import net.minecraft.commands.CommandSourceStack import net.minecraft.commands.Commands @@ -80,175 +82,205 @@ const val CommandExecuteFailure = 0 val CommandRootNodeName = setOf( "随机传送", "rtp" ) + +val CommandNodes: MutableSet> = mutableSetOf() + +/** + * 是否注册过Fabric API的命令注册回调监听器 + */ +var CommandRegistrationCallbackEventHasBeenRegistered = false + +val CommandArgumentName_Radius = Component.translatableWithFallback("command.argument.radius", "Radius(半径)") +val CommandArgumentName_Target = Component.translatableWithFallback("command.argument.target", "PlayerID(被传送玩家名)") +val CommandArgumentName_OriginPosition = Component.translatableWithFallback("command.argument.origin_pos", "OriginPos(随机中心,坐标)") +val CommandArgumentName_OriginEntity = Component.translatableWithFallback("command.argument.origin_entity", "OriginEntity(随机中心,实体)") +val CommandArgumentName_RegionFromPosition = Component.translatableWithFallback( + "command.argument.region_from_pos", + "RegionFrom(随机范围起始位置,坐标)") +val CommandArgumentName_RegionToPosition = Component.translatableWithFallback( + "command.argument.region_to_pos", + "RegionTo(随机范围结束位置,坐标)") +val CommandArgumentName_RegionFromEntity = Component.translatableWithFallback( + "command.argument.region_from_entity", + "RegionFrom(随机范围起始位置,实体)") +val CommandArgumentName_RegionToEntity = Component.translatableWithFallback( + "command.argument.region_to_entity", + "RegionTo(随机范围结束位置,实体)") + /** * 使用Fabric API向游戏内注册命令 *
* @see Fabric Docs (中文) - * @see Fabric Wiki (中文) * @see Fabric Docs (English) - * @see Fabric Wiki (English) */ -fun CommandRegister() { +fun CommandRegister(InGameCommandDispatcherInstance: CommandDispatcher? = null) { + val Register: (CommandDispatcherInstance: CommandDispatcher, NodeName: String) -> Unit = { CommandDispatcherInstance, NodeName -> + val Node = CommandDispatcherInstance.register(Commands.literal(NodeName) + // /rtp + .requires(Commands.hasPermission(PermissionLevel)) + .executes{ commandContext -> ExecuteCommand(commandContext.source) } + + // /rtp back + .then(Commands.literal("back") + .requires(Commands.hasPermission(PermissionLevel)) + .executes { commandContext -> TeleportBack(commandContext.source) } ) + + // /rtp back + .then(Commands.literal("back") + .then(Commands.argument(CommandArgumentName_Target.string, EntityArgument.entity()) + .requires(Commands.hasPermission(PermissionLevel)) + .executes { commandContext -> TeleportBack(commandContext.source, + EntityArgument.getEntity(commandContext, CommandArgumentName_Target.string)) } ) ) + + // /rtp back + .then(Commands.argument(CommandArgumentName_Target.string, EntityArgument.entity()) + .then(Commands.literal("back") + .requires(Commands.hasPermission(PermissionLevel)) + .executes { commandContext -> TeleportBack(commandContext.source, + EntityArgument.getEntity(commandContext, CommandArgumentName_Target.string)) } ) ) + + // /rtp + .then(Commands.argument(CommandArgumentName_Radius.string, IntegerArgumentType.integer(0)) + .requires(Commands.hasPermission(PermissionLevel)) + .executes { commandContext -> ExecuteCommand(commandContext.source, + Radius = IntegerArgumentType.getInteger(commandContext, CommandArgumentName_Radius.string)) } ) + + // /rtp + .then(Commands.argument(CommandArgumentName_Target.string, EntityArgument.entity()) + .requires(Commands.hasPermission(PermissionLevel)) + .executes { commandContext -> ExecuteCommand(commandContext.source, + Entity = EntityArgument.getEntity(commandContext, CommandArgumentName_Target.string)) } ) + + // /rtp + .then(Commands.argument(CommandArgumentName_Radius.string, IntegerArgumentType.integer(0)) + .then(Commands.argument(CommandArgumentName_Target.string, EntityArgument.entity()) + .requires(Commands.hasPermission(PermissionLevel)) + .executes { commandContext -> ExecuteCommand(commandContext.source, + Radius = IntegerArgumentType.getInteger(commandContext, CommandArgumentName_Radius.string), + Entity = EntityArgument.getEntity(commandContext, CommandArgumentName_Target.string)) } ) ) + + // /rtp + .then(Commands.argument(CommandArgumentName_Target.string, EntityArgument.entity()) + .then(Commands.argument(CommandArgumentName_Radius.string, IntegerArgumentType.integer(0)) + .requires(Commands.hasPermission(PermissionLevel)) + .executes { commandContext -> ExecuteCommand(commandContext.source, + Radius = IntegerArgumentType.getInteger(commandContext, CommandArgumentName_Radius.string), + Entity = EntityArgument.getEntity(commandContext, CommandArgumentName_Target.string)) } ) ) + + // /rtp + .then(Commands.argument(CommandArgumentName_Radius.string, IntegerArgumentType.integer(0)) + .then(Commands.argument(CommandArgumentName_OriginPosition.string, Vec2Argument.vec2()) + .requires(Commands.hasPermission(PermissionLevel)) + .executes { commandContext -> ExecuteCommand(commandContext.source, + Radius = IntegerArgumentType.getInteger(commandContext, CommandArgumentName_Radius.string), + Origin = Vec2Argument.getVec2(commandContext, CommandArgumentName_OriginPosition.string)) } ) ) + + // /rtp + .then(Commands.argument(CommandArgumentName_Radius.string, IntegerArgumentType.integer(0)) + .then(Commands.argument(CommandArgumentName_Target.string, EntityArgument.entity()) + .then(Commands.argument(CommandArgumentName_OriginEntity.string, EntityArgument.entity()) + .requires(Commands.hasPermission(PermissionLevel)) + .executes { commandContext -> ExecuteCommand(commandContext.source, + Radius = IntegerArgumentType.getInteger(commandContext, CommandArgumentName_Radius.string), + Entity = EntityArgument.getEntity(commandContext, CommandArgumentName_Target.string), + Origin = Vec2( + EntityArgument.getEntity(commandContext, CommandArgumentName_OriginEntity.string).position().x.toFloat(), + EntityArgument.getEntity(commandContext, CommandArgumentName_OriginEntity.string).position().z.toFloat() ) ) } ) ) ) + + // /rtp + .then(Commands.argument(CommandArgumentName_Radius.string, IntegerArgumentType.integer(0)) + .then(Commands.argument(CommandArgumentName_Target.string, EntityArgument.entity()) + .then(Commands.argument(CommandArgumentName_OriginPosition.string, Vec2Argument.vec2()) + .requires(Commands.hasPermission(PermissionLevel)) + .executes { commandContext -> ExecuteCommand(commandContext.source, + Radius = IntegerArgumentType.getInteger(commandContext, CommandArgumentName_Radius.string), + Entity = EntityArgument.getEntity(commandContext, CommandArgumentName_Target.string), + Origin = Vec2Argument.getVec2(commandContext, CommandArgumentName_OriginPosition.string) ) } ) ) ) + + // /rtp + .then(Commands.argument(CommandArgumentName_Target.string, EntityArgument.entity()) + .then(Commands.argument(CommandArgumentName_Radius.string, IntegerArgumentType.integer(0)) + .then(Commands.argument(CommandArgumentName_OriginEntity.string, EntityArgument.entity()) + .requires(Commands.hasPermission(PermissionLevel)) + .executes { commandContext -> ExecuteCommand(commandContext.source, + Radius = IntegerArgumentType.getInteger(commandContext, CommandArgumentName_Radius.string), + Entity = EntityArgument.getEntity(commandContext, CommandArgumentName_Target.string), + Origin = Vec2( + EntityArgument.getEntity(commandContext, CommandArgumentName_OriginEntity.string).position().x.toFloat(), + EntityArgument.getEntity(commandContext, CommandArgumentName_OriginEntity.string).position().z.toFloat() ) ) } ) ) ) + + // /rtp + .then(Commands.argument(CommandArgumentName_Target.string, EntityArgument.entity()) + .then(Commands.argument(CommandArgumentName_Radius.string, IntegerArgumentType.integer(0)) + .then(Commands.argument(CommandArgumentName_OriginPosition.string, Vec2Argument.vec2()) + .requires(Commands.hasPermission(PermissionLevel)) + .executes { commandContext -> ExecuteCommand(commandContext.source, + Radius = IntegerArgumentType.getInteger(commandContext, CommandArgumentName_Radius.string), + Entity = EntityArgument.getEntity(commandContext, CommandArgumentName_Target.string), + Origin = Vec2Argument.getVec2(commandContext, CommandArgumentName_OriginPosition.string) ) } ) ) ) + + // /rtp + .then(Commands.argument(CommandArgumentName_RegionFromPosition.string, Vec2Argument.vec2()) + .then(Commands.argument(CommandArgumentName_RegionToPosition.string, Vec2Argument.vec2()) + .requires(Commands.hasPermission(PermissionLevel)) + .executes { commandContext -> ExecuteCommand(commandContext.source, + RegionFrom = Vec2Argument.getVec2(commandContext, CommandArgumentName_RegionFromPosition.string), + RegionTo = Vec2Argument.getVec2(commandContext, CommandArgumentName_RegionToPosition.string) ) } ) ) + + // /rtp + .then(Commands.argument(CommandArgumentName_RegionFromPosition.string, Vec2Argument.vec2()) + .then(Commands.argument(CommandArgumentName_RegionToPosition.string, Vec2Argument.vec2()) + .then(Commands.argument(CommandArgumentName_Target.string, EntityArgument.entity()) + .requires(Commands.hasPermission(PermissionLevel)) + .executes { commandContext -> ExecuteCommand(commandContext.source, + Entity = EntityArgument.getEntity(commandContext, CommandArgumentName_Target.string), + RegionFrom = Vec2Argument.getVec2(commandContext, CommandArgumentName_RegionFromPosition.string), + RegionTo = Vec2Argument.getVec2(commandContext, CommandArgumentName_RegionToPosition.string) ) } ) ) ) + + // /rtp + .then(Commands.argument(CommandArgumentName_Target.string, EntityArgument.entity()) + .then(Commands.argument(CommandArgumentName_RegionFromPosition.string, Vec2Argument.vec2()) + .then(Commands.argument(CommandArgumentName_RegionToPosition.string, Vec2Argument.vec2()) + .requires(Commands.hasPermission(PermissionLevel)) + .executes { commandContext -> ExecuteCommand(commandContext.source, + Entity = EntityArgument.getEntity(commandContext, CommandArgumentName_Target.string), + RegionFrom = Vec2Argument.getVec2(commandContext, CommandArgumentName_RegionFromPosition.string), + RegionTo = Vec2Argument.getVec2(commandContext, CommandArgumentName_RegionToPosition.string) ) } ) ) ) + + // /rtp + .then(Commands.argument(CommandArgumentName_RegionFromEntity.string, EntityArgument.entity()) + .then(Commands.argument(CommandArgumentName_RegionToEntity.string, EntityArgument.entity()) + .requires(Commands.hasPermission(PermissionLevel)) + .executes { commandContext -> ExecuteCommand(commandContext.source, + RegionFrom = Vec2( + EntityArgument.getEntity(commandContext, CommandArgumentName_RegionFromEntity.string).position().x.toFloat(), + EntityArgument.getEntity(commandContext, CommandArgumentName_RegionFromEntity.string).position().z.toFloat() ), + RegionTo = Vec2( + EntityArgument.getEntity(commandContext, CommandArgumentName_RegionToEntity.string).position().x.toFloat(), + EntityArgument.getEntity(commandContext, CommandArgumentName_RegionToEntity.string).position().z.toFloat() ) ) } ) ) + + // /rtp + .then(Commands.argument(CommandArgumentName_RegionFromEntity.string, EntityArgument.entity()) + .then(Commands.argument(CommandArgumentName_RegionToEntity.string, EntityArgument.entity()) + .then(Commands.argument(CommandArgumentName_Target.string, EntityArgument.entity()) + .requires(Commands.hasPermission(PermissionLevel)) + .executes { commandContext -> ExecuteCommand(commandContext.source, + Entity = EntityArgument.getEntity(commandContext, CommandArgumentName_Target.string), + RegionFrom = Vec2( + EntityArgument.getEntity(commandContext, CommandArgumentName_RegionFromEntity.string).position().x.toFloat(), + EntityArgument.getEntity(commandContext, CommandArgumentName_RegionFromEntity.string).position().z.toFloat() ), + RegionTo = Vec2( + EntityArgument.getEntity(commandContext, CommandArgumentName_RegionToEntity.string).position().x.toFloat(), + EntityArgument.getEntity(commandContext, CommandArgumentName_RegionToEntity.string).position().z.toFloat() ) ) } ) ) ) ) + CommandNodes.add(Node) } + for (RootNodeName in CommandRootNodeName) { + InGameCommandDispatcherInstance?.let { + Register(it, RootNodeName) } } + if (CommandRegistrationCallbackEventHasBeenRegistered) return for (RootNodeName in CommandRootNodeName) { CommandRegistrationCallback.EVENT.register { dispatcher, context, selection -> - dispatcher.register(Commands.literal(RootNodeName) - // /rtp - .requires(Commands.hasPermission(PermissionLevel)) - .executes{ commandContext -> ExecuteCommand(commandContext.source) } - - // /rtp back - .then(Commands.literal("back") - .requires(Commands.hasPermission(PermissionLevel)) - .executes { commandContext -> TeleportBack(commandContext.source) } ) - - // /rtp back - .then(Commands.literal("back") - .then(Commands.argument(CommandArgumentName_Target, EntityArgument.entity()) - .requires(Commands.hasPermission(PermissionLevel)) - .executes { commandContext -> TeleportBack(commandContext.source, - EntityArgument.getEntity(commandContext, CommandArgumentName_Target)) } ) ) - - // /rtp back - .then(Commands.argument(CommandArgumentName_Target, EntityArgument.entity()) - .then(Commands.literal("back") - .requires(Commands.hasPermission(PermissionLevel)) - .executes { commandContext -> TeleportBack(commandContext.source, - EntityArgument.getEntity(commandContext, CommandArgumentName_Target)) } ) ) - - // /rtp - .then(Commands.argument(CommandArgumentName_Radius, IntegerArgumentType.integer(0)) - .requires(Commands.hasPermission(PermissionLevel)) - .executes { commandContext -> ExecuteCommand(commandContext.source, - Radius = IntegerArgumentType.getInteger(commandContext, CommandArgumentName_Radius)) } ) - - // /rtp - .then(Commands.argument(CommandArgumentName_Target, EntityArgument.entity()) - .requires(Commands.hasPermission(PermissionLevel)) - .executes { commandContext -> ExecuteCommand(commandContext.source, - Entity = EntityArgument.getEntity(commandContext, CommandArgumentName_Target)) } ) - - // /rtp - .then(Commands.argument(CommandArgumentName_Radius, IntegerArgumentType.integer(0)) - .then(Commands.argument(CommandArgumentName_Target, EntityArgument.entity()) - .requires(Commands.hasPermission(PermissionLevel)) - .executes { commandContext -> ExecuteCommand(commandContext.source, - Radius = IntegerArgumentType.getInteger(commandContext, CommandArgumentName_Radius), - Entity = EntityArgument.getEntity(commandContext, CommandArgumentName_Target)) } ) ) - - // /rtp - .then(Commands.argument(CommandArgumentName_Target, EntityArgument.entity()) - .then(Commands.argument(CommandArgumentName_Radius, IntegerArgumentType.integer(0)) - .requires(Commands.hasPermission(PermissionLevel)) - .executes { commandContext -> ExecuteCommand(commandContext.source, - Radius = IntegerArgumentType.getInteger(commandContext, CommandArgumentName_Radius), - Entity = EntityArgument.getEntity(commandContext, CommandArgumentName_Target)) } ) ) - - // /rtp - .then(Commands.argument(CommandArgumentName_Radius, IntegerArgumentType.integer(0)) - .then(Commands.argument(CommandArgumentName_OriginPosition, Vec2Argument.vec2()) - .requires(Commands.hasPermission(PermissionLevel)) - .executes { commandContext -> ExecuteCommand(commandContext.source, - Radius = IntegerArgumentType.getInteger(commandContext, CommandArgumentName_Radius), - Origin = Vec2Argument.getVec2(commandContext, CommandArgumentName_OriginPosition)) } ) ) - - // /rtp - .then(Commands.argument(CommandArgumentName_Radius, IntegerArgumentType.integer(0)) - .then(Commands.argument(CommandArgumentName_Target, EntityArgument.entity()) - .then(Commands.argument(CommandArgumentName_OriginEntity, EntityArgument.entity()) - .requires(Commands.hasPermission(PermissionLevel)) - .executes { commandContext -> ExecuteCommand(commandContext.source, - Radius = IntegerArgumentType.getInteger(commandContext, CommandArgumentName_Radius), - Entity = EntityArgument.getEntity(commandContext, CommandArgumentName_Target), - Origin = Vec2( - EntityArgument.getEntity(commandContext, CommandArgumentName_OriginEntity).position().x.toFloat(), - EntityArgument.getEntity(commandContext, CommandArgumentName_OriginEntity).position().z.toFloat() ) ) } ) ) ) - - // /rtp - .then(Commands.argument(CommandArgumentName_Radius, IntegerArgumentType.integer(0)) - .then(Commands.argument(CommandArgumentName_Target, EntityArgument.entity()) - .then(Commands.argument(CommandArgumentName_OriginPosition, Vec2Argument.vec2()) - .requires(Commands.hasPermission(PermissionLevel)) - .executes { commandContext -> ExecuteCommand(commandContext.source, - Radius = IntegerArgumentType.getInteger(commandContext, CommandArgumentName_Radius), - Entity = EntityArgument.getEntity(commandContext, CommandArgumentName_Target), - Origin = Vec2Argument.getVec2(commandContext, CommandArgumentName_OriginPosition) ) } ) ) ) - - // /rtp - .then(Commands.argument(CommandArgumentName_Target, EntityArgument.entity()) - .then(Commands.argument(CommandArgumentName_Radius, IntegerArgumentType.integer(0)) - .then(Commands.argument(CommandArgumentName_OriginEntity, EntityArgument.entity()) - .requires(Commands.hasPermission(PermissionLevel)) - .executes { commandContext -> ExecuteCommand(commandContext.source, - Radius = IntegerArgumentType.getInteger(commandContext, CommandArgumentName_Radius), - Entity = EntityArgument.getEntity(commandContext, CommandArgumentName_Target), - Origin = Vec2( - EntityArgument.getEntity(commandContext, CommandArgumentName_OriginEntity).position().x.toFloat(), - EntityArgument.getEntity(commandContext, CommandArgumentName_OriginEntity).position().z.toFloat() ) ) } ) ) ) - - // /rtp - .then(Commands.argument(CommandArgumentName_Target, EntityArgument.entity()) - .then(Commands.argument(CommandArgumentName_Radius, IntegerArgumentType.integer(0)) - .then(Commands.argument(CommandArgumentName_OriginPosition, Vec2Argument.vec2()) - .requires(Commands.hasPermission(PermissionLevel)) - .executes { commandContext -> ExecuteCommand(commandContext.source, - Radius = IntegerArgumentType.getInteger(commandContext, CommandArgumentName_Radius), - Entity = EntityArgument.getEntity(commandContext, CommandArgumentName_Target), - Origin = Vec2Argument.getVec2(commandContext, CommandArgumentName_OriginPosition) ) } ) ) ) - - // /rtp - .then(Commands.argument(CommandArgumentName_RegionFromPosition, Vec2Argument.vec2()) - .then(Commands.argument(CommandArgumentName_RegionToPosition, Vec2Argument.vec2()) - .requires(Commands.hasPermission(PermissionLevel)) - .executes { commandContext -> ExecuteCommand(commandContext.source, - RegionFrom = Vec2Argument.getVec2(commandContext, CommandArgumentName_RegionFromPosition), - RegionTo = Vec2Argument.getVec2(commandContext, CommandArgumentName_RegionToPosition) ) } ) ) - - // /rtp - .then(Commands.argument(CommandArgumentName_RegionFromPosition, Vec2Argument.vec2()) - .then(Commands.argument(CommandArgumentName_RegionToPosition, Vec2Argument.vec2()) - .then(Commands.argument(CommandArgumentName_Target, EntityArgument.entity()) - .requires(Commands.hasPermission(PermissionLevel)) - .executes { commandContext -> ExecuteCommand(commandContext.source, - Entity = EntityArgument.getEntity(commandContext, CommandArgumentName_Target), - RegionFrom = Vec2Argument.getVec2(commandContext, CommandArgumentName_RegionFromPosition), - RegionTo = Vec2Argument.getVec2(commandContext, CommandArgumentName_RegionToPosition) ) } ) ) ) - - // /rtp - .then(Commands.argument(CommandArgumentName_Target, EntityArgument.entity()) - .then(Commands.argument(CommandArgumentName_RegionFromPosition, Vec2Argument.vec2()) - .then(Commands.argument(CommandArgumentName_RegionToPosition, Vec2Argument.vec2()) - .requires(Commands.hasPermission(PermissionLevel)) - .executes { commandContext -> ExecuteCommand(commandContext.source, - Entity = EntityArgument.getEntity(commandContext, CommandArgumentName_Target), - RegionFrom = Vec2Argument.getVec2(commandContext, CommandArgumentName_RegionFromPosition), - RegionTo = Vec2Argument.getVec2(commandContext, CommandArgumentName_RegionToPosition) ) } ) ) ) - - // /rtp - .then(Commands.argument(CommandArgumentName_RegionFromEntity, EntityArgument.entity()) - .then(Commands.argument(CommandArgumentName_RegionToEntity, EntityArgument.entity()) - .requires(Commands.hasPermission(PermissionLevel)) - .executes { commandContext -> ExecuteCommand(commandContext.source, - RegionFrom = Vec2( - EntityArgument.getEntity(commandContext, CommandArgumentName_RegionFromEntity).position().x.toFloat(), - EntityArgument.getEntity(commandContext, CommandArgumentName_RegionFromEntity).position().z.toFloat() ), - RegionTo = Vec2( - EntityArgument.getEntity(commandContext, CommandArgumentName_RegionToEntity).position().x.toFloat(), - EntityArgument.getEntity(commandContext, CommandArgumentName_RegionToEntity).position().z.toFloat() ) ) } ) ) - - // /rtp - .then(Commands.argument(CommandArgumentName_RegionFromEntity, EntityArgument.entity()) - .then(Commands.argument(CommandArgumentName_RegionToEntity, EntityArgument.entity()) - .then(Commands.argument(CommandArgumentName_Target, EntityArgument.entity()) - .requires(Commands.hasPermission(PermissionLevel)) - .executes { commandContext -> ExecuteCommand(commandContext.source, - Entity = EntityArgument.getEntity(commandContext, CommandArgumentName_Target), - RegionFrom = Vec2( - EntityArgument.getEntity(commandContext, CommandArgumentName_RegionFromEntity).position().x.toFloat(), - EntityArgument.getEntity(commandContext, CommandArgumentName_RegionFromEntity).position().z.toFloat() ), - RegionTo = Vec2( - EntityArgument.getEntity(commandContext, CommandArgumentName_RegionToEntity).position().x.toFloat(), - EntityArgument.getEntity(commandContext, CommandArgumentName_RegionToEntity).position().z.toFloat() ) ) } ) ) ) - ) } } } + Register(dispatcher, RootNodeName) } } + CommandRegistrationCallbackEventHasBeenRegistered = true } fun ExecuteCommand( Source: CommandSourceStack, diff --git a/src/main/kotlin/xyz/thewhitedog9487/Event/ResourceReloaderListener.kt b/src/main/kotlin/xyz/thewhitedog9487/Event/ResourceReloaderListener.kt new file mode 100644 index 0000000..76a10a9 --- /dev/null +++ b/src/main/kotlin/xyz/thewhitedog9487/Event/ResourceReloaderListener.kt @@ -0,0 +1,35 @@ +package xyz.thewhitedog9487.Event + +import net.fabricmc.fabric.api.resource.v1.ResourceLoader +import net.fabricmc.fabric.api.resource.v1.reloader.SimpleReloadListener +import net.minecraft.resources.Identifier +import net.minecraft.server.packs.PackType +import net.minecraft.server.packs.resources.PreparableReloadListener +import xyz.thewhitedog9487.CommandNodes +import xyz.thewhitedog9487.CommandRegister +import xyz.thewhitedog9487.ModID + +fun ResourceReloaderListenerRegister() { + ResourceLoader.get(PackType.CLIENT_RESOURCES).registerReloadListener( + Identifier.fromNamespaceAndPath( + ModID, + "translate_text_apply"), + object: SimpleReloadListener() { + override fun prepare(p0: PreparableReloadListener.SharedState) {} + + override fun apply(p0: Unit, p1: PreparableReloadListener.SharedState) { + val TempSet = CommandNodes.toSet() + CommandNodes.clear() + for (Node in TempSet) { + MinecraftServerInstanceBackendField + ?.commands + ?.dispatcher + ?.root + ?.children + ?.remove(Node) } + CommandRegister(MinecraftServerInstanceBackendField?.commands?.dispatcher) + MinecraftServerInstanceBackendField + ?.playerList + ?.players + ?.forEach { SPE -> + MinecraftServerInstance.commands.sendCommands(SPE) } } } ) } \ No newline at end of file diff --git a/src/main/kotlin/xyz/thewhitedog9487/Event/ServerLifecycleListener.kt b/src/main/kotlin/xyz/thewhitedog9487/Event/ServerLifecycleListener.kt new file mode 100644 index 0000000..63f54b8 --- /dev/null +++ b/src/main/kotlin/xyz/thewhitedog9487/Event/ServerLifecycleListener.kt @@ -0,0 +1,18 @@ +package xyz.thewhitedog9487.Event + +import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents +import net.minecraft.server.MinecraftServer +import xyz.thewhitedog9487.ModLogger +import xyz.thewhitedog9487.OldPositions + +var MinecraftServerInstanceBackendField: MinecraftServer? = null +val MinecraftServerInstance get() = + MinecraftServerInstanceBackendField ?: throw NullPointerException("当前服务器实例不可用") + +fun ServerLifecycleListenerRegister() { + ServerLifecycleEvents.SERVER_STARTED.register { MinecraftServerInstance -> + MinecraftServerInstanceBackendField = MinecraftServerInstance + OldPositions.clear() + ModLogger.info("已清空传送历史记录") } +ServerLifecycleEvents.SERVER_STOPPED.register { MinecraftServerInstance -> + MinecraftServerInstanceBackendField = null } } \ No newline at end of file diff --git a/src/main/kotlin/xyz/thewhitedog9487/RandomTeleporter.kt b/src/main/kotlin/xyz/thewhitedog9487/RandomTeleporter.kt index e516714..b1519ef 100644 --- a/src/main/kotlin/xyz/thewhitedog9487/RandomTeleporter.kt +++ b/src/main/kotlin/xyz/thewhitedog9487/RandomTeleporter.kt @@ -1,8 +1,13 @@ package xyz.thewhitedog9487 +import net.fabricmc.api.EnvType +import net.fabricmc.api.Environment import net.fabricmc.api.ModInitializer +import net.fabricmc.loader.api.FabricLoader import org.slf4j.Logger import org.slf4j.LoggerFactory +import xyz.thewhitedog9487.Event.ResourceReloaderListenerRegister +import xyz.thewhitedog9487.Event.ServerLifecycleListenerRegister const val ModID = "randomteleporter" const val FriendlyModID = "RandomTeleporter" @@ -19,8 +24,9 @@ object RandomTeleporter : ModInitializer { // However, some things (like resources) may still be uninitialized. // Proceed with mild caution. - ServerLifecycleListenerRegister() + ServerLifecycleListenerRegister() ResourceReloaderListenerRegister() - CommandRegister() - ModLogger.info("RandomTeleporter已写入命令注册回调,目标命令将会在该注册的时候被注册") } -} \ No newline at end of file +// ↑ 命令注册在这个里面,在资源加载完成之后注册事件监听器 + if (FabricLoader.getInstance().environmentType == EnvType.SERVER){ +// 独立服务器根本不可能重载资源,需要手动注册 + CommandRegister() } } } \ No newline at end of file diff --git a/src/main/kotlin/xyz/thewhitedog9487/ResourceReloaderListener.kt b/src/main/kotlin/xyz/thewhitedog9487/ResourceReloaderListener.kt deleted file mode 100644 index 71c2426..0000000 --- a/src/main/kotlin/xyz/thewhitedog9487/ResourceReloaderListener.kt +++ /dev/null @@ -1,45 +0,0 @@ -package xyz.thewhitedog9487 - -import net.fabricmc.fabric.api.resource.v1.ResourceLoader -import net.fabricmc.fabric.api.resource.v1.reloader.SimpleReloadListener -import net.minecraft.network.chat.Component -import net.minecraft.resources.Identifier -import net.minecraft.server.packs.PackType -import net.minecraft.server.packs.resources.PreparableReloadListener - -var CommandArgumentName_Radius = "Radius(半径)" -var CommandArgumentName_Target = "PlayerID(被传送玩家名)" -var CommandArgumentName_OriginPosition = "OriginPos(随机中心,坐标)" -var CommandArgumentName_OriginEntity = "OriginEntity(随机中心,实体)" -var CommandArgumentName_RegionFromPosition = "RegionFrom(随机范围起始位置,坐标)" -var CommandArgumentName_RegionToPosition = "RegionTo(随机范围结束位置,坐标)" -var CommandArgumentName_RegionFromEntity = "RegionFrom(随机范围起始位置,实体)" -var CommandArgumentName_RegionToEntity = "RegionTo(随机范围结束位置,实体)" - -fun ResourceReloaderListenerRegister() { - ResourceLoader.get(PackType.CLIENT_RESOURCES).registerReloadListener( - Identifier.fromNamespaceAndPath(ModID, - "translate_text_apply"), object: SimpleReloadListener() { - override fun prepare(p0: PreparableReloadListener.SharedState) {} - - override fun apply(p0: Unit, p1: PreparableReloadListener.SharedState) { - CommandArgumentName_Radius = - Component.translatableWithFallback("command.argument.radius", "Radius(半径)").string - CommandArgumentName_Target = - Component.translatableWithFallback("command.argument.target", "PlayerID(被传送玩家名)").string - CommandArgumentName_OriginPosition = - Component.translatableWithFallback("command.argument.origin_pos", "OriginPos(随机中心,坐标)").string - CommandArgumentName_OriginEntity = - Component.translatableWithFallback("command.argument.origin_entity", "OriginEntity(随机中心,实体)").string - CommandArgumentName_RegionFromPosition = Component.translatableWithFallback( - "command.argument.region_from_pos", - "RegionFrom(随机范围起始位置,坐标)").string - CommandArgumentName_RegionToPosition = Component.translatableWithFallback( - "command.argument.region_to_pos", - "RegionTo(随机范围结束位置,坐标)").string - CommandArgumentName_RegionFromEntity = Component.translatableWithFallback( - "command.argument.region_from_entity", - "RegionFrom(随机范围起始位置,实体)").string - CommandArgumentName_RegionToEntity = Component.translatableWithFallback( - "command.argument.region_to_entity", - "RegionTo(随机范围结束位置,实体)").string } } ) } \ No newline at end of file diff --git a/src/main/kotlin/xyz/thewhitedog9487/ServerLifecycleListener.kt b/src/main/kotlin/xyz/thewhitedog9487/ServerLifecycleListener.kt deleted file mode 100644 index c1ae6c3..0000000 --- a/src/main/kotlin/xyz/thewhitedog9487/ServerLifecycleListener.kt +++ /dev/null @@ -1,8 +0,0 @@ -package xyz.thewhitedog9487 - -import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents - -fun ServerLifecycleListenerRegister() { - ServerLifecycleEvents.SERVER_STARTED.register { server -> - OldPositions.clear() - ModLogger.info("已清空传送历史记录") } } \ No newline at end of file