From 7ed780ed70985ad2c9ff9e343daae42a4d25acb2 Mon Sep 17 00:00:00 2001 From: Sollace Date: Mon, 12 Dec 2022 15:08:49 +0000 Subject: [PATCH] Villager skinning --- .../client/pony/VariatedTextureSupplier.java | 19 +++++++++++++++++-- .../render/entity/npc/PonyTextures.java | 16 +++++++++++++--- 2 files changed, 30 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/minelittlepony/client/pony/VariatedTextureSupplier.java b/src/main/java/com/minelittlepony/client/pony/VariatedTextureSupplier.java index 13333679..293fce8a 100644 --- a/src/main/java/com/minelittlepony/client/pony/VariatedTextureSupplier.java +++ b/src/main/java/com/minelittlepony/client/pony/VariatedTextureSupplier.java @@ -26,7 +26,7 @@ public class VariatedTextureSupplier implements SimpleSynchronousResourceReloadL return ID; } - private SkinList get(Identifier id) { + public SkinList get(Identifier id) { return entries.computeIfAbsent(id, SkinList::new); } @@ -38,8 +38,9 @@ public class VariatedTextureSupplier implements SimpleSynchronousResourceReloadL return get(poolId, entity.getUuid()); } - private static class SkinList { + public static final class SkinList { private final List textures = new ArrayList<>(); + private final Map> names = new HashMap<>(); private final Identifier id; @@ -56,6 +57,20 @@ public class VariatedTextureSupplier implements SimpleSynchronousResourceReloadL return Optional.ofNullable(textures.get(MathUtil.mod(uuid.hashCode(), textures.size()))); } + public Optional getByName(String name, UUID uuid) { + List options = names.computeIfAbsent(name.toLowerCase(Locale.ROOT).replace(' ', '_') + ".png", id -> { + return textures.stream().filter(texture -> { + return texture.getPath().endsWith(id); + }).toList(); + }); + + if (options.isEmpty()) { + return Optional.empty(); + } + + return Optional.ofNullable(options.get(MathUtil.mod(uuid.hashCode(), options.size()))); + } + public void reloadAll(ResourceManager resourceManager) { textures.clear(); textures.addAll(resourceManager.findResources(id.getPath(), path -> path.getPath().endsWith(".png")).keySet()); diff --git a/src/main/java/com/minelittlepony/client/render/entity/npc/PonyTextures.java b/src/main/java/com/minelittlepony/client/render/entity/npc/PonyTextures.java index 06de9be0..2f6d10d5 100644 --- a/src/main/java/com/minelittlepony/client/render/entity/npc/PonyTextures.java +++ b/src/main/java/com/minelittlepony/client/render/entity/npc/PonyTextures.java @@ -9,11 +9,11 @@ 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.HashMap; -import java.util.Map; -import java.util.Optional; +import java.util.*; /** * Cached pool of villager textures. @@ -51,6 +51,16 @@ public class PonyTextures implem 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());