Jump to content

Recommended Posts

Posted (edited)

I want to have all my ores within one class. I can do them with dimensions on their own, i can do them with variants on their own but I can't figure out the maths for getmetafromstate and getstatefrommeta etc. to make them both work together. Any help would be appreciated.

 

public class EnumHandler
{
	//Variants
	public static enum EnumType implements IStringSerializable
	{
		COPPER(1, "copper"),
		ALUMINIUM(2, "aluminium");
		
		private static final EnumType[] META_LOOKUP = new EnumType[values().length];
		private final int meta;
		private final String name, unlocializedName;
		
		private EnumType(int meta, String name) 
		{
			this(meta, name, name);
		}
		
		private EnumType(int meta, String name, String unlocializedName) 
		{
			this.meta = meta;
			this.name = name;
			this.unlocializedName = unlocializedName;
		}
		
		@Override
		public String getName() 
		{
			return this.name;
		}
		
		public int getMeta() 
		{
			return this.meta;
		}
		
		public String getUnlocializedName() 
		{
			return this.unlocializedName;
		}
		
		@Override
		public String toString() 
		{
			return this.name;
		}
		
		public static EnumType byMetadata(int meta)
		{
			return META_LOOKUP[meta];
		}
		
		static
		{
			for(EnumType enumtype : values())
			{
				META_LOOKUP[enumtype.getMeta()] = enumtype;
			}
		}
	}
	
	//Dimensions
	public static enum EnumDimension implements IStringSerializable
	{
		NETHER(1, "nether"),
		OVERWORLD(2, "overworld"),
		END(3, "end");
		
		private static final EnumDimension[] META_LOOKUP = new EnumDimension[values().length];
		private final int meta;
		private final String name, unlocializedName;
		
		private EnumDimension(int meta, String name) 
		{
			this(meta, name, name);
		}
		
		private EnumDimension(int meta, String name, String unlocializedName) 
		{
			this.meta = meta;
			this.name = name;
			this.unlocializedName = unlocializedName;
		}
		
		@Override
		public String getName() 
		{
			return this.name;
		}
		
		public int getMeta() 
		{
			return this.meta;
		}
		
		public String getUnlocializedName() 
		{
			return this.unlocializedName;
		}
		
		@Override
		public String toString() 
		{
			return this.name;
		}
		
		public static EnumDimension byMetadata(int meta)
		{
			return META_LOOKUP[meta];
		}
		
		static
		{
			for(EnumDimension dimension : values())
			{
				META_LOOKUP[dimension.getMeta()] = dimension;
			}
		}
		
	}
}
public class BlockOres extends Block implements IHasModel,IMetaName
{
	public static final PropertyEnum<EnumHandler.EnumDimension> DIMENSION = PropertyEnum.<EnumHandler.EnumDimension>create("dimension", EnumHandler.EnumDimension.class);
	public static final PropertyEnum<EnumHandler.EnumType> VARIANT = PropertyEnum.<EnumHandler.EnumType>create("variant", EnumHandler.EnumType.class);
	
	private String name;
	
	public BlockOres(String name) 
	{
		super(Material.ROCK);
		
		setUnlocalizedName(name);
		setRegistryName(name);
		this.name = name;
		setSoundType(SoundType.STONE);
		setCreativeTab(Main.TAB_MOD);
		setDefaultState(this.blockState.getBaseState().withProperty(DIMENSION, EnumHandler.EnumDimension.OVERWORLD).withProperty(VARIANT, EnumHandler.EnumType.COPPER));

		BlockInit.BLOCKS.add(this);
		ItemInit.ITEMS.add(new ItemBlockVariants(this).setRegistryName(this.getRegistryName()));
	}
	
	@Override
	public int damageDropped(IBlockState state) 
	{
		return ((EnumHandler.EnumDimension)state.getValue(DIMENSION)).getMeta() * ((EnumHandler.EnumType)state.getValue(VARIANT)).getMeta();
	}
	
	@Override
	public void getSubBlocks(CreativeTabs itemIn, NonNullList<ItemStack> items) 
	{
		for(EnumHandler.EnumDimension dimension : EnumHandler.EnumDimension.values())
		{
			for(EnumHandler.EnumType variant : EnumHandler.EnumType.values())
			{
				items.add(new ItemStack(this, 1, dimension.getMeta() * variant.getMeta()));
			}
		}
	}
	
	@Override
	public IBlockState getStateFromMeta(int meta) 
	{
		return this.getDefaultState().withProperty(VARIANT, EnumHandler.EnumType.byMetadata(meta)).withProperty(DIMENSION, EnumHandler.EnumDimension.byMetadata(meta));
	}
	
	@Override
	public int getMetaFromState(IBlockState state) 
	{
		return ((EnumHandler.EnumDimension)state.getValue(DIMENSION)).getMeta() * ((EnumHandler.EnumType)state.getValue(VARIANT)).getMeta();
	}
	
	@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
	protected BlockStateContainer createBlockState() 
	{
		return new BlockStateContainer(this, new IProperty[] {DIMENSION, VARIANT});
	}
	
	@Override
	public String getSpecialName(ItemStack stack) 
	{
		return EnumHandler.EnumType.values()[stack.getItemDamage()].getName() + "_" + EnumHandler.EnumDimension.values()[stack.getItemDamage()].getName();
	}
	
	@Override
	public void registerModels() 
	{
		for(int i = 0; i < EnumHandler.EnumDimension.values().length; i++)
		{
			for(int j = 0; j < EnumHandler.EnumType.values().length; j++)
			{
				Main.proxy.registerVariantsRenderer(Item.getItemFromBlock(this), i, this.name + "_" + EnumHandler.EnumType.values()[j].getName() + "_" + EnumHandler.EnumDimension.values()[i].getName(), "inventory");
			}
		}
	}

 

Edited by HarryTechReviews
Posted

You have 4 bits of information to work with.

You have an ore type enum that contains two values (and inexplicably starts at 1 instead of 0) and a dimension enum that contains 3 values (again, starting with 1 instead of 0).

 

Two times three is six, which means there's enough bits to store the data in metadata.

Your type enum can be contained within 1 bit (0 / 1) and the dimension needs two (00 / 01 / 10). Now you just need some bitwise math.

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

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.