mirror of
https://github.com/Sollace/Unicopia.git
synced 2025-02-01 11:36:43 +01:00
Fixed error whilst executing the /race command
This commit is contained in:
parent
b1a979eb24
commit
52a4643ece
3 changed files with 26 additions and 8 deletions
|
@ -7,9 +7,13 @@ import org.jetbrains.annotations.Nullable;
|
||||||
import com.google.common.base.Strings;
|
import com.google.common.base.Strings;
|
||||||
import com.minelittlepony.unicopia.ability.magic.Affine;
|
import com.minelittlepony.unicopia.ability.magic.Affine;
|
||||||
import com.minelittlepony.unicopia.util.Registries;
|
import com.minelittlepony.unicopia.util.Registries;
|
||||||
|
import com.mojang.brigadier.context.CommandContext;
|
||||||
|
import com.mojang.brigadier.exceptions.CommandSyntaxException;
|
||||||
|
import com.mojang.brigadier.exceptions.DynamicCommandExceptionType;
|
||||||
|
|
||||||
import net.minecraft.command.argument.RegistryKeyArgumentType;
|
import net.minecraft.command.argument.RegistryKeyArgumentType;
|
||||||
import net.minecraft.entity.player.PlayerEntity;
|
import net.minecraft.entity.player.PlayerEntity;
|
||||||
|
import net.minecraft.server.command.ServerCommandSource;
|
||||||
import net.minecraft.text.Text;
|
import net.minecraft.text.Text;
|
||||||
import net.minecraft.util.Identifier;
|
import net.minecraft.util.Identifier;
|
||||||
import net.minecraft.util.registry.Registry;
|
import net.minecraft.util.registry.Registry;
|
||||||
|
@ -19,6 +23,7 @@ public final class Race implements Affine {
|
||||||
public static final String DEFAULT_ID = "unicopia:human";
|
public static final String DEFAULT_ID = "unicopia:human";
|
||||||
public static final Registry<Race> REGISTRY = Registries.createDefaulted(Unicopia.id("race"), DEFAULT_ID);
|
public static final Registry<Race> REGISTRY = Registries.createDefaulted(Unicopia.id("race"), DEFAULT_ID);
|
||||||
public static final RegistryKey<? extends Registry<Race>> REGISTRY_KEY = REGISTRY.getKey();
|
public static final RegistryKey<? extends Registry<Race>> REGISTRY_KEY = REGISTRY.getKey();
|
||||||
|
private static final DynamicCommandExceptionType UNKNOWN_RACE_EXCEPTION = new DynamicCommandExceptionType(id -> Text.translatable("race.unknown", id));
|
||||||
|
|
||||||
public static Race register(String name, boolean magic, FlightType flight, boolean earth) {
|
public static Race register(String name, boolean magic, FlightType flight, boolean earth) {
|
||||||
return register(Unicopia.id(name), magic, flight, earth);
|
return register(Unicopia.id(name), magic, flight, earth);
|
||||||
|
@ -156,4 +161,17 @@ public final class Race implements Affine {
|
||||||
public static Race fromName(String name) {
|
public static Race fromName(String name) {
|
||||||
return fromName(name, EARTH);
|
return fromName(name, EARTH);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static Race fromArgument(CommandContext<ServerCommandSource> context, String name) throws CommandSyntaxException {
|
||||||
|
Identifier id = context.getArgument(name, RegistryKey.class).getValue();
|
||||||
|
return REGISTRY.getOrEmpty(id).orElseThrow(() -> UNKNOWN_RACE_EXCEPTION.create(id));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -23,7 +23,7 @@ class RacelistCommand {
|
||||||
|
|
||||||
builder.then(CommandManager.literal("allow")
|
builder.then(CommandManager.literal("allow")
|
||||||
.then(CommandManager.argument("race", raceArgument)
|
.then(CommandManager.argument("race", raceArgument)
|
||||||
.executes(context -> toggle(context.getSource(), context.getSource().getPlayer(), context.getArgument("race", Race.class), "allowed", race -> {
|
.executes(context -> toggle(context.getSource(), context.getSource().getPlayer(), Race.fromArgument(context, "race"), "allowed", race -> {
|
||||||
boolean result = Unicopia.getConfig().speciesWhiteList.get().add(race);
|
boolean result = Unicopia.getConfig().speciesWhiteList.get().add(race);
|
||||||
|
|
||||||
Unicopia.getConfig().save();
|
Unicopia.getConfig().save();
|
||||||
|
@ -33,7 +33,7 @@ class RacelistCommand {
|
||||||
));
|
));
|
||||||
builder.then(CommandManager.literal("disallow")
|
builder.then(CommandManager.literal("disallow")
|
||||||
.then(CommandManager.argument("race", raceArgument)
|
.then(CommandManager.argument("race", raceArgument)
|
||||||
.executes(context -> toggle(context.getSource(), context.getSource().getPlayer(), context.getArgument("race", Race.class), "disallowed", race -> {
|
.executes(context -> toggle(context.getSource(), context.getSource().getPlayer(), Race.fromArgument(context, "race"), "disallowed", race -> {
|
||||||
boolean result = Unicopia.getConfig().speciesWhiteList.get().remove(race);
|
boolean result = Unicopia.getConfig().speciesWhiteList.get().remove(race);
|
||||||
|
|
||||||
Unicopia.getConfig().save();
|
Unicopia.getConfig().save();
|
||||||
|
|
|
@ -29,19 +29,19 @@ class SpeciesCommand {
|
||||||
|
|
||||||
builder.then(CommandManager.literal("set")
|
builder.then(CommandManager.literal("set")
|
||||||
.then(CommandManager.argument("race", raceArgument)
|
.then(CommandManager.argument("race", raceArgument)
|
||||||
.executes(context -> set(context.getSource(), context.getSource().getPlayer(), context.getArgument("race", Race.class), true))
|
.executes(context -> set(context.getSource(), context.getSource().getPlayer(), Race.fromArgument(context, "race"), true))
|
||||||
.then(CommandManager.argument("target", EntityArgumentType.player())
|
.then(CommandManager.argument("target", EntityArgumentType.player())
|
||||||
.executes(context -> set(context.getSource(), EntityArgumentType.getPlayer(context, "target"), context.getArgument("race", Race.class), false))
|
.executes(context -> set(context.getSource(), EntityArgumentType.getPlayer(context, "target"), Race.fromArgument(context, "race"), false)))
|
||||||
)));
|
));
|
||||||
|
|
||||||
builder.then(CommandManager.literal("describe")
|
builder.then(CommandManager.literal("describe")
|
||||||
.then(CommandManager.argument("race", raceArgument)
|
.then(CommandManager.argument("race", raceArgument)
|
||||||
.executes(context -> describe(context.getSource().getPlayer(), context.getArgument("race", Race.class))
|
.executes(context -> describe(context.getSource().getPlayer(), Race.fromArgument(context, "race")))
|
||||||
)));
|
));
|
||||||
|
|
||||||
builder.then(CommandManager.literal("list")
|
builder.then(CommandManager.literal("list")
|
||||||
.executes(context -> list(context.getSource().getPlayer())
|
.executes(context -> list(context.getSource().getPlayer())
|
||||||
));
|
));
|
||||||
|
|
||||||
dispatcher.register(builder);
|
dispatcher.register(builder);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue