fix: projection command

This commit is contained in:
xtex 2023-07-04 16:21:42 +08:00
parent 929474ff19
commit 906263cb2b
Signed by: xtex
GPG Key ID: B918086ED8045B91

View File

@ -1,6 +1,5 @@
package quaedam.projection package quaedam.projection
import com.mojang.brigadier.arguments.StringArgumentType.string
import com.mojang.brigadier.builder.LiteralArgumentBuilder.literal import com.mojang.brigadier.builder.LiteralArgumentBuilder.literal
import com.mojang.brigadier.builder.RequiredArgumentBuilder.argument import com.mojang.brigadier.builder.RequiredArgumentBuilder.argument
import com.mojang.brigadier.context.CommandContext import com.mojang.brigadier.context.CommandContext
@ -9,9 +8,9 @@ import net.minecraft.commands.CommandSourceStack
import net.minecraft.commands.arguments.ResourceArgument.resource import net.minecraft.commands.arguments.ResourceArgument.resource
import net.minecraft.core.BlockPos import net.minecraft.core.BlockPos
import net.minecraft.core.Holder import net.minecraft.core.Holder
import net.minecraft.network.chat.Component import net.minecraft.nbt.ListTag
import net.minecraft.nbt.NbtUtils
import quaedam.projector.Projector import quaedam.projector.Projector
import java.util.*
object ProjectionCommand { object ProjectionCommand {
@ -22,10 +21,6 @@ object ProjectionCommand {
.then( .then(
literal<CommandSourceStack>("dump") literal<CommandSourceStack>("dump")
.requires { it.hasPermission(2) } .requires { it.hasPermission(2) }
.then(
argument<CommandSourceStack, String>("path", string())
.executes(::dumpPath)
)
.executes(::dump) .executes(::dump)
) )
.then( .then(
@ -36,10 +31,6 @@ object ProjectionCommand {
"type", "type",
resource(ctx, ProjectionEffectType.registryKey) resource(ctx, ProjectionEffectType.registryKey)
) )
.then(
argument<CommandSourceStack, String>("path", string())
.executes(::getPath)
)
.executes(::get) .executes(::get)
) )
) )
@ -47,7 +38,7 @@ object ProjectionCommand {
} }
} }
private fun dump(ctx: CommandContext<CommandSourceStack>, path: String = ""): Int { private fun dump(ctx: CommandContext<CommandSourceStack>): Int {
val pos = BlockPos( val pos = BlockPos(
ctx.source.position.x.toInt(), ctx.source.position.x.toInt(),
ctx.source.position.y.toInt(), ctx.source.position.y.toInt(),
@ -55,14 +46,13 @@ object ProjectionCommand {
) )
val data = Projector.findNearbyProjectors(ctx.source.level, pos) val data = Projector.findNearbyProjectors(ctx.source.level, pos)
.map { ctx.source.level.getBlockEntity(it)!!.saveWithFullMetadata() } .map { ctx.source.level.getBlockEntity(it)!!.saveWithFullMetadata() }
ctx.source.sendSystemMessage(Component.nbt(path, true, Optional.empty()) { data.stream() }) val tag = ListTag()
tag.addAll(data)
ctx.source.sendSystemMessage(NbtUtils.toPrettyComponent(tag))
return 0 return 0
} }
private fun dumpPath(ctx: CommandContext<CommandSourceStack>) = private fun get(ctx: CommandContext<CommandSourceStack>): Int {
dump(ctx, path = ctx.getArgument("path", String::class.java))
private fun get(ctx: CommandContext<CommandSourceStack>, path: String = ""): Int {
val pos = BlockPos( val pos = BlockPos(
ctx.source.position.x.toInt(), ctx.source.position.x.toInt(),
ctx.source.position.y.toInt(), ctx.source.position.y.toInt(),
@ -71,11 +61,10 @@ object ProjectionCommand {
val type = ctx.getArgument("type", Holder.Reference::class.java).value() as ProjectionEffectType<*> val type = ctx.getArgument("type", Holder.Reference::class.java).value() as ProjectionEffectType<*>
val data = Projector.findNearbyProjections(ctx.source.level, pos, type) val data = Projector.findNearbyProjections(ctx.source.level, pos, type)
.map { it.toNbt() } .map { it.toNbt() }
ctx.source.sendSystemMessage(Component.nbt(path, true, Optional.empty()) { data.stream() }) val tag = ListTag()
tag.addAll(data)
ctx.source.sendSystemMessage(NbtUtils.toPrettyComponent(tag))
return 0 return 0
} }
private fun getPath(ctx: CommandContext<CommandSourceStack>) =
dump(ctx, path = ctx.getArgument("path", String::class.java))
} }