From 58860049f2003faf2426cfd2578e2df3e3be0913 Mon Sep 17 00:00:00 2001 From: core Date: Sat, 22 Jul 2023 17:32:17 -0400 Subject: [PATCH] commands are back --- .../maple/javautil/ConnectionInterface.java | 9 +++ .../maple/mixin/ClientConnectionMixin.java | 11 ++- src/main/kotlin/dev/coredoes/maple/Maple.kt | 4 +- .../maple/commands/ShowHandCommand.kt | 40 +++++++++++ .../coredoes/maple/commands/VClipCommand.kt | 69 +++++++++++++++++++ .../coredoes/maple/manager/CommandManager.kt | 26 +++++++ 6 files changed, 157 insertions(+), 2 deletions(-) create mode 100644 src/main/java/dev/coredoes/maple/javautil/ConnectionInterface.java create mode 100644 src/main/kotlin/dev/coredoes/maple/commands/ShowHandCommand.kt create mode 100644 src/main/kotlin/dev/coredoes/maple/commands/VClipCommand.kt create mode 100644 src/main/kotlin/dev/coredoes/maple/manager/CommandManager.kt diff --git a/src/main/java/dev/coredoes/maple/javautil/ConnectionInterface.java b/src/main/java/dev/coredoes/maple/javautil/ConnectionInterface.java new file mode 100644 index 0000000..a5575b2 --- /dev/null +++ b/src/main/java/dev/coredoes/maple/javautil/ConnectionInterface.java @@ -0,0 +1,9 @@ +package dev.coredoes.maple.javautil; + +import net.minecraft.network.PacketCallbacks; +import net.minecraft.network.packet.Packet; +import org.jetbrains.annotations.Nullable; + +public interface ConnectionInterface { + void maple$sendVolatile(Packet packet, @Nullable PacketCallbacks callbacks); +} diff --git a/src/main/java/dev/coredoes/maple/mixin/ClientConnectionMixin.java b/src/main/java/dev/coredoes/maple/mixin/ClientConnectionMixin.java index e0cba69..e363e50 100644 --- a/src/main/java/dev/coredoes/maple/mixin/ClientConnectionMixin.java +++ b/src/main/java/dev/coredoes/maple/mixin/ClientConnectionMixin.java @@ -1,5 +1,6 @@ package dev.coredoes.maple.mixin; +import dev.coredoes.maple.javautil.ConnectionInterface; import net.minecraft.network.ClientConnection; import net.minecraft.network.PacketCallbacks; import net.minecraft.network.listener.PacketListener; @@ -9,6 +10,7 @@ import net.minecraft.network.packet.s2c.play.HealthUpdateS2CPacket; import net.minecraft.util.Identifier; import org.jetbrains.annotations.Nullable; import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Shadow; import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; @@ -16,7 +18,7 @@ import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; import java.util.Objects; @Mixin(ClientConnection.class) -public class ClientConnectionMixin { +public abstract class ClientConnectionMixin implements ConnectionInterface { @Inject(method = "handlePacket", at = @At("HEAD"), cancellable = true) private static void onHandlePacket(Packet packet, PacketListener listener, CallbackInfo info) { if (packet instanceof HealthUpdateS2CPacket pkt) { @@ -34,4 +36,11 @@ public class ClientConnectionMixin { } } } + + @Shadow + protected abstract void sendImmediately(Packet packet, @Nullable PacketCallbacks callbacks); + + public void maple$sendVolatile(Packet packet, @Nullable PacketCallbacks callbacks) { + this.sendImmediately(packet, callbacks); + } } diff --git a/src/main/kotlin/dev/coredoes/maple/Maple.kt b/src/main/kotlin/dev/coredoes/maple/Maple.kt index 8428f21..1618dea 100644 --- a/src/main/kotlin/dev/coredoes/maple/Maple.kt +++ b/src/main/kotlin/dev/coredoes/maple/Maple.kt @@ -1,6 +1,7 @@ package dev.coredoes.maple import dev.coredoes.maple.event.EventManager +import dev.coredoes.maple.manager.CommandManager import dev.coredoes.maple.manager.GuiManager import dev.coredoes.maple.manager.LOJoinChallengeManager import dev.coredoes.maple.manager.ModuleManager @@ -18,7 +19,8 @@ object Maple : ClientModInitializer { LOJoinChallengeManager, ModuleManager, GuiManager, - dev.coredoes.maple.manager.EventManager + dev.coredoes.maple.manager.EventManager, + CommandManager ) for (i in managers.indices) { diff --git a/src/main/kotlin/dev/coredoes/maple/commands/ShowHandCommand.kt b/src/main/kotlin/dev/coredoes/maple/commands/ShowHandCommand.kt new file mode 100644 index 0000000..98b9500 --- /dev/null +++ b/src/main/kotlin/dev/coredoes/maple/commands/ShowHandCommand.kt @@ -0,0 +1,40 @@ +package dev.coredoes.maple.commands + +import com.mojang.brigadier.CommandDispatcher +import com.mojang.brigadier.builder.LiteralArgumentBuilder +import com.mojang.brigadier.context.CommandContext +import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource +import net.minecraft.client.MinecraftClient +import net.minecraft.entity.Entity +import net.minecraft.text.Text +import net.minecraft.util.hit.EntityHitResult +import net.minecraft.util.hit.HitResult + + +object ShowHandCommand { + fun register(dispatcher: CommandDispatcher) { + dispatcher.register( + LiteralArgumentBuilder.literal("showhand") + .executes(ShowHandCommand::execute) + ) + } + + fun execute(ctx: CommandContext): Int { + val target: HitResult? = MinecraftClient.getInstance().crosshairTarget + + if (target !is EntityHitResult) { + ctx.source.sendError(Text.of("You aren't looking at an entity.")) + return -1 + } + + val entity: Entity = target.entity + + for (item in entity.handItems) { + ctx.source.sendFeedback(Text.of("Item: $item")) + ctx.source.sendFeedback(Text.of("NBT: " + item.nbt.toString())) + } + + return com.mojang.brigadier.Command.SINGLE_SUCCESS + } + +} \ No newline at end of file diff --git a/src/main/kotlin/dev/coredoes/maple/commands/VClipCommand.kt b/src/main/kotlin/dev/coredoes/maple/commands/VClipCommand.kt new file mode 100644 index 0000000..c6239e6 --- /dev/null +++ b/src/main/kotlin/dev/coredoes/maple/commands/VClipCommand.kt @@ -0,0 +1,69 @@ +package dev.coredoes.maple.commands + +import com.mojang.brigadier.Command +import com.mojang.brigadier.CommandDispatcher +import com.mojang.brigadier.arguments.IntegerArgumentType +import com.mojang.brigadier.builder.LiteralArgumentBuilder +import com.mojang.brigadier.context.CommandContext +import dev.coredoes.maple.Maple +import dev.coredoes.maple.javautil.ConnectionInterface +import net.fabricmc.fabric.api.client.command.v2.ClientCommandManager +import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource +import net.minecraft.client.MinecraftClient +import net.minecraft.network.ClientConnection +import net.minecraft.network.packet.c2s.play.PlayerMoveC2SPacket.PositionAndOnGround +import kotlin.math.abs + + +object VClipCommand { + fun register(dispatcher: CommandDispatcher) { + dispatcher.register( + LiteralArgumentBuilder.literal("vclip") + .then( + ClientCommandManager.argument("blocks", IntegerArgumentType.integer()) + .executes { ctx -> + execute(ctx, IntegerArgumentType.getInteger(ctx, "blocks")) + } + ) + ) + } + + private fun execute(ctx: CommandContext, blocks: Int): Int { + Maple.logger.info("vclipping {}", blocks) + assert(MinecraftClient.getInstance().player != null) + val needed = abs(blocks / 10 + 5) + + Maple.logger.info("need {}", needed) + + val conn: ClientConnection = MinecraftClient.getInstance().networkHandler!!.connection + val c: ConnectionInterface = conn as ConnectionInterface + + for (i in 0 until needed) { + c.`maple$sendVolatile`( + PositionAndOnGround( + MinecraftClient.getInstance().player!!.x, + MinecraftClient.getInstance().player!!.y, + MinecraftClient.getInstance().player!!.z, + MinecraftClient.getInstance().player!!.isOnGround + ), null + ) + } + + val x: Double = MinecraftClient.getInstance().player!!.x + val y: Double = MinecraftClient.getInstance().player!!.y + blocks + val z: Double = MinecraftClient.getInstance().player!!.z + + c.`maple$sendVolatile`( + PositionAndOnGround( + x, + y, + z, + MinecraftClient.getInstance().player!!.isOnGround + ), null + ) + + MinecraftClient.getInstance().player!!.setPos(x, y, z) + + return Command.SINGLE_SUCCESS + } +} \ No newline at end of file diff --git a/src/main/kotlin/dev/coredoes/maple/manager/CommandManager.kt b/src/main/kotlin/dev/coredoes/maple/manager/CommandManager.kt new file mode 100644 index 0000000..17d94f6 --- /dev/null +++ b/src/main/kotlin/dev/coredoes/maple/manager/CommandManager.kt @@ -0,0 +1,26 @@ +package dev.coredoes.maple.manager + +import com.mojang.brigadier.CommandDispatcher +import dev.coredoes.maple.Manager +import dev.coredoes.maple.commands.ShowHandCommand +import dev.coredoes.maple.commands.VClipCommand +import net.fabricmc.fabric.api.client.command.v2.ClientCommandRegistrationCallback +import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource +import net.minecraft.command.CommandRegistryAccess + +object CommandManager : Manager { + + + override fun init() { + ClientCommandRegistrationCallback.EVENT.register(CommandManager::registerCommandsInternal) + } + + override fun stringName(): String { + return "CommandManager" + } + + private fun registerCommandsInternal(dispatcher: CommandDispatcher, registryAccess: CommandRegistryAccess) { + ShowHandCommand.register(dispatcher) + VClipCommand.register(dispatcher) + } +} \ No newline at end of file