MineLittlePony/src/main/java/com/minelittlepony/client/pony/PonyManager.java

150 lines
4.6 KiB
Java
Raw Normal View History

2019-03-23 21:49:34 +02:00
package com.minelittlepony.client.pony;
2015-08-01 18:36:33 -04:00
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
2020-04-03 23:54:12 +02:00
import com.minelittlepony.api.pony.IPony;
import com.minelittlepony.api.pony.IPonyManager;
import com.minelittlepony.client.MineLittlePony;
import com.minelittlepony.client.render.IPonyRenderContext;
import com.minelittlepony.client.render.PonyRenderDispatcher;
import com.minelittlepony.client.render.blockentity.skull.PonySkullRenderer;
import com.minelittlepony.settings.PonyConfig;
import com.minelittlepony.settings.PonyLevel;
2021-06-10 19:32:21 +02:00
import org.jetbrains.annotations.Nullable;
2019-03-23 20:17:46 +02:00
import net.fabricmc.fabric.api.resource.SimpleSynchronousResourceReloadListener;
2019-05-27 17:59:15 +02:00
import net.minecraft.client.network.AbstractClientPlayerEntity;
import net.minecraft.client.util.DefaultSkinHelper;
import net.minecraft.resource.ResourceManager;
import net.minecraft.entity.Entity;
import net.minecraft.entity.LivingEntity;
2019-05-27 17:59:15 +02:00
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.util.Identifier;
2016-11-24 23:40:19 -05:00
import java.util.Optional;
2016-11-24 23:40:19 -05:00
import java.util.UUID;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
2015-08-01 18:36:33 -04:00
/**
* The PonyManager is responsible for reading and recoding all the pony data associated with an entity of skin.
*
*/
public class PonyManager implements IPonyManager, SimpleSynchronousResourceReloadListener {
private static final Identifier ID = new Identifier("minelittlepony", "background_ponies");
2022-06-18 18:08:46 +02:00
public static final Identifier BACKGROUND_PONIES = new Identifier("minelittlepony", "textures/entity/pony");
private final PonyConfig config;
private final LoadingCache<Identifier, IPony> poniesCache = CacheBuilder.newBuilder()
.expireAfterAccess(30, TimeUnit.SECONDS)
.build(CacheLoader.from(Pony::new));
public PonyManager(PonyConfig config) {
this.config = config;
2015-08-01 18:36:33 -04:00
}
@Override
public Optional<IPony> getPony(@Nullable Entity entity) {
if (entity instanceof PlayerEntity player) {
return Optional.of(getPony(player));
}
if (entity instanceof LivingEntity living) {
IPonyRenderContext<LivingEntity, ?> dispatcher = PonyRenderDispatcher.getInstance().getPonyRenderer(living);
if (dispatcher != null) {
return Optional.of(dispatcher.getEntityPony(living));
}
}
return Optional.empty();
}
2019-03-23 20:17:46 +02:00
@Override
2019-05-27 17:59:15 +02:00
public IPony getPony(Identifier resource) {
try {
return poniesCache.get(resource);
} catch (ExecutionException e) {
return new Pony(resource, PonyData.MEM_NULL);
}
}
2019-03-23 20:17:46 +02:00
@Override
2019-05-27 17:59:15 +02:00
public IPony getPony(PlayerEntity player) {
2019-07-03 22:06:00 -04:00
if (player.getGameProfile() == null) {
2019-06-05 14:54:38 +02:00
return getDefaultPony(player.getUuid());
}
2019-05-27 17:59:15 +02:00
Identifier skin = getSkin(player);
UUID uuid = player.getGameProfile().getId();
2019-07-03 22:06:00 -04:00
if (skin == null) {
return getDefaultPony(uuid);
}
if (player instanceof IPonyManager.ForcedPony) {
return getPony(skin);
}
return getPony(skin, uuid);
}
2019-03-23 20:17:46 +02:00
@Nullable
2019-07-03 22:06:00 -04:00
private Identifier getSkin(PlayerEntity player) {
2019-05-27 17:59:15 +02:00
if (player instanceof AbstractClientPlayerEntity) {
return ((AbstractClientPlayerEntity)player).getSkinTexture();
2019-03-23 20:17:46 +02:00
}
return null;
}
@Override
2019-05-27 17:59:15 +02:00
public IPony getPony(Identifier resource, UUID uuid) {
IPony pony = getPony(resource);
if (config.ponyLevel.get() == PonyLevel.PONIES && pony.metadata().getRace().isHuman()) {
2018-04-24 17:12:23 +02:00
return getBackgroundPony(uuid);
}
2018-04-24 17:12:23 +02:00
return pony;
2017-06-16 01:41:36 -04:00
}
2019-03-23 20:17:46 +02:00
@Override
public IPony getDefaultPony(UUID uuid) {
2019-07-11 19:20:13 +02:00
if (config.ponyLevel.get() != PonyLevel.PONIES) {
return ((Pony)getPony(DefaultSkinHelper.getTexture(uuid))).markDefaulted();
2015-08-01 18:36:33 -04:00
}
2018-04-24 17:12:23 +02:00
return getBackgroundPony(uuid);
2015-08-01 18:36:33 -04:00
}
2019-03-23 20:17:46 +02:00
@Override
public IPony getBackgroundPony(UUID uuid) {
return ((Pony)getPony(MineLittlePony.getInstance().getVariatedTextures().get(BACKGROUND_PONIES, uuid))).markDefaulted();
}
2015-08-01 18:36:33 -04:00
2019-03-23 20:17:46 +02:00
@Override
public void removePony(Identifier resource) {
poniesCache.invalidate(resource);
2015-08-01 18:36:33 -04:00
}
2016-08-22 17:32:03 -04:00
public void clearCache() {
MineLittlePony.logger.info("Flushed {} cached ponies.", poniesCache.size());
poniesCache.invalidateAll();
}
2016-08-22 17:32:03 -04:00
@Override
public void reload(ResourceManager var1) {
clearCache();
PonySkullRenderer.reload();
2019-05-27 17:59:15 +02:00
}
@Override
public Identifier getFabricId() {
return ID;
}
2015-08-01 18:36:33 -04:00
}