Move the admin and debug commands to a /unicopia command

This commit is contained in:
Sollace 2023-09-04 23:40:41 +01:00
parent 30f5c73609
commit c63bebdce1
No known key found for this signature in database
GPG key ID: E52FACE7B5C773DB
11 changed files with 115 additions and 145 deletions

View file

@ -8,9 +8,9 @@ import com.minelittlepony.unicopia.ability.magic.spell.PlaceableSpell;
import com.minelittlepony.unicopia.ability.magic.spell.effect.CustomisedSpellType;
import com.minelittlepony.unicopia.ability.magic.spell.effect.SpellType;
import com.minelittlepony.unicopia.ability.magic.spell.trait.SpellTraits;
import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.arguments.FloatArgumentType;
import com.mojang.brigadier.builder.ArgumentBuilder;
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
@ -26,9 +26,8 @@ import net.minecraft.util.math.Vec2f;
import net.minecraft.util.math.Vec3d;
public class CastCommand {
public static void register(CommandDispatcher<ServerCommandSource> dispatcher, CommandRegistryAccess registries) {
dispatcher.register(
CommandManager.literal("cast").requires(s -> s.hasPermissionLevel(2))
public static LiteralArgumentBuilder<ServerCommandSource> create(CommandRegistryAccess registries) {
return CommandManager.literal("cast").requires(s -> s.hasPermissionLevel(2))
.then(
buildBranches(
CommandManager.argument("spell", RegistryKeyArgumentType.registryKey(SpellType.REGISTRY_KEY)),
@ -41,8 +40,7 @@ public class CastCommand {
CommandManager.argument("traits", TraitsArgumentType.traits()),
CastCommand::getTraits
))
)
);
);
}
private static ArgumentBuilder<ServerCommandSource, ?> buildBranches(ArgumentBuilder<ServerCommandSource, ?> builder,

View file

@ -6,35 +6,21 @@ import com.minelittlepony.unicopia.Unicopia;
import com.minelittlepony.unicopia.ability.magic.spell.trait.Trait;
import com.minelittlepony.unicopia.client.render.PlayerPoser.Animation;
import com.minelittlepony.unicopia.command.ManaCommand.ManaType;
import net.fabricmc.fabric.api.command.v2.ArgumentTypeRegistry;
import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.command.argument.serialize.ConstantArgumentSerializer;
import net.minecraft.server.MinecraftServer;
public class Commands {
@SuppressWarnings("deprecation")
public static void bootstrap() {
ArgumentTypeRegistry.registerArgumentType(Unicopia.id("animation"), Animation.ArgumentType.class, ConstantArgumentSerializer.of(Animation::argument));
ArgumentTypeRegistry.registerArgumentType(Unicopia.id("animation_recipient"), Animation.Recipient.ArgumentType.class, ConstantArgumentSerializer.of(Animation.Recipient::argument));
ArgumentTypeRegistry.registerArgumentType(Unicopia.id("mana_type"), ManaType.ArgumentType.class, ConstantArgumentSerializer.of(ManaType::argument));
ArgumentTypeRegistry.registerArgumentType(Unicopia.id("trait_type"), Trait.ArgumentType.class, ConstantArgumentSerializer.of(Trait::argument));
ArgumentTypeRegistry.registerArgumentType(Unicopia.id("spell_traits"), TraitsArgumentType.class, ConstantArgumentSerializer.of(TraitsArgumentType::traits));
CommandRegistrationCallback.EVENT.register((dispatcher, registries, environment) -> {
RacelistCommand.register(dispatcher);
EmoteCommand.register(dispatcher);
SpeciesCommand.register(dispatcher, environment);
WorldTribeCommand.register(dispatcher);
GravityCommand.register(dispatcher);
DisguiseCommand.register(dispatcher, registries);
CastCommand.register(dispatcher, registries);
TraitCommand.register(dispatcher);
ManaCommand.register(dispatcher);
if (FabricLoader.getInstance().getGameInstance() instanceof MinecraftServer server) {
server.setFlightEnabled(true);
}
dispatcher.register(EmoteCommand.create());
dispatcher.register(SpeciesCommand.create(environment));
dispatcher.register(UnicopiaCommand.create(registries, environment));
});
}
}

View file

@ -9,9 +9,9 @@ import com.minelittlepony.unicopia.ability.magic.spell.CastingMethod;
import com.minelittlepony.unicopia.ability.magic.spell.effect.SpellType;
import com.minelittlepony.unicopia.entity.player.Pony;
import com.mojang.authlib.GameProfile;
import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.arguments.StringArgumentType;
import com.mojang.brigadier.builder.ArgumentBuilder;
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import com.mojang.brigadier.exceptions.SimpleCommandExceptionType;
@ -34,10 +34,8 @@ import net.minecraft.world.GameRules;
public class DisguiseCommand {
private static final SimpleCommandExceptionType FAILED_EXCEPTION = new SimpleCommandExceptionType(Text.translatable("commands.disguise.notfound"));
public static void register(CommandDispatcher<ServerCommandSource> dispatcher, CommandRegistryAccess registries) {
dispatcher.register(CommandManager
.literal("disguise")
.requires(s -> s.hasPermissionLevel(2))
public static LiteralArgumentBuilder<ServerCommandSource> create(CommandRegistryAccess registries) {
return CommandManager.literal("disguise").requires(s -> s.hasPermissionLevel(2))
.executes(context -> reveal(context.getSource(), context.getSource().getPlayer()))
.then(
CommandManager.argument("target", EntityArgumentType.players())
@ -45,8 +43,7 @@ public class DisguiseCommand {
.then(buildPlayerDisguise(context -> EntityArgumentType.getPlayer(context, "target")))
)
.then(buildEntityDisguise(context -> context.getSource().getPlayer(), registries))
.then(buildPlayerDisguise(context -> context.getSource().getPlayer()))
);
.then(buildPlayerDisguise(context -> context.getSource().getPlayer()));
}
private static ArgumentBuilder<ServerCommandSource, ?> buildEntityDisguise(Arg<ServerPlayerEntity> targetOp, CommandRegistryAccess registries) {

View file

@ -2,16 +2,16 @@ package com.minelittlepony.unicopia.command;
import com.minelittlepony.unicopia.client.render.PlayerPoser.Animation;
import com.minelittlepony.unicopia.entity.player.Pony;
import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.arguments.IntegerArgumentType;
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import net.minecraft.server.command.CommandManager;
import net.minecraft.server.command.ServerCommandSource;
public class EmoteCommand {
static void register(CommandDispatcher<ServerCommandSource> dispatcher) {
dispatcher.register(CommandManager
static LiteralArgumentBuilder<ServerCommandSource> create() {
return CommandManager
.literal("emote")
.then(CommandManager.argument("animation", Animation.argument()).executes(source -> apply(
source.getSource(),
@ -31,7 +31,7 @@ public class EmoteCommand {
IntegerArgumentType.getInteger(source, "duration")
)
))
)));
));
}
static int apply(ServerCommandSource source, Animation animation, Animation.Recipient recipient) throws CommandSyntaxException {

View file

@ -5,7 +5,6 @@ import java.util.stream.Stream;
import com.google.common.collect.Streams;
import com.minelittlepony.unicopia.entity.player.Pony;
import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.arguments.FloatArgumentType;
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
@ -19,24 +18,19 @@ import net.minecraft.world.GameRules;
class GravityCommand {
static void register(CommandDispatcher<ServerCommandSource> dispatcher) {
LiteralArgumentBuilder<ServerCommandSource> builder = CommandManager
.literal("gravity")
.requires(s -> s.hasPermissionLevel(2));
builder.then(CommandManager.literal("get")
.executes(context -> get(context.getSource(), context.getSource().getPlayer(), true))
.then(CommandManager.argument("target", EntityArgumentType.player())
.executes(context -> get(context.getSource(), EntityArgumentType.getPlayer(context, "target"), false))
));
builder.then(CommandManager.literal("set")
.then(CommandManager.argument("gravity", FloatArgumentType.floatArg(-99, 99))
.executes(context -> set(context.getSource(), context.getSource().getPlayer(), FloatArgumentType.getFloat(context, "gravity"), true))
.then(CommandManager.argument("target", EntityArgumentType.player())
.executes(context -> set(context.getSource(), EntityArgumentType.getPlayer(context, "target"), FloatArgumentType.getFloat(context, "gravity"), false))
)));
dispatcher.register(builder);
static LiteralArgumentBuilder<ServerCommandSource> create() {
return CommandManager.literal("gravity").requires(s -> s.hasPermissionLevel(2))
.then(CommandManager.literal("get")
.executes(context -> get(context.getSource(), context.getSource().getPlayer(), true))
.then(CommandManager.argument("target", EntityArgumentType.player())
.executes(context -> get(context.getSource(), EntityArgumentType.getPlayer(context, "target"), false))
))
.then(CommandManager.literal("set")
.then(CommandManager.argument("gravity", FloatArgumentType.floatArg(-99, 99))
.executes(context -> set(context.getSource(), context.getSource().getPlayer(), FloatArgumentType.getFloat(context, "gravity"), true))
.then(CommandManager.argument("target", EntityArgumentType.player())
.executes(context -> set(context.getSource(), EntityArgumentType.getPlayer(context, "target"), FloatArgumentType.getFloat(context, "gravity"), false))
)));
}
static int get(ServerCommandSource source, PlayerEntity player, boolean isSelf) throws CommandSyntaxException {

View file

@ -4,8 +4,8 @@ import java.util.function.Function;
import com.minelittlepony.unicopia.entity.player.MagicReserves;
import com.minelittlepony.unicopia.entity.player.Pony;
import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.arguments.FloatArgumentType;
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import net.minecraft.command.argument.EnumArgumentType;
import net.minecraft.server.command.CommandManager;
@ -14,34 +14,30 @@ import net.minecraft.text.Text;
import net.minecraft.util.StringIdentifiable;
public class ManaCommand {
static void register(CommandDispatcher<ServerCommandSource> dispatcher) {
dispatcher.register(CommandManager
.literal("mana").requires(s -> s.hasPermissionLevel(2))
.then(CommandManager.argument("type", ManaType.argument()).executes(source -> {
var type = source.getArgument("type", ManaType.class);
var pony = Pony.of(source.getSource().getPlayer());
var bar = type.getBar(pony.getMagicalReserves());
static LiteralArgumentBuilder<ServerCommandSource> create() {
return CommandManager.literal("mana").requires(s -> s.hasPermissionLevel(2))
.then(CommandManager.argument("type", ManaType.argument()).executes(source -> {
var type = source.getArgument("type", ManaType.class);
var pony = Pony.of(source.getSource().getPlayer());
var bar = type.getBar(pony.getMagicalReserves());
source.getSource().getPlayer().sendMessage(Text.literal(type.name() + " is " + bar.get() + "/" + bar.getMax()));
return 0;
})
.then(CommandManager.argument("value", FloatArgumentType.floatArg()).executes(source -> {
var type = source.getArgument("type", ManaType.class);
var pony = Pony.of(source.getSource().getPlayer());
var bar = type.getBar(pony.getMagicalReserves());
source.getSource().getPlayer().sendMessage(Text.literal(type.name() + " is " + bar.get() + "/" + bar.getMax()));
return 0;
})
.then(CommandManager.argument("value", FloatArgumentType.floatArg()).executes(source -> {
var type = source.getArgument("type", ManaType.class);
var pony = Pony.of(source.getSource().getPlayer());
var bar = type.getBar(pony.getMagicalReserves());
float value = source.getArgument("value", Float.class);
while (type == ManaType.XP && value > 1) {
pony.getLevel().add(1);
value -= 1;
}
bar.set(value);
source.getSource().getPlayer().sendMessage(Text.literal("Set " + type.name() + " to " + bar.get() + "/" + bar.getMax()));
return 0;
}))
)
);
float value = source.getArgument("value", Float.class);
while (type == ManaType.XP && value > 1) {
pony.getLevel().add(1);
value -= 1;
}
bar.set(value);
source.getSource().getPlayer().sendMessage(Text.literal("Set " + type.name() + " to " + bar.get() + "/" + bar.getMax()));
return 0;
})));
}
enum ManaType implements CommandArgumentEnum<ManaType> {

View file

@ -3,10 +3,8 @@ package com.minelittlepony.unicopia.command;
import java.util.function.Function;
import com.minelittlepony.unicopia.*;
import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import net.minecraft.command.argument.RegistryKeyArgumentType;
import net.minecraft.server.command.CommandManager;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.server.network.ServerPlayerEntity;
@ -15,13 +13,10 @@ import net.minecraft.util.Formatting;
class RacelistCommand {
static void register(CommandDispatcher<ServerCommandSource> dispatcher) {
LiteralArgumentBuilder<ServerCommandSource> builder = CommandManager.literal("racelist").requires(s -> s.hasPermissionLevel(4));
RegistryKeyArgumentType<Race> raceArgument = Race.argument();
builder.then(CommandManager.literal("allow")
.then(CommandManager.argument("race", raceArgument)
static LiteralArgumentBuilder<ServerCommandSource> create() {
return CommandManager.literal("racelist").requires(s -> s.hasPermissionLevel(3))
.then(CommandManager.literal("allow")
.then(CommandManager.argument("race", Race.argument())
.executes(context -> toggle(context.getSource(), context.getSource().getPlayer(), Race.fromArgument(context, "race"), "allowed", race -> {
if (race.isUnset()) {
@ -34,9 +29,9 @@ class RacelistCommand {
return result;
}))
));
builder.then(CommandManager.literal("disallow")
.then(CommandManager.argument("race", raceArgument)
))
.then(CommandManager.literal("disallow")
.then(CommandManager.argument("race", Race.argument())
.executes(context -> toggle(context.getSource(), context.getSource().getPlayer(), Race.fromArgument(context, "race"), "disallowed", race -> {
boolean result = Unicopia.getConfig().speciesWhiteList.get().remove(race.getId().toString());
@ -44,9 +39,7 @@ class RacelistCommand {
return result;
}))
));
dispatcher.register(builder);
));
}
static int toggle(ServerCommandSource source, ServerPlayerEntity player, Race race, String action, Function<Race, Boolean> func) {

View file

@ -5,7 +5,6 @@ import com.minelittlepony.unicopia.Unicopia;
import com.minelittlepony.unicopia.entity.player.Pony;
import com.minelittlepony.unicopia.network.Channel;
import com.minelittlepony.unicopia.network.MsgTribeSelect;
import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import net.minecraft.command.argument.EntityArgumentType;
@ -22,8 +21,8 @@ import net.minecraft.util.Identifier;
import net.minecraft.world.GameRules;
class SpeciesCommand {
static void register(CommandDispatcher<ServerCommandSource> dispatcher, RegistrationEnvironment environment) {
LiteralArgumentBuilder<ServerCommandSource> builder = CommandManager.literal("race");
static LiteralArgumentBuilder<ServerCommandSource> create(RegistrationEnvironment environment) {
LiteralArgumentBuilder<ServerCommandSource> builder = CommandManager.literal("tribe");
if (environment.dedicated) {
if (Unicopia.getConfig().enableCheats.get()) {
@ -35,29 +34,25 @@ class SpeciesCommand {
RegistryKeyArgumentType<Race> raceArgument = Race.argument();
builder.then(CommandManager.literal("get")
return builder
.then(CommandManager.literal("get")
.executes(context -> get(context.getSource(), context.getSource().getPlayer(), true))
.then(CommandManager.argument("target", EntityArgumentType.player())
.executes(context -> get(context.getSource(), EntityArgumentType.getPlayer(context, "target"), false))
));
builder.then(CommandManager.literal("set")
))
.then(CommandManager.literal("set")
.then(CommandManager.argument("race", raceArgument)
.executes(context -> set(context.getSource(), context.getSource().getPlayer(), Race.fromArgument(context, "race"), true))
.then(CommandManager.argument("target", EntityArgumentType.player())
.executes(context -> set(context.getSource(), EntityArgumentType.getPlayer(context, "target"), Race.fromArgument(context, "race"), false)))
));
builder.then(CommandManager.literal("describe")
))
.then(CommandManager.literal("describe")
.then(CommandManager.argument("race", raceArgument)
.executes(context -> describe(context.getSource().getPlayer(), Race.fromArgument(context, "race")))
));
builder.then(CommandManager.literal("list")
))
.then(CommandManager.literal("list")
.executes(context -> list(context.getSource().getPlayer())
));
dispatcher.register(builder);
}
static int set(ServerCommandSource source, PlayerEntity player, Race race, boolean isSelf) {

View file

@ -3,7 +3,6 @@ package com.minelittlepony.unicopia.command;
import com.minelittlepony.unicopia.ability.magic.spell.trait.SpellTraits;
import com.minelittlepony.unicopia.ability.magic.spell.trait.Trait;
import com.minelittlepony.unicopia.entity.player.Pony;
import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.arguments.FloatArgumentType;
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
@ -16,29 +15,24 @@ import net.minecraft.text.*;
import net.minecraft.util.Hand;
class TraitCommand {
static void register(CommandDispatcher<ServerCommandSource> dispatcher) {
LiteralArgumentBuilder<ServerCommandSource> builder = CommandManager
.literal("trait")
.requires(s -> s.hasPermissionLevel(2));
builder.then(CommandManager.literal("add")
.then(CommandManager.argument("trait", Trait.argument())
.then(CommandManager.argument("value", FloatArgumentType.floatArg()).executes(source -> add(
source.getSource(),
source.getSource().getPlayer(),
source.getArgument("trait", Trait.class),
FloatArgumentType.getFloat(source, "value")
)))
));
builder.then(CommandManager.literal("remove")
.then(CommandManager.argument("trait", Trait.argument()).executes(source -> remove(
source.getSource(),
source.getSource().getPlayer(),
source.getArgument("trait", Trait.class)
))
));
dispatcher.register(builder);
static LiteralArgumentBuilder<ServerCommandSource> create() {
return CommandManager.literal("trait").requires(s -> s.hasPermissionLevel(2))
.then(CommandManager.literal("add")
.then(CommandManager.argument("trait", Trait.argument())
.then(CommandManager.argument("value", FloatArgumentType.floatArg()).executes(source -> add(
source.getSource(),
source.getSource().getPlayer(),
source.getArgument("trait", Trait.class),
FloatArgumentType.getFloat(source, "value")
)))
))
.then(CommandManager.literal("remove")
.then(CommandManager.argument("trait", Trait.argument()).executes(source -> remove(
source.getSource(),
source.getSource().getPlayer(),
source.getArgument("trait", Trait.class)
))
));
}
static int add(ServerCommandSource source, PlayerEntity player, Trait trait, float amount) throws CommandSyntaxException {

View file

@ -0,0 +1,23 @@
package com.minelittlepony.unicopia.command;
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import net.minecraft.command.CommandRegistryAccess;
import net.minecraft.server.command.CommandManager;
import net.minecraft.server.command.CommandManager.RegistrationEnvironment;
import net.minecraft.server.command.ServerCommandSource;
class UnicopiaCommand {
public static LiteralArgumentBuilder<ServerCommandSource> create(CommandRegistryAccess registries, RegistrationEnvironment environment) {
return CommandManager.literal("unicopia")
.then(EmoteCommand.create())
.then(SpeciesCommand.create(environment))
.then(RacelistCommand.create())
.then(WorldTribeCommand.create())
.then(GravityCommand.create())
.then(DisguiseCommand.create(registries))
.then(CastCommand.create(registries))
.then(TraitCommand.create())
.then(ManaCommand.create());
}
}

View file

@ -2,7 +2,6 @@ package com.minelittlepony.unicopia.command;
import com.minelittlepony.unicopia.Race;
import com.minelittlepony.unicopia.server.world.UnicopiaWorldProperties;
import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
@ -11,17 +10,12 @@ import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.text.Text;
class WorldTribeCommand {
static void register(CommandDispatcher<ServerCommandSource> dispatcher) {
LiteralArgumentBuilder<ServerCommandSource> builder = CommandManager
.literal("worldtribe")
.requires(s -> s.hasPermissionLevel(4));
builder.then(CommandManager.literal("get").executes(context -> get(context.getSource())));
builder.then(CommandManager.literal("set")
static LiteralArgumentBuilder<ServerCommandSource> create() {
return CommandManager.literal("worldtribe").requires(s -> s.hasPermissionLevel(3))
.then(CommandManager.literal("get").executes(context -> get(context.getSource())))
.then(CommandManager.literal("set")
.then(CommandManager.argument("race", Race.argument())
.executes(context -> set(context.getSource(), Race.fromArgument(context, "race")))));
dispatcher.register(builder);
}
static int get(ServerCommandSource source) throws CommandSyntaxException {