Unicopia/src/main/java/com/minelittlepony/unicopia/Race.java

178 lines
5.5 KiB
Java
Raw Normal View History

package com.minelittlepony.unicopia;
2018-09-12 01:29:49 +02:00
2020-04-25 13:32:33 +02:00
import java.util.Set;
2021-08-04 15:38:03 +02:00
import org.jetbrains.annotations.Nullable;
2018-09-12 01:29:49 +02:00
import com.google.common.base.Strings;
2020-09-23 17:19:28 +02:00
import com.minelittlepony.unicopia.ability.magic.Affine;
2022-08-27 15:07:29 +02:00
import com.minelittlepony.unicopia.util.Registries;
import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import com.mojang.brigadier.exceptions.DynamicCommandExceptionType;
2018-09-12 01:29:49 +02:00
2022-08-27 15:07:29 +02:00
import net.minecraft.command.argument.RegistryKeyArgumentType;
2020-04-15 18:12:00 +02:00
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.text.Text;
import net.minecraft.util.Identifier;
2022-08-27 15:07:29 +02:00
import net.minecraft.util.registry.Registry;
import net.minecraft.util.registry.RegistryKey;
public final class Race implements Affine {
public static final String DEFAULT_ID = "unicopia:human";
public static final Registry<Race> REGISTRY = Registries.createDefaulted(Unicopia.id("race"), DEFAULT_ID);
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));
2022-08-27 15:07:29 +02:00
public static Race register(String name, boolean magic, FlightType flight, boolean earth) {
return register(Unicopia.id(name), magic, flight, earth);
}
public static Race register(Identifier id, boolean magic, FlightType flight, boolean earth) {
return Registry.register(REGISTRY, id, new Race(magic, flight, earth));
}
public static RegistryKeyArgumentType<Race> argument() {
return RegistryKeyArgumentType.registryKey(REGISTRY_KEY);
}
2020-04-15 18:12:00 +02:00
/**
* The default, unset race.
* This is used if there are no other races.
*/
2022-08-27 15:07:29 +02:00
public static final Race HUMAN = register("human", false, FlightType.NONE, false);
public static final Race EARTH = register("earth", false, FlightType.NONE, true);
public static final Race UNICORN = register("unicorn", true, FlightType.NONE, false);
public static final Race PEGASUS = register("pegasus", false, FlightType.AVIAN, false);
public static final Race BAT = register("bat", false, FlightType.AVIAN, false);
public static final Race ALICORN = register("alicorn", true, FlightType.AVIAN, true);
public static final Race CHANGELING = register("changeling", false, FlightType.INSECTOID, false);
public static void bootstrap() {}
2018-09-12 01:29:49 +02:00
private final boolean magic;
2020-10-08 09:39:30 +02:00
private final FlightType flight;
2018-09-12 01:29:49 +02:00
private final boolean earth;
2020-10-08 09:39:30 +02:00
Race(boolean magic, FlightType flight, boolean earth) {
2018-09-12 01:29:49 +02:00
this.magic = magic;
this.flight = flight;
this.earth = earth;
}
2020-09-23 17:19:28 +02:00
@Override
public Affinity getAffinity() {
return this == CHANGELING ? Affinity.BAD : Affinity.NEUTRAL;
}
2020-04-27 18:09:19 +02:00
public boolean hasIronGut() {
return isUsable() && this != CHANGELING;
}
2020-01-27 11:05:22 +01:00
public boolean isUsable() {
return !isDefault();
}
2018-09-12 01:29:49 +02:00
public boolean isDefault() {
return this == HUMAN;
}
2019-02-09 13:26:03 +01:00
public boolean isOp() {
return this == ALICORN;
}
2020-10-08 09:39:30 +02:00
public FlightType getFlightType() {
2018-09-12 01:29:49 +02:00
return flight;
}
2020-10-08 09:39:30 +02:00
public boolean canFly() {
return !getFlightType().isGrounded();
}
2018-09-12 01:29:49 +02:00
public boolean canCast() {
return magic;
}
public boolean canUseEarth() {
return earth;
}
public boolean canInteractWithClouds() {
2020-04-25 15:46:29 +02:00
return canFly() && this != CHANGELING && this != BAT;
}
public Text getDisplayName() {
2022-06-25 00:19:55 +02:00
return Text.translatable(getTranslationKey());
}
public Text getAltDisplayName() {
2022-06-25 00:19:55 +02:00
return Text.translatable(getTranslationKey() + ".alt");
}
2019-01-31 16:21:14 +01:00
public String getTranslationKey() {
2022-08-27 15:07:29 +02:00
Identifier id = REGISTRY.getId(this);
return String.format("%s.race.%s", id.getNamespace(), id.getPath().toLowerCase());
2018-09-12 01:29:49 +02:00
}
public boolean isPermitted(@Nullable PlayerEntity sender) {
2021-08-04 15:38:03 +02:00
if (isOp() && (sender == null || !sender.getAbilities().creativeMode)) {
2020-04-15 18:12:00 +02:00
return false;
}
Set<Race> whitelist = Unicopia.getConfig().speciesWhiteList.get();
2020-04-25 13:32:33 +02:00
return isDefault()
|| whitelist.isEmpty()
|| whitelist.contains(this);
2020-04-15 18:12:00 +02:00
}
public Race validate(PlayerEntity sender) {
if (!isPermitted(sender)) {
if (this == EARTH) {
return HUMAN;
}
return EARTH.validate(sender);
}
return this;
}
2019-02-09 13:26:03 +01:00
public boolean equals(String s) {
2022-08-27 15:07:29 +02:00
return REGISTRY.getId(this).toString().equalsIgnoreCase(s)
2019-01-31 16:21:14 +01:00
|| getTranslationKey().equalsIgnoreCase(s);
2018-09-12 01:29:49 +02:00
}
public static Race fromName(String s, Race def) {
2018-09-12 01:29:49 +02:00
if (!Strings.isNullOrEmpty(s)) {
2022-08-27 15:07:29 +02:00
Identifier id = Identifier.tryParse(s);
if (id != null) {
if (id.getNamespace() == Identifier.DEFAULT_NAMESPACE) {
id = new Identifier(Unicopia.DEFAULT_NAMESPACE, id.getPath());
}
return REGISTRY.getOrEmpty(id).orElse(def);
2018-09-12 01:29:49 +02:00
}
}
return def;
2018-09-12 01:29:49 +02:00
}
public static Race fromName(String name) {
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));
}
2018-09-12 01:29:49 +02:00
}