From b4b98ccfe64e3fec98d291f68b234a1a3c523c73 Mon Sep 17 00:00:00 2001 From: Sollace Date: Mon, 12 Dec 2022 16:22:52 +0000 Subject: [PATCH] Modularise and move npc textures to their own package --- .../entity/npc/AbstractNpcRenderer.java | 4 +- .../entity/npc/VillagerPonyRenderer.java | 2 + .../entity/npc/ZomponyVillagerRenderer.java | 2 + .../npc/textures/CustomPonyTextures.java | 86 +++++++++++++++++++ .../npc/{ => textures}/PonyTextures.java | 23 +---- .../npc/textures/SillyPonyTextures.java | 27 ++++++ .../npc/{ => textures}/TextureSupplier.java | 2 +- 7 files changed, 122 insertions(+), 24 deletions(-) create mode 100644 src/main/java/com/minelittlepony/client/render/entity/npc/textures/CustomPonyTextures.java rename src/main/java/com/minelittlepony/client/render/entity/npc/{ => textures}/PonyTextures.java (78%) create mode 100644 src/main/java/com/minelittlepony/client/render/entity/npc/textures/SillyPonyTextures.java rename src/main/java/com/minelittlepony/client/render/entity/npc/{ => textures}/TextureSupplier.java (88%) diff --git a/src/main/java/com/minelittlepony/client/render/entity/npc/AbstractNpcRenderer.java b/src/main/java/com/minelittlepony/client/render/entity/npc/AbstractNpcRenderer.java index 8b9335db..f6e1529a 100644 --- a/src/main/java/com/minelittlepony/client/render/entity/npc/AbstractNpcRenderer.java +++ b/src/main/java/com/minelittlepony/client/render/entity/npc/AbstractNpcRenderer.java @@ -13,6 +13,8 @@ import com.minelittlepony.api.pony.meta.Race; import com.minelittlepony.api.pony.meta.Wearable; import com.minelittlepony.client.model.*; import com.minelittlepony.client.render.entity.PonyRenderer; +import com.minelittlepony.client.render.entity.npc.textures.*; + import java.util.HashMap; import java.util.Map; @@ -29,7 +31,7 @@ abstract class AbstractNpcRenderer public AbstractNpcRenderer(EntityRendererFactory.Context context, String type, TextureSupplier formatter) { super(context, ModelType.getPlayerModel(Race.EARTH).getKey(false)); entityType = type; - baseTextures = new PonyTextures<>(formatter); + baseTextures = new SillyPonyTextures<>(new CustomPonyTextures<>(new PonyTextures<>(formatter)), formatter); clothing = new NpcClothingFeature<>(this, entityType); addFeature(clothing); } diff --git a/src/main/java/com/minelittlepony/client/render/entity/npc/VillagerPonyRenderer.java b/src/main/java/com/minelittlepony/client/render/entity/npc/VillagerPonyRenderer.java index 9ee0ac14..3857e0af 100644 --- a/src/main/java/com/minelittlepony/client/render/entity/npc/VillagerPonyRenderer.java +++ b/src/main/java/com/minelittlepony/client/render/entity/npc/VillagerPonyRenderer.java @@ -6,6 +6,8 @@ import net.minecraft.entity.passive.VillagerEntity; import net.minecraft.util.math.MathHelper; import com.minelittlepony.client.model.ClientPonyModel; +import com.minelittlepony.client.render.entity.npc.textures.PonyTextures; +import com.minelittlepony.client.render.entity.npc.textures.TextureSupplier; public class VillagerPonyRenderer extends AbstractNpcRenderer { diff --git a/src/main/java/com/minelittlepony/client/render/entity/npc/ZomponyVillagerRenderer.java b/src/main/java/com/minelittlepony/client/render/entity/npc/ZomponyVillagerRenderer.java index 0b014d27..7a5d6988 100644 --- a/src/main/java/com/minelittlepony/client/render/entity/npc/ZomponyVillagerRenderer.java +++ b/src/main/java/com/minelittlepony/client/render/entity/npc/ZomponyVillagerRenderer.java @@ -7,6 +7,8 @@ import net.minecraft.entity.mob.ZombieVillagerEntity; import com.minelittlepony.client.model.ClientPonyModel; import com.minelittlepony.client.model.IMobModel; +import com.minelittlepony.client.render.entity.npc.textures.PonyTextures; +import com.minelittlepony.client.render.entity.npc.textures.TextureSupplier; public class ZomponyVillagerRenderer extends AbstractNpcRenderer { diff --git a/src/main/java/com/minelittlepony/client/render/entity/npc/textures/CustomPonyTextures.java b/src/main/java/com/minelittlepony/client/render/entity/npc/textures/CustomPonyTextures.java new file mode 100644 index 00000000..f096c928 --- /dev/null +++ b/src/main/java/com/minelittlepony/client/render/entity/npc/textures/CustomPonyTextures.java @@ -0,0 +1,86 @@ +package com.minelittlepony.client.render.entity.npc.textures; + +import net.minecraft.block.entity.SkullBlockEntity; +import net.minecraft.entity.LivingEntity; +import net.minecraft.util.Identifier; +import org.jetbrains.annotations.Nullable; + +import com.minelittlepony.api.config.PonyConfig; +import com.minelittlepony.api.config.PonyLevel; +import com.minelittlepony.api.pony.IPony; +import com.minelittlepony.client.MineLittlePony; +import com.minelittlepony.client.SkinsProxy; +import com.minelittlepony.client.pony.PonyManager; +import com.mojang.authlib.GameProfile; + +import java.util.*; + +public class CustomPonyTextures implements TextureSupplier { + + private final TextureSupplier fallback; + private final Map customNameCache = new HashMap<>(); + + public CustomPonyTextures(TextureSupplier fallback) { + this.fallback = fallback; + } + + @Override + public Identifier supplyTexture(T entity) { + Identifier override = getCustomTexture(entity); + if (override != null) { + return override; + } + return fallback.supplyTexture(entity); + } + + @Nullable + private Identifier getCustomTexture(T entity) { + if (!entity.hasCustomName()) { + return null; + } + + String key = entity.getCustomName().getString() + "_" + entity.getUuidAsString(); + + if (!customNameCache.containsKey(key)) { + customNameCache.put(key, new Entry(entity)); + } + return customNameCache.get(key).getTexture(); + } + + class Entry { + private final UUID uuid; + private final Identifier texture; + + @Nullable + private GameProfile profile; + + Entry(T entity) { + uuid = entity.getUuid(); + texture = MineLittlePony.getInstance().getVariatedTextures() + .get(PonyManager.BACKGROUND_PONIES) + .getByName(entity.getCustomName().getString(), uuid) + .orElse(null); + + if (texture == null) { + SkullBlockEntity.loadProperties(new GameProfile(null, entity.getCustomName().getString()), resolved -> { + profile = resolved; + }); + } + } + + public Identifier getTexture() { + if (profile != null) { + Identifier skin = SkinsProxy.instance.getSkinTexture(profile); + if (skin != null) { + if (IPony.getManager().getPony(skin).race().isHuman()) { + if (PonyConfig.getInstance().ponyLevel.get() == PonyLevel.PONIES) { + return IPony.getManager().getBackgroundPony(uuid).texture(); + } + } + return skin; + } + } + return texture; + } + } +} diff --git a/src/main/java/com/minelittlepony/client/render/entity/npc/PonyTextures.java b/src/main/java/com/minelittlepony/client/render/entity/npc/textures/PonyTextures.java similarity index 78% rename from src/main/java/com/minelittlepony/client/render/entity/npc/PonyTextures.java rename to src/main/java/com/minelittlepony/client/render/entity/npc/textures/PonyTextures.java index 2f6d10d5..adbf858c 100644 --- a/src/main/java/com/minelittlepony/client/render/entity/npc/PonyTextures.java +++ b/src/main/java/com/minelittlepony/client/render/entity/npc/textures/PonyTextures.java @@ -1,4 +1,4 @@ -package com.minelittlepony.client.render.entity.npc; +package com.minelittlepony.client.render.entity.npc.textures; import net.minecraft.client.MinecraftClient; import net.minecraft.entity.LivingEntity; @@ -9,8 +9,6 @@ import net.minecraft.village.VillagerDataContainer; import net.minecraft.village.VillagerProfession; import net.minecraft.village.VillagerType; -import com.minelittlepony.client.MineLittlePony; -import com.minelittlepony.client.pony.PonyManager; import com.minelittlepony.util.ResourceUtil; import java.util.*; @@ -26,9 +24,6 @@ public class PonyTextures implem private final Map cache = new HashMap<>(); - private final Identifier egg; - private final Identifier egg2; - private final ResourceManager resourceManager = MinecraftClient.getInstance().getResourceManager(); /** @@ -41,26 +36,10 @@ public class PonyTextures implem public PonyTextures(TextureSupplier formatter) { this.formatter = formatter; this.fallback = formatter.supplyTexture("villager_pony"); - this.egg = formatter.supplyTexture("silly_pony"); - this.egg2 = formatter.supplyTexture("tiny_silly_pony"); } @Override public Identifier supplyTexture(T entity) { - if (isBestPony(entity)) { - return entity.isBaby() ? egg2 : egg; - } - - if (entity.hasCustomName()) { - Optional override = MineLittlePony.getInstance().getVariatedTextures() - .get(PonyManager.BACKGROUND_PONIES) - .getByName(entity.getCustomName().getString(), entity.getUuid()); - - if (override.isPresent()) { - return override.get(); - } - } - VillagerData t = entity.getVillagerData(); return getTexture(t.getType(), t.getProfession()); diff --git a/src/main/java/com/minelittlepony/client/render/entity/npc/textures/SillyPonyTextures.java b/src/main/java/com/minelittlepony/client/render/entity/npc/textures/SillyPonyTextures.java new file mode 100644 index 00000000..f348a04e --- /dev/null +++ b/src/main/java/com/minelittlepony/client/render/entity/npc/textures/SillyPonyTextures.java @@ -0,0 +1,27 @@ +package com.minelittlepony.client.render.entity.npc.textures; + +import net.minecraft.entity.LivingEntity; +import net.minecraft.util.Identifier; +import net.minecraft.village.VillagerDataContainer; + +public class SillyPonyTextures implements TextureSupplier { + + private final TextureSupplier fallback; + + private final Identifier egg; + private final Identifier egg2; + + public SillyPonyTextures(TextureSupplier fallback, TextureSupplier formatter) { + this.fallback = fallback; + this.egg = formatter.supplyTexture("silly_pony"); + this.egg2 = formatter.supplyTexture("tiny_silly_pony"); + } + + @Override + public Identifier supplyTexture(T entity) { + if (PonyTextures.isBestPony(entity)) { + return entity.isBaby() ? egg2 : egg; + } + return fallback.supplyTexture(entity); + } +} diff --git a/src/main/java/com/minelittlepony/client/render/entity/npc/TextureSupplier.java b/src/main/java/com/minelittlepony/client/render/entity/npc/textures/TextureSupplier.java similarity index 88% rename from src/main/java/com/minelittlepony/client/render/entity/npc/TextureSupplier.java rename to src/main/java/com/minelittlepony/client/render/entity/npc/textures/TextureSupplier.java index 9a140988..cc4b4548 100644 --- a/src/main/java/com/minelittlepony/client/render/entity/npc/TextureSupplier.java +++ b/src/main/java/com/minelittlepony/client/render/entity/npc/textures/TextureSupplier.java @@ -1,4 +1,4 @@ -package com.minelittlepony.client.render.entity.npc; +package com.minelittlepony.client.render.entity.npc.textures; import net.minecraft.util.Identifier;