Jump to content

How to get a block's texture reliably?


Two

Recommended Posts

I am currently trying to write a minimap addon and I am desperately trying to find a method that allows me to get a block's top texture reliably.

 

So far I tried to get the texture directly from the IIcon by casting it to a TextureAtlasSprite if possible, but that is limited to only a few blocks.

 

I also tried to fetch the image directly from the texture that holds the block faces, but that feels very hacky to me and requires exact timing to actually get the block texture properly.

 

Is there a better/reliable way to get the top texture of any block that has one?

My Mods

 

New Dawn - A completely new terrain engine for Minecraft

TwoTility - Blocks and Items for a better Minecraft

TwoGraves - Keeps your items safe on death

Link to comment
Share on other sites

That was my second try, however the texture I get is very strange and does not include many blocks that are present in the world. The UV of leaves for example points to an empty part of that texture, and I am not sure yet why. It might be an error on my part, but then I figure that at some point the textures are loaded from disk anyway, so why get them via texture buffer?

 

As far as I understand it, there has to be some point where the texture files are actually loaded from disk in the form of a BufferedImage. What I would like to do is to either somehow do the same, or to intercept that either while it is loading or at a later time if that image data is kept in memory afterwards.

My Mods

 

New Dawn - A completely new terrain engine for Minecraft

TwoTility - Blocks and Items for a better Minecraft

TwoGraves - Keeps your items safe on death

Link to comment
Share on other sites

Took a little more try and error, but finally I got it.

 

For future reference, this is how you load any texture from a given block IIcon (use block.getIcon to get that icon):

 

  
/* Returns the pixel data of the texture described by icon in ARGB format */
static int[] loadTexture(final IIcon icon) {
    try {
      final ResourceLocation resourceLocation = getResourceLocation(icon.getIconName());
      final IResourceManager resourceManager = Minecraft.getMinecraft().getResourceManager();
      final IResource resource = resourceManager.getResource(resourceLocation);
      final InputStream in = resource.getInputStream();
      final BufferedImage image = ImageIO.read(in);
      return image.getRGB(0, 0, image.getWidth(), image.getHeight(), null, 0, image.getWidth());
    } catch (Exception e) {
      FMLLog.log(Level.ERROR, e, "Unable to load texture '%s'", icon.getIconName());
    }
    return null;
  }

  static ResourceLocation getResourceLocation(final String blockTexture) {
    String domain = "minecraft";
    String path = blockTexture;
    final int domainSeparator = blockTexture.indexOf(':');

    if (domainSeparator >= 0) {
      path = blockTexture.substring(domainSeparator + 1);

      if (domainSeparator > 1) {
        domain = blockTexture.substring(0, domainSeparator);
      }
    }

    final String resourcePath = "textures/blocks/" + path + ".png";  // base path and PNG are hardcoded in Minecraft
    return new ResourceLocation(domain.toLowerCase(), resourcePath);
  }

My Mods

 

New Dawn - A completely new terrain engine for Minecraft

TwoTility - Blocks and Items for a better Minecraft

TwoGraves - Keeps your items safe on death

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.