Jump to content

[1.8.9][Solved] Block metadata value reverts to 0


Recommended Posts

Posted

I'm confused as to why when I place my block's item of metadata value 2, it reverts to 0. This is the same for metadata value 1.

 

ItemBlock Meta class:

 

 

package com.test.blocks;

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

public class ItemBlockMeta extends ItemBlock
{
public ItemBlockMeta(Block block)
{
	super(block);
	if (!(block instanceof IMetaBlockName))
	{
		throw new IllegalArgumentException(String.format("The given Block %s is not an instanceof ISpecialBlockName!", block.getUnlocalizedName()));
	}

	this.setMaxDamage(0);
	this.setHasSubtypes(true);
}



public int getMetaData(int damage)
{
	return damage;
}

@Override
public String getUnlocalizedName(ItemStack stack)
{
	return super.getUnlocalizedName(stack) + "." + ((IMetaBlockName)this.block).getSpecialName(stack);
}
}

 

 

 

BlockFabric class:

 

 

package com.test.blocks;

import java.util.List;

import net.minecraft.block.Block;
import net.minecraft.block.material.Material;
import net.minecraft.block.properties.IProperty;
import net.minecraft.block.properties.PropertyEnum;
import net.minecraft.block.state.BlockState;
import net.minecraft.block.state.IBlockState;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.util.BlockPos;
import net.minecraft.util.MovingObjectPosition;
import net.minecraft.world.World;

import com.test.Main;
import com.test.util.EnumHandler;

public class BlockFabric extends Block implements IMetaBlockName
{
public static final PropertyEnum TYPE = PropertyEnum.create("type", EnumHandler.BlockFabricTypes.class);

public BlockFabric()
{
	super(Material.cloth);
	this.setUnlocalizedName("fabric");
	this.setRegistryName("fabric");
	this.setCreativeTab(Main.tabTest);
	this.setDefaultState(this.blockState.getBaseState().withProperty(TYPE, EnumHandler.BlockFabricTypes.DARKRED));
}

@Override
public void getSubBlocks(Item item, CreativeTabs tab, List<ItemStack> list)
{
	list.add(new ItemStack(item, 1, 0));
	list.add(new ItemStack(item, 1, 1));
	list.add(new ItemStack(item, 1, 2));
}

@Override
protected BlockState createBlockState()
{
	return new BlockState(this, new IProperty[] { TYPE });
}

@Override
public IBlockState getStateFromMeta(int meta)
{
	if (meta == 0)
	{
		return this.getDefaultState().withProperty(TYPE, EnumHandler.BlockFabricTypes.DARKRED);
	}
	if (meta == 1)
	{
		return this.getDefaultState().withProperty(TYPE, EnumHandler.BlockFabricTypes.DARKYELLOW);
	}
	if (meta == 2)
	{
		return this.getDefaultState().withProperty(TYPE, EnumHandler.BlockFabricTypes.PALEYELLOW);
	}
	return this.getDefaultState().withProperty(TYPE, EnumHandler.BlockFabricTypes.DARKRED);
}

@Override
public int getMetaFromState(IBlockState state)
{
	EnumHandler.BlockFabricTypes type = (EnumHandler.BlockFabricTypes) state.getValue(TYPE);
	return type.getID();
}

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

@Override
public String getSpecialName(ItemStack stack)
{
	if (stack.getItemDamage() == 0)
	{
		return "darkred";
	}
	if (stack.getItemDamage() == 1)
	{
		return "darkyellow";
	}
	if (stack.getItemDamage() == 2)
	{
		return "paleyellow";
	}
	return "darkred";
}

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

 

 

 

TestBlocks class:

 

 

package com.test.init;

import net.minecraft.block.Block;
import net.minecraft.block.material.Material;
import net.minecraft.client.Minecraft;
import net.minecraft.client.resources.model.ModelResourceLocation;
import net.minecraft.item.Item;
import net.minecraftforge.client.model.ModelLoader;
import net.minecraftforge.fml.common.registry.GameRegistry;

import com.test.Main;
import com.test.blocks.BlockDisplaySeparator;
import com.test.blocks.BlockFabric;
import com.test.blocks.BlockFlashPortal;
import com.test.blocks.BlockGui;
import com.test.blocks.BlockHeroesPlusCrafter;
import com.test.blocks.ItemBlockMeta;
import com.test.client.Reference;
import com.test.util.EnumHandler;

public class TestBlocks
{
public static Block flashverse_rock;
public static BlockFlashPortal flashverse_portal;
/*public static Block gui_block;*/
public static Block heroes_plus_crafter;
public static Block display_separator;
public static Block fabric;

public static void main()
{
	init();
	register();
}

public static void init()
{
	flashverse_rock = new Block(Material.rock).setUnlocalizedName("flashverse_rock").setRegistryName("flashverse_rock").setCreativeTab(Main.tabTest);
	flashverse_portal = new BlockFlashPortal();
	/*gui_block = new BlockGui(Material.rock, "gui_block").setRegistryName("gui_block");*/
	heroes_plus_crafter = new BlockHeroesPlusCrafter(Material.rock);
	display_separator = new BlockDisplaySeparator("display_separator", Material.anvil).setRegistryName("display_separator").setCreativeTab(Main.tabTest);
	fabric = new BlockFabric();
}

public static void register()
{
	GameRegistry.registerBlock(flashverse_rock, flashverse_rock.getRegistryName());
	GameRegistry.registerBlock(flashverse_portal, flashverse_portal.getRegistryName());
	GameRegistry.registerBlock(heroes_plus_crafter, heroes_plus_crafter.getRegistryName());
	/*GameRegistry.registerBlock(gui_block, gui_block.getRegistryName());*/
	GameRegistry.registerBlock(display_separator, display_separator.getRegistryName());
	GameRegistry.registerBlock(fabric, ItemBlockMeta.class, "fabric");
}

public static void registerRenders()
{
	registerRender(flashverse_rock);
	registerRender(flashverse_portal);
	registerRender(heroes_plus_crafter);
	/*registerRender(gui_block);*/
	registerRender(display_separator);

	for (int i = 0; i < EnumHandler.BlockFabricTypes.values().length; i++)
	{
		registerRender(fabric, i, EnumHandler.BlockFabricTypes.values()[i].getName() + "_fabric");
	}
}

public static void registerRender(Block block)
{
	Minecraft.getMinecraft().getRenderItem().getItemModelMesher().register(Item.getItemFromBlock(block), 0, new ModelResourceLocation(Reference.MOD_ID + ":" + block.getUnlocalizedName().substring(5), "inventory"));
}

public static void registerRender(Block block, int meta, String fileName)
{
	Minecraft.getMinecraft().getRenderItem().getItemModelMesher().register(Item.getItemFromBlock(block), meta, new ModelResourceLocation(Reference.MOD_ID + ":" + fileName, "inventory"));
}
}

 

 

 

ClientProxy class:

 

 

package com.test.client.proxies;

import net.minecraft.client.Minecraft;
import net.minecraft.client.resources.model.ModelBakery;
import net.minecraft.item.Item;
import net.minecraft.util.ResourceLocation;

import com.test.client.Reference;
import com.test.gui.GuiItem;
import com.test.init.TestBlocks;
import com.test.init.TestEntities;
import com.test.init.TestItems;

public class ClientProxy extends CommonProxy
{	
@Override
public void preInit()
{
	TestEntities.registerRenders();
}

@Override
public void init()
{
	TestBlocks.registerRenders();
	TestItems.registerRenders();
	ModelBakery.registerItemVariants(Item.getItemFromBlock(TestBlocks.fabric), new ResourceLocation(Reference.MOD_ID + ":darkred_fabric"), new ResourceLocation(Reference.MOD_ID + ":darkyellow_fabric"), new ResourceLocation(Reference.MOD_ID + ":paleyellow_fabric"));
}

@Override
public void postInit()
{
}

@Override
public void openMyGui()
{
	Minecraft.getMinecraft().displayGuiScreen(new GuiItem());
}
}

 

 

 

Main class:

 

 

package com.test;

import net.minecraft.block.Block;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.entity.SharedMonsterAttributes;
import net.minecraft.item.Item;
import net.minecraft.potion.Potion;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.fluids.Fluid;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.common.Mod.EventHandler;
import net.minecraftforge.fml.common.Mod.Instance;
import net.minecraftforge.fml.common.SidedProxy;
import net.minecraftforge.fml.common.event.FMLInitializationEvent;
import net.minecraftforge.fml.common.event.FMLPostInitializationEvent;
import net.minecraftforge.fml.common.event.FMLPreInitializationEvent;
import net.minecraftforge.fml.common.network.NetworkRegistry;

import com.test.client.Reference;
import com.test.client.tabTest;
import com.test.client.handler.IExtendedEntityPropertiesHandler;
import com.test.client.handler.SpeedforceEventHandler;
import com.test.client.proxies.CommonProxy;
import com.test.dimensions.TestBiomesRegistry;
import com.test.dimensions.TestDimensions;
import com.test.gui.GuiHandler;
import com.test.heroespluscrafter.TestRecipes;
import com.test.init.TestBlocks;
import com.test.init.TestEntities;
import com.test.init.TestItems;
import com.test.init.TestTileEntities;

@Mod(modid = Reference.MOD_ID, name = Reference.MOD_NAME, version = Reference.VERSION)
public class Main
{
@Instance(Reference.MOD_ID)
public static Main instance;

@SidedProxy(clientSide = Reference.CLIENT_PROXY, serverSide = Reference.SERVER_PROXY)
public static CommonProxy proxy;

public static Potion positive_speedforce;
public static Potion negative_speedforce;
public static Potion broken_speedforce;
public static Potion flight;

public static final int heroesCrafterID = 0;

public static final CreativeTabs tabTest = new tabTest("TestTab");

@EventHandler
public static void preInit(FMLPreInitializationEvent event)
{
	proxy.preInit();

	TestDimensions.init();
	TestBlocks.main();
	TestItems.main();
}

@EventHandler
public static void init(FMLInitializationEvent event)
{
	proxy.init();
	TestEntities.init();
	NetworkRegistry.INSTANCE.registerGuiHandler(instance, new GuiHandler());
	MinecraftForge.EVENT_BUS.register(new SpeedforceEventHandler());
	MinecraftForge.EVENT_BUS.register(new IExtendedEntityPropertiesHandler());
	positive_speedforce = new Abilities(new ResourceLocation("positive_speedforce"), false, 0).setIconIndex(0, 0).setPotionName("Speed Force").registerPotionAttributeModifier(SharedMonsterAttributes.movementSpeed, "f3f125ac-a67f-4188-aff7-24f0cf75b010", 1.0D, 2);
	negative_speedforce = new Abilities(new ResourceLocation("negative_speedforce"), false, 0).setIconIndex(0, 1).setPotionName("Negative Speed Force").registerPotionAttributeModifier(SharedMonsterAttributes.movementSpeed, "c9782a5f-67ef-49cf-b915-687ed1d9c1d8", 1.25D, 2);
	broken_speedforce = new Abilities(new ResourceLocation("broken_speedforce"), false, 0).setIconIndex(0, 2).setPotionName("Broken Speed Force").registerPotionAttributeModifier(SharedMonsterAttributes.movementSpeed, "076987f2-75e8-49cf-9996-bf818b64cb46", 1.5D, 2);
	flight = new Abilities(new ResourceLocation("flight"), false, 0).setIconIndex(1, 2).setPotionName("Flight");
}

@EventHandler
public static void postInit(FMLPostInitializationEvent event)
{
	proxy.postInit();
	TestRecipes.init();
}
}

 

 

 

Fabric.json Blockstates:

 

 

{
    "variants": {
        "type=darkred": { "model" : "test:darkred_fabric" },
        "type=darkyellow": { "model" : "test:darkyellow_fabric" },
        "type=paleyellow": { "model" : "test:paleyellow_fabric" }
    }
}

 

 

width=620 height=260http://www.startrek.com/uploads/assets/articles/61c89a9d73c284bda486afaeaf01cdb27180359b.jpg[/img]

Till next time. Thank you for delivering funny scenes to Star Trek as Chekov :) . Will always remember you

Posted

You need to override

Item#getMetadata(int)

(not

getMeta[b]D[/b]ata

) to allow the

ItemBlock

to place block metadata values other than 0. This is why you should always annotate override methods with

@Override

.

 

The tutorial's code appears to be poorly written. In addition to not overriding the correct method, it doesn't specify the generic type argument of the

PropertyEnum

field and uses a series of

if

statements for metadata/unlocalised names instead of encapsulating them in the enum.

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 need to override

Item#getMetadata(int)

(not

getMeta[b]D[/b]ata

) to allow the

ItemBlock

to place block metadata values other than 0. This is why you should always annotate override methods with

@Override

.

 

The tutorial's code appears to be poorly written. In addition to not overriding the correct method, it doesn't specify the generic type argument of the

PropertyEnum

field and uses a series of

if

statements for metadata/unlocalised names instead of encapsulating them in the enum.

 

Thank you! I thought the getMetadata method existed in the ItemBlock class and when I checked it, there was no method, so I was stumped. But now, I've learnt that the getMetadata method exists in the Item class. Thanks a lot!

width=620 height=260http://www.startrek.com/uploads/assets/articles/61c89a9d73c284bda486afaeaf01cdb27180359b.jpg[/img]

Till next time. Thank you for delivering funny scenes to Star Trek as Chekov :) . Will always remember you

Posted

You need to override

Item#getMetadata(int)

(not

getMeta[b]D[/b]ata

) to allow the

ItemBlock

to place block metadata values other than 0. This is why you should always annotate override methods with

@Override

.

 

The tutorial's code appears to be poorly written. In addition to not overriding the correct method, it doesn't specify the generic type argument of the

PropertyEnum

field and uses a series of

if

statements for metadata/unlocalised names instead of encapsulating them in the enum.

 

Thank you! I thought the getMetadata method existed in the ItemBlock class and when I checked it, there was no method, so I was stumped. But now, I've learnt that the getMetadata method exists in the Item class. Thanks a lot!

width=620 height=260http://www.startrek.com/uploads/assets/articles/61c89a9d73c284bda486afaeaf01cdb27180359b.jpg[/img]

Till next time. Thank you for delivering funny scenes to Star Trek as Chekov :) . Will always remember you

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.