mirror of
https://github.com/Sollace/Unicopia.git
synced 2025-02-17 10:24:23 +01:00
Only recompute the composite race when neccessary
This commit is contained in:
parent
1b94eb0f78
commit
3cf8000fca
4 changed files with 31 additions and 18 deletions
|
@ -2,12 +2,14 @@ package com.minelittlepony.unicopia;
|
||||||
|
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.util.function.Predicate;
|
import java.util.function.Predicate;
|
||||||
|
import java.util.function.Supplier;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
import org.spongepowered.include.com.google.common.base.Objects;
|
import org.spongepowered.include.com.google.common.base.Objects;
|
||||||
|
|
||||||
import com.google.common.base.Strings;
|
import com.google.common.base.Strings;
|
||||||
|
import com.google.common.base.Suppliers;
|
||||||
import com.minelittlepony.unicopia.ability.magic.Affine;
|
import com.minelittlepony.unicopia.ability.magic.Affine;
|
||||||
import com.minelittlepony.unicopia.util.RegistryUtils;
|
import com.minelittlepony.unicopia.util.RegistryUtils;
|
||||||
import com.mojang.brigadier.context.CommandContext;
|
import com.mojang.brigadier.context.CommandContext;
|
||||||
|
@ -22,7 +24,7 @@ import net.minecraft.util.Identifier;
|
||||||
import net.minecraft.registry.Registry;
|
import net.minecraft.registry.Registry;
|
||||||
import net.minecraft.registry.RegistryKey;
|
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 String DEFAULT_ID = "unicopia:unset";
|
||||||
public static final Registry<Race> REGISTRY = RegistryUtils.createDefaulted(Unicopia.id("race"), DEFAULT_ID);
|
public static final Registry<Race> REGISTRY = RegistryUtils.createDefaulted(Unicopia.id("race"), DEFAULT_ID);
|
||||||
public static final RegistryKey<? extends Registry<Race>> REGISTRY_KEY = REGISTRY.getKey();
|
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) {
|
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() {
|
public static RegistryKeyArgumentType<Race> argument() {
|
||||||
|
@ -55,6 +57,14 @@ public record Race (boolean canCast, FlightType flightType, boolean canUseEarth,
|
||||||
|
|
||||||
public static void bootstrap() {}
|
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
|
@Override
|
||||||
public Affinity getAffinity() {
|
public Affinity getAffinity() {
|
||||||
return this == CHANGELING ? Affinity.BAD : Affinity.NEUTRAL;
|
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 record Composite (Race physical, @Nullable Race pseudo) {
|
||||||
public static Composite DEFAULT = new Composite(Race.HUMAN, null);
|
|
||||||
|
|
||||||
public Race collapsed() {
|
public Race collapsed() {
|
||||||
return pseudo == null ? physical : pseudo;
|
return pseudo == null ? physical : pseudo;
|
||||||
}
|
}
|
||||||
|
|
|
@ -104,7 +104,7 @@ public class Unicopia implements ModInitializer {
|
||||||
Optional<Pony> getPony();
|
Optional<Pony> getPony();
|
||||||
|
|
||||||
default Race.Composite getPlayerSpecies() {
|
default Race.Composite getPlayerSpecies() {
|
||||||
return getPony().map(Pony::getCompositeRace).orElse(Race.Composite.DEFAULT);
|
return getPony().map(Pony::getCompositeRace).orElse(Race.HUMAN.composite());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -21,7 +21,7 @@ public interface Equine<T extends Entity> extends NbtSerialisable, Tickable, Pro
|
||||||
void setSpecies(Race race);
|
void setSpecies(Race race);
|
||||||
|
|
||||||
default Race.Composite getCompositeRace() {
|
default Race.Composite getCompositeRace() {
|
||||||
return new Race.Composite(getSpecies(), null);
|
return getSpecies().composite();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -94,6 +94,8 @@ public class Pony extends Living<PlayerEntity> implements Copyable<Pony>, Update
|
||||||
|
|
||||||
private final Interpolator interpolator = new LinearInterpolator();
|
private final Interpolator interpolator = new LinearInterpolator();
|
||||||
|
|
||||||
|
@Nullable
|
||||||
|
private Race.Composite compositeRace;
|
||||||
private Race respawnRace = Race.UNSET;
|
private Race respawnRace = Race.UNSET;
|
||||||
|
|
||||||
private boolean dirty;
|
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.
|
* This includes illusions and shape-shifting but excludes items that grant abilities without changing their race.
|
||||||
*/
|
*/
|
||||||
public Race getObservedSpecies() {
|
public Race getObservedSpecies() {
|
||||||
return getSpellSlot()
|
return getCompositeRace().physical();
|
||||||
.get(SpellPredicate.IS_MIMIC, true)
|
|
||||||
.map(AbstractDisguiseSpell::getDisguise)
|
|
||||||
.map(EntityAppearance::getAppearance)
|
|
||||||
.flatMap(Pony::of)
|
|
||||||
.map(Pony::getSpecies)
|
|
||||||
.orElseGet(this::getSpecies);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -218,12 +214,21 @@ public class Pony extends Living<PlayerEntity> implements Copyable<Pony>, Update
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public Race.Composite getCompositeRace() {
|
public Race.Composite getCompositeRace() {
|
||||||
return new Race.Composite(getObservedSpecies(),
|
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.UNICORN_AMULET.test(entity) ? Race.UNICORN
|
||||||
: AmuletSelectors.ALICORN_AMULET.test(entity) ? Race.ALICORN
|
: AmuletSelectors.ALICORN_AMULET.test(entity) ? Race.ALICORN
|
||||||
: null
|
: null
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
return compositeRace;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setSpecies(Race race) {
|
public void setSpecies(Race race) {
|
||||||
|
|
Loading…
Reference in a new issue