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

264 lines
9.3 KiB
Java
Raw Normal View History

package com.minelittlepony.unicopia;
2018-09-12 01:29:49 +02:00
import java.util.*;
import java.util.function.Predicate;
import java.util.function.Supplier;
import java.util.stream.Collectors;
2021-08-04 15:38:03 +02:00
import org.jetbrains.annotations.Nullable;
import org.spongepowered.include.com.google.common.base.Objects;
2018-09-12 01:29:49 +02:00
import com.google.common.base.Strings;
import com.google.common.base.Suppliers;
2020-09-23 17:19:28 +02:00
import com.minelittlepony.unicopia.ability.magic.Affine;
2022-12-19 00:12:49 +01:00
import com.minelittlepony.unicopia.util.RegistryUtils;
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;
2024-02-12 20:36:43 +01:00
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.text.Text;
import net.minecraft.util.Identifier;
2022-12-18 22:07:24 +01:00
import net.minecraft.registry.Registry;
import net.minecraft.registry.RegistryKey;
2022-08-27 15:07:29 +02:00
2023-11-08 13:58:31 +01:00
public record Race (Supplier<Composite> compositeSupplier, Availability availability, boolean canCast, FlightType flightType, boolean canUseEarth, boolean isNocturnal, boolean canHang) implements Affine {
public static final String DEFAULT_ID = "unicopia:unset";
2022-12-19 00:12:49 +01:00
public static final Registry<Race> REGISTRY = RegistryUtils.createDefaulted(Unicopia.id("race"), DEFAULT_ID);
public static final Registry<Race> COMMAND_REGISTRY = RegistryUtils.createDefaulted(Unicopia.id("race/grantable"), DEFAULT_ID);
2022-08-27 15:07:29 +02:00
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
2023-11-08 13:58:31 +01:00
public static Race register(String name, Availability availability, boolean magic, FlightType flight, boolean earth, boolean nocturnal, boolean canHang) {
return register(Unicopia.id(name), availability, magic, flight, earth, nocturnal, canHang);
2022-08-27 15:07:29 +02:00
}
2023-11-08 13:58:31 +01:00
public static Race register(Identifier id, Availability availability, boolean magic, FlightType flight, boolean earth, boolean nocturnal, boolean canHang) {
Race race = Registry.register(REGISTRY, id, new Race(Suppliers.memoize(() -> new Composite(REGISTRY.get(id), null, null)), availability, magic, flight, earth, nocturnal, canHang));
if (availability.isGrantable()) {
Registry.register(COMMAND_REGISTRY, id, race);
}
return race;
2022-08-27 15:07:29 +02:00
}
public static RegistryKeyArgumentType<Race> argument() {
return RegistryKeyArgumentType.registryKey(COMMAND_REGISTRY.getKey());
2022-08-27 15:07:29 +02:00
}
2020-04-15 18:12:00 +02:00
/**
* The default, unset race.
* This is used if there are no other races.
*/
2023-11-08 13:58:31 +01:00
public static final Race UNSET = register("unset", Availability.COMMANDS, false, FlightType.NONE, false, false, false);
public static final Race HUMAN = register("human", Availability.COMMANDS, false, FlightType.NONE, false, false, false);
public static final Race EARTH = register("earth", Availability.DEFAULT, false, FlightType.NONE, true, false, false);
public static final Race UNICORN = register("unicorn", Availability.DEFAULT, true, FlightType.NONE, false, false, false);
public static final Race PEGASUS = register("pegasus", Availability.DEFAULT, false, FlightType.AVIAN, false, false, false);
public static final Race BAT = register("bat", Availability.DEFAULT, false, FlightType.AVIAN, false, true, true);
public static final Race ALICORN = register("alicorn", Availability.COMMANDS, true, FlightType.AVIAN, true, false, false);
public static final Race CHANGELING = register("changeling", Availability.DEFAULT, false, FlightType.INSECTOID, false, false, true);
public static final Race KIRIN = register("kirin", Availability.DEFAULT, true, FlightType.NONE, false, false, false);
public static final Race HIPPOGRIFF = register("hippogriff", Availability.DEFAULT, false, FlightType.AVIAN, false, false, false);
public static final Race SEAPONY = register("seapony", Availability.NONE, false, FlightType.NONE, false, false, false);
2022-08-27 15:07:29 +02:00
public static void bootstrap() {}
2018-09-12 01:29:49 +02:00
public Composite composite() {
return compositeSupplier.get();
}
public Composite composite(@Nullable Race pseudo, @Nullable Race potential) {
return pseudo == null && potential == null ? composite() : new Composite(this, pseudo, potential);
}
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 !isHuman();
2020-04-27 18:09:19 +02:00
}
public boolean isUnset() {
return this == UNSET;
2020-01-27 11:05:22 +01:00
}
public boolean isEquine() {
return !isHuman();
}
2023-11-08 13:58:31 +01:00
public boolean isFish() {
return this == SEAPONY;
}
public boolean isHuman() {
return this == UNSET || this == HUMAN;
2018-09-12 01:29:49 +02:00
}
public boolean isDayurnal() {
return !isNocturnal();
}
2020-10-08 09:39:30 +02:00
public boolean canFly() {
2023-04-30 02:41:21 +02:00
return !flightType().isGrounded();
2018-09-12 01:29:49 +02:00
}
public boolean canInteractWithClouds() {
return canFly() && this != CHANGELING;
}
public boolean canInfluenceWeather() {
2023-11-08 13:58:31 +01:00
return canFly() && this != CHANGELING && this != BAT && this != HIPPOGRIFF;
}
public boolean hasPersistentWeatherMagic() {
return canInfluenceWeather();
}
public Identifier getId() {
2023-10-08 00:48:23 +02:00
return REGISTRY.getId(this);
}
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() {
Identifier id = getId();
2022-08-27 15:07:29 +02:00
return String.format("%s.race.%s", id.getNamespace(), id.getPath().toLowerCase());
2018-09-12 01:29:49 +02:00
}
public Identifier getIcon() {
Identifier id = getId();
return new Identifier(id.getNamespace(), "textures/gui/race/" + id.getPath() + ".png");
}
public boolean isPermitted(@Nullable PlayerEntity sender) {
return AllowList.INSTANCE.permits(this);
2020-04-15 18:12:00 +02:00
}
public Race validate(PlayerEntity sender) {
if (!isPermitted(sender)) {
2024-02-12 20:36:43 +01:00
Race alternative = this == EARTH ? HUMAN : EARTH.validate(sender);
if (alternative != this && sender instanceof ServerPlayerEntity spe) {
spe.sendMessageToClient(Text.translatable("respawn.reason.illegal_race", getDisplayName()), false);
2020-04-15 18:12:00 +02:00
}
2024-02-12 20:36:43 +01:00
return alternative;
2020-04-15 18:12:00 +02:00
}
return this;
}
public Race or(Race other) {
return isEquine() ? this : other;
}
@Override
public int hashCode() {
return getId().hashCode();
}
@Override
public boolean equals(Object o) {
return o instanceof Race race && Objects.equal(race.getId(), getId());
}
@Override
public String toString() {
return "Race{ " + getId().toString() + " }";
}
2019-02-09 13:26:03 +01:00
public boolean equals(String s) {
return getId().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));
}
public static Set<Race> allPermitted(PlayerEntity player) {
return REGISTRY.stream().filter(r -> r.isPermitted(player)).collect(Collectors.toSet());
}
public record Composite (Race physical, @Nullable Race pseudo, @Nullable Race potential) {
public Race collapsed() {
return pseudo == null ? physical : pseudo;
}
public boolean includes(Race race) {
return physical == race || pseudo == race;
}
public boolean any(Predicate<Race> test) {
return test.test(physical) || (pseudo != null && test.test(pseudo));
}
public boolean canUseEarth() {
return any(Race::canUseEarth);
}
public boolean canFly() {
return any(Race::canFly);
}
public boolean canCast() {
return any(Race::canCast);
}
public boolean canInteractWithClouds() {
return any(Race::canInteractWithClouds);
}
public boolean canInfluenceWeather() {
return any(Race::canInfluenceWeather);
}
public boolean hasPersistentWeatherMagic() {
return any(Race::hasPersistentWeatherMagic);
}
public FlightType flightType() {
2023-10-09 16:14:28 +02:00
if (pseudo() == null) {
return physical().flightType();
}
return physical().flightType().or(pseudo().flightType());
}
}
2018-09-12 01:29:49 +02:00
}