Skip to content

Commit 8a6a312

Browse files
committed
Refactor ChunkPos
1 parent 16455fb commit 8a6a312

File tree

1 file changed

+55
-44
lines changed

1 file changed

+55
-44
lines changed
Lines changed: 55 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,100 +1,111 @@
11
package com.falsepattern.lib.compat;
22

33
import com.falsepattern.lib.StableAPI;
4+
import lombok.*;
45
import net.minecraft.entity.Entity;
56

67
import javax.annotation.concurrent.Immutable;
78

9+
/**
10+
* A functional equivalent to ChunkPos present in Minecraft 1.12.
11+
*/
812
@Immutable
13+
@EqualsAndHashCode
14+
@AllArgsConstructor
915
@StableAPI(since = "0.6.0")
1016
public class ChunkPos {
1117
/**
12-
* The X position of this Chunk Coordinate Pair
18+
* The x position of the chunk.
1319
*/
1420
public final int x;
1521
/**
16-
* The Z position of this Chunk Coordinate Pair
22+
* The z position of the chunk.
1723
*/
1824
public final int z;
1925

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;
2834
}
2935

3036
/**
3137
* 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
3242
*/
3343
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;
5245
}
5346

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;
6057
}
6158

6259
/**
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
6463
*/
6564
public int getXStart() {
66-
return this.x << 4;
65+
return x << 4;
6766
}
6867

6968
/**
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
7172
*/
7273
public int getZStart() {
73-
return this.z << 4;
74+
return z << 4;
7475
}
7576

7677
/**
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
7881
*/
7982
public int getXEnd() {
80-
return (this.x << 4) + 15;
83+
return (x << 4) + 15;
8184
}
8285

8386
/**
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
8590
*/
8691
public int getZEnd() {
87-
return (this.z << 4) + 15;
92+
return (z << 4) + 15;
8893
}
8994

9095
/**
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
92102
*/
93103
public BlockPos getBlock(int x, int y, int z) {
94104
return new BlockPos((this.x << 4) + x, y, (this.z << 4) + z);
95105
}
96106

107+
@Override
97108
public String toString() {
98-
return "[" + this.x + ", " + this.z + "]";
109+
return String.format("[%d, %d]", x, z);
99110
}
100111
}

0 commit comments

Comments
 (0)