From ea50514b8f05600eca1f9c18ec28cc6cc2b3f27b Mon Sep 17 00:00:00 2001 From: xtex Date: Tue, 4 Jul 2023 15:36:03 +0800 Subject: [PATCH] feat: projection test command --- common/src/main/kotlin/quaedam/Quaedam.kt | 2 + .../quaedam/projection/ProjectionCommand.kt | 81 +++++++++++++++++++ 2 files changed, 83 insertions(+) create mode 100644 common/src/main/kotlin/quaedam/projection/ProjectionCommand.kt diff --git a/common/src/main/kotlin/quaedam/Quaedam.kt b/common/src/main/kotlin/quaedam/Quaedam.kt index ea6839d..4b5dd9d 100644 --- a/common/src/main/kotlin/quaedam/Quaedam.kt +++ b/common/src/main/kotlin/quaedam/Quaedam.kt @@ -9,6 +9,7 @@ import net.minecraft.world.item.CreativeModeTab import net.minecraft.world.item.ItemStack import net.minecraft.world.item.Items import org.slf4j.LoggerFactory +import quaedam.projection.ProjectionCommand import quaedam.projection.ProjectionEffectType import quaedam.projection.SkylightProjection import quaedam.projection.swarm.SwarmProjection @@ -41,6 +42,7 @@ object Quaedam { ProjectionEffectType SkylightProjection SwarmProjection + ProjectionCommand creativeModeTabs.register() items.register() diff --git a/common/src/main/kotlin/quaedam/projection/ProjectionCommand.kt b/common/src/main/kotlin/quaedam/projection/ProjectionCommand.kt new file mode 100644 index 0000000..fc2642d --- /dev/null +++ b/common/src/main/kotlin/quaedam/projection/ProjectionCommand.kt @@ -0,0 +1,81 @@ +package quaedam.projection + +import com.mojang.brigadier.arguments.StringArgumentType.string +import com.mojang.brigadier.builder.LiteralArgumentBuilder.literal +import com.mojang.brigadier.builder.RequiredArgumentBuilder.argument +import com.mojang.brigadier.context.CommandContext +import dev.architectury.event.events.common.CommandRegistrationEvent +import net.minecraft.commands.CommandSourceStack +import net.minecraft.commands.arguments.ResourceArgument.resource +import net.minecraft.core.BlockPos +import net.minecraft.core.Holder +import net.minecraft.network.chat.Component +import quaedam.projector.Projector +import java.util.* + +object ProjectionCommand { + + init { + CommandRegistrationEvent.EVENT.register { dispatcher, ctx, _ -> + dispatcher.register( + literal("quaedam_projection") + .then( + literal("dump") + .requires { it.hasPermission(2) } + .then( + argument("path", string()) + .executes(::dumpPath) + ) + .executes(::dump) + ) + .then( + literal("get") + .requires { it.hasPermission(2) } + .then( + argument>>( + "type", + resource(ctx, ProjectionEffectType.registryKey) + ) + .then( + argument("path", string()) + .executes(::getPath) + ) + .executes(::get) + ) + ) + ) + } + } + + private fun dump(ctx: CommandContext, path: String = ""): Int { + val pos = BlockPos( + ctx.source.position.x.toInt(), + ctx.source.position.y.toInt(), + ctx.source.position.z.toInt() + ) + val data = Projector.findNearbyProjectors(ctx.source.level, pos) + .map { ctx.source.level.getBlockEntity(it)!!.saveWithFullMetadata() } + ctx.source.sendSystemMessage(Component.nbt(path, false, Optional.empty()) { data.stream() }) + return 0 + } + + private fun dumpPath(ctx: CommandContext) = + dump(ctx, path = ctx.getArgument("path", String::class.java)) + + private fun get(ctx: CommandContext, path: String = ""): Int { + val pos = BlockPos( + ctx.source.position.x.toInt(), + ctx.source.position.y.toInt(), + ctx.source.position.z.toInt() + ) + val type = ctx.getArgument("type", Holder.Reference::class.java).value() as ProjectionEffectType<*> + val data = Projector.findNearbyProjections(ctx.source.level, pos, type) + .map { it.toNbt() } + ctx.source.sendSystemMessage(Component.nbt(path, false, Optional.empty()) { data.stream() }) + return 0 + } + + private fun getPath(ctx: CommandContext) = + dump(ctx, path = ctx.getArgument("path", String::class.java)) + +} \ No newline at end of file