From 324b16b1f5a0626459a5ad2cd4caffa7b475c94a Mon Sep 17 00:00:00 2001 From: Misode Date: Sat, 23 Dec 2023 22:33:30 +0100 Subject: [PATCH] Add dummy mine --- .../misode/packtest/commands/DummyCommand.java | 16 +++++++++++++++- .../io/github/misode/packtest/dummy/Dummy.java | 4 ++-- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/src/main/java/io/github/misode/packtest/commands/DummyCommand.java b/src/main/java/io/github/misode/packtest/commands/DummyCommand.java index 18d1308..bc4d8e6 100644 --- a/src/main/java/io/github/misode/packtest/commands/DummyCommand.java +++ b/src/main/java/io/github/misode/packtest/commands/DummyCommand.java @@ -13,6 +13,7 @@ import io.github.misode.packtest.dummy.Dummy; import net.minecraft.commands.CommandSourceStack; import net.minecraft.commands.arguments.EntityArgument; +import net.minecraft.commands.arguments.coordinates.BlockPosArgument; import net.minecraft.commands.arguments.coordinates.Vec3Argument; import net.minecraft.commands.arguments.selector.EntitySelector; import net.minecraft.core.BlockPos; @@ -52,6 +53,7 @@ public class DummyCommand { private static final DynamicCommandExceptionType ERROR_USE_ITEM = createError("cannot use that item"); private static final DynamicCommandExceptionType ERROR_INTERACT_BLOCK = createError("cannot interact with that block"); private static final DynamicCommandExceptionType ERROR_INTERACT_ENTITY = createError("cannot interact with that entity"); + private static final DynamicCommandExceptionType ERROR_MINE_BLOCK = createError("failed to mine block"); private static DynamicCommandExceptionType createError(String message) { return new DynamicCommandExceptionType(name -> Component.literal("Dummy " + name + " " + message)); @@ -98,6 +100,9 @@ public static void register(CommandDispatcher dispatcher) { .then(literal("attack") .then(argument("entity", EntityArgument.entity()) .executes(DummyCommand::attackEntity))) + .then(literal("mine") + .then(argument("pos", BlockPosArgument.blockPos()) + .executes(DummyCommand::mineBlock))) )); } @@ -141,7 +146,7 @@ private static int spawnFixedName(CommandContext ctx) throws throw ERROR_PLAYER_EXISTS.create(name); } ResourceKey dimension = source.getLevel().dimension(); - Dummy.createRandom(name, server, dimension, source.getPosition()); + Dummy.create(name, server, dimension, source.getPosition()); return 1; } @@ -264,4 +269,13 @@ private static int attackEntity(CommandContext ctx) throws C dummy.swing(InteractionHand.MAIN_HAND); return 1; } + + private static int mineBlock(CommandContext ctx) throws CommandSyntaxException { + Dummy dummy = getDummy(ctx); + BlockPos pos = BlockPosArgument.getBlockPos(ctx, "pos"); + if (!dummy.gameMode.destroyBlock(pos)) { + throw ERROR_MINE_BLOCK.create(dummy.getUsername()); + } + return 1; + } } diff --git a/src/main/java/io/github/misode/packtest/dummy/Dummy.java b/src/main/java/io/github/misode/packtest/dummy/Dummy.java index 72994ca..6771846 100644 --- a/src/main/java/io/github/misode/packtest/dummy/Dummy.java +++ b/src/main/java/io/github/misode/packtest/dummy/Dummy.java @@ -38,13 +38,13 @@ public static Dummy createRandom(MinecraftServer server, ResourceKey dime while (tries++ < 10) { String playerName = "Dummy" + random.nextInt(100, 1000); if (server.getPlayerList().getPlayerByName(playerName) == null) { - return createRandom(playerName, server, dimensionId, pos); + return create(playerName, server, dimensionId, pos); } } throw new IllegalStateException("Failed to spawn dummy with a random name"); } - public static Dummy createRandom(String username, MinecraftServer server, ResourceKey dimensionId, Vec3 pos) { + public static Dummy create(String username, MinecraftServer server, ResourceKey dimensionId, Vec3 pos) { ServerLevel level = server.getLevel(dimensionId); GameProfileCache.setUsesAuthentication(false); GameProfile profile;