Skip to content

Commit

Permalink
Fix dummy spawn
Browse files Browse the repository at this point in the history
  • Loading branch information
misode committed Dec 24, 2023
1 parent e13faec commit a2fc111
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package io.github.misode.packtest;

public interface PackTestPlayerName {
String packtest$getPlayerName();
}
15 changes: 11 additions & 4 deletions src/main/java/io/github/misode/packtest/commands/DummyCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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");
Expand All @@ -62,7 +65,7 @@ private static DynamicCommandExceptionType createError(String message) {
public static void register(CommandDispatcher<CommandSourceStack> 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")
Expand Down Expand Up @@ -134,8 +137,12 @@ private static Dummy getDummy(CommandContext<CommandSourceStack> ctx) throws Com
throw ERROR_DUMMY_NOT_FOUND.create();
}

private static int spawnFixedName(CommandContext<CommandSourceStack> ctx) throws CommandSyntaxException {
String name = StringArgumentType.getString(ctx, "name");
private static int spawn(CommandContext<CommandSourceStack> 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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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() {
Expand All @@ -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;
}
}

0 comments on commit a2fc111

Please sign in to comment.