-
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathBlockPos.java
More file actions
536 lines (469 loc) · 18 KB
/
BlockPos.java
File metadata and controls
536 lines (469 loc) · 18 KB
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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
/*
* This file is part of FalsePatternLib.
*
* Copyright (C) 2022-2025 FalsePattern
* All Rights Reserved
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* FalsePatternLib is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, only version 3 of the License.
*
* FalsePatternLib is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with FalsePatternLib. If not, see <https://www.gnu.org/licenses/>.
*/
package com.falsepattern.lib.compat;
import com.falsepattern.lib.util.MathUtil;
import com.google.common.collect.AbstractIterator;
import lombok.Getter;
import lombok.NonNull;
import lombok.Setter;
import lombok.val;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import net.minecraft.entity.Entity;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.Vec3;
import javax.annotation.concurrent.Immutable;
import java.util.ArrayList;
import java.util.List;
import static net.minecraft.util.EnumFacing.DOWN;
import static net.minecraft.util.EnumFacing.EAST;
import static net.minecraft.util.EnumFacing.NORTH;
import static net.minecraft.util.EnumFacing.SOUTH;
import static net.minecraft.util.EnumFacing.UP;
import static net.minecraft.util.EnumFacing.WEST;
@Immutable
public class BlockPos extends Vec3i {
/**
* An immutable block pos with zero as all coordinates.
*/
public static final BlockPos ORIGIN = new BlockPos(0, 0, 0);
private static final Logger LOGGER = LogManager.getLogger();
private static final int NUM_X_BITS = 1 + MathUtil.log2(MathUtil.smallestEncompassingPowerOfTwo(30000000));
private static final int NUM_Z_BITS = NUM_X_BITS;
private static final int NUM_Y_BITS = 64 - NUM_X_BITS - NUM_Z_BITS;
private static final long Y_MASK = (1L << NUM_Y_BITS) - 1L;
private static final int Y_SHIFT = NUM_Z_BITS;
private static final int X_SHIFT = Y_SHIFT + NUM_Y_BITS;
private static final long Z_MASK = (1L << NUM_Z_BITS) - 1L;
private static final long X_MASK = (1L << NUM_X_BITS) - 1L;
public BlockPos(@NonNull Entity source) {
this(source.posX, source.posY, source.posZ);
}
public BlockPos(double x, double y, double z) {
super(x, y, z);
}
public BlockPos(@NonNull Vec3 vec) {
this(vec.xCoord, vec.yCoord, vec.zCoord);
}
public BlockPos(@NonNull Vec3i source) {
this(source.getX(), source.getY(), source.getZ());
}
public BlockPos(int x, int y, int z) {
super(x, y, z);
}
/**
* Create a BlockPos from a serialized long value (created by toLong)
*/
public static BlockPos fromLong(long serialized) {
return new BlockPos((int) (serialized << 64 - X_SHIFT - NUM_X_BITS >> 64 - NUM_X_BITS),
(int) (serialized << 64 - Y_SHIFT - NUM_Y_BITS >> 64 - NUM_Y_BITS),
(int) (serialized << 64 - NUM_Z_BITS >> 64 - NUM_Z_BITS));
}
/**
* Create an Iterable that returns all positions in the box specified by the given corners. There is no requirement
* that one corner is greater than the other; individual coordinates will be swapped as needed.
* <p>
* In situations where it is usable, prefer
* {@link #getAllInBoxMutable(BlockPos, BlockPos) instead as it has better performance (fewer allocations)
* <p>
*
* @param from One corner of the box
* @param to Another corner of the box
* <p>
*
* @see #getAllInBox(int, int, int, int, int, int)
* @see #getAllInBoxMutable(BlockPos, BlockPos)
*/
public static Iterable<BlockPos> getAllInBox(@NonNull BlockPos from, @NonNull BlockPos to) {
return getAllInBox(Math.min(from.getX(), to.getX()),
Math.min(from.getY(), to.getY()),
Math.min(from.getZ(), to.getZ()),
Math.max(from.getX(), to.getX()),
Math.max(from.getY(), to.getY()),
Math.max(from.getZ(), to.getZ()));
}
/**
* Create an Iterable that returns all positions in the box specified by the coordinates. <strong>Coordinates must
* be in order</strong>; e.g. x0 <= x1.
* <p>
* In situations where it is usable, prefer
* {@link #getAllInBoxMutable(BlockPos, BlockPos) instead as it has better performance (fewer allocations)
* <p>
*
* @param x0 The lower x coordinate
* @param y0 The lower y coordinate
* @param z0 The lower z coordinate
* @param x1 The upper x coordinate
* @param y1 The upper y coordinate
* @param z1 The upper z coordinate
* <p>
*
* @see #getAllInBox(BlockPos, BlockPos)
* @see #getAllInBoxMutable(BlockPos, BlockPos)
*/
public static Iterable<BlockPos> getAllInBox(final int x0, final int y0, final int z0, final int x1, final int y1, final int z1) {
return () -> new AbstractIterator<BlockPos>() {
private boolean first = true;
private int lastX;
private int lastY;
private int lastZ;
@Override
protected BlockPos computeNext() {
if (first) {
first = false;
lastX = x0;
lastY = y0;
lastZ = z0;
return new BlockPos(x0, y0, z0);
}
if (lastX == x1 && lastY == y1 && lastZ == z1) {
return endOfData();
}
if (lastX < x1) {
lastX++;
} else if (lastY < y1) {
lastX = x0;
lastY++;
} else if (lastZ < z1) {
lastX = x0;
lastY = y0;
lastZ++;
}
return new BlockPos(lastX, lastY, lastZ);
}
};
}
/**
* Creates an Iterable that returns all positions in the box specified by the given corners. There is no requirement
* that one corner is greater than the other; individual coordinates will be swapped as needed.
* <p>
* This method uses {@link BlockPos.MutableBlockPos MutableBlockPos} instead of regular BlockPos, which grants
* better performance. However, the resulting BlockPos instances can only be used inside the iteration loop (as
* otherwise the value will change), unless {@link #toImmutable()} is called. This method is ideal for searching
* large areas and only storing a few locations.
*
* @param from One corner of the box
* @param to Another corner of the box
*
* @see #getAllInBox(BlockPos, BlockPos)
* @see #getAllInBox(int, int, int, int, int, int)
* @see #getAllInBoxMutable(BlockPos, BlockPos)
*/
public static Iterable<BlockPos.MutableBlockPos> getAllInBoxMutable(@NonNull BlockPos from, @NonNull BlockPos to) {
return getAllInBoxMutable(Math.min(from.getX(), to.getX()),
Math.min(from.getY(), to.getY()),
Math.min(from.getZ(), to.getZ()),
Math.max(from.getX(), to.getX()),
Math.max(from.getY(), to.getY()),
Math.max(from.getZ(), to.getZ()));
}
/**
* Creates an Iterable that returns all positions in the box specified by the given corners. <strong>Coordinates
* must be in order</strong>; e.g. x0 <= x1.
* <p>
* This method uses {@link BlockPos.MutableBlockPos MutableBlockPos} instead of regular BlockPos, which grants
* better performance. However, the resulting BlockPos instances can only be used inside the iteration loop (as
* otherwise the value will change), unless {@link #toImmutable()} is called. This method is ideal for searching
* large areas and only storing a few locations.
*
* @param x0 The lower x coordinate
* @param y0 The lower y coordinate
* @param z0 The lower z coordinate
* @param x1 The upper x coordinate
* @param y1 The upper y coordinate
* @param z1 The upper z coordinate
*
* @see #getAllInBox(BlockPos, BlockPos)
* @see #getAllInBox(int, int, int, int, int, int)
* @see #getAllInBoxMutable(BlockPos, BlockPos)
*/
public static Iterable<BlockPos.MutableBlockPos> getAllInBoxMutable(final int x0, final int y0, final int z0, final int x1, final int y1, final int z1) {
return () -> new AbstractIterator<MutableBlockPos>() {
private MutableBlockPos pos;
@Override
protected MutableBlockPos computeNext() {
if (pos == null) {
pos = new MutableBlockPos(x0, y0, z0);
return pos;
} else if (pos.x == x1 && pos.y == y1 && pos.z == z1) {
return endOfData();
} else {
if (pos.x < x1) {
pos.x++;
} else if (pos.y < y1) {
pos.x = x0;
pos.y++;
} else if (pos.z < z1) {
pos.x = x0;
pos.y = y0;
pos.z++;
}
return pos;
}
}
};
}
public long toLong() {
return ((long) x & X_MASK) << X_SHIFT | ((long) y & Y_MASK) << Y_SHIFT | ((long) z & Z_MASK);
}
/**
* Add the given coordinates to the coordinates of this BlockPos
*/
public BlockPos add(double x, double y, double z) {
if (x == 0.0D && y == 0.0D && z == 0.0D) {
return this;
}
return new BlockPos(this.x + x, this.y + y, this.z + z);
}
/**
* Add the given Vector to this BlockPos
*/
public BlockPos add(@NonNull Vec3i vec) {
return add(vec.getX(), vec.getY(), vec.getZ());
}
/**
* Add the given coordinates to the coordinates of this BlockPos
*/
public BlockPos add(int x, int y, int z) {
if (x == 0 && y == 0 && z == 0) {
return this;
}
return new BlockPos(this.x + x, this.y + y, this.z + z);
}
/**
* Subtract the given Vector from this BlockPos
*/
public BlockPos subtract(@NonNull Vec3i vec) {
return add(-vec.getX(), -vec.getY(), -vec.getZ());
}
public BlockPos up() {
return up(1);
}
public BlockPos up(int blocks) {
return offset(UP, blocks);
}
public BlockPos offset(@NonNull EnumFacing facing, int blocks) {
if (blocks == 0) {
return this;
}
return new BlockPos(x + facing.getFrontOffsetX() * blocks,
y + facing.getFrontOffsetY() * blocks,
z + facing.getFrontOffsetZ() * blocks);
}
public BlockPos down() {
return down(1);
}
public BlockPos down(int blocks) {
return offset(DOWN, blocks);
}
public BlockPos north() {
return north(1);
}
public BlockPos north(int blocks) {
return offset(NORTH, blocks);
}
public BlockPos south() {
return south(1);
}
public BlockPos south(int blocks) {
return offset(SOUTH, blocks);
}
public BlockPos west() {
return west(1);
}
public BlockPos west(int blocks) {
return offset(WEST, blocks);
}
public BlockPos east() {
return east(1);
}
public BlockPos east(int blocks) {
return offset(EAST, blocks);
}
public BlockPos offset(@NonNull EnumFacing facing) {
return offset(facing, 1);
}
public BlockPos rotate(@NonNull Rotation rotation) {
switch (rotation) {
case CLOCKWISE_90:
return new BlockPos(-this.getZ(), this.getY(), this.getX());
case CLOCKWISE_180:
return new BlockPos(-this.getX(), this.getY(), -this.getZ());
case COUNTERCLOCKWISE_90:
return new BlockPos(this.getZ(), this.getY(), -this.getX());
case NONE:
default:
return this;
}
}
@Override
public BlockPos crossProduct(@NonNull Vec3i vec) {
return new BlockPos(y * vec.getZ() - z * vec.getY(),
z * vec.getX() - x * vec.getZ(),
x * vec.getY() - y * vec.getX());
}
public BlockPos toImmutable() {
return this;
}
/**
* @since 0.10.0
*/
@Setter
@Getter
public static class MutableBlockPos extends BlockPos {
/**
* Mutable X Coordinate
*/
protected int x;
/**
* Mutable Y Coordinate
*/
protected int y;
/**
* Mutable Z Coordinate
*/
protected int z;
public MutableBlockPos() {
this(ORIGIN);
}
public MutableBlockPos(@NonNull BlockPos blockPos) {
this(blockPos.getX(), blockPos.getY(), blockPos.getZ());
}
public MutableBlockPos(int x, int y, int z) {
super(ORIGIN);
this.x = x;
this.y = y;
this.z = z;
}
@Override
public BlockPos add(double x, double y, double z) {
return super.add(x, y, z).toImmutable();
}
@Override
public BlockPos add(int x, int y, int z) {
return super.add(x, y, z).toImmutable();
}
@Override
public BlockPos offset(@NonNull EnumFacing facing, int blocks) {
return super.offset(facing, blocks).toImmutable();
}
@Override
public BlockPos rotate(@NonNull Rotation rotation) {
return super.rotate(rotation).toImmutable();
}
@Override
public BlockPos toImmutable() {
return new BlockPos(this);
}
public BlockPos.MutableBlockPos setPos(@NonNull Entity entity) {
return this.setPos(entity.posX, entity.posY, entity.posZ);
}
public BlockPos.MutableBlockPos setPos(double x, double y, double z) {
return setPos(MathUtil.floor(x), MathUtil.floor(y), MathUtil.floor(z));
}
public BlockPos.MutableBlockPos setPos(int x, int y, int z) {
this.x = x;
this.y = y;
this.z = z;
return this;
}
public BlockPos.MutableBlockPos setPos(@NonNull Vec3i vec) {
return this.setPos(vec.getX(), vec.getY(), vec.getZ());
}
public BlockPos.MutableBlockPos move(@NonNull EnumFacing facing) {
return this.move(facing, 1);
}
public BlockPos.MutableBlockPos move(@NonNull EnumFacing facing, int blocks) {
return this.setPos(x + facing.getFrontOffsetX() * blocks,
y + facing.getFrontOffsetY() * blocks,
z + facing.getFrontOffsetZ() * blocks);
}
}
/**
* @since 0.10.0
*/
public static final class PooledMutableBlockPos extends BlockPos.MutableBlockPos {
private static final List<BlockPos.PooledMutableBlockPos> POOL = new ArrayList<>();
private boolean released;
private PooledMutableBlockPos(int x, int y, int z) {
super(x, y, z);
}
public static BlockPos.PooledMutableBlockPos retain() {
return retain(0, 0, 0);
}
public static BlockPos.PooledMutableBlockPos retain(int x, int y, int z) {
synchronized (POOL) {
if (!POOL.isEmpty()) {
val pos = POOL.remove(POOL.size() - 1);
if (pos != null && pos.released) {
pos.released = false;
pos.setPos(x, y, z);
return pos;
}
}
}
return new BlockPos.PooledMutableBlockPos(x, y, z);
}
public static BlockPos.PooledMutableBlockPos retain(double x, double y, double z) {
return retain(MathUtil.floor(x), MathUtil.floor(y), MathUtil.floor(z));
}
public static BlockPos.PooledMutableBlockPos retain(@NonNull Vec3i vec) {
return retain(vec.getX(), vec.getY(), vec.getZ());
}
public void release() {
synchronized (POOL) {
if (POOL.size() < 100) {
POOL.add(this);
}
this.released = true;
}
}
@Override
public BlockPos.PooledMutableBlockPos setPos(@NonNull Entity entity) {
return (BlockPos.PooledMutableBlockPos) super.setPos(entity);
}
@Override
public BlockPos.PooledMutableBlockPos setPos(double x, double y, double z) {
return (BlockPos.PooledMutableBlockPos) super.setPos(x, y, z);
}
@Override
public BlockPos.PooledMutableBlockPos setPos(int x, int y, int z) {
if (released) {
BlockPos.LOGGER.error("PooledMutableBlockPosition modified after it was released.", new Throwable());
released = false;
}
return (BlockPos.PooledMutableBlockPos) super.setPos(x, y, z);
}
@Override
public BlockPos.PooledMutableBlockPos setPos(@NonNull Vec3i vec) {
return (BlockPos.PooledMutableBlockPos) super.setPos(vec);
}
@Override
public BlockPos.PooledMutableBlockPos move(@NonNull EnumFacing facing) {
return (BlockPos.PooledMutableBlockPos) super.move(facing);
}
@Override
public BlockPos.PooledMutableBlockPos move(@NonNull EnumFacing facing, int blocks) {
return (BlockPos.PooledMutableBlockPos) super.move(facing, blocks);
}
}
}