BlockEvents.java 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. package me.km.blocks;
  2. import com.google.common.collect.Sets;
  3. import java.util.List;
  4. import java.util.Random;
  5. import java.util.Set;
  6. import me.km.utils.ReflectionUtils;
  7. import net.minecraft.block.Block;
  8. import net.minecraft.block.material.Material;
  9. import net.minecraft.block.state.IBlockState;
  10. import net.minecraft.enchantment.EnchantmentProtection;
  11. import net.minecraft.entity.Entity;
  12. import net.minecraft.entity.EntityLivingBase;
  13. import net.minecraft.entity.item.EntityTNTPrimed;
  14. import net.minecraft.entity.player.EntityPlayer;
  15. import net.minecraft.init.Blocks;
  16. import net.minecraft.init.SoundEvents;
  17. import net.minecraft.util.DamageSource;
  18. import net.minecraft.util.EnumParticleTypes;
  19. import net.minecraft.util.SoundCategory;
  20. import net.minecraft.util.math.AxisAlignedBB;
  21. import net.minecraft.util.math.BlockPos;
  22. import net.minecraft.util.math.MathHelper;
  23. import net.minecraft.util.math.Vec3d;
  24. import net.minecraft.world.Explosion;
  25. import net.minecraft.world.World;
  26. import net.minecraftforge.event.world.ExplosionEvent;
  27. import net.minecraftforge.fml.common.eventhandler.EventPriority;
  28. import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
  29. public class BlockEvents
  30. {
  31. @SubscribeEvent(receiveCanceled = false, priority = EventPriority.LOWEST)
  32. public void onExplosion(ExplosionEvent.Start e)
  33. {
  34. Explosion ex = e.getExplosion();
  35. Entity ent = ReflectionUtils.getExploder(ex);
  36. if(ReflectionUtils.getSize(ex) > 10)
  37. {
  38. ReflectionUtils.setSize(ex, 10);
  39. }
  40. e.setCanceled(true);
  41. doExplosionA(ex, e.getWorld(), ent instanceof EntityTNTPrimed);
  42. doExplosionB(ex, e.getWorld(), true);
  43. }
  44. public void doExplosionA(Explosion ex, World w, boolean tnt)
  45. {
  46. Set<BlockPos> set = Sets.<BlockPos>newHashSet();
  47. int i = 16;
  48. Vec3d v = ex.getPosition();
  49. float explosionSize = ReflectionUtils.getSize(ex);
  50. Entity exploder = ReflectionUtils.getExploder(ex);
  51. for(int j = 0; j < 16; ++j)
  52. {
  53. for(int k = 0; k < 16; ++k)
  54. {
  55. for(int l = 0; l < 16; ++l)
  56. {
  57. if(j == 0 || j == 15 || k == 0 || k == 15 || l == 0 || l == 15)
  58. {
  59. double d0 = j / 15f * 2 - 1;
  60. double d1 = k / 15f * 2 - 1;
  61. double d2 = l / 15f * 2 - 1;
  62. double d3 = Math.sqrt(d0 * d0 + d1 * d1 + d2 * d2);
  63. d0 = d0 / d3;
  64. d1 = d1 / d3;
  65. d2 = d2 / d3;
  66. float f = explosionSize * (0.7f + w.rand.nextFloat() * 0.6f);
  67. double d4 = v.x;
  68. double d6 = v.y;
  69. double d8 = v.z;
  70. for(float f1 = 0.3F; f > 0.0F; f -= 0.22500001F)
  71. {
  72. BlockPos blockpos = new BlockPos(d4, d6, d8);
  73. IBlockState iblockstate = w.getBlockState(blockpos);
  74. if (iblockstate.getMaterial() != Material.AIR)
  75. {
  76. float f2 = exploder != null ? exploder.getExplosionResistance(ex, w, blockpos, iblockstate) : iblockstate.getBlock().getExplosionResistance(w, blockpos, null, ex);
  77. // TNT hook
  78. if(tnt && f2 <= 10)
  79. {
  80. f2 = Math.min(f2, 1);
  81. }
  82. // end
  83. f -= (f2 + 0.3F) * 0.3F;
  84. }
  85. if (f > 0.0F && (exploder == null || exploder.canExplosionDestroyBlock(ex, w, blockpos, iblockstate, f)))
  86. {
  87. set.add(blockpos);
  88. }
  89. d4 += d0 * 0.3d;
  90. d6 += d1 * 0.3d;
  91. d8 += d2 * 0.3d;
  92. }
  93. }
  94. }
  95. }
  96. }
  97. ex.getAffectedBlockPositions().addAll(set);
  98. float f3 = explosionSize * 2;
  99. int k1 = MathHelper.floor(v.x - f3 - 1);
  100. int l1 = MathHelper.floor(v.x + f3 + 1);
  101. int i2 = MathHelper.floor(v.y - f3 - 1);
  102. int i1 = MathHelper.floor(v.y + f3 + 1);
  103. int j2 = MathHelper.floor(v.z - f3 - 1);
  104. int j1 = MathHelper.floor(v.z + f3 + 1);
  105. List<Entity> list = w.getEntitiesWithinAABBExcludingEntity(exploder, new AxisAlignedBB(k1, i2, j2, l1, i1, j1));
  106. net.minecraftforge.event.ForgeEventFactory.onExplosionDetonate(w, ex, list, f3);
  107. Vec3d vec3d = new Vec3d(v.x, v.y, v.z);
  108. for(int k2 = 0; k2 < list.size(); k2++)
  109. {
  110. Entity entity = list.get(k2);
  111. if(!entity.isImmuneToExplosions())
  112. {
  113. double d12 = entity.getDistance(v.x, v.y, v.z) / f3;
  114. if(d12 <= 1.0D)
  115. {
  116. double d5 = entity.posX - v.x;
  117. double d7 = entity.posY + (double)entity.getEyeHeight() - v.y;
  118. double d9 = entity.posZ - v.z;
  119. double d13 = MathHelper.sqrt(d5 * d5 + d7 * d7 + d9 * d9);
  120. if(d13 != 0.0D)
  121. {
  122. d5 = d5 / d13;
  123. d7 = d7 / d13;
  124. d9 = d9 / d13;
  125. double d14 = w.getBlockDensity(vec3d, entity.getEntityBoundingBox());
  126. double d10 = (1.0D - d12) * d14;
  127. entity.attackEntityFrom(DamageSource.causeExplosionDamage(ex), (float)((int)((d10 * d10 + d10) / 2 * 7 * f3 + 1)));
  128. double d11 = d10;
  129. if(entity instanceof EntityLivingBase)
  130. {
  131. d11 = EnchantmentProtection.getBlastDamageReduction((EntityLivingBase) entity, d10);
  132. }
  133. entity.motionX += d5 * d11;
  134. entity.motionY += d7 * d11;
  135. entity.motionZ += d9 * d11;
  136. if (entity instanceof EntityPlayer)
  137. {
  138. EntityPlayer entityplayer = (EntityPlayer)entity;
  139. if (!entityplayer.isSpectator() && (!entityplayer.isCreative() || !entityplayer.capabilities.isFlying))
  140. {
  141. ex.getPlayerKnockbackMap().put(entityplayer, new Vec3d(d5 * d10, d7 * d10, d9 * d10));
  142. }
  143. }
  144. }
  145. }
  146. }
  147. }
  148. }
  149. private void doExplosionB(Explosion ex, World w, boolean spawnParticles)
  150. {
  151. Vec3d v = ex.getPosition();
  152. double x = v.x;
  153. double y = v.y;
  154. double z = v.z;
  155. w.playSound(null, x, y, z, SoundEvents.ENTITY_GENERIC_EXPLODE, SoundCategory.BLOCKS, 4, (1 + (w.rand.nextFloat() - w.rand.nextFloat()) * 0.2f) * 0.7f);
  156. if(ReflectionUtils.getSize(ex) >= 2 && ReflectionUtils.isSmoking(ex))
  157. {
  158. w.spawnParticle(EnumParticleTypes.EXPLOSION_HUGE, x, y, z, 1, 0, 0, new int[0]);
  159. }
  160. else
  161. {
  162. w.spawnParticle(EnumParticleTypes.EXPLOSION_LARGE, x, y, z, 1, 0, 0, new int[0]);
  163. }
  164. if(ReflectionUtils.isSmoking(ex))
  165. {
  166. float explosionSize = ReflectionUtils.getSize(ex);
  167. ex.getAffectedBlockPositions().stream().forEach(blockpos ->
  168. {
  169. IBlockState iblockstate = w.getBlockState(blockpos);
  170. Block block = iblockstate.getBlock();
  171. if(spawnParticles)
  172. {
  173. double d0 = blockpos.getX() + w.rand.nextFloat();
  174. double d1 = blockpos.getY() + w.rand.nextFloat();
  175. double d2 = blockpos.getZ() + w.rand.nextFloat();
  176. double d3 = d0 - x;
  177. double d4 = d1 - y;
  178. double d5 = d2 - z;
  179. double d6 = MathHelper.sqrt(d3 * d3 + d4 * d4 + d5 * d5);
  180. d3 = d3 / d6;
  181. d4 = d4 / d6;
  182. d5 = d5 / d6;
  183. double d7 = 0.5D / (d6 / explosionSize + 0.1D);
  184. d7 = d7 * (w.rand.nextFloat() * w.rand.nextFloat() + 0.3F);
  185. d3 = d3 * d7;
  186. d4 = d4 * d7;
  187. d5 = d5 * d7;
  188. w.spawnParticle(EnumParticleTypes.EXPLOSION_NORMAL, (d0 + x) / 2, (d1 + y) / 2, (d2 + z) / 2, d3, d4, d5, new int[0]);
  189. w.spawnParticle(EnumParticleTypes.SMOKE_NORMAL, d0, d1, d2, d3, d4, d5, new int[0]);
  190. }
  191. if(iblockstate.getMaterial() != Material.AIR)
  192. {
  193. if(block.canDropFromExplosion(ex))
  194. {
  195. block.dropBlockAsItem(w, blockpos, w.getBlockState(blockpos), 0);
  196. }
  197. block.onBlockExploded(w, blockpos, ex);
  198. }
  199. });
  200. }
  201. if(ReflectionUtils.isFlaming(ex))
  202. {
  203. Random rand = ReflectionUtils.getRandom(ex);
  204. ex.getAffectedBlockPositions().stream()
  205. .filter(pos -> (w.getBlockState(pos).getMaterial() == Material.AIR &&
  206. w.getBlockState(pos.down()).isFullBlock() &&
  207. rand.nextInt(3) == 0))
  208. .forEach(pos -> w.setBlockState(pos, Blocks.FIRE.getDefaultState()));
  209. }
  210. }
  211. }