2020-04-15 14:22:03 +02:00
|
|
|
package com.minelittlepony.unicopia;
|
2018-09-12 01:29:49 +02:00
|
|
|
|
2022-12-10 00:55:53 +01:00
|
|
|
import java.util.*;
|
|
|
|
import java.util.function.Predicate;
|
2022-10-17 17:28:13 +02:00
|
|
|
import java.util.stream.Collectors;
|
2018-09-12 22:37:06 +02:00
|
|
|
|
2021-08-04 15:38:03 +02:00
|
|
|
import org.jetbrains.annotations.Nullable;
|
2022-10-17 17:28:13 +02:00
|
|
|
import org.spongepowered.include.com.google.common.base.Objects;
|
2020-09-28 20:18:10 +02:00
|
|
|
|
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-12-19 00:12:49 +01:00
|
|
|
import com.minelittlepony.unicopia.util.RegistryUtils;
|
2022-08-27 15:51:49 +02:00
|
|
|
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;
|
2022-08-27 15:51:49 +02:00
|
|
|
import net.minecraft.server.command.ServerCommandSource;
|
2021-02-09 19:43:19 +01:00
|
|
|
import net.minecraft.text.Text;
|
2020-09-23 21:56:57 +02:00
|
|
|
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-08-06 22:46:07 +02:00
|
|
|
public record Race (boolean canCast, FlightType flightType, boolean canUseEarth, boolean isNocturnal, boolean canHang) implements Affine {
|
2023-01-21 01:28:59 +01:00
|
|
|
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);
|
2022-08-27 15:07:29 +02:00
|
|
|
public static final RegistryKey<? extends Registry<Race>> REGISTRY_KEY = REGISTRY.getKey();
|
2022-08-27 15:51:49 +02:00
|
|
|
private static final DynamicCommandExceptionType UNKNOWN_RACE_EXCEPTION = new DynamicCommandExceptionType(id -> Text.translatable("race.unknown", id));
|
2022-08-27 15:07:29 +02:00
|
|
|
|
2023-08-06 22:46:07 +02:00
|
|
|
public static Race register(String name, boolean magic, FlightType flight, boolean earth, boolean nocturnal, boolean canHang) {
|
|
|
|
return register(Unicopia.id(name), magic, flight, earth, nocturnal, canHang);
|
2022-08-27 15:07:29 +02:00
|
|
|
}
|
|
|
|
|
2023-08-06 22:46:07 +02:00
|
|
|
public static Race register(Identifier id, boolean magic, FlightType flight, boolean earth, boolean nocturnal, boolean canHang) {
|
|
|
|
return Registry.register(REGISTRY, id, new Race(magic, flight, earth, nocturnal, canHang));
|
2022-08-27 15:07:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public static RegistryKeyArgumentType<Race> argument() {
|
|
|
|
return RegistryKeyArgumentType.registryKey(REGISTRY_KEY);
|
|
|
|
}
|
2020-04-15 18:12:00 +02:00
|
|
|
|
2019-01-29 13:13:06 +01:00
|
|
|
/**
|
|
|
|
* The default, unset race.
|
|
|
|
* This is used if there are no other races.
|
|
|
|
*/
|
2023-08-06 22:46:07 +02:00
|
|
|
public static final Race UNSET = register("unset", false, FlightType.NONE, false, false, false);
|
|
|
|
public static final Race HUMAN = register("human", false, FlightType.NONE, false, false, false);
|
|
|
|
public static final Race EARTH = register("earth", false, FlightType.NONE, true, false, false);
|
|
|
|
public static final Race UNICORN = register("unicorn", true, FlightType.NONE, false, false, false);
|
|
|
|
public static final Race PEGASUS = register("pegasus", false, FlightType.AVIAN, false, false, false);
|
|
|
|
public static final Race BAT = register("bat", false, FlightType.AVIAN, false, true, true);
|
|
|
|
public static final Race ALICORN = register("alicorn", true, FlightType.AVIAN, true, false, false);
|
|
|
|
public static final Race CHANGELING = register("changeling", false, FlightType.INSECTOID, false, false, true);
|
2022-08-27 15:07:29 +02:00
|
|
|
|
|
|
|
public static void bootstrap() {}
|
2018-09-12 01:29:49 +02:00
|
|
|
|
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() {
|
2023-01-21 01:28:59 +01:00
|
|
|
return !isHuman() && this != CHANGELING;
|
2020-04-27 18:09:19 +02:00
|
|
|
}
|
|
|
|
|
2023-01-21 01:28:59 +01:00
|
|
|
public boolean isUnset() {
|
|
|
|
return this == UNSET;
|
2020-01-27 11:05:22 +01:00
|
|
|
}
|
|
|
|
|
2023-01-21 01:28:59 +01:00
|
|
|
public boolean isEquine() {
|
|
|
|
return !isHuman();
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean isHuman() {
|
|
|
|
return this == UNSET || this == HUMAN;
|
2018-09-12 01:29:49 +02:00
|
|
|
}
|
|
|
|
|
2023-05-21 18:39:02 +02:00
|
|
|
public boolean isDayurnal() {
|
|
|
|
return !isNocturnal();
|
|
|
|
}
|
|
|
|
|
2019-02-09 13:26:03 +01:00
|
|
|
public boolean isOp() {
|
|
|
|
return this == ALICORN;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-09-16 00:45:44 +02:00
|
|
|
public boolean canInteractWithClouds() {
|
2020-04-25 15:46:29 +02:00
|
|
|
return canFly() && this != CHANGELING && this != BAT;
|
2018-09-16 00:45:44 +02:00
|
|
|
}
|
|
|
|
|
2022-10-17 17:28:13 +02:00
|
|
|
public Identifier getId() {
|
|
|
|
Identifier id = REGISTRY.getId(this);
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
2021-02-09 19:43:19 +01:00
|
|
|
public Text getDisplayName() {
|
2022-06-25 00:19:55 +02:00
|
|
|
return Text.translatable(getTranslationKey());
|
2021-02-09 19:43:19 +01:00
|
|
|
}
|
|
|
|
|
2021-08-06 01:06:57 +02:00
|
|
|
public Text getAltDisplayName() {
|
2022-06-25 00:19:55 +02:00
|
|
|
return Text.translatable(getTranslationKey() + ".alt");
|
2021-08-06 01:06:57 +02:00
|
|
|
}
|
|
|
|
|
2019-01-31 16:21:14 +01:00
|
|
|
public String getTranslationKey() {
|
2022-10-17 17:28:13 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-09-18 14:16:28 +02:00
|
|
|
public Identifier getIcon() {
|
2022-10-17 17:28:13 +02:00
|
|
|
Identifier id = getId();
|
2022-09-18 14:16:28 +02:00
|
|
|
return new Identifier(id.getNamespace(), "textures/gui/race/" + id.getPath() + ".png");
|
|
|
|
}
|
|
|
|
|
2020-09-28 20:18:10 +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;
|
|
|
|
}
|
|
|
|
|
2022-10-17 17:28:13 +02:00
|
|
|
Set<String> whitelist = Unicopia.getConfig().speciesWhiteList.get();
|
2020-04-25 13:32:33 +02:00
|
|
|
|
2023-01-21 01:28:59 +01:00
|
|
|
return isUnset()
|
2020-04-25 13:32:33 +02:00
|
|
|
|| whitelist.isEmpty()
|
2022-10-17 17:28:13 +02:00
|
|
|
|| whitelist.contains(getId().toString());
|
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;
|
|
|
|
}
|
|
|
|
|
2022-10-17 17:28:13 +02:00
|
|
|
@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) {
|
2022-10-17 17:28:13 +02:00
|
|
|
return getId().toString().equalsIgnoreCase(s)
|
2019-01-31 16:21:14 +01:00
|
|
|
|| getTranslationKey().equalsIgnoreCase(s);
|
2018-09-12 01:29:49 +02:00
|
|
|
}
|
|
|
|
|
2018-09-16 00:45:44 +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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-16 00:45:44 +02:00
|
|
|
return def;
|
2018-09-12 01:29:49 +02:00
|
|
|
}
|
|
|
|
|
2019-01-29 13:13:06 +01:00
|
|
|
public static Race fromName(String name) {
|
|
|
|
return fromName(name, EARTH);
|
|
|
|
}
|
2022-08-27 15:51:49 +02:00
|
|
|
|
|
|
|
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));
|
|
|
|
}
|
2022-10-17 17:28:13 +02:00
|
|
|
|
|
|
|
public static Set<Race> allPermitted(PlayerEntity player) {
|
|
|
|
return REGISTRY.stream().filter(r -> r.isPermitted(player)).collect(Collectors.toSet());
|
|
|
|
}
|
2022-12-10 00:55:53 +01:00
|
|
|
|
2023-08-29 16:17:44 +02:00
|
|
|
public record Composite (Race physical, @Nullable Race pseudo) {
|
2023-09-01 20:09:13 +02:00
|
|
|
public static Composite DEFAULT = new Composite(Race.HUMAN, null);
|
|
|
|
|
|
|
|
public Race collapsed() {
|
|
|
|
return pseudo == null ? physical : pseudo;
|
|
|
|
}
|
|
|
|
|
2022-12-10 00:55:53 +01:00
|
|
|
public boolean includes(Race race) {
|
|
|
|
return physical == race || pseudo == race;
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean any(Predicate<Race> test) {
|
2023-08-29 16:17:44 +02:00
|
|
|
return test.test(physical) || (pseudo != null && test.test(pseudo));
|
2022-12-10 00:55:53 +01:00
|
|
|
}
|
2023-09-01 20:09:13 +02:00
|
|
|
|
|
|
|
public boolean canUseEarth() {
|
|
|
|
return any(Race::canUseEarth);
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean canFly() {
|
|
|
|
return any(Race::canFly);
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean canCast() {
|
|
|
|
return any(Race::canCast);
|
|
|
|
}
|
2022-12-10 00:55:53 +01:00
|
|
|
}
|
2018-09-12 01:29:49 +02:00
|
|
|
}
|
2022-08-27 15:51:49 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|