Jump to content
View in the app

A better way to browse. Learn more.

Forge Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Featured Replies

Posted

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)

...override getHarvestLevel?

Edited by Draco18s

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.

  • Author

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!

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.

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...

Important Information

By using this site, you agree to our Terms of Use.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.