2023-09-25 21:07:09 +01:00
|
|
|
package com.minelittlepony.client;
|
2015-08-01 18:36:33 -04:00
|
|
|
|
2024-04-30 18:32:05 +01:00
|
|
|
import com.google.common.base.MoreObjects;
|
2022-12-08 21:19:40 +00:00
|
|
|
import com.google.common.cache.*;
|
2022-12-11 00:38:00 +00:00
|
|
|
import com.minelittlepony.api.config.PonyConfig;
|
|
|
|
import com.minelittlepony.api.config.PonyLevel;
|
2024-04-30 18:32:05 +01:00
|
|
|
import com.minelittlepony.api.events.PonySkinResolver;
|
2023-09-25 21:07:09 +01:00
|
|
|
import com.minelittlepony.api.pony.*;
|
2022-06-11 20:46:24 +02:00
|
|
|
import com.minelittlepony.client.render.blockentity.skull.PonySkullRenderer;
|
2022-12-11 00:38:00 +00:00
|
|
|
|
2021-06-10 19:32:21 +02:00
|
|
|
import org.jetbrains.annotations.Nullable;
|
2019-03-23 20:17:46 +02:00
|
|
|
|
2022-06-17 23:42:17 +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;
|
2022-12-08 20:53:30 +00:00
|
|
|
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
|
|
|
|
2022-12-08 20:53:30 +00:00
|
|
|
import java.util.Optional;
|
2016-11-24 23:40:19 -05:00
|
|
|
import java.util.UUID;
|
2019-07-13 20:11:14 -04:00
|
|
|
import java.util.concurrent.ExecutionException;
|
2019-06-24 20:08:51 -04:00
|
|
|
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");
|
2017-06-12 23:55:50 -04:00
|
|
|
|
2019-03-23 19:48:20 +02:00
|
|
|
private final PonyConfig config;
|
2015-11-17 00:17:35 -05:00
|
|
|
|
2023-09-25 21:07:09 +01:00
|
|
|
private final LoadingCache<Identifier, Pony> defaultedPoniesCache = CacheBuilder.newBuilder()
|
2022-12-08 21:19:40 +00:00
|
|
|
.expireAfterAccess(30, TimeUnit.SECONDS)
|
2023-09-26 15:59:07 +01:00
|
|
|
.build(CacheLoader.from(resource -> new Pony(resource, PonyDataLoader.parse(resource, true))));
|
2022-12-08 21:19:40 +00:00
|
|
|
|
2023-09-25 21:07:09 +01:00
|
|
|
private final LoadingCache<Identifier, Pony> poniesCache = CacheBuilder.newBuilder()
|
2019-06-24 20:08:51 -04:00
|
|
|
.expireAfterAccess(30, TimeUnit.SECONDS)
|
2023-09-26 15:59:07 +01:00
|
|
|
.build(CacheLoader.from(resource -> new Pony(resource, PonyDataLoader.parse(resource, false))));
|
2015-11-17 00:17:35 -05:00
|
|
|
|
2023-09-25 21:07:09 +01:00
|
|
|
public PonyManagerImpl(PonyConfig config) {
|
2015-11-17 00:17:35 -05:00
|
|
|
this.config = config;
|
2022-12-11 00:38:00 +00:00
|
|
|
Instance.instance = this;
|
2015-08-01 18:36:33 -04:00
|
|
|
}
|
|
|
|
|
2023-09-25 21:07:09 +01:00
|
|
|
private Pony loadPony(Identifier resource, boolean defaulted) {
|
2022-12-10 16:45:38 +00:00
|
|
|
try {
|
2023-09-25 21:07:09 +01:00
|
|
|
return (defaulted ? defaultedPoniesCache : poniesCache).get(resource);
|
2022-12-10 16:45:38 +00:00
|
|
|
} catch (ExecutionException e) {
|
2023-09-25 21:07:09 +01:00
|
|
|
return new Pony(resource, PonyDataLoader.NULL);
|
2022-12-10 16:45:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-08 20:53:30 +00:00
|
|
|
@Override
|
2023-09-26 15:59:07 +01:00
|
|
|
public Pony getPony(PlayerEntity player) {
|
2024-04-30 18:32:05 +01:00
|
|
|
final UUID id = player instanceof ForcedPony ? null : player.getGameProfile() == null ? player.getUuid() : player.getGameProfile().getId();
|
2024-05-04 19:43:24 +01:00
|
|
|
@Nullable
|
2024-04-30 18:32:05 +01:00
|
|
|
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);
|
|
|
|
}
|
2024-04-30 18:32:05 +01:00
|
|
|
return getPony(skin, id);
|
2023-09-25 21:07:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2023-09-26 15:59:07 +01:00
|
|
|
public Optional<Pony> getPony(LivingEntity entity) {
|
2022-12-08 20:53:30 +00:00
|
|
|
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));
|
2022-12-08 20:53:30 +00:00
|
|
|
}
|
|
|
|
|
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);
|
2018-08-26 18:30:04 -04:00
|
|
|
}
|
2018-04-29 20:19:30 +02:00
|
|
|
|
2023-09-26 15:59:07 +01:00
|
|
|
Pony pony = loadPony(resource, false);
|
2021-05-16 16:20:02 +02:00
|
|
|
|
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-08 21:19:40 +00:00
|
|
|
}
|
|
|
|
|
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;
|
2018-04-26 15:56:36 +02:00
|
|
|
}
|
2015-08-01 18:36:33 -04:00
|
|
|
|
2019-06-24 20:08:51 -04:00
|
|
|
public void removePony(Identifier resource) {
|
|
|
|
poniesCache.invalidate(resource);
|
2022-12-08 21:19:40 +00:00
|
|
|
defaultedPoniesCache.invalidate(resource);
|
2015-08-01 18:36:33 -04:00
|
|
|
}
|
2016-08-22 17:32:03 -04:00
|
|
|
|
2020-04-04 00:24:08 +02:00
|
|
|
public void clearCache() {
|
|
|
|
MineLittlePony.logger.info("Flushed {} cached ponies.", poniesCache.size());
|
|
|
|
poniesCache.invalidateAll();
|
2022-12-08 21:19:40 +00:00
|
|
|
defaultedPoniesCache.invalidateAll();
|
2020-04-04 00:24:08 +02:00
|
|
|
}
|
|
|
|
|
2016-08-22 17:32:03 -04:00
|
|
|
@Override
|
2024-05-05 17:42:57 +01:00
|
|
|
public void reload(ResourceManager manager) {
|
2022-06-17 23:42:17 +02:00
|
|
|
clearCache();
|
2024-05-05 17:42:57 +01:00
|
|
|
PonySkullRenderer.INSTANCE.reload();
|
2019-05-27 17:59:15 +02:00
|
|
|
}
|
|
|
|
|
2019-07-03 14:16:03 +02:00
|
|
|
@Override
|
|
|
|
public Identifier getFabricId() {
|
|
|
|
return ID;
|
|
|
|
}
|
2015-08-01 18:36:33 -04:00
|
|
|
}
|