Jump to content

[1.12] Setting different harvest levels for blockstates?


DoomCrystal

Recommended Posts

I want to add many very similar ores to the game, so I created an Enum and a Block with a PropertyEnum to serve as the "base" mod ore block. Code:

public class BlockModOre extends Block implements IMetaBlockName{

	public static final PropertyEnum TYPE = PropertyEnum.create("type", Ores.class);
	
	
	public BlockModOre(Material materialIn) {
		super(materialIn);
		this.setUnlocalizedName("ore");
		this.setRegistryName(new ResourceLocation(Reference.MODID, "ore"));
		this.setCreativeTab(Atomic.tab);
		this.setDefaultState(this.blockState.getBaseState().withProperty(TYPE, Ores.BAUXITE));
		this.setHardness(3);
		this.setResistance(15);
	}
	@Override
	public int getMetaFromState(IBlockState state) {
		Ores type = state.getValue(TYPE);
		return type.getID();
	}
	@Override
	public IBlockState getStateFromMeta(int meta) {
		return this.getDefaultState().withProperty(TYPE, Ores.values()[meta]);
	}
	@Override
	protected BlockStateContainer createBlockState() {
		return new BlockStateContainer(this, new IProperty[] {TYPE});
	}
	@Override
	public void getSubBlocks(CreativeTabs itemIn, NonNullList<ItemStack> items)
    {
		for(int i = 0; i < Ores.values().length; i++) {
			items.add(new ItemStack(this, 1, i));
		}
    }
	@Override
	public String getSpecialName(ItemStack stack) {
		return Ores.values()[stack.getItemDamage()].getName();
	}
	@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 int damageDropped(IBlockState state) {
        return getMetaFromState(state);
    }
}

All these block have the same Hardness and Resistance, the same for all ores, but some require different levels of pickaxe to harvest them. The blocks are initialized through this method in ModBlocks:

public static void init(RegistryEvent.Register<Block> event) {
		for(Block b : blocks) {
			if(b instanceof IMetaBlockName) {
				ModItems.addItemBlock(b, new ItemBlockBase(b));
			}
			else {
				ModItems.addItemBlock(b);
			}
			event.getRegistry().register(b);
		}
	}

The "Ores" enum has a property which contains each ore's harvest level, but I cant see where to place the typical setHarvestLevel("pickaxe", HARVESTLEVEL). Thanks in Advance! (Everything else about the block works perfectly right now)

Link to comment
Share on other sites

...override getHarvestLevel?

Edited by Draco18s
  • Like 1

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.

Link to comment
Share on other sites

Thank you for pointing me back in that direction. I had originally searched there but skipped this method because my real question was how to access this block's state from within its own class. Turns out getHarvestLevel takes the state as a parameter, allowing me to access it that way. Thanks!

Link to comment
Share on other sites

1 hour ago, DoomCrystal said:

Thank you for pointing me back in that direction. I had originally searched there but skipped this method because my real question was how to access this block's state from within its own class. Turns out getHarvestLevel takes the state as a parameter, allowing me to access it that way. Thanks!

 

Blocks are singletons, there's only one instance per block type. This means that a Block can't have a single IBlockState, BlockPos or World field, since a Block can have any number of valid states at any number of positions in any number of dimensions. Instead, any method that needs access to the these receives them as parameters.

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

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.