粘兽
实现了加载外部的贴图,已经可以在药水UI上使用,

部分代码:


但是不知道如何让物品也可以使用到这样的贴图。
希望各位带哥指条明路。

roj234
本帖最后由 roj234 于 2019-5-13 19:47 编辑

复制代码
  1. Filename: Textureloader.java
复制代码
  1. Filename: WebTexture.java
  2. package mi.util.client.texture;

  3. import mi.MI;

  4. import java.awt.image.BufferedImage;
  5. import java.io.IOException;
  6. import java.io.InputStream;
  7. import net.minecraft.client.renderer.texture.AbstractTexture;
  8. import net.minecraft.client.renderer.texture.TextureUtil;
  9. import net.minecraft.client.resources.IResource;
  10. import net.minecraft.client.resources.IResourceManager;
  11. import net.minecraft.client.resources.data.TextureMetadataSection;
  12. import net.minecraft.util.ResourceLocation;
  13. import net.minecraftforge.fml.relauncher.Side;
  14. import net.minecraftforge.fml.relauncher.SideOnly;
  15. import org.apache.commons.io.IOUtils;

  16. @SideOnly(Side.CLIENT)
  17. public class WebTexture
  18.   extends AbstractTexture
  19. {
  20.   protected final ResourceLocation textureLocation;
  21.   private final InputStream is;

  22.   public WebTexture(ResourceLocation loc, InputStream image)
  23.   {
  24.     this.textureLocation = loc;
  25.     this.is = image;
  26.   }

  27.   public void loadTexture(IResourceManager man)
  28.     throws IOException
  29.   {
  30.     deleteGlTexture();

  31.     IResource res = null;
  32.     try
  33.     {
  34.       res = man.getResource(this.textureLocation);
  35.       BufferedImage img = TextureUtil.readBufferedImage(is);

  36.       boolean blur = false;
  37.       boolean clamp = false;
  38.       if (res.hasMetadata()) {
  39.         try
  40.         {
  41.           TextureMetadataSection meta = (TextureMetadataSection)res.getMetadata("texture");
  42.           if (meta != null)
  43.           {
  44.             blur = meta.getTextureBlur();
  45.             clamp = meta.getTextureClamp();
  46.           }
  47.         }
  48.         catch (RuntimeException e)
  49.         {
  50.           MI.logger().warn("Failed reading metadata of: "+this.textureLocation,e);
  51.         }
  52.       }
  53.       TextureUtil.uploadTextureImageAllocate(getGlTextureId(), img, blur, clamp);
  54.     }
  55.     finally
  56.     {
  57.       IOUtils.closeQuietly(res);
  58.     }
  59.   }
  60. }
复制代码
  1. package mi.util.client.texture;

  2. import mi.Config;
  3. import mi.util.ReflectionHelper;

  4. import net.minecraft.client.renderer.texture.TextureManager;
  5. import net.minecraft.client.resources.*;
  6. import net.minecraft.client.Minecraft;
  7. import net.minecraft.util.ResourceLocation;

  8. import java.io.*;
  9. import java.util.*;

  10. public final class TextureLoader implements IResourceManagerReloadListener{
  11.         private static TextureManager engine;
  12.         private static final Map<ResourceLocation, InputStream> data = new HashMap<ResourceLocation, InputStream>();
  13.         private static Map<String, FallbackResourceManager> resMap;
  14.         
  15.         public TextureLoader() {
  16.                 //engine = FMLClientHandler.instance().getClient().renderEngine;
  17.                 engine = Minecraft.getMinecraft().getTextureManager();
  18.                
  19.                 IReloadableResourceManager man;
  20.                 try{
  21.                         if(Config.isMDKEnviroment)
  22.                                 man = (IReloadableResourceManager)ReflectionHelper.getPrivateField(engine, "resourceManager");
  23.                         else
  24.                                 man = (IReloadableResourceManager)ReflectionHelper.getPrivateField(engine, "field_110582_d");
  25.                 }catch(Exception e){
  26.                         e.printStackTrace();
  27.                         throw new RuntimeException("TextureManager get failed");
  28.                 }
  29.                 man.registerReloadListener(this);
  30.                
  31.                 onResourceManagerReload(man);
  32.         }

  33.         public static InputStream getStreamByLoc(ResourceLocation resloc) throws IOException{
  34.                 if (resloc.getPath().contains("..")) {
  35.                         throw new IOException("Invalid relative path to resource: " + resloc);
  36.                 }
  37.                 FallbackResourceManager man = resMap.get(resloc.getNamespace());
  38.                 if (man != null) {
  39.                         List<IResourcePack> resourcePacks;
  40.                         try{
  41.                                 if(Config.isMDKEnviroment)
  42.                                         resourcePacks = (List<IResourcePack>)ReflectionHelper.getPrivateField(man, "resourcePacks");
  43.                                 else
  44.                                         resourcePacks = (List<IResourcePack>)ReflectionHelper.getPrivateField(man, "field_110540_a");
  45.                         }catch(Exception e){
  46.                                 throw new IOException("Resourcepack get failed");
  47.                         }
  48.                         IResourcePack pack = null;
  49.                         for (int i = resourcePacks.size() - 1; i >= 0; i--) {
  50.                                 pack = resourcePacks.get(i);
  51.                                 
  52.                                 if (pack.resourceExists(resloc)) {
  53.                                         return pack.getInputStream(resloc);
  54.                                 }
  55.                         }
  56.                 }
  57.                 throw new FileNotFoundException("TextureNotFoundException: "+resloc.toString());
  58.         }

  59.         
  60.         @Override
  61.         public void onResourceManagerReload(IResourceManager man){
  62.                 try{
  63.                         if(Config.isMDKEnviroment)
  64.                                 resMap = (Map<String, FallbackResourceManager>)ReflectionHelper.getPrivateField(man, "domainResourceManagers");
  65.                         else
  66.                                 resMap = (Map<String, FallbackResourceManager>)ReflectionHelper.getPrivateField(man, "field_110548_a");
  67.                 }catch(Exception e){
  68.                         e.printStackTrace();
  69.                         //throw new AntiCheatException("Texture map get failed");
  70.                 }
  71.                 loadAll();
  72.                 bindAll();
  73.         }
  74.         
  75.         public static void add(String modid, String type, String name ,InputStream stream){
  76.                 data.put(new ResourceLocation(modid, "textures/"+type+"/"+name+".png"), stream);
  77.         }
  78.         
  79.         public static void loadAll(){
  80.                 for(Map.Entry<ResourceLocation, InputStream> entry : data.entrySet()){
  81.                         engine.loadTexture(entry.getKey(), new WebTexture(entry.getKey(), entry.getValue()));
  82.                 }
  83.         }
  84.         
  85.         public static void bindAll(){
  86.                 for(ResourceLocation k : data.keySet()){
  87.       
复制代码

                 engine.bindTexture(k);
                }
        }
        
}

刚刚手写的有点错误已经修改好了

对了怎么折叠?

用法:
1  TextureLoader.add("modid", "type", "name", InputStream);
2  new TextureLoader();

示例(这里是MC的石头)
TextureLoader.add("minecraft", "blocks", "stone", InputStream);

InputStream是图片
PPPS破烂MCBBS编辑器



森林蝙蝠

至于吗,都动上反射了?ModelLoader.setCustomModelResourceLocation或者ModelBakeryEvent,了解一下。

roj234
森林蝙蝠 发表于 2019-5-13 19:37
至于吗,都动上反射了?ModelLoader.setCustomModelResourceLocation或者ModelBakeryEvent,了解一下。 ...
  1. ResourceLocation resloc;
  2.         if(item instanceof ItemBlock)
  3.         {
  4.             ItemBlock itemBlock = (ItemBlock) item;
  5.             Block block = itemBlock.getBlock();
  6.             resloc = block.getRegistryName();
  7.         }else{
  8.             resloc = item.getRegistryName();
  9.         }
  10.         ModelLoader.setCustomModelResourceLocation(item,meta,new ModelResourceLocation(resloc, "inventory"));
复制代码

然后呢。。。怎么设置

森林蝙蝠
roj234 发表于 2019-5-13 19:48
然后呢。。。怎么设置

不用ItemBlock,那个方法就是给物品用的。

roj234
本帖最后由 roj234 于 2019-5-13 19:54 编辑
森林蝙蝠 发表于 2019-5-13 19:51
不用ItemBlock,那个方法就是给物品用的。

直接Item.getRegistryName()?
不过我想的是上面的问题
这里只能设定模型文件而不是png

森林蝙蝠
本帖最后由 森林蝙蝠 于 2019-5-13 21:13 编辑
roj234 发表于 2019-5-13 19:52
直接Item.getRegistryName()?
不过我想的是上面的问题
这里只能设定模型文件而不是png

是的,或者new ResourceLocation(文件名)也可以。
ModelLoader.setCustomModelResourceLocation(Item,0,new ModelResourceLocation(注册名),“inventory”);


3TUSK
楼上所有回复都可以无视,请参考这个:
https://harbinger.covertdragon.t ... 1%E5%9D%97%EF%BC%9F

幻刺呀
涨知识了 哈哈哈