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.

[1.10.2][FIXED thank you Draco18s!] Break blocks with enchantments

Featured Replies

Posted

I have created a hammer, but I have a problem (yes another problem), I don't know how to break blocks with enchantments so for example silk touch gives you the silk touch block. Here is the code that needs to be changed:

 

worldIn.destroyBlock(offset2, true);

 

Full souce code:

http://pastebin.com/UHZCSs6K

World#DestroyBlock bypasses all the code that is specific to the player harvesting blocks.  If you want to harvest blocks you need to call the code responsible for harvesting blocks.

 

If you want Silk Touch to work, then use your IDE to figure out where the code already references Silk Touch.

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.

Block#harvestBlock doesn't actually set the block to air.  You would know this if you actually looked at the method.

 

Point is, you can't call just one method, or you completely bypass the set of hooks designed to let other mods know that the block was harvested and removed, or in some cases, letting the block itself know.

 

Take a look at PlayerInterationManager#tryHarvestBlock(BlockPos)

 

The series of things to do is:

Block#canHarvestBlock (ensure you can actually harvest the block: you might be able to skip this if you're destroying same-type blocks only)

ItemStack#onBlockDestroyed (you would probably skip this because this is how your Item knows that it destroyed a block, and if that calls these methods again, you have a problem)

Block#removedByPlayer (automatically called Block#onBlockHarvested; default empty method, but used by various blocks to handle TE containers)

Block#onBlockDestroyedByPlayer

Block#harvestBlock (finally get the harvested drops to...drop)

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

I have now changed it to this:

if (state.getBlock().canHarvestBlock(worldIn, offset2, player)) {

state.getBlock().removedByPlayer(state, worldIn, offset2, player, true);

state.getBlock().onBlockDestroyedByPlayer(worldIn, offset2, state);

state.getBlock().harvestBlock(worldIn, player, offset2, state, null, itemStack);

} else {

worldIn.setBlockToAir(offset2);

}

 

The error:

http://pastebin.com/iyU6Z4ec

Gosh. It's almost like you're asking a block of diamond ore what type of dirt it is.

 

When you change position you need to get a NEW BLOCKSTATE or check that the two blocks ARE THE SAME FIRST

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.

state.getBlock() //the block I mined out (diamond ore)

...removedByPlayer(state, worldIn, offset2, player, true); //offset2, the location of a different block (dirt), state: the diamond ore...

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, but there are 2 problems:

1. I can't make it be able to not break unbreakeble blocks

2. Only the original block drops (all of them breakes)

 

if (worldIn.getBlockState(offset2).getBlock().canHarvestBlock(worldIn, offset2, player)) {

worldIn.getBlockState(offset2).getBlock().removedByPlayer(state, worldIn, offset2, player, true);

worldIn.getBlockState(offset2).getBlock().onBlockDestroyedByPlayer(worldIn, offset2, state);

worldIn.getBlockState(offset2).getBlock().harvestBlock(worldIn, player, offset2, state, null,

itemStack);

} else {

if (worldIn.isBlockModifiable(player, offset2)) {

worldIn.setBlockToAir(offset2);

}

}

  • Author

Still doen't work, updated code:

if (worldIn.getBlockState(offset2).getBlock().canHarvestBlock(worldIn, offset2, player)) {

worldIn.getBlockState(offset2).getBlock().removedByPlayer(worldIn.getBlockState(offset2), worldIn,

offset2, player, true);

worldIn.getBlockState(offset2).getBlock().onBlockDestroyedByPlayer(worldIn, offset2,

worldIn.getBlockState(offset2));

worldIn.getBlockState(offset2).getBlock().harvestBlock(worldIn, player, offset2,

worldIn.getBlockState(offset2), null, itemStack);

} else {

if (worldIn.isBlockModifiable(player, offset2)) {

worldIn.setBlockToAir(offset2);

}

}

So, defined "doesn't work" for me here, because this worked for me just fine:

 

	public boolean onBlockDestroyed(ItemStack stack, World world, IBlockState state, BlockPos pos, EntityLivingBase entityLiving) {
	if(entityLiving instanceof EntityPlayer) {
		EntityPlayer harvester = (EntityPlayer) entityLiving;
		Iterable<BlockPos> list = BlockPos.getAllInBox(pos.add(-1, -1, -1), pos.add(1,1,1));
		for(BlockPos offset2 : list) {
			IBlockState state2 = world.getBlockState(offset2);
			Block block2 = state2.getBlock();
			boolean canharvest = block2.canHarvestBlock(world, offset2, harvester);
			if (canharvest) {
				block2.removedByPlayer(state2, world, offset2, harvester, true);
				block2.onBlockDestroyedByPlayer(world, offset2, state2);
				block2.harvestBlock(world, harvester, offset2, state2, null, stack);
			}
			else {
				if (world.isBlockModifiable(harvester, offset2)) {
					world.setBlockToAir(offset2);
				}
			}
		}
	}
        return super.onBlockDestroyed(stack, world, state, pos, entityLiving);
    }

width=800 height=449http://s32.postimg.org/uemjtabol/2016_07_29_13_18_51.png[/img]

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

It now works, THANK YOU so much! I don't know why it didn't work before. The only change was that I had IBlockState offsetState = worldIn.getBlockState(offset2); at the top instead of worldIn.getBlockState(offset2) on every line. Don't know why that made a different, but the important thing is that it now works

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.