Modularise and move npc textures to their own package

This commit is contained in:
Sollace 2022-12-12 16:22:52 +00:00
parent 7ed780ed70
commit b4b98ccfe6
7 changed files with 122 additions and 24 deletions

View file

@ -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<T extends MobEntity & VillagerDataContainer>
public AbstractNpcRenderer(EntityRendererFactory.Context context, String type, TextureSupplier<String> 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);
}

View file

@ -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<VillagerEntity> {

View file

@ -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<ZombieVillagerEntity> {

View file

@ -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<T extends LivingEntity> implements TextureSupplier<T> {
private final TextureSupplier<T> fallback;
private final Map<String, Entry> customNameCache = new HashMap<>();
public CustomPonyTextures(TextureSupplier<T> 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;
}
}
}

View file

@ -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<T extends LivingEntity & VillagerDataContainer> implem
private final Map<String, Identifier> 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<T extends LivingEntity & VillagerDataContainer> implem
public PonyTextures(TextureSupplier<String> 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<Identifier> 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());

View file

@ -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<T extends LivingEntity & VillagerDataContainer> implements TextureSupplier<T> {
private final TextureSupplier<T> fallback;
private final Identifier egg;
private final Identifier egg2;
public SillyPonyTextures(TextureSupplier<T> fallback, TextureSupplier<String> 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);
}
}

View file

@ -1,4 +1,4 @@
package com.minelittlepony.client.render.entity.npc;
package com.minelittlepony.client.render.entity.npc.textures;
import net.minecraft.util.Identifier;