From 84a78ce4b9c8291312c9ab3feec75550088c7c6a Mon Sep 17 00:00:00 2001 From: Sollace Date: Thu, 8 Dec 2022 21:19:40 +0000 Subject: [PATCH] Change Pony into a record and track whether a IPony instance was returned as a default skin or not separate from the texture id --- .../com/minelittlepony/api/pony/IPony.java | 5 +- .../client/hdskins/DummyPony.java | 4 +- .../client/mixin/MixinClientPlayerEntity.java | 3 +- .../com/minelittlepony/client/pony/Pony.java | 62 +++---------------- .../client/pony/PonyManager.java | 24 +++++-- .../client/render/EquineRenderManager.java | 7 ++- 6 files changed, 40 insertions(+), 65 deletions(-) diff --git a/src/main/java/com/minelittlepony/api/pony/IPony.java b/src/main/java/com/minelittlepony/api/pony/IPony.java index 83f6db9a..c5ced63c 100644 --- a/src/main/java/com/minelittlepony/api/pony/IPony.java +++ b/src/main/java/com/minelittlepony/api/pony/IPony.java @@ -7,6 +7,7 @@ import org.jetbrains.annotations.Nullable; import com.google.common.collect.ComparisonChain; import com.minelittlepony.api.pony.meta.Race; import com.minelittlepony.client.MineLittlePony; +import com.minelittlepony.settings.PonyConfig; public interface IPony extends Comparable { @@ -41,7 +42,9 @@ public interface IPony extends Comparable { /** * Gets the race associated with this pony. */ - Race race(); + default Race race() { + return PonyConfig.getEffectiveRace(metadata().getRace()); + } /** * Gets the texture used for rendering this pony. diff --git a/src/main/java/com/minelittlepony/client/hdskins/DummyPony.java b/src/main/java/com/minelittlepony/client/hdskins/DummyPony.java index 592caae5..643c5192 100644 --- a/src/main/java/com/minelittlepony/client/hdskins/DummyPony.java +++ b/src/main/java/com/minelittlepony/client/hdskins/DummyPony.java @@ -7,13 +7,13 @@ import com.minelittlepony.api.pony.IPony; import com.minelittlepony.api.pony.IPonyManager; import com.minelittlepony.client.IPreviewModel; import com.minelittlepony.client.MineLittlePony; -import com.minelittlepony.client.pony.Pony; +import com.minelittlepony.client.render.EquineRenderManager; import com.minelittlepony.hdskins.client.dummy.*; /** * Dummy model used for the skin uploading screen. */ -class DummyPony extends DummyPlayer implements IPreviewModel, ModelAttributes.Swimmer, IPonyManager.ForcedPony, Pony.RegistrationHandler { +class DummyPony extends DummyPlayer implements IPreviewModel, ModelAttributes.Swimmer, IPonyManager.ForcedPony, EquineRenderManager.RegistrationHandler { public DummyPony(ClientWorld world, PlayerSkins textures) { super(world, textures); diff --git a/src/main/java/com/minelittlepony/client/mixin/MixinClientPlayerEntity.java b/src/main/java/com/minelittlepony/client/mixin/MixinClientPlayerEntity.java index 474042ba..1479621e 100644 --- a/src/main/java/com/minelittlepony/client/mixin/MixinClientPlayerEntity.java +++ b/src/main/java/com/minelittlepony/client/mixin/MixinClientPlayerEntity.java @@ -3,6 +3,7 @@ package com.minelittlepony.client.mixin; import com.minelittlepony.api.pony.IPony; import com.minelittlepony.client.MineLittlePony; import com.minelittlepony.client.pony.Pony; +import com.minelittlepony.client.render.EquineRenderManager; import net.minecraft.client.network.AbstractClientPlayerEntity; import net.minecraft.client.network.ClientPlayerEntity; @@ -18,7 +19,7 @@ import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; @Mixin(ClientPlayerEntity.class) -abstract class MixinClientPlayerEntity extends AbstractClientPlayerEntity implements Pony.RegistrationHandler { +abstract class MixinClientPlayerEntity extends AbstractClientPlayerEntity implements EquineRenderManager.RegistrationHandler { public MixinClientPlayerEntity() { super(null, null); } @Nullable diff --git a/src/main/java/com/minelittlepony/client/pony/Pony.java b/src/main/java/com/minelittlepony/client/pony/Pony.java index 3606a5d9..b320f412 100644 --- a/src/main/java/com/minelittlepony/client/pony/Pony.java +++ b/src/main/java/com/minelittlepony/client/pony/Pony.java @@ -1,76 +1,32 @@ package com.minelittlepony.client.pony; -import com.google.common.base.MoreObjects; import com.minelittlepony.api.pony.IPony; import com.minelittlepony.api.pony.IPonyData; -import com.minelittlepony.api.pony.meta.Race; import com.minelittlepony.api.pony.network.MsgPonyData; -import com.minelittlepony.settings.PonyConfig; import net.minecraft.util.Identifier; -import org.jetbrains.annotations.Unmodifiable; - -@Unmodifiable -public class Pony implements IPony { - private final Identifier texture; - private final Memoize metadata; - - private boolean defaulted; - - Pony(Identifier resource, Memoize data) { - texture = resource; - metadata = data; - } +public record Pony ( + Identifier texture, + Memoize memoizedData, + boolean defaulted + ) implements IPony { Pony(Identifier resource) { - this(resource, PonyData.parse(resource)); - } - - public IPony markDefaulted() { - defaulted = true; - return this; - } - - @Override - public boolean defaulted() { - return defaulted; + this(resource, PonyData.parse(resource), false); } @Override public boolean hasMetadata() { - return metadata.isPresent(); - } - - @Override - public Race race() { - return PonyConfig.getEffectiveRace(metadata().getRace()); - } - - @Override - public Identifier texture() { - return texture; + return memoizedData.isPresent(); } @Override public IPonyData metadata() { - return metadata.get(PonyData.NULL); - } - - @Override - public String toString() { - return MoreObjects.toStringHelper(this) - .add("texture", texture) - .add("metadata", metadata) - .add("defaulted", defaulted) - .toString(); - } - - public interface RegistrationHandler { - boolean shouldUpdateRegistration(IPony pony); + return memoizedData.get(PonyData.NULL); } public static IPony snapshot(IPony pony) { - return new Pony(pony.texture(), Memoize.of(new MsgPonyData(pony.metadata(), pony.defaulted()))); + return new Pony(pony.texture(), Memoize.of(new MsgPonyData(pony.metadata(), pony.defaulted())), pony.defaulted()); } } diff --git a/src/main/java/com/minelittlepony/client/pony/PonyManager.java b/src/main/java/com/minelittlepony/client/pony/PonyManager.java index 4ab38f3a..9533e827 100644 --- a/src/main/java/com/minelittlepony/client/pony/PonyManager.java +++ b/src/main/java/com/minelittlepony/client/pony/PonyManager.java @@ -1,8 +1,6 @@ package com.minelittlepony.client.pony; -import com.google.common.cache.CacheBuilder; -import com.google.common.cache.CacheLoader; -import com.google.common.cache.LoadingCache; +import com.google.common.cache.*; import com.minelittlepony.api.pony.IPony; import com.minelittlepony.api.pony.IPonyManager; import com.minelittlepony.client.MineLittlePony; @@ -38,6 +36,10 @@ public class PonyManager implements IPonyManager, SimpleSynchronousResourceReloa private final PonyConfig config; + private final Cache defaultedPoniesCache = CacheBuilder.newBuilder() + .expireAfterAccess(30, TimeUnit.SECONDS) + .build(); + private final LoadingCache poniesCache = CacheBuilder.newBuilder() .expireAfterAccess(30, TimeUnit.SECONDS) .build(CacheLoader.from(Pony::new)); @@ -67,7 +69,7 @@ public class PonyManager implements IPonyManager, SimpleSynchronousResourceReloa try { return poniesCache.get(resource); } catch (ExecutionException e) { - return new Pony(resource, PonyData.MEM_NULL); + return new Pony(resource, PonyData.MEM_NULL, true); } } @@ -111,10 +113,18 @@ public class PonyManager implements IPonyManager, SimpleSynchronousResourceReloa return pony; } + private IPony getAsDefaulted(IPony pony) { + try { + return defaultedPoniesCache.get(pony.texture(), () -> new Pony(pony.texture(), ((Pony)pony).memoizedData(), true)); + } catch (ExecutionException e) { + return pony; + } + } + @Override public IPony getDefaultPony(UUID uuid) { if (config.ponyLevel.get() != PonyLevel.PONIES) { - return ((Pony)getPony(DefaultSkinHelper.getTexture(uuid))).markDefaulted(); + return getAsDefaulted(getPony(DefaultSkinHelper.getTexture(uuid))); } return getBackgroundPony(uuid); @@ -122,17 +132,19 @@ public class PonyManager implements IPonyManager, SimpleSynchronousResourceReloa @Override public IPony getBackgroundPony(UUID uuid) { - return ((Pony)getPony(MineLittlePony.getInstance().getVariatedTextures().get(BACKGROUND_PONIES, uuid))).markDefaulted(); + return getAsDefaulted(getPony(MineLittlePony.getInstance().getVariatedTextures().get(BACKGROUND_PONIES, uuid))); } @Override public void removePony(Identifier resource) { poniesCache.invalidate(resource); + defaultedPoniesCache.invalidate(resource); } public void clearCache() { MineLittlePony.logger.info("Flushed {} cached ponies.", poniesCache.size()); poniesCache.invalidateAll(); + defaultedPoniesCache.invalidateAll(); } @Override diff --git a/src/main/java/com/minelittlepony/client/render/EquineRenderManager.java b/src/main/java/com/minelittlepony/client/render/EquineRenderManager.java index 08a0bf93..1a390c48 100644 --- a/src/main/java/com/minelittlepony/client/render/EquineRenderManager.java +++ b/src/main/java/com/minelittlepony/client/render/EquineRenderManager.java @@ -8,7 +8,6 @@ import com.minelittlepony.api.pony.network.fabric.PonyDataCallback; import com.minelittlepony.client.MineLittlePony; import com.minelittlepony.client.model.IPonyModel; import com.minelittlepony.client.model.ModelWrapper; -import com.minelittlepony.client.pony.Pony; import com.minelittlepony.client.transform.PonyPosture; import com.minelittlepony.mson.api.ModelKey; import com.minelittlepony.util.MathUtil; @@ -168,7 +167,7 @@ public class EquineRenderManager