Skip to content

Commit

Permalink
Add dummy mine
Browse files Browse the repository at this point in the history
  • Loading branch information
misode committed Dec 23, 2023
1 parent 7d02bff commit 324b16b
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -98,6 +100,9 @@ public static void register(CommandDispatcher<CommandSourceStack> dispatcher) {
.then(literal("attack")
.then(argument("entity", EntityArgument.entity())
.executes(DummyCommand::attackEntity)))
.then(literal("mine")
.then(argument("pos", BlockPosArgument.blockPos())
.executes(DummyCommand::mineBlock)))
));
}

Expand Down Expand Up @@ -141,7 +146,7 @@ private static int spawnFixedName(CommandContext<CommandSourceStack> ctx) throws
throw ERROR_PLAYER_EXISTS.create(name);
}
ResourceKey<Level> dimension = source.getLevel().dimension();
Dummy.createRandom(name, server, dimension, source.getPosition());
Dummy.create(name, server, dimension, source.getPosition());
return 1;
}

Expand Down Expand Up @@ -264,4 +269,13 @@ private static int attackEntity(CommandContext<CommandSourceStack> ctx) throws C
dummy.swing(InteractionHand.MAIN_HAND);
return 1;
}

private static int mineBlock(CommandContext<CommandSourceStack> 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;
}
}
4 changes: 2 additions & 2 deletions src/main/java/io/github/misode/packtest/dummy/Dummy.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@ public static Dummy createRandom(MinecraftServer server, ResourceKey<Level> 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<Level> dimensionId, Vec3 pos) {
public static Dummy create(String username, MinecraftServer server, ResourceKey<Level> dimensionId, Vec3 pos) {
ServerLevel level = server.getLevel(dimensionId);
GameProfileCache.setUsesAuthentication(false);
GameProfile profile;
Expand Down

0 comments on commit 324b16b

Please sign in to comment.