From 7152880131b86c03fad96aa144fa31be79e216d2 Mon Sep 17 00:00:00 2001 From: Goodbird Date: Wed, 24 Jan 2024 22:53:31 +0300 Subject: [PATCH] Added utils classes for texture and animation management --- .../utils/AnimationFileUtil.java | 30 +++++ .../cnpcgeckoaddon/utils/NpcTextureUtils.java | 125 ++++++++++++++++++ 2 files changed, 155 insertions(+) create mode 100644 src/main/java/com/goodbird/cnpcgeckoaddon/utils/AnimationFileUtil.java create mode 100644 src/main/java/com/goodbird/cnpcgeckoaddon/utils/NpcTextureUtils.java diff --git a/src/main/java/com/goodbird/cnpcgeckoaddon/utils/AnimationFileUtil.java b/src/main/java/com/goodbird/cnpcgeckoaddon/utils/AnimationFileUtil.java new file mode 100644 index 0000000..318b010 --- /dev/null +++ b/src/main/java/com/goodbird/cnpcgeckoaddon/utils/AnimationFileUtil.java @@ -0,0 +1,30 @@ +package com.goodbird.cnpcgeckoaddon.utils; + +import net.minecraft.util.ResourceLocation; +import software.bernie.geckolib3.core.builder.Animation; +import software.bernie.geckolib3.file.AnimationFile; +import software.bernie.geckolib3.resource.GeckoLibCache; + +import java.util.List; +import java.util.Vector; + +public class AnimationFileUtil { + public static List getAnimationList(String animFileName) { + Vector list = new Vector<>(); + AnimationFile file = GeckoLibCache.getInstance().getAnimations().get(new ResourceLocation(animFileName)); + if (file != null) { + for (Animation anim : file.getAllAnimations()) { + list.add(anim.animationName); + } + } + return list; + } + + public static List getAnimationFileList() { + Vector list = new Vector<>(); + for (ResourceLocation resLoc : GeckoLibCache.getInstance().getAnimations().keySet()) { + list.add(resLoc.toString()); + } + return list; + } +} diff --git a/src/main/java/com/goodbird/cnpcgeckoaddon/utils/NpcTextureUtils.java b/src/main/java/com/goodbird/cnpcgeckoaddon/utils/NpcTextureUtils.java new file mode 100644 index 0000000..f56d78e --- /dev/null +++ b/src/main/java/com/goodbird/cnpcgeckoaddon/utils/NpcTextureUtils.java @@ -0,0 +1,125 @@ +package com.goodbird.cnpcgeckoaddon.utils; + +import com.mojang.authlib.minecraft.MinecraftProfileTexture; +import net.minecraft.client.Minecraft; +import net.minecraft.client.entity.AbstractClientPlayer; +import net.minecraft.client.renderer.texture.TextureManager; +import net.minecraft.client.resources.IResource; +import net.minecraft.client.resources.SkinManager; +import net.minecraft.util.ResourceLocation; +import noppes.npcs.client.ClientCacheHandler; +import noppes.npcs.client.ImageDownloadAlt; +import noppes.npcs.client.renderer.ImageBufferDownloadAlt; +import noppes.npcs.client.renderer.RenderNPCInterface; +import noppes.npcs.entity.EntityCustomNpc; +import noppes.npcs.entity.EntityNPCInterface; +import com.goodbird.cnpcgeckoaddon.mixin.impl.MixinRenderNPCInterface; + +import javax.imageio.ImageIO; +import java.awt.image.BufferedImage; +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.nio.charset.StandardCharsets; +import java.security.MessageDigest; +import java.util.Map; + +public class NpcTextureUtils { + public static ResourceLocation getNpcTexture(EntityNPCInterface npc){ + if (npc.textureLocation == null) { + if (npc.display.skinType == 0) { + if (npc instanceof EntityCustomNpc && ((EntityCustomNpc) npc).modelData.entityClass == null) { + if (!(npc.display.texture).isEmpty()) { + try { + npc.textureLocation = adjustLocalTexture(npc, new ResourceLocation(npc.display.texture)); + } catch (IOException ignored) { + } + } + } else { + npc.textureLocation = new ResourceLocation(npc.display.texture); + } + } else if(RenderNPCInterface.LastTextureTick < 5) { //fixes request flood somewhat + return AbstractClientPlayer.locationStevePng; + } else if(npc.display.skinType == 1 && npc.display.playerProfile != null) { + Minecraft minecraft = Minecraft.getMinecraft(); + Map map = minecraft.func_152342_ad().func_152788_a(npc.display.playerProfile); + if (map.containsKey(MinecraftProfileTexture.Type.SKIN)){ + npc.textureLocation = minecraft.func_152342_ad().func_152792_a((MinecraftProfileTexture)map.get(MinecraftProfileTexture.Type.SKIN), MinecraftProfileTexture.Type.SKIN); + } + RenderNPCInterface.LastTextureTick = 0; + } else if (npc.display.skinType == 2 || npc.display.skinType == 3) { + try { + MessageDigest digest = MessageDigest.getInstance("MD5"); + byte[] hash = digest.digest(npc.display.url.getBytes(StandardCharsets.UTF_8)); + StringBuilder sb = new StringBuilder(2*hash.length); + for (byte b : hash) { + sb.append(String.format("%02x", b&0xff)); + } + if (npc.display.skinType == 2) { + npc.textureLocation = new ResourceLocation("skins/" + sb); + ClientCacheHandler.getNPCTexture(npc.display.url, false, npc.textureLocation); + } else { + npc.textureLocation = new ResourceLocation("skins64/" + sb); + ClientCacheHandler.getNPCTexture(npc.display.url, true, npc.textureLocation); + } + RenderNPCInterface.LastTextureTick = 0; + } catch(Exception ignored){} + } else { + return AbstractClientPlayer.locationStevePng; + } + } + return npc.textureLocation; + } + + private static ResourceLocation adjustLocalTexture(EntityNPCInterface npc, ResourceLocation location) throws IOException { + InputStream inputstream = null; + + ResourceLocation var22; + try { + TextureManager texturemanager = Minecraft.getMinecraft().getTextureManager(); + texturemanager.deleteTexture(location); + IResource iresource = Minecraft.getMinecraft().getResourceManager().getResource(location); + inputstream = iresource.getInputStream(); + BufferedImage bufferedimage = ImageIO.read(inputstream); + int totalWidth = bufferedimage.getWidth(); + int totalHeight = bufferedimage.getHeight(); + if (totalHeight > 32 && npc.display.modelType == 0) { + bufferedimage = bufferedimage.getSubimage(0, 0, totalWidth, 32); + } + + ImageDownloadAlt object = new ImageDownloadAlt((File)null, npc.display.texture, SkinManager.field_152793_a, new ImageBufferDownloadAlt(false)); + object.setBufferedImage(bufferedimage); + + try { + MessageDigest digest = MessageDigest.getInstance("MD5"); + byte[] hash = digest.digest(npc.display.texture.getBytes("UTF-8")); + StringBuilder sb = new StringBuilder(2 * hash.length); + byte[] var13 = hash; + int var14 = hash.length; + + for(int var15 = 0; var15 < var14; ++var15) { + byte b = var13[var15]; + sb.append(String.format("%02x", b & 255)); + } + + if (totalHeight > 32 && npc.display.modelType == 0) { + location = new ResourceLocation("skin/" + sb.toString()); + } else { + location = new ResourceLocation("skin64/" + sb.toString()); + } + } catch (Exception var20) { + } + + texturemanager.loadTexture(location, object); + var22 = location; + } finally { + if (inputstream != null) { + inputstream.close(); + } + + } + + return var22; + } + +}