Teleporter .java

net.minecraft.world.Teleporter (class, 431 lines): members: destinationCoordinateCache, removeStalePortalLocations, destinationCoordinateKeys, makePortal.

net.minecraft.worldclass431 linescommon16.6 KB

API Surface (4)

void placeInPortal(Entity entityIn, float rotationYaw)
boolean placeInExistingPortal(Entity entityIn, float rotationYaw)
boolean makePortal(Entity entityIn)
void removeStalePortalLocations(long worldTime)

Fields & Constants (4)

worldServerInstance
random
destinationCoordinateCache
destinationCoordinateKeys

Full Source

Teleporter.java431 lines · MCP 1.8.9 · verbatimOriginal ↗
1
package net.minecraft.world;
2
3
import com.google.common.collect.Lists;
4
import java.util.Iterator;
5
import java.util.List;
6
import java.util.Random;
7
import net.minecraft.block.BlockPortal;
8
import net.minecraft.block.state.IBlockState;
9
import net.minecraft.block.state.pattern.BlockPattern;
10
import net.minecraft.entity.Entity;
11
import net.minecraft.init.Blocks;
12
import net.minecraft.util.BlockPos;
13
import net.minecraft.util.EnumFacing;
14
import net.minecraft.util.LongHashMap;
15
import net.minecraft.util.MathHelper;
16
17
public class Teleporter
18
{
19
    private final WorldServer worldServerInstance;
20
21
    /** A private Random() function in Teleporter */
22
    private final Random random;
23
    private final LongHashMap<Teleporter.PortalPosition> destinationCoordinateCache = new LongHashMap();
24
    private final List<Long> destinationCoordinateKeys = Lists.<Long>newArrayList();
25
26
    public Teleporter(WorldServer worldIn)
27
    {
28
        this.worldServerInstance = worldIn;
29
        this.random = new Random(worldIn.getSeed());
30
    }
31
32
    public void placeInPortal(Entity entityIn, float rotationYaw)
33
    {
34
        if (this.worldServerInstance.provider.getDimensionId() != 1)
35
        {
36
            if (!this.placeInExistingPortal(entityIn, rotationYaw))
37
            {
38
                this.makePortal(entityIn);
39
                this.placeInExistingPortal(entityIn, rotationYaw);
40
            }
41
        }
42
        else
43
        {
44
            int i = MathHelper.floor_double(entityIn.posX);
45
            int j = MathHelper.floor_double(entityIn.posY) - 1;
46
            int k = MathHelper.floor_double(entityIn.posZ);
47
            int l = 1;
48
            int i1 = 0;
49
50
            for (int j1 = -2; j1 <= 2; ++j1)
51
            {
52
                for (int k1 = -2; k1 <= 2; ++k1)
53
                {
54
                    for (int l1 = -1; l1 < 3; ++l1)
55
                    {
56
                        int i2 = i + k1 * l + j1 * i1;
57
                        int j2 = j + l1;
58
                        int k2 = k + k1 * i1 - j1 * l;
59
                        boolean flag = l1 < 0;
60
                        this.worldServerInstance.setBlockState(new BlockPos(i2, j2, k2), flag ? Blocks.obsidian.getDefaultState() : Blocks.air.getDefaultState());
61
                    }
62
                }
63
            }
64
65
            entityIn.setLocationAndAngles((double)i, (double)j, (double)k, entityIn.rotationYaw, 0.0F);
66
            entityIn.motionX = entityIn.motionY = entityIn.motionZ = 0.0D;
67
        }
68
    }
69
70
    public boolean placeInExistingPortal(Entity entityIn, float rotationYaw)
71
    {
72
        int i = 128;
73
        double d0 = -1.0D;
74
        int j = MathHelper.floor_double(entityIn.posX);
75
        int k = MathHelper.floor_double(entityIn.posZ);
76
        boolean flag = true;
77
        BlockPos blockpos = BlockPos.ORIGIN;
78
        long l = ChunkCoordIntPair.chunkXZ2Int(j, k);
79
80
        if (this.destinationCoordinateCache.containsItem(l))
81
        {
82
            Teleporter.PortalPosition teleporter$portalposition = (Teleporter.PortalPosition)this.destinationCoordinateCache.getValueByKey(l);
83
            d0 = 0.0D;
84
            blockpos = teleporter$portalposition;
85
            teleporter$portalposition.lastUpdateTime = this.worldServerInstance.getTotalWorldTime();
86
            flag = false;
87
        }
88
        else
89
        {
90
            BlockPos blockpos3 = new BlockPos(entityIn);
91
92
            for (int i1 = -128; i1 <= 128; ++i1)
93
            {
94
                BlockPos blockpos2;
95
96
                for (int j1 = -128; j1 <= 128; ++j1)
97
                {
98
                    for (BlockPos blockpos1 = blockpos3.add(i1, this.worldServerInstance.getActualHeight() - 1 - blockpos3.getY(), j1); blockpos1.getY() >= 0; blockpos1 = blockpos2)
99
                    {
100
                        blockpos2 = blockpos1.down();
101
102
                        if (this.worldServerInstance.getBlockState(blockpos1).getBlock() == Blocks.portal)
103
                        {
104
                            while (this.worldServerInstance.getBlockState(blockpos2 = blockpos1.down()).getBlock() == Blocks.portal)
105
                            {
106
                                blockpos1 = blockpos2;
107
                            }
108
109
                            double d1 = blockpos1.distanceSq(blockpos3);
110
111
                            if (d0 < 0.0D || d1 < d0)
112
                            {
113
                                d0 = d1;
114
                                blockpos = blockpos1;
115
                            }
116
                        }
117
                    }
118
                }
119
            }
120
        }
121
122
        if (d0 >= 0.0D)
123
        {
124
            if (flag)
125
            {
126
                this.destinationCoordinateCache.add(l, new Teleporter.PortalPosition(blockpos, this.worldServerInstance.getTotalWorldTime()));
127
                this.destinationCoordinateKeys.add(Long.valueOf(l));
128
            }
129
130
            double d5 = (double)blockpos.getX() + 0.5D;
131
            double d6 = (double)blockpos.getY() + 0.5D;
132
            double d7 = (double)blockpos.getZ() + 0.5D;
133
            BlockPattern.PatternHelper blockpattern$patternhelper = Blocks.portal.func_181089_f(this.worldServerInstance, blockpos);
134
            boolean flag1 = blockpattern$patternhelper.getFinger().rotateY().getAxisDirection() == EnumFacing.AxisDirection.NEGATIVE;
135
            double d2 = blockpattern$patternhelper.getFinger().getAxis() == EnumFacing.Axis.X ? (double)blockpattern$patternhelper.getPos().getZ() : (double)blockpattern$patternhelper.getPos().getX();
136
            d6 = (double)(blockpattern$patternhelper.getPos().getY() + 1) - entityIn.func_181014_aG().yCoord * (double)blockpattern$patternhelper.func_181119_e();
137
138
            if (flag1)
139
            {
140
                ++d2;
141
            }
142
143
            if (blockpattern$patternhelper.getFinger().getAxis() == EnumFacing.Axis.X)
144
            {
145
                d7 = d2 + (1.0D - entityIn.func_181014_aG().xCoord) * (double)blockpattern$patternhelper.func_181118_d() * (double)blockpattern$patternhelper.getFinger().rotateY().getAxisDirection().getOffset();
146
            }
147
            else
148
            {
149
                d5 = d2 + (1.0D - entityIn.func_181014_aG().xCoord) * (double)blockpattern$patternhelper.func_181118_d() * (double)blockpattern$patternhelper.getFinger().rotateY().getAxisDirection().getOffset();
150
            }
151
152
            float f = 0.0F;
153
            float f1 = 0.0F;
154
            float f2 = 0.0F;
155
            float f3 = 0.0F;
156
157
            if (blockpattern$patternhelper.getFinger().getOpposite() == entityIn.getTeleportDirection())
158
            {
159
                f = 1.0F;
160
                f1 = 1.0F;
161
            }
162
            else if (blockpattern$patternhelper.getFinger().getOpposite() == entityIn.getTeleportDirection().getOpposite())
163
            {
164
                f = -1.0F;
165
                f1 = -1.0F;
166
            }
167
            else if (blockpattern$patternhelper.getFinger().getOpposite() == entityIn.getTeleportDirection().rotateY())
168
            {
169
                f2 = 1.0F;
170
                f3 = -1.0F;
171
            }
172
            else
173
            {
174
                f2 = -1.0F;
175
                f3 = 1.0F;
176
            }
177
178
            double d3 = entityIn.motionX;
179
            double d4 = entityIn.motionZ;
180
            entityIn.motionX = d3 * (double)f + d4 * (double)f3;
181
            entityIn.motionZ = d3 * (double)f2 + d4 * (double)f1;
182
            entityIn.rotationYaw = rotationYaw - (float)(entityIn.getTeleportDirection().getOpposite().getHorizontalIndex() * 90) + (float)(blockpattern$patternhelper.getFinger().getHorizontalIndex() * 90);
183
            entityIn.setLocationAndAngles(d5, d6, d7, entityIn.rotationYaw, entityIn.rotationPitch);
184
            return true;
185
        }
186
        else
187
        {
188
            return false;
189
        }
190
    }
191
192
    public boolean makePortal(Entity entityIn)
193
    {
194
        int i = 16;
195
        double d0 = -1.0D;
196
        int j = MathHelper.floor_double(entityIn.posX);
197
        int k = MathHelper.floor_double(entityIn.posY);
198
        int l = MathHelper.floor_double(entityIn.posZ);
199
        int i1 = j;
200
        int j1 = k;
201
        int k1 = l;
202
        int l1 = 0;
203
        int i2 = this.random.nextInt(4);
204
        BlockPos.MutableBlockPos blockpos$mutableblockpos = new BlockPos.MutableBlockPos();
205
206
        for (int j2 = j - i; j2 <= j + i; ++j2)
207
        {
208
            double d1 = (double)j2 + 0.5D - entityIn.posX;
209
210
            for (int l2 = l - i; l2 <= l + i; ++l2)
211
            {
212
                double d2 = (double)l2 + 0.5D - entityIn.posZ;
213
                label142:
214
215
                for (int j3 = this.worldServerInstance.getActualHeight() - 1; j3 >= 0; --j3)
216
                {
217
                    if (this.worldServerInstance.isAirBlock(blockpos$mutableblockpos.set(j2, j3, l2)))
218
                    {
219
                        while (j3 > 0 && this.worldServerInstance.isAirBlock(blockpos$mutableblockpos.set(j2, j3 - 1, l2)))
220
                        {
221
                            --j3;
222
                        }
223
224
                        for (int k3 = i2; k3 < i2 + 4; ++k3)
225
                        {
226
                            int l3 = k3 % 2;
227
                            int i4 = 1 - l3;
228
229
                            if (k3 % 4 >= 2)
230
                            {
231
                                l3 = -l3;
232
                                i4 = -i4;
233
                            }
234
235
                            for (int j4 = 0; j4 < 3; ++j4)
236
                            {
237
                                for (int k4 = 0; k4 < 4; ++k4)
238
                                {
239
                                    for (int l4 = -1; l4 < 4; ++l4)
240
                                    {
241
                                        int i5 = j2 + (k4 - 1) * l3 + j4 * i4;
242
                                        int j5 = j3 + l4;
243
                                        int k5 = l2 + (k4 - 1) * i4 - j4 * l3;
244
                                        blockpos$mutableblockpos.set(i5, j5, k5);
245
246
                                        if (l4 < 0 && !this.worldServerInstance.getBlockState(blockpos$mutableblockpos).getBlock().getMaterial().isSolid() || l4 >= 0 && !this.worldServerInstance.isAirBlock(blockpos$mutableblockpos))
247
                                        {
248
                                            continue label142;
249
                                        }
250
                                    }
251
                                }
252
                            }
253
254
                            double d5 = (double)j3 + 0.5D - entityIn.posY;
255
                            double d7 = d1 * d1 + d5 * d5 + d2 * d2;
256
257
                            if (d0 < 0.0D || d7 < d0)
258
                            {
259
                                d0 = d7;
260
                                i1 = j2;
261
                                j1 = j3;
262
                                k1 = l2;
263
                                l1 = k3 % 4;
264
                            }
265
                        }
266
                    }
267
                }
268
            }
269
        }
270
271
        if (d0 < 0.0D)
272
        {
273
            for (int l5 = j - i; l5 <= j + i; ++l5)
274
            {
275
                double d3 = (double)l5 + 0.5D - entityIn.posX;
276
277
                for (int j6 = l - i; j6 <= l + i; ++j6)
278
                {
279
                    double d4 = (double)j6 + 0.5D - entityIn.posZ;
280
                    label562:
281
282
                    for (int i7 = this.worldServerInstance.getActualHeight() - 1; i7 >= 0; --i7)
283
                    {
284
                        if (this.worldServerInstance.isAirBlock(blockpos$mutableblockpos.set(l5, i7, j6)))
285
                        {
286
                            while (i7 > 0 && this.worldServerInstance.isAirBlock(blockpos$mutableblockpos.set(l5, i7 - 1, j6)))
287
                            {
288
                                --i7;
289
                            }
290
291
                            for (int k7 = i2; k7 < i2 + 2; ++k7)
292
                            {
293
                                int j8 = k7 % 2;
294
                                int j9 = 1 - j8;
295
296
                                for (int j10 = 0; j10 < 4; ++j10)
297
                                {
298
                                    for (int j11 = -1; j11 < 4; ++j11)
299
                                    {
300
                                        int j12 = l5 + (j10 - 1) * j8;
301
                                        int i13 = i7 + j11;
302
                                        int j13 = j6 + (j10 - 1) * j9;
303
                                        blockpos$mutableblockpos.set(j12, i13, j13);
304
305
                                        if (j11 < 0 && !this.worldServerInstance.getBlockState(blockpos$mutableblockpos).getBlock().getMaterial().isSolid() || j11 >= 0 && !this.worldServerInstance.isAirBlock(blockpos$mutableblockpos))
306
                                        {
307
                                            continue label562;
308
                                        }
309
                                    }
310
                                }
311
312
                                double d6 = (double)i7 + 0.5D - entityIn.posY;
313
                                double d8 = d3 * d3 + d6 * d6 + d4 * d4;
314
315
                                if (d0 < 0.0D || d8 < d0)
316
                                {
317
                                    d0 = d8;
318
                                    i1 = l5;
319
                                    j1 = i7;
320
                                    k1 = j6;
321
                                    l1 = k7 % 2;
322
                                }
323
                            }
324
                        }
325
                    }
326
                }
327
            }
328
        }
329
330
        int i6 = i1;
331
        int k2 = j1;
332
        int k6 = k1;
333
        int l6 = l1 % 2;
334
        int i3 = 1 - l6;
335
336
        if (l1 % 4 >= 2)
337
        {
338
            l6 = -l6;
339
            i3 = -i3;
340
        }
341
342
        if (d0 < 0.0D)
343
        {
344
            j1 = MathHelper.clamp_int(j1, 70, this.worldServerInstance.getActualHeight() - 10);
345
            k2 = j1;
346
347
            for (int j7 = -1; j7 <= 1; ++j7)
348
            {
349
                for (int l7 = 1; l7 < 3; ++l7)
350
                {
351
                    for (int k8 = -1; k8 < 3; ++k8)
352
                    {
353
                        int k9 = i6 + (l7 - 1) * l6 + j7 * i3;
354
                        int k10 = k2 + k8;
355
                        int k11 = k6 + (l7 - 1) * i3 - j7 * l6;
356
                        boolean flag = k8 < 0;
357
                        this.worldServerInstance.setBlockState(new BlockPos(k9, k10, k11), flag ? Blocks.obsidian.getDefaultState() : Blocks.air.getDefaultState());
358
                    }
359
                }
360
            }
361
        }
362
363
        IBlockState iblockstate = Blocks.portal.getDefaultState().withProperty(BlockPortal.AXIS, l6 != 0 ? EnumFacing.Axis.X : EnumFacing.Axis.Z);
364
365
        for (int i8 = 0; i8 < 4; ++i8)
366
        {
367
            for (int l8 = 0; l8 < 4; ++l8)
368
            {
369
                for (int l9 = -1; l9 < 4; ++l9)
370
                {
371
                    int l10 = i6 + (l8 - 1) * l6;
372
                    int l11 = k2 + l9;
373
                    int k12 = k6 + (l8 - 1) * i3;
374
                    boolean flag1 = l8 == 0 || l8 == 3 || l9 == -1 || l9 == 3;
375
                    this.worldServerInstance.setBlockState(new BlockPos(l10, l11, k12), flag1 ? Blocks.obsidian.getDefaultState() : iblockstate, 2);
376
                }
377
            }
378
379
            for (int i9 = 0; i9 < 4; ++i9)
380
            {
381
                for (int i10 = -1; i10 < 4; ++i10)
382
                {
383
                    int i11 = i6 + (i9 - 1) * l6;
384
                    int i12 = k2 + i10;
385
                    int l12 = k6 + (i9 - 1) * i3;
386
                    BlockPos blockpos = new BlockPos(i11, i12, l12);
387
                    this.worldServerInstance.notifyNeighborsOfStateChange(blockpos, this.worldServerInstance.getBlockState(blockpos).getBlock());
388
                }
389
            }
390
        }
391
392
        return true;
393
    }
394
395
    /**
396
     * called periodically to remove out-of-date portal locations from the cache list. Argument par1 is a
397
     * WorldServer.getTotalWorldTime() value.
398
     */
399
    public void removeStalePortalLocations(long worldTime)
400
    {
401
        if (worldTime % 100L == 0L)
402
        {
403
            Iterator<Long> iterator = this.destinationCoordinateKeys.iterator();
404
            long i = worldTime - 300L;
405
406
            while (iterator.hasNext())
407
            {
408
                Long olong = (Long)iterator.next();
409
                Teleporter.PortalPosition teleporter$portalposition = (Teleporter.PortalPosition)this.destinationCoordinateCache.getValueByKey(olong.longValue());
410
411
                if (teleporter$portalposition == null || teleporter$portalposition.lastUpdateTime < i)
412
                {
413
                    iterator.remove();
414
                    this.destinationCoordinateCache.remove(olong.longValue());
415
                }
416
            }
417
        }
418
    }
419
420
    public class PortalPosition extends BlockPos
421
    {
422
        public long lastUpdateTime;
423
424
        public PortalPosition(BlockPos pos, long lastUpdate)
425
        {
426
            super(pos.getX(), pos.getY(), pos.getZ());
427
            this.lastUpdateTime = lastUpdate;
428
        }
429
    }
430
}
431

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