summaryrefslogtreecommitdiff
path: root/src/main/java/jp/plusplus/fbs/world/autumn/TeleporterAutumn.java
blob: 8ff296ac78120f30fcfe2a08558ee1437fbee03a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
package jp.plusplus.fbs.world.autumn;

import jp.plusplus.fbs.FBS;
import jp.plusplus.fbs.block.BlockCore;
import net.minecraft.block.Block;
import net.minecraft.block.BlockPortal;
import net.minecraft.entity.Entity;
import net.minecraft.init.Blocks;
import net.minecraft.util.Direction;
import net.minecraft.util.LongHashMap;
import net.minecraft.util.MathHelper;
import net.minecraft.world.ChunkCoordIntPair;
import net.minecraft.world.Teleporter;
import net.minecraft.world.WorldServer;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Random;

/**
 * Created by plusplus_F on 2015/11/07.
 */
public class TeleporterAutumn extends Teleporter {
    private final LongHashMap destinationCoordinateCache = new LongHashMap();
    private final List destinationCoordinateKeys = new ArrayList();
    private final WorldServer worldServerInstance;
    private final Random random;

    public TeleporterAutumn(WorldServer p_i1963_1_) {
        super(p_i1963_1_);
        worldServerInstance=p_i1963_1_;
        random=new Random();
    }

    // 近くにポータルがあったらそこに出現させる処理
    // このサンプルではネザーポータルの処理をそのまま利用する
    @Override
    public boolean placeInExistingPortal(Entity entity, double p_77184_2_, double p_77184_4_, double p_77184_6_, float p_77184_8_) {
        short short1 = 128;
        double d3 = -1.0D;
        int i = 0;
        int j = 0;
        int k = 0;
        int l = MathHelper.floor_double(entity.posX);
        int i1 = MathHelper.floor_double(entity.posZ);
        long j1 = ChunkCoordIntPair.chunkXZ2Int(l, i1);
        boolean flag = true;
        double z;
        int l3;

        if (this.destinationCoordinateCache.containsItem(j1)) {
            Teleporter.PortalPosition portalposition = (Teleporter.PortalPosition) this.destinationCoordinateCache.getValueByKey(j1);
            d3 = 0.0D;
            i = portalposition.posX;
            j = portalposition.posY;
            k = portalposition.posZ;
            portalposition.lastUpdateTime = this.worldServerInstance.getTotalWorldTime();
            flag = false;
        } else {
            for (l3 = l - short1; l3 <= l + short1; ++l3) {
                double d4 = (double) l3 + 0.5D - entity.posX;

                for (int l1 = i1 - short1; l1 <= i1 + short1; ++l1) {
                    double d5 = (double) l1 + 0.5D - entity.posZ;

                    for (int i2 = this.worldServerInstance.getActualHeight() - 1; i2 >= 0; --i2) {
                        if (this.worldServerInstance.getBlock(l3, i2, l1) == BlockCore.portal2) {
                            while (this.worldServerInstance.getBlock(l3, i2 - 1, l1) == BlockCore.portal2) {
                                --i2;
                            }

                            z = (double) i2 + 0.5D - entity.posY;
                            double d8 = d4 * d4 + z * z + d5 * d5;

                            if (d3 < 0.0D || d8 < d3) {
                                d3 = d8;
                                i = l3;
                                j = i2;
                                k = l1;
                            }
                        }
                    }
                }
            }
        }

        if (d3 >= 0.0D) {
            if (flag) {
                this.destinationCoordinateCache.add(j1, new Teleporter.PortalPosition(i, j, k, this.worldServerInstance.getTotalWorldTime()));
                this.destinationCoordinateKeys.add(Long.valueOf(j1));
            }

            double x = (double) i + 0.5D;
            double y = (double) j + 0.5D;
            z = (double) k + 0.5D;

            entity.motionX = entity.motionY = entity.motionZ = 0.0D;

            entity.setLocationAndAngles(x, y+1, z, entity.rotationYaw, entity.rotationPitch);
            return true;
        } else {
            return false;
        }
    }

    // ポータルを作成する処理
    // このサンプルではネザーポータルの処理をそのまま利用する
    @Override
    public boolean makePortal(Entity p_85188_1_) {
        byte b0 = 16;
        double d0 = -1.0D;
        int i = MathHelper.floor_double(p_85188_1_.posX);
        int j = MathHelper.floor_double(p_85188_1_.posY);
        int k = MathHelper.floor_double(p_85188_1_.posZ);
        int l = i;
        int i1 = j;
        int j1 = k;
        int k1 = 0;
        int l1 = this.random.nextInt(4);
        int x;
        double d1;
        int z;
        double d2;
        int y;
        int j3;
        int k3;
        int l3;
        int i4;
        int j4;
        int k4;
        int x2;
        int y2;
        double d3;
        double d4;

        for (x = i - b0; x <= i + b0; ++x) {
            d1 = (double) x + 0.5D - p_85188_1_.posX;

            for (z = k - b0; z <= k + b0; ++z) {
                d2 = (double) z + 0.5D - p_85188_1_.posZ;
                label274:

                for (y = this.worldServerInstance.getActualHeight() - 1; y >= 0; --y) {
                    if (this.worldServerInstance.isAirBlock(x, y, z)) {
                        while (y > 0 && this.worldServerInstance.isAirBlock(x, y - 1, z)) {
                            --y;
                        }

                        for (j3 = l1; j3 < l1 + 4; ++j3) {
                            k3 = j3 % 2;
                            l3 = 1 - k3;

                            if (j3 % 4 >= 2) {
                                k3 = -k3;
                                l3 = -l3;
                            }

                            for (i4 = 0; i4 < 3; ++i4) {
                                for (j4 = 0; j4 < 4; ++j4) {
                                    for (k4 = -1; k4 < 4; ++k4) {
                                        x2 = x + (j4 - 1) * k3 + i4 * l3;
                                        y2 = y + k4;
                                        int z2 = z + (j4 - 1) * l3 - i4 * k3;

                                        if (k4 < 0 && !this.worldServerInstance.getBlock(x2, y2, z2).getMaterial().isSolid() || k4 >= 0 && !this.worldServerInstance.isAirBlock(x2, y2, z2)) {
                                            continue label274;
                                        }
                                    }
                                }
                            }

                            d3 = (double) y + 0.5D - p_85188_1_.posY;
                            d4 = d1 * d1 + d3 * d3 + d2 * d2;

                            if (d0 < 0.0D || d4 < d0) {
                                d0 = d4;
                                l = x;
                                i1 = y;
                                j1 = z;
                                k1 = j3 % 4;
                            }
                        }
                    }
                }
            }
        }

        if (d0 < 0.0D) {
            for (x = i - b0; x <= i + b0; ++x) {
                d1 = (double) x + 0.5D - p_85188_1_.posX;

                for (z = k - b0; z <= k + b0; ++z) {
                    d2 = (double) z + 0.5D - p_85188_1_.posZ;
                    label222:

                    for (y = this.worldServerInstance.getActualHeight() - 1; y >= 0; --y) {
                        if (this.worldServerInstance.isAirBlock(x, y, z)) {
                            while (y > 0 && this.worldServerInstance.isAirBlock(x, y - 1, z)) {
                                --y;
                            }

                            for (j3 = l1; j3 < l1 + 2; ++j3) {
                                k3 = j3 % 2;
                                l3 = 1 - k3;

                                for (i4 = 0; i4 < 4; ++i4) {
                                    for (j4 = -1; j4 < 4; ++j4) {
                                        k4 = x + (i4 - 1) * k3;
                                        x2 = y + j4;
                                        y2 = z + (i4 - 1) * l3;

                                        if (j4 < 0 && !this.worldServerInstance.getBlock(k4, x2, y2).getMaterial().isSolid() || j4 >= 0 && !this.worldServerInstance.isAirBlock(k4, x2, y2)) {
                                            continue label222;
                                        }
                                    }
                                }

                                d3 = (double) y + 0.5D - p_85188_1_.posY;
                                d4 = d1 * d1 + d3 * d3 + d2 * d2;

                                if (d0 < 0.0D || d4 < d0) {
                                    d0 = d4;
                                    l = x;
                                    i1 = y;
                                    j1 = z;
                                    k1 = j3 % 2;
                                }
                            }
                        }
                    }
                }
            }
        }

        int k5 = l;
        int j2 = i1;
        z = j1;
        int l5 = k1 % 2;
        int l2 = 1 - l5;

        if (k1 % 4 >= 2) {
            l5 = -l5;
            l2 = -l2;
        }

        boolean flag;

        if (d0 < 0.0D) {
            if (i1 < 70) {
                i1 = 70;
            }

            if (i1 > this.worldServerInstance.getActualHeight() - 10) {
                i1 = this.worldServerInstance.getActualHeight() - 10;
            }

            j2 = i1;

            for (y = -1; y <= 1; ++y) {
                for (j3 = 1; j3 < 3; ++j3) {
                    for (k3 = -1; k3 < 3; ++k3) {
                        l3 = k5 + (j3 - 1) * l5 + y * l2;
                        i4 = j2 + k3;
                        j4 = z + (j3 - 1) * l2 - y * l5;
                        flag = k3 < 0;
                        this.worldServerInstance.setBlock(l3, i4, j4, flag ? BlockCore.plank : Blocks.air);
                    }
                }
            }
        }

        worldServerInstance.setBlock(k5, j2-1, z, BlockCore.portal2, 0, 2);
        /*
        for (y = 0; y < 4; ++y) {
            for (j3 = 0; j3 < 4; ++j3) {
                for (k3 = -1; k3 < 4; ++k3) {
                    l3 = k5 + (j3 - 1) * l5;
                    i4 = j2 + k3;
                    j4 = z + (j3 - 1) * l2;
                    flag = j3 == 0 || j3 == 3 || k3 == -1 || k3 == 3;
                    this.worldServerInstance.setBlock(l3, i4, j4, (Block) (flag ? BlockCore.plank : BlockCore.portal2), 0, 2);
                }
            }

            for (j3 = 0; j3 < 4; ++j3) {
                for (k3 = -1; k3 < 4; ++k3) {
                    l3 = k5 + (j3 - 1) * l5;
                    i4 = j2 + k3;
                    j4 = z + (j3 - 1) * l2;
                    this.worldServerInstance.notifyBlocksOfNeighborChange(l3, i4, j4, this.worldServerInstance.getBlock(l3, i4, j4));
                }
            }
        }
        */

        return true;
    }

    public void removeStalePortalLocations(long p_85189_1_) {
        if (p_85189_1_ % 100L == 0L) {
            Iterator iterator = this.destinationCoordinateKeys.iterator();
            long j = p_85189_1_ - 600L;

            while (iterator.hasNext()) {
                Long olong = (Long) iterator.next();
                Teleporter.PortalPosition portalposition = (Teleporter.PortalPosition) this.destinationCoordinateCache.getValueByKey(olong.longValue());

                if (portalposition == null || portalposition.lastUpdateTime < j) {
                    iterator.remove();
                    this.destinationCoordinateCache.remove(olong.longValue());
                }
            }
        }
    }

    @Override
    public void placeInPortal(Entity p_77185_1_, double p_77185_2_, double p_77185_4_, double p_77185_6_, float p_77185_8_) {
        if (!this.placeInExistingPortal(p_77185_1_, p_77185_2_, p_77185_4_, p_77185_6_, p_77185_8_)) {
            this.makePortal(p_77185_1_);
            this.placeInExistingPortal(p_77185_1_, p_77185_2_, p_77185_4_, p_77185_6_, p_77185_8_);
        }

        /*
        if (this.worldServerInstance.provider.dimensionId != FBS.dimensionAutumnId) {
            if (!this.placeInExistingPortal(p_77185_1_, p_77185_2_, p_77185_4_, p_77185_6_, p_77185_8_)) {
                this.makePortal(p_77185_1_);
                this.placeInExistingPortal(p_77185_1_, p_77185_2_, p_77185_4_, p_77185_6_, p_77185_8_);
            }
        } else {
            int i = MathHelper.floor_double(p_77185_1_.posX);
            int j = MathHelper.floor_double(p_77185_1_.posY) - 1;
            int k = MathHelper.floor_double(p_77185_1_.posZ);
            byte b0 = 1;
            byte b1 = 0;

            for (int l = -4; l <= 4; ++l) {
                for (int i1 = -4; i1 <= 4; ++i1) {
                    for (int j1 = -1; j1 < 3; ++j1) {
                        int blockX = i + i1 * b0 + l * b1;
                        int blockY = j + j1;
                        int blockZ = k + i1 * b1 - l * b0;
                        boolean flag = j1 < 0;
                        this.worldServerInstance.setBlock(blockX, blockY, blockZ, flag ? BlockCore.plank : Blocks.air);
                    }
                }
            }

            p_77185_1_.setLocationAndAngles((double) i+2, (double) j, (double) k+2, p_77185_1_.rotationYaw, 0.0F);
            p_77185_1_.motionX = p_77185_1_.motionY = p_77185_1_.motionZ = 0.0D;
        }
        */
    }
}