Refactor ChunkPos

This commit is contained in:
basdxz committed 2022-05-31 14:12:17 +01:00
1 parent 16455fbb20
commit 8a6a3127a9
1 file changed
+57 -46
@@ -1,100 +1,111 @@
package com.falsepattern.lib.compat;
import com.falsepattern.lib.StableAPI;
import lombok.*;
import net.minecraft.entity.Entity;
import javax.annotation.concurrent.Immutable;
/**
* A functional equivalent to ChunkPos present in Minecraft 1.12.
*/
@Immutable
@EqualsAndHashCode
@AllArgsConstructor
@StableAPI(since = "0.6.0")
public class ChunkPos {
/**
* The X position of this Chunk Coordinate Pair
* The x position of the chunk.
*/
public final int x;
/**
* The Z position of this Chunk Coordinate Pair
* The z position of the chunk.
*/
public final int z;
public ChunkPos(int x, int z) {
this.x = x;
this.z = z;
}
public ChunkPos(BlockPos pos) {
this.x = pos.getX() >> 4;
this.z = pos.getZ() >> 4;
/**
* Instantiates a new ChunkPos.
*
* @param blockPos the block pos
*/
public ChunkPos(@NonNull BlockPos blockPos) {
x = blockPos.getX() >> 4;
z = blockPos.getZ() >> 4;
}
/**
* Converts the chunk coordinate pair to a long
*
* @param x the chunk x
* @param z the chunk x
* @return the unique chunk long
*/
public static long asLong(int x, int z) {
return (long) x & 4294967295L | ((long) z & 4294967295L) << 32;
}
public int hashCode() {
int i = 1664525 * this.x + 1013904223;
int j = 1664525 * (this.z ^ -559038737) + 1013904223;
return i ^ j;
}
public boolean equals(Object p_equals_1_) {
if (this == p_equals_1_) {
return true;
} else if (!(p_equals_1_ instanceof ChunkPos)) {
return false;
} else {
ChunkPos chunkpos = (ChunkPos) p_equals_1_;
return this.x == chunkpos.x && this.z == chunkpos.z;
}
}
public double getDistanceSq(Entity entityIn) {
double d0 = this.x * 16 + 8;
double d1 = this.z * 16 + 8;
double d2 = d0 - entityIn.posX;
double d3 = d1 - entityIn.posZ;
return d2 * d2 + d3 * d3;
return (long) x & 0xFFFFFFFFL | ((long) z & 0xFFFFFFFFL) << 32;
}
/**
* Get the first world X coordinate that belongs to this Chunk
* Gets distance sq.
*
* @param entity the entity
* @return the distance sq
*/
public double getDistanceSq(@NonNull Entity entity) {
val dX = ((x << 4) + 8) - entity.posX;
val dY = ((z << 4) + 8) - entity.posZ;
return dX * dX + dY * dY;
}
/**
* Get the first x pos inside this chunk.
*
* @return the x start
*/
public int getXStart() {
return this.x << 4;
return x << 4;
}
/**
* Get the first world Z coordinate that belongs to this Chunk
* Get the first z pos inside this chunk.
*
* @return the z start
*/
public int getZStart() {
return this.z << 4;
return z << 4;
}
/**
* Get the last world X coordinate that belongs to this Chunk
* Get the last x pos inside this chunk.
*
* @return the x end
*/
public int getXEnd() {
return (this.x << 4) + 15;
return (x << 4) + 15;
}
/**
* Get the last world Z coordinate that belongs to this Chunk
* Get the last z pos inside this chunk.
*
* @return the z end
*/
public int getZEnd() {
return (this.z << 4) + 15;
return (z << 4) + 15;
}
/**
* Get the World coordinates of the Block with the given Chunk coordinates relative to this chunk
* Get BlockPos with respect to this chunk.
*
* @param x the x pos
* @param y the y pos
* @param z the z pos
* @return the relative block position
*/
public BlockPos getBlock(int x, int y, int z) {
return new BlockPos((this.x << 4) + x, y, (this.z << 4) + z);
}
@Override
public String toString() {
return "[" + this.x + ", " + this.z + "]";
return String.format("[%d, %d]", x, z);
}
}