Jump to content

[1.11.2] Forge's blockstates with subBlocks


Erfurt

Recommended Posts

Hey guys,

 

I was wondering if there's an easy way to use forge's blockstates with subBlocks? Would it work the same as for regular blocks, meaning I don't need to make a .json file for the models.block and the models.item?

What I mean is, like the way you can use it with a PropertyBool. Example below.

{
    "forge_marker": 1,
    "defaults": {
        "textures": {
            "all": "#texture"
        }
    },
    "variants": {
        "normal"    : { "model": "#model", "uvlock": true                          },
        "#bool"     : { 
            "true"  : { "model": "#model", "uvlock": true, "submodel": "#subModel" },
            "false" : { "model": "#model", "uvlock": true                          },
        }
    }
}

 

I'm using a PropertyEnum to add my subBlocks, and I call it type. So in my mind it should be something like this.

{
    "forge_marker": 1,
    "defaults": {
        "particle": "#all",
        "model": "#model",
        "uvlock": true
    },
    "variants": {
        "type"  : { 
            "#enumName1" : { "textures": { "all": "#texture" } },
            "#enumName2" : { "textures": { "all": "#texture" } }
        }
    }
}

 

Also if I have both in my class, wouldn't it be something like this?

{
    "forge_marker": 1,
    "defaults": {
        "particle": "#all"
    },
    "variants": {
        "type"  : { 
            "#enumName1" : { "textures": { "all": "#texture" } },
            "#enumName2" : { "textures": { "all": "#texture" } }
        },
        "#bool"     : { 
            "true"  : { "model": "#model", "uvlock": true, "submodel": "#subModel" },
            "false" : { "model": "#model", "uvlock": true                          },
        }
    }
}

 

I should mention that at this times, it's only the texture that seems to not be working.

Link to comment
Share on other sites

Those examples look correct.

 

If it's not working for you, post your real code and blockstates file(s) and the FML log.

Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.

Link to comment
Share on other sites

21 minutes ago, Choonster said:

Those examples look correct.

 

If it's not working for you, post your real code and blockstates file(s) and the FML log.

Here's all my code.

Spoiler

How I register my renders.


public static void registerRenders()
{
	for(int i = 0; i < DecorationTypes.values().length; i++)
	{
		registerSubRender(BlockRegistry.chimney, i, DecorationTypes.values()[i].getName());
	}
}

public static void registerSubRender(Block block, int meta, String fileName)
{
	Item item = Item.getItemFromBlock(block);
	ModelLoader.setCustomModelResourceLocation(item, meta, new ModelResourceLocation(item.getRegistryName(), "_" + fileName));
}

 

My BlockClass


public class ChimneyBlock extends Block implements IMetaBlockName
{
	public static final PropertyBool SLAB_STAIR = PropertyBool.create("slab_stair");
	public static final PropertyEnum TYPE = PropertyEnum.create("type", DecorationTypes.class);
	
	protected static final double pixel = 1/16D;
	protected static final AxisAlignedBB CHIMNEY_BLOCK_AABB = new AxisAlignedBB(2*pixel, 0.0D, 2*pixel, 14*pixel, 1.0D, 14*pixel);

	public ChimneyBlock()
	{
		super(Material.ROCK);
		setHardness(1.0F);
		setSoundType(SoundType.STONE);
		isToolEffective("pickaxe", getDefaultState());
		setCreativeTab(CreativeTabs.DECORATIONS);
		setDefaultState(blockState.getBaseState().withProperty(SLAB_STAIR, Boolean.valueOf(false)).withProperty(TYPE, DecorationTypes.STONEBRICK));
	}
	
	@Override
	public boolean isOpaqueCube(IBlockState state)
	{
		return false;
	}
	
	@Override
	public boolean isFullCube(IBlockState state)
	{
		return false;
	}

	@Override
	@SideOnly(Side.CLIENT)
	public BlockRenderLayer getBlockLayer()
	{
        return BlockRenderLayer.CUTOUT;
	}

	@Override
	public AxisAlignedBB getBoundingBox(IBlockState state, IBlockAccess source, BlockPos pos)
	{
        return CHIMNEY_BLOCK_AABB;
	}
	
	@Override
	@Nullable
	public AxisAlignedBB getCollisionBoundingBox(IBlockState blockState, IBlockAccess worldIn, BlockPos pos)
	{
        return CHIMNEY_BLOCK_AABB;
	}

	@Override
	@SideOnly(Side.CLIENT)
	public void randomDisplayTick(IBlockState stateIn, World worldIn, BlockPos pos, Random rand)
	{
		for (int i = 0; i < 3; ++i)
		{
			double d0 = (double)pos.getX() + rand.nextDouble();
			double d1 = (double)pos.getY() + rand.nextDouble() * 0.5D + 1.0D;
			double d2 = (double)pos.getZ() + rand.nextDouble();
			worldIn.spawnParticle(EnumParticleTypes.SMOKE_LARGE, d0, d1, d2, 0.0D, 0.0D, 0.0D, new int[0]);
		}
	}
	
	@Override
	public IBlockState getActualState(IBlockState state, IBlockAccess world, BlockPos pos)
	{
		IBlockState block_under = world.getBlockState(pos.down());
		if(block_under.getProperties().containsValue(BlockSlab.EnumBlockHalf.BOTTOM) || block_under.getProperties().containsValue(BlockStairs.EnumHalf.BOTTOM))
		{
			state = state.withProperty(SLAB_STAIR, true);
		}
		else
			state = state.withProperty(SLAB_STAIR, false);
        return state;
	}
	
	@Override
	public int getMetaFromState(IBlockState state)
	{
		DecorationTypes type = (DecorationTypes) state.getValue(TYPE);
        return type.getMeta();
	}
	
	@Override
	public IBlockState getStateFromMeta(int meta)
	{
		return this.getDefaultState().withProperty(TYPE, DecorationTypes.values()[meta]);
	}
	
	@Override
	protected BlockStateContainer createBlockState()
	{
		return new BlockStateContainer(this, new IProperty[] {SLAB_STAIR, TYPE});
	}
	
	@Override
	public void getSubBlocks(Item itemIn, CreativeTabs tab, NonNullList<ItemStack> list)
	{
		for(int i = 0; i < DecorationTypes.values().length; i++)
		{
			list.add(new ItemStack(itemIn, 1, i));
		}
	}

	@Override
	public String getSpecialName(ItemStack stack)
	{
		return DecorationTypes.values()[stack.getItemDamage()].getName();
	}
}

 

My enum


public static enum DecorationTypes implements IStringSerializable
{
	STONEBRICK("stonebrick", 0),
	ENDBRICK("endbrick", 1),
	BRICK("brick", 2),
	COBBLESTONE("cobblestone", 3),
	SANDSTONE("sandstone", 4),
	RED_SANDSTONE("red_sandstone", 5);
	
	private int meta;
	private String name;
	
	private DecorationTypes(String name, int meta)
	{
		this.meta = meta;
		this.name = name;
	}
	
	@Override
	public String getName()
	{
		return this.name;
	}
	
	public int getMeta()
	{
		return meta;
	}
	
	@Override
	public String toString()
	{
		return getName();
	}
}

 

My ItemBlockClass


public class ItemSubBlock extends ItemBlock
{
	public ItemSubBlock(Block block)
	{
		super(block);
		if(!(block instanceof IMetaBlockName))
		{
			throw new IllegalArgumentException(String.format("The given Block %s is not an instance of IMetaBlockName!", block.getUnlocalizedName()));
		}
		this.setHasSubtypes(true);
		this.setMaxDamage(0);
	}
	
	@Override
	public String getUnlocalizedName(ItemStack stack)
	{
		return super.getUnlocalizedName() + "." + ((IMetaBlockName) this.block).getSpecialName(stack);
	}
	
	@Override
	public int getMetadata(int damage)
	{
		return damage;
	}
}

 

How I do the registerModelBakery


@Override
public void registerModelBakeryVariants()
{
	ModelBakery.registerItemVariants(Item.getItemFromBlock(BlockRegistry.chimney), new ResourceLocation(Reference.MOD_ID, "chimney_stonebrick"), 
			new ResourceLocation(Reference.MOD_ID, "chimney_endbrick"), 
			new ResourceLocation(Reference.MOD_ID, "chimney_brick"), 
			new ResourceLocation(Reference.MOD_ID, "chimney_cobblestone"), 
			new ResourceLocation(Reference.MOD_ID, "chimney_sandstone"), 
			new ResourceLocation(Reference.MOD_ID, "chimney_red_sandstone"));
}

 

My blockstate file (it's named "chimney.json")


{
    "forge_marker": 1,
    "defaults": {
        "particle" : "#all"
    },
    "variants": {
        "slab_stair"        : { 
            "true"          : { "model": "em:chimney", "uvlock": true, "submodel": "em:chimney_bottom" },
            "false"         : { "model": "em:chimney", "uvlock": true                                  },
        },
        "type"              : {
            "stonebrick"    : { "textures": { "all": "blocks/stonebrick"                             } },
            "endbrick"      : { "textures": { "all": "blocks/endbrick"                               } },
            "brick"         : { "textures": { "all": "blocks/brick"                                  } },
            "cobblestone"   : { "textures": { "all": "blocks/cobblestone"                            } },
            "sandstone"     : { "textures": { "all": "blocks/sandstone_top"                          } },
            "red_sandstone" : { "textures": { "all": "blocks/red_sandstone_top"                      } }
        }
    }
}

 

I personally think it's most likely to be the way I register my renders, or the registerModelBakery, that's the problem, but I don't really know.

Link to comment
Share on other sites

So you want to use the blockstates variants for the item models? The ModelResourceLocation you pass to ModelLoader.setCustomModelResourceLocation needs to have the blockstates file's name as the domain and path (the first constructor argument, which looks correct) and a full variant name as the variant (the second constructor argument, which looks incorrect).

 

An example variant from the chimney.json blockstates file would be "slab_stair=false,type=stonebrick". You're using variants like "_stonebrick", which aren't defined by the blockstates file.

 

The FML log (logs/fml-client-latest.log in the game directory) will usually tell you why a model failed to load, please post it (using Gist/Pastebin) in future.

 

If you're using ModelLoader.setCustomModelResourceLocation, ModelBakery.registerItemVariants is automatically called for you; you don't need to call it yourself.

 

In my mod, I use StateMapperBase#getPropertyString to create a property string (like "slab_stair=false,type=stonebrick") from an IBlockState when registering item models for blocks with variants. You can see how I do this here.

Edited by Choonster

Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.

Link to comment
Share on other sites

1 hour ago, Choonster said:

So you want to use the blockstates variants for the item models? The ModelResourceLocation you pass to ModelLoader.setCustomModelResourceLocation needs to have the blockstates file's name as the domain and path (the first constructor argument, which looks correct) and a full variant name as the variant (the second constructor argument, which looks incorrect).

 

An example variant from the chimney.json blockstates file would be "slab_stair=false,type=stonebrick". You're using variants like "_stonebrick", which aren't defined by the blockstates file.

 

The FML log (logs/fml-client-latest.log in the game directory) will usually tell you why a model failed to load, please post it (using Gist/Pastebin) in future.

 

If you're using ModelLoader.setCustomModelResourceLocation, ModelBakery.registerItemVariants is automatically called for you; you don't need to call it yourself.

 

In my mod, I use StateMapperBase#getPropertyString to create a property string (like "slab_stair=false,type=stonebrick") from an IBlockState when registering item models for blocks with variants. You can see how I do this here.

Maybe it's me who's a complete idiot or something, but I don't get it. I have removed the modelBakery stuff, and also removed the ' "_" + ' from the registerRender. Which is what I get was wrong? Still doesn't work. Does my blockstate look correct to you, I want my types to be named "stonebrick" and not "_stonebrick"? Here's the fml-client-latest.log, there's a lot of stuff in my mod atm, so it's a bit long.

 

EDIT: I haven't changed the json file

Edited by Erfurt
Link to comment
Share on other sites

Quote

[18:22:42] [Client thread/ERROR] [FML/]: Exception loading blockstate for the variant em:chimney#slab_stair=true,type=red_sandstone:
java.lang.Exception: Could not load model definition for variant em:chimney
...
Caused by: java.lang.RuntimeException: Encountered an exception when loading model definition of 'em:chimney' from: 'em:blockstates/chimney.json' in resourcepack: 'FMLFileResourcePack:Eadore mod'
...
Caused by: com.google.gson.JsonSyntaxException: com.google.gson.stream.MalformedJsonException: Expected name at line 10 column 10

 

You have an invalid trailing comma on line 9 of the blockstates file.

 

  • Like 1

Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.

Link to comment
Share on other sites

4 minutes ago, Choonster said:

 

You have an invalid trailing comma on line 9 of the blockstates file.

 

 

5 minutes ago, diesieben07 said:

Your JSON is invalid. See the JsonSyntaxExceptions in the log.

I feel like an ass... So stupid of me not noticing that... Everything works now, thanks for your help

Link to comment
Share on other sites

1 hour ago, Choonster said:

 

You have an invalid trailing comma on line 9 of the blockstates file.

 

Actually, not everything is working... The items doesn't work. Do I need to make separate json files for them, or is it possible to get them from the blockstate?

 

Don't know if you'll need this, but here's the newest fml-client-latest.log

Edited by Erfurt
Link to comment
Share on other sites

16 minutes ago, diesieben07 said:

Forge will use the blockstate json for items as well.

This is the way I have to do to make the items work.

{
    "forge_marker": 1,
    "defaults": {
        "particle" : "#all",
        "model": "em:chimney",
        "uvlock": true
    },
    "variants": {
        "slab_stair"        : { 
            "true"          : { "submodel": "em:chimney_bottom"                   },
            "false"         : { }
        },
        "type"              : {
            "stonebrick"    : { "textures": { "all": "blocks/stonebrick"        } },
            "endbrick"      : { "textures": { "all": "blocks/end_bricks"        } },
            "brick"         : { "textures": { "all": "blocks/brick"             } },
            "cobblestone"   : { "textures": { "all": "blocks/cobblestone"       } },
            "sandstone"     : { "textures": { "all": "blocks/sandstone_top"     } },
            "red_sandstone" : { "textures": { "all": "blocks/red_sandstone_top" } }
        },
        "stonebrick"    : { "model": "em:chimney", "uvlock": true, "textures": { "all": "blocks/stonebrick"        } },
        "endbrick"      : { "model": "em:chimney", "uvlock": true, "textures": { "all": "blocks/end_bricks"        } },
        "brick"         : { "model": "em:chimney", "uvlock": true, "textures": { "all": "blocks/brick"             } },
        "cobblestone"   : { "model": "em:chimney", "uvlock": true, "textures": { "all": "blocks/cobblestone"       } },
        "sandstone"     : { "model": "em:chimney", "uvlock": true, "textures": { "all": "blocks/sandstone_top"     } },
        "red_sandstone" : { "model": "em:chimney", "uvlock": true, "textures": { "all": "blocks/red_sandstone_top" } }
    }
}

 

But I would like to know if there's a way to use the "type" part, for the items?

Link to comment
Share on other sites

5 hours ago, Erfurt said:

This is the way I have to do to make the items work.

 

But I would like to know if there's a way to use the "type" part, for the items?

 

Like I said, use a variant that already exists in the blockstates file (e.g. "slab_stair=false,type=stonebrick") as the variant of the ModelResourceLocation you pass to ModelLoader.setCustomModelResourceLocation. You can either create this string with regular string concatenation/formatting or from an IBlockState using StateMapperBase#getPropertyString.

Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.

Link to comment
Share on other sites

12 hours ago, Choonster said:

 

Like I said, use a variant that already exists in the blockstates file (e.g. "slab_stair=false,type=stonebrick") as the variant of the ModelResourceLocation you pass to ModelLoader.setCustomModelResourceLocation. You can either create this string with regular string concatenation/formatting or from an IBlockState using StateMapperBase#getPropertyString.

Ahh, got it now :D

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.