Jump to content

[1.12.2] Get texture from block [Solved]


_Doc

Recommended Posts

I am attempting to get the texture from any block. When I try to use .getRegistryName() on a block such as spruce log, andesite, green wool. I get back log, stone, and wool. Well, I want to be able to get spruce_log or green_wool so I can get the correct .json file to get the texture path from. I am doing it this way so if there is a block from another mod and they use a different path for their textures I can still acquire it from the model.json 

public boolean setSelected(ItemStack stack) {
		if (stacks.contains(stack)) {
			String display;
			
			Block block = Block.getBlockFromItem(stack.getItem());
			ResourceLocation reg = block.getRegistryName();
			ResourceLocation location = new ResourceLocation(reg.toString());
			
			String asset = "assets/" + location.getResourceDomain() + "/" + "models/" + location.getResourcePath() + ".json"+"\n";			
			caption = asset;
			this.selected = stack;
			raiseEvent(new GuiControlChangedEvent(this));
			return true;
		}
		return false;
	}

 

Edited by _Doc
Link to comment
Share on other sites

BlockRendererDispatcher#getModelForState(IBlockState)#getQuads#get#getSprite

 

Will get you the sprite in the texture map which you will use for anything to do with rendering. If for some reason you need the path of that sprite I’m pretty sure there’s a method on the sprite to get it’s resource location, however this will not always be a valid file or a file that contains what you expect because some textures are inbuilt (the missing texture) and others use animation (water, lava) and color (the enchantment texture) when they are rendered

Edited by Cadiboo

About Me

Spoiler

My Discord - Cadiboo#8887

My WebsiteCadiboo.github.io

My ModsCadiboo.github.io/projects

My TutorialsCadiboo.github.io/tutorials

Versions below 1.14.4 are no longer supported on this forum. Use the latest version to receive support.

When asking support remember to include all relevant log files (logs are found in .minecraft/logs/), code if applicable and screenshots if possible.

Only download mods from trusted sites like CurseForge (minecraft.curseforge.com). A list of bad sites can be found here, with more information available at stopmodreposts.org

Edit your own signature at www.minecraftforge.net/forum/settings/signature/ (Make sure to check its compatibility with the Dark Theme)

Link to comment
Share on other sites

Right now it is crashing at getModelForState(state) it gives me a null pointer exception. Had thought it was due to the BlockRendererDispatcher(null, new BlockColors()) having the null value. But, not sure how to give it the desired object of BlockModelShapes. 

 

public boolean setSelected(ItemStack stack) {
        if (stacks.contains(stack)) {
            String display;
            
            Block block = Block.getBlockFromItem(stack.getItem());
            //ResourceLocation reg = block.getRegistryName();
            //ResourceLocation location = new ResourceLocation(reg.toString());
            BlockRendererDispatcher ren = new BlockRendererDispatcher(null, new BlockColors());
            
            IBlockState state = block.getBlockState().getBaseState();
            display = ren.getModelForState(state).toString();
            //.getQuads(null, null, 0).get(0).getSprite()
            //String asset = "assets/" + location.getResourceDomain() + "/" + "models/" + location.getResourcePath() + ".json"+"\n";            
            caption = display;
            this.selected = stack;
            raiseEvent(new GuiControlChangedEvent(this));
            return true;
        }
        return false;
    }

 

Link to comment
Share on other sites

4 minutes ago, _Doc said:

BlockRendererDispatcher ren = new BlockRendererDispatcher(null, new BlockColors());

Don't do this. You're passing in null as the thing thats meant to do the model lookup that you call in the next line.

use Minecraft#getBlockRendererDispatcher()

  • Thanks 1

About Me

Spoiler

My Discord - Cadiboo#8887

My WebsiteCadiboo.github.io

My ModsCadiboo.github.io/projects

My TutorialsCadiboo.github.io/tutorials

Versions below 1.14.4 are no longer supported on this forum. Use the latest version to receive support.

When asking support remember to include all relevant log files (logs are found in .minecraft/logs/), code if applicable and screenshots if possible.

Only download mods from trusted sites like CurseForge (minecraft.curseforge.com). A list of bad sites can be found here, with more information available at stopmodreposts.org

Edit your own signature at www.minecraftforge.net/forum/settings/signature/ (Make sure to check its compatibility with the Dark Theme)

Link to comment
Share on other sites

Thank you, Cadiboo! It returns the correct texture path. The other issue with the "andesite" giving me "stone" was due to me going from ItemStack to Item to Block. It seems it lost it's metadata when it became an item. Here is the working code that gives me the correct texture path for the given block.


public boolean setSelected(ItemStack stack) {
        if (stacks.contains(stack)) {
            Block block = Block.getBlockFromItem(stack.getItem());
            Item item = stack.getItem();
            int damage = item.getDamage(stack);
            int meta = item.getMetadata(damage);
            IBlockState state = block.getStateFromMeta(meta);
            
            ResourceLocation reg = block.getRegistryName();
            ResourceLocation location = new ResourceLocation(reg.toString());
            Minecraft minecraft = Minecraft.getMinecraft();
            BlockRendererDispatcher ren = minecraft.getBlockRendererDispatcher();
            
            texture = ren.getModelForState(state).getQuads(state, EnumFacing.NORTH, 0).get(0).getSprite().toString();
            
            caption = stack.getDisplayName();
            this.selected = stack;
            raiseEvent(new GuiControlChangedEvent(this));
            return true;
        }
        return false;
    }

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



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • Muyern Trust Hacker stands as a beacon of trust and reliability in the ever-expanding realm of digital asset recovery. In a world where online scams and cybercrime proliferate, finding a trustworthy ally like Muyern Trust Hacker can be a game-changer for those who have fallen victim to fraudulent schemes. My journey with Muyern Trust Hacker began after a devastating loss to a fake investment firm. Amidst the despair and frustration, Muyern Trust Hacker emerged as a guiding light, offering hope and tangible solutions to reclaim what was rightfully mine. What truly sets Muyern Trust Hacker apart is its unwavering commitment to its clients. Unlike other recovery firms that may prioritize profit over people, Muyern Trust Hacker operates with integrity and transparency at every turn. From the initial consultation to the successful recovery of funds, their dedication to providing top-notch service is evident in every interaction. Muyern Trust Hacker's expertise is nothing short of exceptional. With a deep understanding of cybersecurity protocols and digital forensics, they possess the technical acumen to navigate even the most complex cases. Whether recovering stolen cryptocurrencies, tracing fraudulent transactions, or combating identity theft, Muyern Trust Hacker approaches each challenge with precision and skill. What truly impressed me about Muyern Trust Hacker was their commitment to delivering results promptly. In an industry where time is of the essence, their swift action and efficient approach ensure that clients can reclaim their assets with minimal delay. This alleviates the financial strain caused by the loss and provides much-needed peace of mind. Furthermore, the level of personalized attention and support provided by Muyern Trust Hacker is unparalleled. Unlike larger firms where clients may feel like just another case number, Muyern Trust Hacker fosters a genuine connection with each individual. From regular updates on the progress of the recovery process to tailored strategies based on the specific circumstances, clients can rest assured knowing that their case is receiving the attention it deserves. In addition to their impressive track record of success, Muyern Trust Hacker's commitment to ongoing support is commendable. Even after the recovery process is complete, they continue to offer guidance and advice to ensure that clients are equipped with the knowledge and tools necessary to safeguard their assets in the future. In conclusion, Muyern Trust Hacker is more than just a recovery firm – they are a trusted partner in the fight against online scams and fraud. With their unwavering dedication to their clients, unmatched technical expertise, and proven track record of success, Muyern Trust Hacker stands as a beacon of hope for those seeking justice in the digital world. Tele gram at: muyerntrusthackertech
    • I'm having the same problem as this other post and it seems whoever answered it deleted their account or something, so I cannot see the answer. I followed the same steps they said in their post. Can someone pls help. Other Post:   
    • idk why https://pastebin.com/uiaAQXRS
    • now it just says this -> https://pastebin.com/eAEFjLkJ
    • Well, I gave up so thanks for the help, it crashed with a mod that I can’t name with the infamous mod of Jenny’s 
  • Topics

×
×
  • Create New...

Important Information

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