Change Pony into a record and track whether a IPony instance was returned as a default skin or not separate from the texture id

This commit is contained in:
Sollace 2022-12-08 21:19:40 +00:00
parent d7d0c71ca5
commit 84a78ce4b9
6 changed files with 40 additions and 65 deletions

View file

@ -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<IPony> {
@ -41,7 +42,9 @@ public interface IPony extends Comparable<IPony> {
/**
* 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.

View file

@ -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);

View file

@ -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

View file

@ -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<IPonyData> metadata;
private boolean defaulted;
Pony(Identifier resource, Memoize<IPonyData> data) {
texture = resource;
metadata = data;
}
public record Pony (
Identifier texture,
Memoize<IPonyData> 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());
}
}

View file

@ -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<Identifier, IPony> defaultedPoniesCache = CacheBuilder.newBuilder()
.expireAfterAccess(30, TimeUnit.SECONDS)
.build();
private final LoadingCache<Identifier, IPony> 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

View file

@ -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<T extends LivingEntity, M extends EntityModel<T
}
private void updateForEntity(IPony pony, Entity entity) {
if (pony.hasMetadata() && entity instanceof Pony.RegistrationHandler && ((Pony.RegistrationHandler)entity).shouldUpdateRegistration(pony)) {
if (pony.hasMetadata() && entity instanceof RegistrationHandler && ((RegistrationHandler)entity).shouldUpdateRegistration(pony)) {
entity.calculateDimensions();
PlayerEntity clientPlayer = MinecraftClient.getInstance().player;
@ -216,4 +215,8 @@ public class EquineRenderManager<T extends LivingEntity, M extends EntityModel<T
return y;
}
public interface RegistrationHandler {
boolean shouldUpdateRegistration(IPony pony);
}
}