Posted August 2, 201411 yr 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)
August 2, 201411 yr The last parameter should be 'true' to destroy blocks. Also, make sure you create the explosion only on the server side, i.e. when the world is NOT remote, otherwise you will end up with 'ghost' blocks of various sorts. http://i.imgur.com/NdrFdld.png[/img]
August 2, 201411 yr 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?
August 3, 201411 yr 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!
August 3, 201411 yr 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); } } }
August 3, 201411 yr 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 You'd have to look at the Explosion class to make sure, though, but that's how I remember it working. http://i.imgur.com/NdrFdld.png[/img]
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.