1
1
package com .falsepattern .lib .compat ;
2
2
3
3
import com .falsepattern .lib .StableAPI ;
4
+ import lombok .*;
4
5
import net .minecraft .entity .Entity ;
5
6
6
7
import javax .annotation .concurrent .Immutable ;
7
8
9
+ /**
10
+ * A functional equivalent to ChunkPos present in Minecraft 1.12.
11
+ */
8
12
@ Immutable
13
+ @ EqualsAndHashCode
14
+ @ AllArgsConstructor
9
15
@ StableAPI (since = "0.6.0" )
10
16
public class ChunkPos {
11
17
/**
12
- * The X position of this Chunk Coordinate Pair
18
+ * The x position of the chunk.
13
19
*/
14
20
public final int x ;
15
21
/**
16
- * The Z position of this Chunk Coordinate Pair
22
+ * The z position of the chunk.
17
23
*/
18
24
public final int z ;
19
25
20
- public ChunkPos ( int x , int z ) {
21
- this . x = x ;
22
- this . z = z ;
23
- }
24
-
25
- public ChunkPos (BlockPos pos ) {
26
- this . x = pos .getX () >> 4 ;
27
- this . z = pos .getZ () >> 4 ;
26
+ /**
27
+ * Instantiates a new ChunkPos.
28
+ *
29
+ * @param blockPos the block pos
30
+ */
31
+ public ChunkPos (@ NonNull BlockPos blockPos ) {
32
+ x = blockPos .getX () >> 4 ;
33
+ z = blockPos .getZ () >> 4 ;
28
34
}
29
35
30
36
/**
31
37
* Converts the chunk coordinate pair to a long
38
+ *
39
+ * @param x the chunk x
40
+ * @param z the chunk x
41
+ * @return the unique chunk long
32
42
*/
33
43
public static long asLong (int x , int z ) {
34
- return (long ) x & 4294967295L | ((long ) z & 4294967295L ) << 32 ;
35
- }
36
-
37
- public int hashCode () {
38
- int i = 1664525 * this .x + 1013904223 ;
39
- int j = 1664525 * (this .z ^ -559038737 ) + 1013904223 ;
40
- return i ^ j ;
41
- }
42
-
43
- public boolean equals (Object p_equals_1_ ) {
44
- if (this == p_equals_1_ ) {
45
- return true ;
46
- } else if (!(p_equals_1_ instanceof ChunkPos )) {
47
- return false ;
48
- } else {
49
- ChunkPos chunkpos = (ChunkPos ) p_equals_1_ ;
50
- return this .x == chunkpos .x && this .z == chunkpos .z ;
51
- }
44
+ return (long ) x & 0xFFFFFFFFL | ((long ) z & 0xFFFFFFFFL ) << 32 ;
52
45
}
53
46
54
- public double getDistanceSq (Entity entityIn ) {
55
- double d0 = this .x * 16 + 8 ;
56
- double d1 = this .z * 16 + 8 ;
57
- double d2 = d0 - entityIn .posX ;
58
- double d3 = d1 - entityIn .posZ ;
59
- return d2 * d2 + d3 * d3 ;
47
+ /**
48
+ * Gets distance sq.
49
+ *
50
+ * @param entity the entity
51
+ * @return the distance sq
52
+ */
53
+ public double getDistanceSq (@ NonNull Entity entity ) {
54
+ val dX = ((x << 4 ) + 8 ) - entity .posX ;
55
+ val dY = ((z << 4 ) + 8 ) - entity .posZ ;
56
+ return dX * dX + dY * dY ;
60
57
}
61
58
62
59
/**
63
- * Get the first world X coordinate that belongs to this Chunk
60
+ * Get the first x pos inside this chunk.
61
+ *
62
+ * @return the x start
64
63
*/
65
64
public int getXStart () {
66
- return this . x << 4 ;
65
+ return x << 4 ;
67
66
}
68
67
69
68
/**
70
- * Get the first world Z coordinate that belongs to this Chunk
69
+ * Get the first z pos inside this chunk.
70
+ *
71
+ * @return the z start
71
72
*/
72
73
public int getZStart () {
73
- return this . z << 4 ;
74
+ return z << 4 ;
74
75
}
75
76
76
77
/**
77
- * Get the last world X coordinate that belongs to this Chunk
78
+ * Get the last x pos inside this chunk.
79
+ *
80
+ * @return the x end
78
81
*/
79
82
public int getXEnd () {
80
- return (this . x << 4 ) + 15 ;
83
+ return (x << 4 ) + 15 ;
81
84
}
82
85
83
86
/**
84
- * Get the last world Z coordinate that belongs to this Chunk
87
+ * Get the last z pos inside this chunk.
88
+ *
89
+ * @return the z end
85
90
*/
86
91
public int getZEnd () {
87
- return (this . z << 4 ) + 15 ;
92
+ return (z << 4 ) + 15 ;
88
93
}
89
94
90
95
/**
91
- * Get the World coordinates of the Block with the given Chunk coordinates relative to this chunk
96
+ * Get BlockPos with respect to this chunk.
97
+ *
98
+ * @param x the x pos
99
+ * @param y the y pos
100
+ * @param z the z pos
101
+ * @return the relative block position
92
102
*/
93
103
public BlockPos getBlock (int x , int y , int z ) {
94
104
return new BlockPos ((this .x << 4 ) + x , y , (this .z << 4 ) + z );
95
105
}
96
106
107
+ @ Override
97
108
public String toString () {
98
- return "[" + this . x + ", " + this . z + "]" ;
109
+ return String . format ( "[%d, %d]" , x , z ) ;
99
110
}
100
111
}
0 commit comments