MineLittlePony/src/main/java/com/minelittlepony/client/PonyManagerImpl.java

136 lines
5.1 KiB
Java
Raw Normal View History

package com.minelittlepony.client;
2015-08-01 18:36:33 -04:00
import com.google.common.base.MoreObjects;
import com.google.common.cache.*;
import com.minelittlepony.api.config.PonyConfig;
import com.minelittlepony.api.config.PonyLevel;
import com.minelittlepony.api.events.PonySkinResolver;
import com.minelittlepony.api.pony.*;
import com.minelittlepony.client.render.blockentity.skull.PonySkullRenderer;
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;
2023-09-26 15:59:07 +01:00
import net.minecraft.client.MinecraftClient;
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.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
2023-09-26 15:59:07 +01:00
public class PonyManagerImpl implements PonyManager, SimpleSynchronousResourceReloadListener {
2024-06-04 23:43:55 +01:00
private static final Identifier ID = MineLittlePony.id("background_ponies");
private final PonyConfig config;
private final LoadingCache<Identifier, Pony> defaultedPoniesCache = CacheBuilder.newBuilder()
.expireAfterAccess(30, TimeUnit.SECONDS)
2023-09-26 15:59:07 +01:00
.build(CacheLoader.from(resource -> new Pony(resource, PonyDataLoader.parse(resource, true))));
private final LoadingCache<Identifier, Pony> poniesCache = CacheBuilder.newBuilder()
.expireAfterAccess(30, TimeUnit.SECONDS)
2023-09-26 15:59:07 +01:00
.build(CacheLoader.from(resource -> new Pony(resource, PonyDataLoader.parse(resource, false))));
public PonyManagerImpl(PonyConfig config) {
this.config = config;
Instance.instance = this;
2015-08-01 18:36:33 -04:00
}
private Pony loadPony(Identifier resource, boolean defaulted) {
2022-12-10 16:45:38 +00:00
try {
return (defaulted ? defaultedPoniesCache : poniesCache).get(resource);
2022-12-10 16:45:38 +00:00
} catch (ExecutionException e) {
return new Pony(resource, PonyDataLoader.NULL);
2022-12-10 16:45:38 +00:00
}
}
@Override
2023-09-26 15:59:07 +01:00
public Pony getPony(PlayerEntity player) {
final UUID id = player instanceof ForcedPony ? null : player.getGameProfile() == null ? player.getUuid() : player.getGameProfile().getId();
2024-05-04 19:43:24 +01:00
@Nullable
Identifier skin = getSkin(player);
2024-05-04 19:43:24 +01:00
if (skin != null) {
skin = MoreObjects.firstNonNull(PonySkinResolver.EVENT.invoker().onPonySkinResolving(player, s -> getPony(s, id), skin), skin);
}
return getPony(skin, id);
}
@Override
2023-09-26 15:59:07 +01:00
public Optional<Pony> getPony(LivingEntity entity) {
if (entity instanceof PlayerEntity player) {
return Optional.of(getPony(player));
}
2024-05-04 19:43:24 +01:00
@Nullable
2023-09-26 15:59:07 +01:00
Identifier skin = getSkin(entity);
2024-05-04 19:43:24 +01:00
if (skin != null) {
skin = MoreObjects.firstNonNull(PonySkinResolver.EVENT.invoker().onPonySkinResolving(entity, s -> getPony(s, null), skin), skin);
}
2023-09-26 15:59:07 +01:00
return skin == null ? Optional.empty() : Optional.of(getPony(skin, null));
}
2019-03-23 20:17:46 +02:00
@Override
2023-09-26 15:59:07 +01:00
public Pony getPony(@Nullable Identifier resource, @Nullable UUID uuid) {
if (resource == null) {
return uuid == null ? loadPony(DefaultSkinHelper.getTexture(), true) : getBackgroundPony(uuid);
}
2023-09-26 15:59:07 +01:00
Pony pony = loadPony(resource, false);
2023-09-26 15:59:07 +01:00
if (uuid != null && PonyConfig.getInstance().ponyLevel.get() == PonyLevel.PONIES && pony.metadata().race().isHuman()) {
2018-04-24 17:12:23 +02:00
return getBackgroundPony(uuid);
}
return pony;
2017-06-16 01:41:36 -04:00
}
2022-12-10 16:45:38 +00:00
@Override
2023-09-26 15:59:07 +01:00
public Pony getBackgroundPony(@Nullable UUID uuid) {
if (config.ponyLevel.get() == PonyLevel.PONIES) {
2023-12-08 17:23:34 +00:00
return loadPony(MineLittlePony.getInstance().getVariatedTextures().get(VariatedTextureSupplier.BACKGROUND_PONIES_POOL, uuid).orElse(DefaultSkinHelper.getSkinTextures(uuid).texture()), true);
2023-09-26 15:59:07 +01:00
}
2023-12-08 17:23:34 +00:00
return loadPony(DefaultSkinHelper.getSkinTextures(uuid).texture(), true);
}
2022-12-10 16:45:38 +00:00
@Nullable
2023-09-26 15:59:07 +01:00
private Identifier getSkin(LivingEntity entity) {
if (entity instanceof PlayerEntity player) {
if (player.getGameProfile() != null && player instanceof AbstractClientPlayerEntity clientPlayer) {
2023-12-08 17:23:34 +00:00
return clientPlayer.getSkinTextures().texture();
2023-09-26 15:59:07 +01:00
}
} else {
if (MineLittlePony.getInstance().getRenderDispatcher().getPonyRenderer(entity) != null) {
return MinecraftClient.getInstance().getEntityRenderDispatcher().getRenderer(entity).getTexture(entity);
}
2015-08-01 18:36:33 -04:00
}
2018-04-24 17:12:23 +02:00
2022-12-10 16:45:38 +00:00
return null;
}
2015-08-01 18:36:33 -04:00
public void removePony(Identifier resource) {
poniesCache.invalidate(resource);
defaultedPoniesCache.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();
defaultedPoniesCache.invalidateAll();
}
2016-08-22 17:32:03 -04:00
@Override
public void reload(ResourceManager manager) {
clearCache();
PonySkullRenderer.INSTANCE.reload();
2019-05-27 17:59:15 +02:00
}
@Override
public Identifier getFabricId() {
return ID;
}
2015-08-01 18:36:33 -04:00
}