diff --git a/src/main/java/io/github/misode/packtest/PackTestPlayerName.java b/src/main/java/io/github/misode/packtest/PackTestPlayerName.java new file mode 100644 index 0000000..968758a --- /dev/null +++ b/src/main/java/io/github/misode/packtest/PackTestPlayerName.java @@ -0,0 +1,5 @@ +package io.github.misode.packtest; + +public interface PackTestPlayerName { + String packtest$getPlayerName(); +} 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 bc4d8e6..6567f7a 100644 --- a/src/main/java/io/github/misode/packtest/commands/DummyCommand.java +++ b/src/main/java/io/github/misode/packtest/commands/DummyCommand.java @@ -3,13 +3,13 @@ import com.mojang.brigadier.CommandDispatcher; import com.mojang.brigadier.arguments.BoolArgumentType; import com.mojang.brigadier.arguments.IntegerArgumentType; -import com.mojang.brigadier.arguments.StringArgumentType; import com.mojang.brigadier.builder.RequiredArgumentBuilder; import com.mojang.brigadier.context.CommandContext; import com.mojang.brigadier.exceptions.CommandSyntaxException; import com.mojang.brigadier.exceptions.Dynamic2CommandExceptionType; import com.mojang.brigadier.exceptions.DynamicCommandExceptionType; import com.mojang.brigadier.exceptions.SimpleCommandExceptionType; +import io.github.misode.packtest.PackTestPlayerName; import io.github.misode.packtest.dummy.Dummy; import net.minecraft.commands.CommandSourceStack; import net.minecraft.commands.arguments.EntityArgument; @@ -39,6 +39,9 @@ public class DummyCommand { private static final SimpleCommandExceptionType ERROR_DUMMY_NOT_FOUND = new SimpleCommandExceptionType( Component.literal("No dummy was found") ); + private static final SimpleCommandExceptionType ERROR_NO_NAME = new SimpleCommandExceptionType( + Component.literal("Cannot spawn dummy without a name") + ); private static final DynamicCommandExceptionType ERROR_DUMMY_EXISTS = createError("is already logged on"); private static final DynamicCommandExceptionType ERROR_PLAYER_EXISTS = createError("is already a player"); private static final DynamicCommandExceptionType ERROR_NOT_ON_GROUND = createError("is not on the ground"); @@ -62,7 +65,7 @@ private static DynamicCommandExceptionType createError(String message) { public static void register(CommandDispatcher dispatcher) { dispatcher.register(literal("dummy").then(dummyName() .then(literal("spawn") - .executes(DummyCommand::spawnFixedName)) + .executes(DummyCommand::spawn)) .then(literal("leave") .executes(DummyCommand::leave)) .then(literal("respawn") @@ -134,8 +137,12 @@ private static Dummy getDummy(CommandContext ctx) throws Com throw ERROR_DUMMY_NOT_FOUND.create(); } - private static int spawnFixedName(CommandContext ctx) throws CommandSyntaxException { - String name = StringArgumentType.getString(ctx, "name"); + private static int spawn(CommandContext ctx) throws CommandSyntaxException { + EntitySelector selector = ctx.getArgument("dummy", EntitySelector.class); + String name = ((PackTestPlayerName)selector).packtest$getPlayerName(); + if (name == null) { + throw ERROR_NO_NAME.create(); + } CommandSourceStack source = ctx.getSource(); MinecraftServer server = source.getServer(); ServerPlayer player = server.getPlayerList().getPlayerByName(name); diff --git a/src/main/java/io/github/misode/packtest/mixin/EntitySelectorMixin.java b/src/main/java/io/github/misode/packtest/mixin/EntitySelectorMixin.java index 70a6b51..8a0824e 100644 --- a/src/main/java/io/github/misode/packtest/mixin/EntitySelectorMixin.java +++ b/src/main/java/io/github/misode/packtest/mixin/EntitySelectorMixin.java @@ -1,17 +1,24 @@ package io.github.misode.packtest.mixin; import io.github.misode.packtest.PackTestArgumentSource; +import io.github.misode.packtest.PackTestPlayerName; import net.minecraft.commands.arguments.selector.EntitySelector; +import org.spongepowered.asm.mixin.Final; import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Shadow; import org.spongepowered.asm.mixin.Unique; /** * Adds a new field for storing the original text of the entity selector */ @Mixin(EntitySelector.class) -public class EntitySelectorMixin implements PackTestArgumentSource { +public class EntitySelectorMixin implements PackTestArgumentSource, PackTestPlayerName { @Unique - public String packtestSource; + private String packtestSource; + + @Shadow + @Final + private String playerName; @Override public String packtest$getSource() { @@ -22,4 +29,8 @@ public class EntitySelectorMixin implements PackTestArgumentSource { public void packtest$setSource(String source) { this.packtestSource = source; } + + public String packtest$getPlayerName() { + return this.playerName; + } }