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

379 lines
13 KiB
Java
Raw Normal View History

package com.minelittlepony.unicopia;
2018-09-12 01:29:49 +02:00
import java.util.*;
2024-04-08 21:45:46 +02:00
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;
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;
2024-04-08 21:45:46 +02:00
import com.minelittlepony.unicopia.ability.Abilities;
import com.minelittlepony.unicopia.ability.Ability;
2020-09-23 17:19:28 +02:00
import com.minelittlepony.unicopia.ability.magic.Affine;
2024-05-22 16:25:35 +02:00
import com.minelittlepony.unicopia.network.track.TrackableDataType;
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;
2024-04-08 21:45:46 +02:00
import com.mojang.serialization.Codec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
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;
2024-04-08 21:45:46 +02:00
import net.minecraft.util.Util;
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
2024-04-08 21:45:46 +02:00
public record Race (
List<Ability<?>> abilities,
Affinity affinity,
Availability availability,
FlightType flightType,
boolean canCast,
boolean hasIronGut,
boolean canUseEarth,
boolean isNocturnal,
boolean canHang,
boolean isFish,
boolean canInfluenceWeather,
boolean canInteractWithClouds
) 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);
2024-05-22 16:25:35 +02:00
public static final TrackableDataType<Race> TRACKABLE_TYPE = TrackableDataType.RACE;
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("commands.race.fail", id));
2024-04-08 21:45:46 +02:00
private static final Function<Race, Composite> COMPOSITES = Util.memoize(race -> new Composite(race, null, null));
public static final Codec<Race> CODEC = RecordCodecBuilder.create(i -> i.group(
Abilities.REGISTRY.getCodec().listOf().fieldOf("abilities").forGetter(Race::abilities),
Affinity.CODEC.fieldOf("affinity").forGetter(Race::affinity),
Availability.CODEC.fieldOf("availability").forGetter(Race::availability),
FlightType.CODEC.fieldOf("flight").forGetter(Race::flightType),
Codec.BOOL.fieldOf("magic").forGetter(Race::canCast),
Codec.BOOL.fieldOf("can_forage").forGetter(Race::hasIronGut),
Codec.BOOL.fieldOf("earth_pony_strength").forGetter(Race::canUseEarth),
Codec.BOOL.fieldOf("nocturnal").forGetter(Race::isNocturnal),
Codec.BOOL.fieldOf("hanging").forGetter(Race::canHang),
Codec.BOOL.fieldOf("aquatic").forGetter(Race::isFish),
Codec.BOOL.fieldOf("weather_magic").forGetter(Race::canInfluenceWeather),
Codec.BOOL.fieldOf("cloud_magic").forGetter(Race::canInteractWithClouds)
).apply(i, Race::new));
2020-04-15 18:12:00 +02:00
/**
* The default, unset race.
* This is used if there are no other races.
*/
2024-04-08 21:45:46 +02:00
public static final Race UNSET = register("unset", new Builder().availability(Availability.COMMANDS));
2024-09-24 16:16:49 +02:00
public static final Race HUMAN = register("human", new Builder());
2024-04-08 21:45:46 +02:00
public static final Race EARTH = register("earth", new Builder().foraging().earth()
.abilities(Abilities.HUG, Abilities.STOMP, Abilities.KICK, Abilities.GROW)
);
public static final Race UNICORN = register("unicorn", new Builder().foraging().magic()
2024-04-08 23:13:08 +02:00
.abilities(Abilities.TELEPORT, Abilities.CAST, Abilities.GROUP_TELEPORT, Abilities.SHOOT, Abilities.DISPELL)
2024-04-08 21:45:46 +02:00
);
public static final Race PEGASUS = register("pegasus", new Builder().foraging().flight(FlightType.AVIAN).weatherMagic().cloudMagic()
.abilities(Abilities.TOGGLE_FLIGHT, Abilities.RAINBOOM, Abilities.CAPTURE_CLOUD, Abilities.CARRY)
);
public static final Race BAT = register("bat", new Builder().foraging().flight(FlightType.AVIAN).canHang().cloudMagic().nocturnal()
2024-04-08 21:45:46 +02:00
.abilities(Abilities.TOGGLE_FLIGHT, Abilities.CARRY, Abilities.HANG, Abilities.EEEE)
);
public static final Race ALICORN = register("alicorn", new Builder().foraging().availability(Availability.COMMANDS).flight(FlightType.AVIAN).earth().magic().weatherMagic().cloudMagic()
.abilities(
2024-04-08 23:13:08 +02:00
Abilities.TELEPORT, Abilities.GROUP_TELEPORT, Abilities.CAST, Abilities.SHOOT, Abilities.DISPELL,
2024-04-08 21:45:46 +02:00
Abilities.TOGGLE_FLIGHT, Abilities.RAINBOOM, Abilities.CAPTURE_CLOUD, Abilities.CARRY,
Abilities.HUG, Abilities.STOMP, Abilities.KICK, Abilities.GROW,
Abilities.TIME
)
);
public static final Race CHANGELING = register("changeling", new Builder().foraging().affinity(Affinity.BAD).flight(FlightType.INSECTOID).canHang()
.abilities(Abilities.DISPELL, Abilities.TOGGLE_FLIGHT, Abilities.FEED, Abilities.DISGUISE, Abilities.CARRY)
);
public static final Race KIRIN = register("kirin", new Builder().foraging().magic()
.abilities(Abilities.DISPELL, Abilities.RAGE, Abilities.NIRIK_BLAST, Abilities.KIRIN_CAST)
);
public static final Race HIPPOGRIFF = register("hippogriff", new Builder().foraging().flight(FlightType.AVIAN).cloudMagic()
.abilities(Abilities.TOGGLE_FLIGHT, Abilities.SCREECH, Abilities.PECK, Abilities.DASH, Abilities.CARRY)
);
public static final Race SEAPONY = register("seapony", new Builder().availability(Availability.COMMANDS).foraging().fish()
2024-04-08 21:45:46 +02:00
.abilities(Abilities.SONAR_PULSE)
);
2022-08-27 15:07:29 +02:00
public static void bootstrap() {}
2018-09-12 01:29:49 +02:00
public Composite composite() {
2024-04-08 21:45:46 +02:00
return COMPOSITES.apply(this);
}
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() {
2024-04-08 21:45:46 +02:00
return affinity;
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();
}
public boolean isHuman() {
2024-04-08 21:45:46 +02:00
return isUnset() || 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 hasPersistentWeatherMagic() {
return canInfluenceWeather();
}
2024-04-08 21:45:46 +02:00
public boolean canUse(Ability<?> ability) {
return abilities.contains(ability);
}
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() {
2024-04-08 21:45:46 +02:00
return Util.createTranslationKey("race", getId());
2018-09-12 01:29:49 +02:00
}
public Identifier getIcon() {
2024-04-08 21:45:46 +02:00
return getId().withPath(p -> "textures/gui/race/" + p + ".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 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) {
2024-09-26 22:36:35 +02:00
id = Unicopia.id(id.getPath());
2022-08-27 15:07:29 +02:00
}
return REGISTRY.getOrEmpty(id).orElse(def);
2018-09-12 01:29:49 +02:00
}
}
return def;
2018-09-12 01:29:49 +02:00
}
2024-04-08 21:45:46 +02:00
public static Race register(String name, Builder builder) {
return register(Unicopia.id(name), builder);
}
public static Race register(Identifier id, Builder builder) {
Race race = Registry.register(REGISTRY, id, builder.build());
if (race.availability().isGrantable()) {
Registry.register(COMMAND_REGISTRY, id, race);
}
return race;
}
public static RegistryKeyArgumentType<Race> argument() {
return RegistryKeyArgumentType.registryKey(COMMAND_REGISTRY.getKey());
}
public static Race fromArgument(CommandContext<ServerCommandSource> context, String name) throws CommandSyntaxException {
Identifier id = context.getArgument(name, RegistryKey.class).getValue();
final Identifier idf = id;
if (id.getNamespace() == Identifier.DEFAULT_NAMESPACE && !REGISTRY.containsId(id)) {
2024-09-26 22:36:35 +02:00
id = REGISTRY_KEY.getValue().withPath(id.getPath());
}
return REGISTRY.getOrEmpty(id).orElseThrow(() -> UNKNOWN_RACE_EXCEPTION.create(idf));
}
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);
}
2024-04-08 21:45:46 +02:00
public boolean canUse(Ability<?> ability) {
return any(r -> r.canUse(ability));
}
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());
}
}
2024-04-08 21:45:46 +02:00
public static final class Builder {
private final List<Ability<?>> abilities = new ArrayList<>();
private Affinity affinity = Affinity.NEUTRAL;
private Availability availability = Availability.DEFAULT;
private boolean canCast;
private boolean hasIronGut;
private FlightType flightType = FlightType.NONE;
private boolean canUseEarth;
private boolean isNocturnal;
private boolean canHang;
private boolean isFish;
private boolean canInfluenceWeather;
private boolean canInteractWithClouds;
public Builder abilities(Ability<?>...abilities) {
this.abilities.addAll(List.of(abilities));
return this;
}
public Builder foraging() {
hasIronGut = true;
return this;
}
public Builder affinity(Affinity affinity) {
this.affinity = affinity;
return this;
}
public Builder availability(Availability availability) {
this.availability = availability;
return this;
}
public Builder flight(FlightType flight) {
flightType = flight;
return this;
}
public Builder magic() {
canCast = true;
return this;
}
public Builder earth() {
canUseEarth = true;
return this;
}
public Builder nocturnal() {
isNocturnal = true;
return this;
}
public Builder canHang() {
canHang = true;
return this;
}
public Builder fish() {
isFish = true;
return this;
}
public Builder weatherMagic() {
canInfluenceWeather = true;
return this;
}
public Builder cloudMagic() {
canInteractWithClouds = true;
return this;
}
public Race build() {
return new Race(List.copyOf(abilities), affinity, availability, flightType, canCast, hasIronGut, canUseEarth, isNocturnal, canHang, isFish, canInfluenceWeather, canInteractWithClouds);
}
}
2018-09-12 01:29:49 +02:00
}