package me.km.blocks; import com.google.common.collect.Sets; import java.util.List; import java.util.Random; import java.util.Set; import me.km.utils.ReflectionUtils; import net.minecraft.block.Block; import net.minecraft.block.material.Material; import net.minecraft.block.state.IBlockState; import net.minecraft.enchantment.EnchantmentProtection; import net.minecraft.entity.Entity; import net.minecraft.entity.EntityLivingBase; import net.minecraft.entity.item.EntityTNTPrimed; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.init.Blocks; import net.minecraft.init.SoundEvents; import net.minecraft.util.DamageSource; import net.minecraft.util.EnumParticleTypes; import net.minecraft.util.SoundCategory; import net.minecraft.util.math.AxisAlignedBB; import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.MathHelper; import net.minecraft.util.math.Vec3d; import net.minecraft.world.Explosion; import net.minecraft.world.World; import net.minecraftforge.event.world.ExplosionEvent; import net.minecraftforge.fml.common.eventhandler.EventPriority; import net.minecraftforge.fml.common.eventhandler.SubscribeEvent; public class BlockEvents { @SubscribeEvent(receiveCanceled = false, priority = EventPriority.LOWEST) public void onExplosion(ExplosionEvent.Start e) { Explosion ex = e.getExplosion(); Entity ent = ReflectionUtils.getExploder(ex); if(ReflectionUtils.getSize(ex) > 10) { ReflectionUtils.setSize(ex, 10); } e.setCanceled(true); doExplosionA(ex, e.getWorld(), ent instanceof EntityTNTPrimed); doExplosionB(ex, e.getWorld(), true); } public void doExplosionA(Explosion ex, World w, boolean tnt) { Set set = Sets.newHashSet(); int i = 16; Vec3d v = ex.getPosition(); float explosionSize = ReflectionUtils.getSize(ex); Entity exploder = ReflectionUtils.getExploder(ex); for(int j = 0; j < 16; ++j) { for(int k = 0; k < 16; ++k) { for(int l = 0; l < 16; ++l) { if(j == 0 || j == 15 || k == 0 || k == 15 || l == 0 || l == 15) { double d0 = j / 15f * 2 - 1; double d1 = k / 15f * 2 - 1; double d2 = l / 15f * 2 - 1; double d3 = Math.sqrt(d0 * d0 + d1 * d1 + d2 * d2); d0 = d0 / d3; d1 = d1 / d3; d2 = d2 / d3; float f = explosionSize * (0.7f + w.rand.nextFloat() * 0.6f); double d4 = v.x; double d6 = v.y; double d8 = v.z; for(float f1 = 0.3F; f > 0.0F; f -= 0.22500001F) { BlockPos blockpos = new BlockPos(d4, d6, d8); IBlockState iblockstate = w.getBlockState(blockpos); if (iblockstate.getMaterial() != Material.AIR) { float f2 = exploder != null ? exploder.getExplosionResistance(ex, w, blockpos, iblockstate) : iblockstate.getBlock().getExplosionResistance(w, blockpos, null, ex); // TNT hook if(tnt && f2 <= 10) { f2 = Math.min(f2, 1); } // end f -= (f2 + 0.3F) * 0.3F; } if (f > 0.0F && (exploder == null || exploder.canExplosionDestroyBlock(ex, w, blockpos, iblockstate, f))) { set.add(blockpos); } d4 += d0 * 0.3d; d6 += d1 * 0.3d; d8 += d2 * 0.3d; } } } } } ex.getAffectedBlockPositions().addAll(set); float f3 = explosionSize * 2; int k1 = MathHelper.floor(v.x - f3 - 1); int l1 = MathHelper.floor(v.x + f3 + 1); int i2 = MathHelper.floor(v.y - f3 - 1); int i1 = MathHelper.floor(v.y + f3 + 1); int j2 = MathHelper.floor(v.z - f3 - 1); int j1 = MathHelper.floor(v.z + f3 + 1); List list = w.getEntitiesWithinAABBExcludingEntity(exploder, new AxisAlignedBB(k1, i2, j2, l1, i1, j1)); net.minecraftforge.event.ForgeEventFactory.onExplosionDetonate(w, ex, list, f3); Vec3d vec3d = new Vec3d(v.x, v.y, v.z); for(int k2 = 0; k2 < list.size(); k2++) { Entity entity = list.get(k2); if(!entity.isImmuneToExplosions()) { double d12 = entity.getDistance(v.x, v.y, v.z) / f3; if(d12 <= 1.0D) { double d5 = entity.posX - v.x; double d7 = entity.posY + (double)entity.getEyeHeight() - v.y; double d9 = entity.posZ - v.z; double d13 = MathHelper.sqrt(d5 * d5 + d7 * d7 + d9 * d9); if(d13 != 0.0D) { d5 = d5 / d13; d7 = d7 / d13; d9 = d9 / d13; double d14 = w.getBlockDensity(vec3d, entity.getEntityBoundingBox()); double d10 = (1.0D - d12) * d14; entity.attackEntityFrom(DamageSource.causeExplosionDamage(ex), (float)((int)((d10 * d10 + d10) / 2 * 7 * f3 + 1))); double d11 = d10; if(entity instanceof EntityLivingBase) { d11 = EnchantmentProtection.getBlastDamageReduction((EntityLivingBase) entity, d10); } entity.motionX += d5 * d11; entity.motionY += d7 * d11; entity.motionZ += d9 * d11; if (entity instanceof EntityPlayer) { EntityPlayer entityplayer = (EntityPlayer)entity; if (!entityplayer.isSpectator() && (!entityplayer.isCreative() || !entityplayer.capabilities.isFlying)) { ex.getPlayerKnockbackMap().put(entityplayer, new Vec3d(d5 * d10, d7 * d10, d9 * d10)); } } } } } } } private void doExplosionB(Explosion ex, World w, boolean spawnParticles) { Vec3d v = ex.getPosition(); double x = v.x; double y = v.y; double z = v.z; w.playSound(null, x, y, z, SoundEvents.ENTITY_GENERIC_EXPLODE, SoundCategory.BLOCKS, 4, (1 + (w.rand.nextFloat() - w.rand.nextFloat()) * 0.2f) * 0.7f); if(ReflectionUtils.getSize(ex) >= 2 && ReflectionUtils.isSmoking(ex)) { w.spawnParticle(EnumParticleTypes.EXPLOSION_HUGE, x, y, z, 1, 0, 0, new int[0]); } else { w.spawnParticle(EnumParticleTypes.EXPLOSION_LARGE, x, y, z, 1, 0, 0, new int[0]); } if(ReflectionUtils.isSmoking(ex)) { float explosionSize = ReflectionUtils.getSize(ex); ex.getAffectedBlockPositions().stream().forEach(blockpos -> { IBlockState iblockstate = w.getBlockState(blockpos); Block block = iblockstate.getBlock(); if(spawnParticles) { double d0 = blockpos.getX() + w.rand.nextFloat(); double d1 = blockpos.getY() + w.rand.nextFloat(); double d2 = blockpos.getZ() + w.rand.nextFloat(); double d3 = d0 - x; double d4 = d1 - y; double d5 = d2 - z; double d6 = MathHelper.sqrt(d3 * d3 + d4 * d4 + d5 * d5); d3 = d3 / d6; d4 = d4 / d6; d5 = d5 / d6; double d7 = 0.5D / (d6 / explosionSize + 0.1D); d7 = d7 * (w.rand.nextFloat() * w.rand.nextFloat() + 0.3F); d3 = d3 * d7; d4 = d4 * d7; d5 = d5 * d7; w.spawnParticle(EnumParticleTypes.EXPLOSION_NORMAL, (d0 + x) / 2, (d1 + y) / 2, (d2 + z) / 2, d3, d4, d5, new int[0]); w.spawnParticle(EnumParticleTypes.SMOKE_NORMAL, d0, d1, d2, d3, d4, d5, new int[0]); } if(iblockstate.getMaterial() != Material.AIR) { if(block.canDropFromExplosion(ex)) { block.dropBlockAsItem(w, blockpos, w.getBlockState(blockpos), 0); } block.onBlockExploded(w, blockpos, ex); } }); } if(ReflectionUtils.isFlaming(ex)) { Random rand = ReflectionUtils.getRandom(ex); ex.getAffectedBlockPositions().stream() .filter(pos -> (w.getBlockState(pos).getMaterial() == Material.AIR && w.getBlockState(pos.down()).isFullBlock() && rand.nextInt(3) == 0)) .forEach(pos -> w.setBlockState(pos, Blocks.FIRE.getDefaultState())); } } }