Only recompute the composite race when neccessary

This commit is contained in:
Sollace 2023-09-03 11:06:44 +01:00
parent 1b94eb0f78
commit 3cf8000fca
No known key found for this signature in database
GPG key ID: E52FACE7B5C773DB
4 changed files with 31 additions and 18 deletions

View file

@ -2,12 +2,14 @@ package com.minelittlepony.unicopia;
import java.util.*;
import java.util.function.Predicate;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import org.jetbrains.annotations.Nullable;
import org.spongepowered.include.com.google.common.base.Objects;
import com.google.common.base.Strings;
import com.google.common.base.Suppliers;
import com.minelittlepony.unicopia.ability.magic.Affine;
import com.minelittlepony.unicopia.util.RegistryUtils;
import com.mojang.brigadier.context.CommandContext;
@ -22,7 +24,7 @@ import net.minecraft.util.Identifier;
import net.minecraft.registry.Registry;
import net.minecraft.registry.RegistryKey;
public record Race (boolean canCast, FlightType flightType, boolean canUseEarth, boolean isNocturnal, boolean canHang) implements Affine {
public record Race (Supplier<Composite> compositeSupplier, boolean canCast, FlightType flightType, boolean canUseEarth, boolean isNocturnal, boolean canHang) implements Affine {
public static final String DEFAULT_ID = "unicopia:unset";
public static final Registry<Race> REGISTRY = RegistryUtils.createDefaulted(Unicopia.id("race"), DEFAULT_ID);
public static final RegistryKey<? extends Registry<Race>> REGISTRY_KEY = REGISTRY.getKey();
@ -33,7 +35,7 @@ public record Race (boolean canCast, FlightType flightType, boolean canUseEarth,
}
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));
return Registry.register(REGISTRY, id, new Race(Suppliers.memoize(() -> new Composite(REGISTRY.get(id), null)), magic, flight, earth, nocturnal, canHang));
}
public static RegistryKeyArgumentType<Race> argument() {
@ -55,6 +57,14 @@ public record Race (boolean canCast, FlightType flightType, boolean canUseEarth,
public static void bootstrap() {}
public Composite composite() {
return compositeSupplier.get();
}
public Composite composite(@Nullable Race pseudo) {
return pseudo == null ? composite() : new Composite(this, pseudo);
}
@Override
public Affinity getAffinity() {
return this == CHANGELING ? Affinity.BAD : Affinity.NEUTRAL;
@ -187,8 +197,6 @@ public record Race (boolean canCast, FlightType flightType, boolean canUseEarth,
}
public record Composite (Race physical, @Nullable Race pseudo) {
public static Composite DEFAULT = new Composite(Race.HUMAN, null);
public Race collapsed() {
return pseudo == null ? physical : pseudo;
}

View file

@ -104,7 +104,7 @@ public class Unicopia implements ModInitializer {
Optional<Pony> getPony();
default Race.Composite getPlayerSpecies() {
return getPony().map(Pony::getCompositeRace).orElse(Race.Composite.DEFAULT);
return getPony().map(Pony::getCompositeRace).orElse(Race.HUMAN.composite());
}
}
}

View file

@ -21,7 +21,7 @@ public interface Equine<T extends Entity> extends NbtSerialisable, Tickable, Pro
void setSpecies(Race race);
default Race.Composite getCompositeRace() {
return new Race.Composite(getSpecies(), null);
return getSpecies().composite();
}
/**

View file

@ -94,6 +94,8 @@ public class Pony extends Living<PlayerEntity> implements Copyable<Pony>, Update
private final Interpolator interpolator = new LinearInterpolator();
@Nullable
private Race.Composite compositeRace;
private Race respawnRace = Race.UNSET;
private boolean dirty;
@ -203,13 +205,7 @@ public class Pony extends Living<PlayerEntity> implements Copyable<Pony>, Update
* This includes illusions and shape-shifting but excludes items that grant abilities without changing their race.
*/
public Race getObservedSpecies() {
return getSpellSlot()
.get(SpellPredicate.IS_MIMIC, true)
.map(AbstractDisguiseSpell::getDisguise)
.map(EntityAppearance::getAppearance)
.flatMap(Pony::of)
.map(Pony::getSpecies)
.orElseGet(this::getSpecies);
return getCompositeRace().physical();
}
/**
@ -218,11 +214,20 @@ public class Pony extends Living<PlayerEntity> implements Copyable<Pony>, Update
*/
@Override
public Race.Composite getCompositeRace() {
return new Race.Composite(getObservedSpecies(),
AmuletSelectors.UNICORN_AMULET.test(entity) ? Race.UNICORN
: AmuletSelectors.ALICORN_AMULET.test(entity) ? Race.ALICORN
: null
);
if (compositeRace == null || entity.age % 2 == 0) {
compositeRace = getSpellSlot()
.get(SpellPredicate.IS_MIMIC, true)
.map(AbstractDisguiseSpell::getDisguise)
.map(EntityAppearance::getAppearance)
.flatMap(Pony::of)
.map(Pony::getSpecies)
.orElseGet(this::getSpecies).composite(
AmuletSelectors.UNICORN_AMULET.test(entity) ? Race.UNICORN
: AmuletSelectors.ALICORN_AMULET.test(entity) ? Race.ALICORN
: null
);
}
return compositeRace;
}
@Override