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

Hey Modders,

 

I'm testing an idea of an ultra sensitive block that has to be harvested with silk touch else it will explode upon being harvested. For this, I created an event handler which subscribed to the harvest drop event and checks to see if the block was harvested with silk touch and if it isn't, it creates an explosion (Code below)

 

@SubscribeEvent
public void explosion(BlockEvent.HarvestDropsEvent event){

	if(event.block == Blocks.grass || event.block == Blocks.dirt){
		if(event.isSilkTouching == false){
			event.world.createExplosion(event.harvester, event.harvester.posX, event.harvester.posY, event.harvester.posZ, 10, false);
		}
	}
}

 

However, when I load up a newly generated world, I am able to mine up the block, and able to see the explosion but the terrain and the player are unaffected. Could you guys please tell me what I am missing? I've taken a look at the Creeper code as well as the TNT code and it looks to be the same (I've even tried throwing in the condition to check and see if the world is remote and it doesn't change anything)

  • Author

I'm noticing now that I am getting a pretty consistent crash with a Null Point Exception and after poking at it with a stick, it seems like the EntityPlayer is being set to null before getting passed to the create explosion.

 

Is there a way to make this more stable? I've thought about checking whether or not the event.Harvester is null before going to the create explosion, but is that the best way?

  • Author

Apologies. I must have been very tired when I read the docs the first time around because I completely missed that.

 

Also thank you for the suggestion!

  • Author

One follow up question: When I implement the resulting explosion, it destroys the terrain but does not damage the player.

 

Is this because of the fact that I am implementing the create explosion in the event? Here's the updated code:

 

@SubscribeEvent
public void explosion(BlockEvent.BreakEvent event){
	Block block = event.block;
	EntityPlayer player = event.getPlayer();
	World world = event.world;
	int x = event.x;
	int y = event.y;
	int z = event.z;
	int blockMetadata = event.blockMetadata;

	if (!world.isRemote)
        	{       
		if((block == Blocks.grass || block == Blocks.dirt) && EnchantmentHelper.getSilkTouchModifier(player)!=true) {
			world.createExplosion(player, x, y, z, 3, true);

			}
		}
        }

I'm pretty sure the entity passed in does not take any damage from the explosion, as it is the one that 'caused' it, and since you're passing the player, well there you go :P You'd have to look at the Explosion class to make sure, though, but that's how I remember it working.

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.