Jump to content

Recommended Posts

Posted (edited)

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
Posted (edited)

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)

Posted

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;
    }

 

Posted
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)

Posted

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;
    }

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.