Explosion .java

net.minecraft.world.Explosion (class): members: clearAffectedBlockPositions, getPlayerKnockbackMap, getExplosivePlacedBy, playerKnockbackMap, doExplosionA.

net.minecraft.worldclass262 linescommon11.4 KB

API Surface (6)

void doExplosionA()
void doExplosionB(boolean spawnParticles)
Map<EntityPlayer, Vec3> getPlayerKnockbackMap()
EntityLivingBase getExplosivePlacedBy()
void clearAffectedBlockPositions()
List<BlockPos> getAffectedBlockPositions()

Fields & Constants (11)

isFlaming
isSmoking
explosionRNG
worldObj
explosionX
explosionY
explosionZ
exploder
explosionSize
affectedBlockPositions
playerKnockbackMap

String literals (1)

"random.explode"

Full Source

Explosion.java262 lines · MCP 1.8.9 · verbatimOriginal ↗
1
package net.minecraft.world;
2
3
import com.google.common.collect.Lists;
4
import com.google.common.collect.Maps;
5
import com.google.common.collect.Sets;
6
import java.util.List;
7
import java.util.Map;
8
import java.util.Random;
9
import java.util.Set;
10
import net.minecraft.block.Block;
11
import net.minecraft.block.material.Material;
12
import net.minecraft.block.state.IBlockState;
13
import net.minecraft.enchantment.EnchantmentProtection;
14
import net.minecraft.entity.Entity;
15
import net.minecraft.entity.EntityLivingBase;
16
import net.minecraft.entity.item.EntityTNTPrimed;
17
import net.minecraft.entity.player.EntityPlayer;
18
import net.minecraft.init.Blocks;
19
import net.minecraft.util.AxisAlignedBB;
20
import net.minecraft.util.BlockPos;
21
import net.minecraft.util.DamageSource;
22
import net.minecraft.util.EnumParticleTypes;
23
import net.minecraft.util.MathHelper;
24
import net.minecraft.util.Vec3;
25
26
public class Explosion
27
{
28
    /** whether or not the explosion sets fire to blocks around it */
29
    private final boolean isFlaming;
30
31
    /** whether or not this explosion spawns smoke particles */
32
    private final boolean isSmoking;
33
    private final Random explosionRNG;
34
    private final World worldObj;
35
    private final double explosionX;
36
    private final double explosionY;
37
    private final double explosionZ;
38
    private final Entity exploder;
39
    private final float explosionSize;
40
    private final List<BlockPos> affectedBlockPositions;
41
    private final Map<EntityPlayer, Vec3> playerKnockbackMap;
42
43
    public Explosion(World worldIn, Entity entityIn, double x, double y, double z, float size, List<BlockPos> affectedPositions)
44
    {
45
        this(worldIn, entityIn, x, y, z, size, false, true, affectedPositions);
46
    }
47
48
    public Explosion(World worldIn, Entity entityIn, double x, double y, double z, float size, boolean flaming, boolean smoking, List<BlockPos> affectedPositions)
49
    {
50
        this(worldIn, entityIn, x, y, z, size, flaming, smoking);
51
        this.affectedBlockPositions.addAll(affectedPositions);
52
    }
53
54
    public Explosion(World worldIn, Entity entityIn, double x, double y, double z, float size, boolean flaming, boolean smoking)
55
    {
56
        this.explosionRNG = new Random();
57
        this.affectedBlockPositions = Lists.<BlockPos>newArrayList();
58
        this.playerKnockbackMap = Maps.<EntityPlayer, Vec3>newHashMap();
59
        this.worldObj = worldIn;
60
        this.exploder = entityIn;
61
        this.explosionSize = size;
62
        this.explosionX = x;
63
        this.explosionY = y;
64
        this.explosionZ = z;
65
        this.isFlaming = flaming;
66
        this.isSmoking = smoking;
67
    }
68
69
    /**
70
     * Does the first part of the explosion (destroy blocks)
71
     */
72
    public void doExplosionA()
73
    {
74
        Set<BlockPos> set = Sets.<BlockPos>newHashSet();
75
        int i = 16;
76
77
        for (int j = 0; j < 16; ++j)
78
        {
79
            for (int k = 0; k < 16; ++k)
80
            {
81
                for (int l = 0; l < 16; ++l)
82
                {
83
                    if (j == 0 || j == 15 || k == 0 || k == 15 || l == 0 || l == 15)
84
                    {
85
                        double d0 = (double)((float)j / 15.0F * 2.0F - 1.0F);
86
                        double d1 = (double)((float)k / 15.0F * 2.0F - 1.0F);
87
                        double d2 = (double)((float)l / 15.0F * 2.0F - 1.0F);
88
                        double d3 = Math.sqrt(d0 * d0 + d1 * d1 + d2 * d2);
89
                        d0 = d0 / d3;
90
                        d1 = d1 / d3;
91
                        d2 = d2 / d3;
92
                        float f = this.explosionSize * (0.7F + this.worldObj.rand.nextFloat() * 0.6F);
93
                        double d4 = this.explosionX;
94
                        double d6 = this.explosionY;
95
                        double d8 = this.explosionZ;
96
97
                        for (float f1 = 0.3F; f > 0.0F; f -= 0.22500001F)
98
                        {
99
                            BlockPos blockpos = new BlockPos(d4, d6, d8);
100
                            IBlockState iblockstate = this.worldObj.getBlockState(blockpos);
101
102
                            if (iblockstate.getBlock().getMaterial() != Material.air)
103
                            {
104
                                float f2 = this.exploder != null ? this.exploder.getExplosionResistance(this, this.worldObj, blockpos, iblockstate) : iblockstate.getBlock().getExplosionResistance((Entity)null);
105
                                f -= (f2 + 0.3F) * 0.3F;
106
                            }
107
108
                            if (f > 0.0F && (this.exploder == null || this.exploder.verifyExplosion(this, this.worldObj, blockpos, iblockstate, f)))
109
                            {
110
                                set.add(blockpos);
111
                            }
112
113
                            d4 += d0 * 0.30000001192092896D;
114
                            d6 += d1 * 0.30000001192092896D;
115
                            d8 += d2 * 0.30000001192092896D;
116
                        }
117
                    }
118
                }
119
            }
120
        }
121
122
        this.affectedBlockPositions.addAll(set);
123
        float f3 = this.explosionSize * 2.0F;
124
        int k1 = MathHelper.floor_double(this.explosionX - (double)f3 - 1.0D);
125
        int l1 = MathHelper.floor_double(this.explosionX + (double)f3 + 1.0D);
126
        int i2 = MathHelper.floor_double(this.explosionY - (double)f3 - 1.0D);
127
        int i1 = MathHelper.floor_double(this.explosionY + (double)f3 + 1.0D);
128
        int j2 = MathHelper.floor_double(this.explosionZ - (double)f3 - 1.0D);
129
        int j1 = MathHelper.floor_double(this.explosionZ + (double)f3 + 1.0D);
130
        List<Entity> list = this.worldObj.getEntitiesWithinAABBExcludingEntity(this.exploder, new AxisAlignedBB((double)k1, (double)i2, (double)j2, (double)l1, (double)i1, (double)j1));
131
        Vec3 vec3 = new Vec3(this.explosionX, this.explosionY, this.explosionZ);
132
133
        for (int k2 = 0; k2 < list.size(); ++k2)
134
        {
135
            Entity entity = (Entity)list.get(k2);
136
137
            if (!entity.isImmuneToExplosions())
138
            {
139
                double d12 = entity.getDistance(this.explosionX, this.explosionY, this.explosionZ) / (double)f3;
140
141
                if (d12 <= 1.0D)
142
                {
143
                    double d5 = entity.posX - this.explosionX;
144
                    double d7 = entity.posY + (double)entity.getEyeHeight() - this.explosionY;
145
                    double d9 = entity.posZ - this.explosionZ;
146
                    double d13 = (double)MathHelper.sqrt_double(d5 * d5 + d7 * d7 + d9 * d9);
147
148
                    if (d13 != 0.0D)
149
                    {
150
                        d5 = d5 / d13;
151
                        d7 = d7 / d13;
152
                        d9 = d9 / d13;
153
                        double d14 = (double)this.worldObj.getBlockDensity(vec3, entity.getEntityBoundingBox());
154
                        double d10 = (1.0D - d12) * d14;
155
                        entity.attackEntityFrom(DamageSource.setExplosionSource(this), (float)((int)((d10 * d10 + d10) / 2.0D * 8.0D * (double)f3 + 1.0D)));
156
                        double d11 = EnchantmentProtection.func_92092_a(entity, d10);
157
                        entity.motionX += d5 * d11;
158
                        entity.motionY += d7 * d11;
159
                        entity.motionZ += d9 * d11;
160
161
                        if (entity instanceof EntityPlayer && !((EntityPlayer)entity).capabilities.disableDamage)
162
                        {
163
                            this.playerKnockbackMap.put((EntityPlayer)entity, new Vec3(d5 * d10, d7 * d10, d9 * d10));
164
                        }
165
                    }
166
                }
167
            }
168
        }
169
    }
170
171
    /**
172
     * Does the second part of the explosion (sound, particles, drop spawn)
173
     */
174
    public void doExplosionB(boolean spawnParticles)
175
    {
176
        this.worldObj.playSoundEffect(this.explosionX, this.explosionY, this.explosionZ, "random.explode", 4.0F, (1.0F + (this.worldObj.rand.nextFloat() - this.worldObj.rand.nextFloat()) * 0.2F) * 0.7F);
177
178
        if (this.explosionSize >= 2.0F && this.isSmoking)
179
        {
180
            this.worldObj.spawnParticle(EnumParticleTypes.EXPLOSION_HUGE, this.explosionX, this.explosionY, this.explosionZ, 1.0D, 0.0D, 0.0D, new int[0]);
181
        }
182
        else
183
        {
184
            this.worldObj.spawnParticle(EnumParticleTypes.EXPLOSION_LARGE, this.explosionX, this.explosionY, this.explosionZ, 1.0D, 0.0D, 0.0D, new int[0]);
185
        }
186
187
        if (this.isSmoking)
188
        {
189
            for (BlockPos blockpos : this.affectedBlockPositions)
190
            {
191
                Block block = this.worldObj.getBlockState(blockpos).getBlock();
192
193
                if (spawnParticles)
194
                {
195
                    double d0 = (double)((float)blockpos.getX() + this.worldObj.rand.nextFloat());
196
                    double d1 = (double)((float)blockpos.getY() + this.worldObj.rand.nextFloat());
197
                    double d2 = (double)((float)blockpos.getZ() + this.worldObj.rand.nextFloat());
198
                    double d3 = d0 - this.explosionX;
199
                    double d4 = d1 - this.explosionY;
200
                    double d5 = d2 - this.explosionZ;
201
                    double d6 = (double)MathHelper.sqrt_double(d3 * d3 + d4 * d4 + d5 * d5);
202
                    d3 = d3 / d6;
203
                    d4 = d4 / d6;
204
                    d5 = d5 / d6;
205
                    double d7 = 0.5D / (d6 / (double)this.explosionSize + 0.1D);
206
                    d7 = d7 * (double)(this.worldObj.rand.nextFloat() * this.worldObj.rand.nextFloat() + 0.3F);
207
                    d3 = d3 * d7;
208
                    d4 = d4 * d7;
209
                    d5 = d5 * d7;
210
                    this.worldObj.spawnParticle(EnumParticleTypes.EXPLOSION_NORMAL, (d0 + this.explosionX * 1.0D) / 2.0D, (d1 + this.explosionY * 1.0D) / 2.0D, (d2 + this.explosionZ * 1.0D) / 2.0D, d3, d4, d5, new int[0]);
211
                    this.worldObj.spawnParticle(EnumParticleTypes.SMOKE_NORMAL, d0, d1, d2, d3, d4, d5, new int[0]);
212
                }
213
214
                if (block.getMaterial() != Material.air)
215
                {
216
                    if (block.canDropFromExplosion(this))
217
                    {
218
                        block.dropBlockAsItemWithChance(this.worldObj, blockpos, this.worldObj.getBlockState(blockpos), 1.0F / this.explosionSize, 0);
219
                    }
220
221
                    this.worldObj.setBlockState(blockpos, Blocks.air.getDefaultState(), 3);
222
                    block.onBlockDestroyedByExplosion(this.worldObj, blockpos, this);
223
                }
224
            }
225
        }
226
227
        if (this.isFlaming)
228
        {
229
            for (BlockPos blockpos1 : this.affectedBlockPositions)
230
            {
231
                if (this.worldObj.getBlockState(blockpos1).getBlock().getMaterial() == Material.air && this.worldObj.getBlockState(blockpos1.down()).getBlock().isFullBlock() && this.explosionRNG.nextInt(3) == 0)
232
                {
233
                    this.worldObj.setBlockState(blockpos1, Blocks.fire.getDefaultState());
234
                }
235
            }
236
        }
237
    }
238
239
    public Map<EntityPlayer, Vec3> getPlayerKnockbackMap()
240
    {
241
        return this.playerKnockbackMap;
242
    }
243
244
    /**
245
     * Returns either the entity that placed the explosive block, the entity that caused the explosion or null.
246
     */
247
    public EntityLivingBase getExplosivePlacedBy()
248
    {
249
        return this.exploder == null ? null : (this.exploder instanceof EntityTNTPrimed ? ((EntityTNTPrimed)this.exploder).getTntPlacedBy() : (this.exploder instanceof EntityLivingBase ? (EntityLivingBase)this.exploder : null));
250
    }
251
252
    public void clearAffectedBlockPositions()
253
    {
254
        this.affectedBlockPositions.clear();
255
    }
256
257
    public List<BlockPos> getAffectedBlockPositions()
258
    {
259
        return this.affectedBlockPositions;
260
    }
261
}
262

Deobfuscated MCP 1.8.9 source of Explosion from Marcelektro/MavenMCP-1.8.9 (net/minecraft/world/Explosion.java), stored verbatim.