Jump to content

Recommended Posts

Posted

Dear all

 

I recently started to update from 1.7.10 to 1.9 (Forge: 12.16.1.1887) and I am experiencing a really hard time with my old setup of a meta data based block. The setup was really simple: Having a general colored Block (meta data based) class, an enum for the minecraft colors and its ItemBlock so that I only had to specify its name and general block properties (hardness etc) for further colored blocks.

 

Following vanilla minecraft code, MinecraftByExample and the Forge BlockState documentation I've created the following block class (which contains the enum):

 

 

import org.bitbucket.pickaxe_engineer.polycraft.mod.item.ItemBlockMetaData;

import net.minecraft.block.material.Material;
import net.minecraft.block.properties.PropertyEnum;
import net.minecraft.block.state.BlockStateContainer;
import net.minecraft.block.state.IBlockState;
import net.minecraft.client.renderer.block.model.ModelResourceLocation;
import net.minecraft.client.renderer.block.statemap.IStateMapper;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.IStringSerializable;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.RayTraceResult;
import net.minecraft.world.World;
import net.minecraftforge.client.model.ModelLoader;
import net.minecraftforge.fml.common.registry.GameRegistry;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;

/**
* Class which provides generic functionality to have a colored block. Colored
* means the standard 16 minecraft colors are supported and registered. As well
* as this, the corresponding jsons and textures need to be present with the
* format blockname_color for a mod examplemod which adds colored bricks it would
* be examplemod:coloredBricks_color, where color is the lower case name of the
* minecraft color, light_gray and the other lights are underscore separated.
* The unlocalized name is built in a similar way: tile.blockname_color.name
* 
* Corresponding itemblock is ItemBlockMetaData.
* 
* @author pickaxe_engineer
*
*/
public class BlockGenericColored extends GenericBlock implements IMetaDataBlock {

/**
 * The property constant 'color' of this block.
 * EnumColor is a nested enum of this class and created in a similar way as the
 * vanilla colours (so the same order of colors)
 */
public static final PropertyEnum COLOR = PropertyEnum.create("color", EnumColor.class);

/**
 * Creates a new {@link BlockGenericColored} based on a given material and name.
 * Since {@link BlockGenericColored} is a child class of {@link GenericBlock},
 * the default hardness, resistance and toolclass are inherited. Set these values within
 * the subclass of this, if you want to modify them.
 * Don't forget to set the default blockstate as well
 * @param baseName
 * @param material
 */
public BlockGenericColored(String baseName, Material material) {
	super(baseName, material);
	setDefaultState(blockState.getBaseState().withProperty(COLOR, EnumColor.WHITE));
}

/**
 * Registers the model of this block
 * Since this is a blockstate based block, it registers all its variants
 * Called during preInit of ClientProxy *after* the block itself got registered
 */
@SideOnly(Side.CLIENT)
@Override
public void initModel() {
	ModelResourceLocation loc = new ModelResourceLocation(getRegistryName(), "inventory");
	for(EnumColor col : EnumColor.values()){
		ModelLoader.setCustomModelResourceLocation(Item.getItemFromBlock(this), col.ordinal(), loc);
	}
}

/**
 * Registers the block, called at the end of the constructor and thus in the preInit of commonproxy
 * Overwritten to use the ItemBLockMetaData
 */
@Override
protected void register(String name) {
	setRegistryName(name);
	GameRegistry.register(this);
	GameRegistry.register(new ItemBlockMetaData(this), getRegistryName());
}

@Override
protected BlockStateContainer createBlockState() {
	return new BlockStateContainer(this, COLOR);
}

@Override
public ItemStack getPickBlock(IBlockState state, RayTraceResult target, World world, BlockPos pos,
		EntityPlayer player) {
	return new ItemStack(Item.getItemFromBlock(this), 1, getMetaFromState(world.getBlockState(pos)));
}

@Override
public IBlockState getStateFromMeta(int meta) {
	return getDefaultState().withProperty(COLOR, EnumColor.values()[meta % EnumColor.values().length]);
}

@Override
public int getMetaFromState(IBlockState state) {
	EnumColor color = (EnumColor) state.getValue(COLOR);
	return color.ordinal();
}

@Override
public int damageDropped(IBlockState state) {
	return getMetaFromState(state);
}

@Override
public void getSubBlocks(Item itemIn, CreativeTabs tab, List<ItemStack> list) {
	for (EnumColor col : EnumColor.values()) {
		list.add(new ItemStack(itemIn, 1, col.ordinal()));
	}
}

@Override
public boolean canSilkHarvest(World world, BlockPos pos, IBlockState state, EntityPlayer player) {
	// if silk touch has an effect, return true.
	// If this returns true, overwrite also createStackedBlock
	return super.canSilkHarvest(world, pos, state, player);
}

@Override
protected ItemStack createStackedBlock(IBlockState state) {
	// Needed if silk touch has an effect
	return super.createStackedBlock(state);
}

@Override
public IBlockState onBlockPlaced(World worldIn, BlockPos pos, EnumFacing facing, float hitX, float hitY, float hitZ,
		int meta, EntityLivingBase placer) {
	EnumColor color = EnumColor.values()[meta % EnumColor.values().length];
	return getDefaultState().withProperty(COLOR, color);
}

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

public static enum EnumColor implements IStringSerializable {
	WHITE, ORANGE, MAGENTA, LIGHT_BLUE, YELLOW, LIME, PINK, GRAY, LIGHT_GRAY, CYAN, PURPLE, BLUE, BROWN, GREEN, RED, BLACK;

	@Override
	public String getName() {
		return this.name().toLowerCase();
	}

	@Override
	public String toString() {
		return getName();
	}
}

}

 

 

And the corresponding ItemBlockMetaData class:

 

package org.bitbucket.pickaxe_engineer.polycraft.mod.item;

import org.bitbucket.pickaxe_engineer.polycraft.mod.block.IMetaDataBlock;

import net.minecraft.block.Block;
import net.minecraft.item.ItemBlock;
import net.minecraft.item.ItemStack;

/**
* Generic metadata supporting itemblock
* @author pickaxe_engineer
*
*/
public class ItemBlockMetaData extends ItemBlock {

public ItemBlockMetaData(Block block){
	super(block);
	if(block instanceof IMetaDataBlock){
		setMaxDamage(0);
		setHasSubtypes(true);
	}else{
		// TODO Proper exception message
		throw new IllegalArgumentException("Given block is not of type IMetaDataBlock");
	}

}

@Override
public int getMetadata(int damage) {
	return damage;
}

@Override
public String getUnlocalizedName(ItemStack stack) {
	// TODO: Rewrite to make a special copy of block
	// Since iae may be caugth and still running, this would cause an exception.
	return super.getUnlocalizedName(stack) + "_" + ((IMetaDataBlock)block).getSpecialName(stack);
}

}

 

 

And this blockstates json

{
"forge_marker":1,
"defaults":{
	"model":"cube"
},
"variants":{
   "normal":[{}],
   "inventory":[{}],
   "color":{
	  "white":{
	      "textures":{
	          "all":"polycraft:blocks/concrete_white"
	      }
	  },
	  "orange":{
	      "textures":{
                  "all":"polycraft:blocks/concrete_orange"
              }
	  },
	  "magenta":{
	      "textures":{
                  "all":"polycraft:blocks/concrete_magenta"
              }
	  },
	  "light_blue":{
	      "textures":{
                  "all":"polycraft:blocks/concrete_light_blue"
              }
	  },
	  "yellow":{
	      "textures":{
                  "all":"polycraft:blocks/concrete_yellow"
              }
	  },
	  "lime":{
	      "textures":{
                  "all":"polycraft:blocks/concrete_lime"
              }
	  },
	  "pink":{
	      "textures":{
                  "all":"polycraft:blocks/concrete_pink"
              }
	  },
	  "gray":{
	      "textures":{
                  "all":"polycraft:blocks/concrete_gray"
              }
	  },
	  "light_gray":{
	      "textures":{
                  "all":"polycraft:blocks/concrete_light_gray"
              }
	  },
	  "cyan":{
	      "textures":{
                  "all":"polycraft:blocks/concrete_cyan"
              }
          },
	  "purple":{
	      "textures":{
                  "all":"polycraft:blocks/concrete_purple"
              }
	  },
	  "blue":{
	      "textures":{
                  "all":"polycraft:blocks/concrete_blue"
              }
	  },
	  "brown":{
	      "textures":{
                  "all":"polycraft:blocks/concrete_brown"
              }
	  },
	  "green":{
	      "textures":{
                  "all":"polycraft:blocks/concrete_green"
              }
	  },
	  "red":{
	      "textures":{
                  "all":"polycraft:blocks/concrete_red"
              }
	  },
	  "black":{
	      "textures":{
                  "all":"polycraft:blocks/concrete_black"
              }
	  }
	}
}
}

 

 

The issue that occurrs is, the block's texture (in world and as itemblocks) are not rendered, resulting in the beloved missingtexture texture. Additional confusion is created by the fact, that absolutely no exceptions are thrown, nor any form of warning. This is confirmed as far that if I would remove one of the mentioned textures, I receive the error, that said texture is not there.

 

Furthermore, via F3-debug I can confirm that the correct block was placed (having an orange one, results in an orange one). Listing them all in the creative tab works fine as well.

 

I guess that I've somehow missed a point in the forge block state json, which would be obvious for an experienced forge coder.

 

Thank you in advance for replying!

 

Sincerely -pick

Since English is not my mother tongue, my sentences may are confusing.

 

I'm coding java for a long time now - just MC and forge stop me sometimes.

Posted

Dear all

 

I recently started to update from 1.7.10 to 1.9 (Forge: 12.16.1.1887) and I am experiencing a really hard time with my old setup of a meta data based block. The setup was really simple: Having a general colored Block (meta data based) class, an enum for the minecraft colors and its ItemBlock so that I only had to specify its name and general block properties (hardness etc) for further colored blocks.

 

Following vanilla minecraft code, MinecraftByExample and the Forge BlockState documentation I've created the following block class (which contains the enum):

 

 

import org.bitbucket.pickaxe_engineer.polycraft.mod.item.ItemBlockMetaData;

import net.minecraft.block.material.Material;
import net.minecraft.block.properties.PropertyEnum;
import net.minecraft.block.state.BlockStateContainer;
import net.minecraft.block.state.IBlockState;
import net.minecraft.client.renderer.block.model.ModelResourceLocation;
import net.minecraft.client.renderer.block.statemap.IStateMapper;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.IStringSerializable;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.RayTraceResult;
import net.minecraft.world.World;
import net.minecraftforge.client.model.ModelLoader;
import net.minecraftforge.fml.common.registry.GameRegistry;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;

/**
* Class which provides generic functionality to have a colored block. Colored
* means the standard 16 minecraft colors are supported and registered. As well
* as this, the corresponding jsons and textures need to be present with the
* format blockname_color for a mod examplemod which adds colored bricks it would
* be examplemod:coloredBricks_color, where color is the lower case name of the
* minecraft color, light_gray and the other lights are underscore separated.
* The unlocalized name is built in a similar way: tile.blockname_color.name
* 
* Corresponding itemblock is ItemBlockMetaData.
* 
* @author pickaxe_engineer
*
*/
public class BlockGenericColored extends GenericBlock implements IMetaDataBlock {

/**
 * The property constant 'color' of this block.
 * EnumColor is a nested enum of this class and created in a similar way as the
 * vanilla colours (so the same order of colors)
 */
public static final PropertyEnum COLOR = PropertyEnum.create("color", EnumColor.class);

/**
 * Creates a new {@link BlockGenericColored} based on a given material and name.
 * Since {@link BlockGenericColored} is a child class of {@link GenericBlock},
 * the default hardness, resistance and toolclass are inherited. Set these values within
 * the subclass of this, if you want to modify them.
 * Don't forget to set the default blockstate as well
 * @param baseName
 * @param material
 */
public BlockGenericColored(String baseName, Material material) {
	super(baseName, material);
	setDefaultState(blockState.getBaseState().withProperty(COLOR, EnumColor.WHITE));
}

/**
 * Registers the model of this block
 * Since this is a blockstate based block, it registers all its variants
 * Called during preInit of ClientProxy *after* the block itself got registered
 */
@SideOnly(Side.CLIENT)
@Override
public void initModel() {
	ModelResourceLocation loc = new ModelResourceLocation(getRegistryName(), "inventory");
	for(EnumColor col : EnumColor.values()){
		ModelLoader.setCustomModelResourceLocation(Item.getItemFromBlock(this), col.ordinal(), loc);
	}
}

/**
 * Registers the block, called at the end of the constructor and thus in the preInit of commonproxy
 * Overwritten to use the ItemBLockMetaData
 */
@Override
protected void register(String name) {
	setRegistryName(name);
	GameRegistry.register(this);
	GameRegistry.register(new ItemBlockMetaData(this), getRegistryName());
}

@Override
protected BlockStateContainer createBlockState() {
	return new BlockStateContainer(this, COLOR);
}

@Override
public ItemStack getPickBlock(IBlockState state, RayTraceResult target, World world, BlockPos pos,
		EntityPlayer player) {
	return new ItemStack(Item.getItemFromBlock(this), 1, getMetaFromState(world.getBlockState(pos)));
}

@Override
public IBlockState getStateFromMeta(int meta) {
	return getDefaultState().withProperty(COLOR, EnumColor.values()[meta % EnumColor.values().length]);
}

@Override
public int getMetaFromState(IBlockState state) {
	EnumColor color = (EnumColor) state.getValue(COLOR);
	return color.ordinal();
}

@Override
public int damageDropped(IBlockState state) {
	return getMetaFromState(state);
}

@Override
public void getSubBlocks(Item itemIn, CreativeTabs tab, List<ItemStack> list) {
	for (EnumColor col : EnumColor.values()) {
		list.add(new ItemStack(itemIn, 1, col.ordinal()));
	}
}

@Override
public boolean canSilkHarvest(World world, BlockPos pos, IBlockState state, EntityPlayer player) {
	// if silk touch has an effect, return true.
	// If this returns true, overwrite also createStackedBlock
	return super.canSilkHarvest(world, pos, state, player);
}

@Override
protected ItemStack createStackedBlock(IBlockState state) {
	// Needed if silk touch has an effect
	return super.createStackedBlock(state);
}

@Override
public IBlockState onBlockPlaced(World worldIn, BlockPos pos, EnumFacing facing, float hitX, float hitY, float hitZ,
		int meta, EntityLivingBase placer) {
	EnumColor color = EnumColor.values()[meta % EnumColor.values().length];
	return getDefaultState().withProperty(COLOR, color);
}

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

public static enum EnumColor implements IStringSerializable {
	WHITE, ORANGE, MAGENTA, LIGHT_BLUE, YELLOW, LIME, PINK, GRAY, LIGHT_GRAY, CYAN, PURPLE, BLUE, BROWN, GREEN, RED, BLACK;

	@Override
	public String getName() {
		return this.name().toLowerCase();
	}

	@Override
	public String toString() {
		return getName();
	}
}

}

 

 

And the corresponding ItemBlockMetaData class:

 

package org.bitbucket.pickaxe_engineer.polycraft.mod.item;

import org.bitbucket.pickaxe_engineer.polycraft.mod.block.IMetaDataBlock;

import net.minecraft.block.Block;
import net.minecraft.item.ItemBlock;
import net.minecraft.item.ItemStack;

/**
* Generic metadata supporting itemblock
* @author pickaxe_engineer
*
*/
public class ItemBlockMetaData extends ItemBlock {

public ItemBlockMetaData(Block block){
	super(block);
	if(block instanceof IMetaDataBlock){
		setMaxDamage(0);
		setHasSubtypes(true);
	}else{
		// TODO Proper exception message
		throw new IllegalArgumentException("Given block is not of type IMetaDataBlock");
	}

}

@Override
public int getMetadata(int damage) {
	return damage;
}

@Override
public String getUnlocalizedName(ItemStack stack) {
	// TODO: Rewrite to make a special copy of block
	// Since iae may be caugth and still running, this would cause an exception.
	return super.getUnlocalizedName(stack) + "_" + ((IMetaDataBlock)block).getSpecialName(stack);
}

}

 

 

And this blockstates json

{
"forge_marker":1,
"defaults":{
	"model":"cube"
},
"variants":{
   "normal":[{}],
   "inventory":[{}],
   "color":{
	  "white":{
	      "textures":{
	          "all":"polycraft:blocks/concrete_white"
	      }
	  },
	  "orange":{
	      "textures":{
                  "all":"polycraft:blocks/concrete_orange"
              }
	  },
	  "magenta":{
	      "textures":{
                  "all":"polycraft:blocks/concrete_magenta"
              }
	  },
	  "light_blue":{
	      "textures":{
                  "all":"polycraft:blocks/concrete_light_blue"
              }
	  },
	  "yellow":{
	      "textures":{
                  "all":"polycraft:blocks/concrete_yellow"
              }
	  },
	  "lime":{
	      "textures":{
                  "all":"polycraft:blocks/concrete_lime"
              }
	  },
	  "pink":{
	      "textures":{
                  "all":"polycraft:blocks/concrete_pink"
              }
	  },
	  "gray":{
	      "textures":{
                  "all":"polycraft:blocks/concrete_gray"
              }
	  },
	  "light_gray":{
	      "textures":{
                  "all":"polycraft:blocks/concrete_light_gray"
              }
	  },
	  "cyan":{
	      "textures":{
                  "all":"polycraft:blocks/concrete_cyan"
              }
          },
	  "purple":{
	      "textures":{
                  "all":"polycraft:blocks/concrete_purple"
              }
	  },
	  "blue":{
	      "textures":{
                  "all":"polycraft:blocks/concrete_blue"
              }
	  },
	  "brown":{
	      "textures":{
                  "all":"polycraft:blocks/concrete_brown"
              }
	  },
	  "green":{
	      "textures":{
                  "all":"polycraft:blocks/concrete_green"
              }
	  },
	  "red":{
	      "textures":{
                  "all":"polycraft:blocks/concrete_red"
              }
	  },
	  "black":{
	      "textures":{
                  "all":"polycraft:blocks/concrete_black"
              }
	  }
	}
}
}

 

 

The issue that occurrs is, the block's texture (in world and as itemblocks) are not rendered, resulting in the beloved missingtexture texture. Additional confusion is created by the fact, that absolutely no exceptions are thrown, nor any form of warning. This is confirmed as far that if I would remove one of the mentioned textures, I receive the error, that said texture is not there.

 

Furthermore, via F3-debug I can confirm that the correct block was placed (having an orange one, results in an orange one). Listing them all in the creative tab works fine as well.

 

I guess that I've somehow missed a point in the forge block state json, which would be obvious for an experienced forge coder.

 

Thank you in advance for replying!

 

Sincerely -pick

Since English is not my mother tongue, my sentences may are confusing.

 

I'm coding java for a long time now - just MC and forge stop me sometimes.

Posted

You're using the wrong model.

minecraft:cube

is a cube with a separate texture on each side,

minecraft:cube_all

is a cube with a single texture on all sides.

 

When registering models for the item form, you should use the colour name as the variant of the

ModelResourceLocation

instead of

"inventory"

. This will allow each metadata value of the item to use the corresponding block model.

 

I suggest using the existing

EnumDyeColor

and extending

BlockColored

.

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.

Posted

You're using the wrong model.

minecraft:cube

is a cube with a separate texture on each side,

minecraft:cube_all

is a cube with a single texture on all sides.

 

When registering models for the item form, you should use the colour name as the variant of the

ModelResourceLocation

instead of

"inventory"

. This will allow each metadata value of the item to use the corresponding block model.

 

I suggest using the existing

EnumDyeColor

and extending

BlockColored

.

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.

Posted

Thank you for this informative reply. It was really due to the wrong model.

 

Further I really appreciate your git repo, I personally learn the forge / mc mechaniques best by reading code, so thumbs up for your work there.

 

Sincerely -pick

Since English is not my mother tongue, my sentences may are confusing.

 

I'm coding java for a long time now - just MC and forge stop me sometimes.

Posted

Thank you for this informative reply. It was really due to the wrong model.

 

Further I really appreciate your git repo, I personally learn the forge / mc mechaniques best by reading code, so thumbs up for your work there.

 

Sincerely -pick

Since English is not my mother tongue, my sentences may are confusing.

 

I'm coding java for a long time now - just MC and forge stop me sometimes.

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.