Creative Tab and Commands

This commit is contained in:
skippyall
2025-04-25 15:33:17 +02:00
parent 935b2225c9
commit ec31e090b9
14 changed files with 252 additions and 79 deletions
@@ -0,0 +1,52 @@
package io.github.skippyall.minions.command;
import com.mojang.brigadier.Message;
import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.exceptions.CommandExceptionType;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import com.mojang.brigadier.exceptions.SimpleCommandExceptionType;
import com.mojang.brigadier.suggestion.SuggestionProvider;
import com.mojang.brigadier.suggestion.Suggestions;
import com.mojang.brigadier.suggestion.SuggestionsBuilder;
import io.github.skippyall.minions.minion.MinionData;
import io.github.skippyall.minions.minion.MinionPersistentState;
import io.github.skippyall.minions.minion.MinionProfileUtils;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.text.Text;
import java.util.Optional;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
public class MinionArgument {
public static final SimpleCommandExceptionType MINION_NOT_PRESENT = new SimpleCommandExceptionType(Text.translatable("minions.command.minion.not_present"));
public static final MinionSuggestionProvider SUGGESTION_PROVIDER = new MinionSuggestionProvider();
public static MinionData parse(String argument) throws CommandSyntaxException {
Optional<MinionData> data = Optional.empty();
if(argument.startsWith(MinionProfileUtils.PREFIX)) {
data = MinionPersistentState.INSTANCE.getMinionWithName(argument);
} else {
try {
data = Optional.ofNullable(MinionPersistentState.INSTANCE.getMinionData(UUID.fromString(argument)));
} catch (IllegalArgumentException ignored) {}
}
if(data.isEmpty()) {
throw MINION_NOT_PRESENT.create();
}
return data.get();
}
public static class MinionSuggestionProvider implements SuggestionProvider<ServerCommandSource> {
@Override
public CompletableFuture<Suggestions> getSuggestions(CommandContext<ServerCommandSource> context, SuggestionsBuilder builder) throws CommandSyntaxException {
for (MinionData data : MinionPersistentState.INSTANCE.getMinionDataList()) {
builder.suggest(data.name());
}
return builder.buildFuture();
}
}
}
@@ -0,0 +1,18 @@
package io.github.skippyall.minions.command;
import com.mojang.brigadier.CommandDispatcher;
import net.minecraft.server.command.ServerCommandSource;
import static net.minecraft.server.command.CommandManager.literal;
public class MinionsCommand {
public static void register(CommandDispatcher<ServerCommandSource> dispatcher) {
dispatcher.register(
literal("minions")
.then(SpawnSubcommand.SPAWN)
.then(MobCapDebugSubcommand.MOB_CAP_DEBUG)
);
}
}
@@ -0,0 +1,24 @@
package io.github.skippyall.minions.command;
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import com.mojang.brigadier.context.CommandContext;
import io.github.skippyall.minions.mixinhelper.ChunkLevelManager$DistanceFromNearestPlayerTrackerAccessor;
import io.github.skippyall.minions.mixinhelper.ChunkLevelManagerAccessor;
import io.github.skippyall.minions.mixins.antimobcap.ServerChunkManagerAccessor;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.server.world.ChunkTicketManager;
import net.minecraft.text.Text;
import static net.minecraft.server.command.CommandManager.literal;
public class MobCapDebugSubcommand {
public static final LiteralArgumentBuilder<ServerCommandSource> MOB_CAP_DEBUG = literal("mobcapdebug")
.executes(MobCapDebugSubcommand::mobcapdebugCommand);
public static int mobcapdebugCommand(CommandContext<ServerCommandSource> context) {
ChunkTicketManager ticketManager = ((ServerChunkManagerAccessor)context.getSource().getWorld().getChunkManager()).getTicketManager();
int tickedChunkCount = ((ChunkLevelManager$DistanceFromNearestPlayerTrackerAccessor)((ChunkLevelManagerAccessor)ticketManager).minions$getMinionless()).minions$getTickedChunkCount();
context.getSource().sendFeedback(() -> Text.of(String.valueOf(tickedChunkCount)), false);
return 0;
}
}
@@ -0,0 +1,47 @@
package io.github.skippyall.minions.command;
import com.mojang.brigadier.arguments.BoolArgumentType;
import com.mojang.brigadier.arguments.StringArgumentType;
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import io.github.skippyall.minions.minion.MinionData;
import io.github.skippyall.minions.minion.fakeplayer.MinionFakePlayer;
import net.minecraft.command.argument.PosArgument;
import net.minecraft.command.argument.Vec3ArgumentType;
import net.minecraft.server.command.ServerCommandSource;
import static net.minecraft.server.command.CommandManager.argument;
import static net.minecraft.server.command.CommandManager.literal;
public class SpawnSubcommand {
public static final LiteralArgumentBuilder<ServerCommandSource> SPAWN = literal("spawn")
.then(argument("minion", StringArgumentType.word())
.suggests(MinionArgument.SUGGESTION_PROVIDER)
.then(argument("pos", Vec3ArgumentType.vec3()))
.executes(context ->
spawnCommand(
context.getSource(),
StringArgumentType.getString(context, "minion"),
Vec3ArgumentType.getPosArgument(context, "pos"),
false
)
)
.then(argument("force", BoolArgumentType.bool())
.executes(context ->
spawnCommand(
context.getSource(),
StringArgumentType.getString(context, "minion"),
Vec3ArgumentType.getPosArgument(context, "pos"),
BoolArgumentType.getBool(context, "force")
)
)
)
);
public static int spawnCommand(ServerCommandSource source, String minion, PosArgument pos, boolean force) throws CommandSyntaxException {
MinionData data = MinionArgument.parse(minion);
MinionFakePlayer.spawnMinion(data, source.getWorld(), pos.getPos(source), pos.getRotation(source), force);
return 0;
}
}