commands are back

This commit is contained in:
core 2023-07-22 17:32:17 -04:00
parent eb3f323758
commit 58860049f2
Signed by: core
GPG Key ID: FDBF740DADDCEECF
6 changed files with 157 additions and 2 deletions

View File

@ -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);
}

View File

@ -1,5 +1,6 @@
package dev.coredoes.maple.mixin; package dev.coredoes.maple.mixin;
import dev.coredoes.maple.javautil.ConnectionInterface;
import net.minecraft.network.ClientConnection; import net.minecraft.network.ClientConnection;
import net.minecraft.network.PacketCallbacks; import net.minecraft.network.PacketCallbacks;
import net.minecraft.network.listener.PacketListener; import net.minecraft.network.listener.PacketListener;
@ -9,6 +10,7 @@ import net.minecraft.network.packet.s2c.play.HealthUpdateS2CPacket;
import net.minecraft.util.Identifier; import net.minecraft.util.Identifier;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import org.spongepowered.asm.mixin.Mixin; 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.At;
import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
@ -16,7 +18,7 @@ import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import java.util.Objects; import java.util.Objects;
@Mixin(ClientConnection.class) @Mixin(ClientConnection.class)
public class ClientConnectionMixin { public abstract class ClientConnectionMixin implements ConnectionInterface {
@Inject(method = "handlePacket", at = @At("HEAD"), cancellable = true) @Inject(method = "handlePacket", at = @At("HEAD"), cancellable = true)
private static <T extends PacketListener> void onHandlePacket(Packet<T> packet, PacketListener listener, CallbackInfo info) { private static <T extends PacketListener> void onHandlePacket(Packet<T> packet, PacketListener listener, CallbackInfo info) {
if (packet instanceof HealthUpdateS2CPacket pkt) { 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);
}
} }

View File

@ -1,6 +1,7 @@
package dev.coredoes.maple package dev.coredoes.maple
import dev.coredoes.maple.event.EventManager import dev.coredoes.maple.event.EventManager
import dev.coredoes.maple.manager.CommandManager
import dev.coredoes.maple.manager.GuiManager import dev.coredoes.maple.manager.GuiManager
import dev.coredoes.maple.manager.LOJoinChallengeManager import dev.coredoes.maple.manager.LOJoinChallengeManager
import dev.coredoes.maple.manager.ModuleManager import dev.coredoes.maple.manager.ModuleManager
@ -18,7 +19,8 @@ object Maple : ClientModInitializer {
LOJoinChallengeManager, LOJoinChallengeManager,
ModuleManager, ModuleManager,
GuiManager, GuiManager,
dev.coredoes.maple.manager.EventManager dev.coredoes.maple.manager.EventManager,
CommandManager
) )
for (i in managers.indices) { for (i in managers.indices) {

View File

@ -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<FabricClientCommandSource>) {
dispatcher.register(
LiteralArgumentBuilder.literal<FabricClientCommandSource?>("showhand")
.executes(ShowHandCommand::execute)
)
}
fun execute(ctx: CommandContext<FabricClientCommandSource>): 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
}
}

View File

@ -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<FabricClientCommandSource>) {
dispatcher.register(
LiteralArgumentBuilder.literal<FabricClientCommandSource?>("vclip")
.then(
ClientCommandManager.argument("blocks", IntegerArgumentType.integer())
.executes { ctx ->
execute(ctx, IntegerArgumentType.getInteger(ctx, "blocks"))
}
)
)
}
private fun execute(ctx: CommandContext<FabricClientCommandSource>, 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
}
}

View File

@ -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<FabricClientCommandSource>, registryAccess: CommandRegistryAccess) {
ShowHandCommand.register(dispatcher)
VClipCommand.register(dispatcher)
}
}