Files
FalsePatternLib/src/main/java/com/falsepattern/lib/compat/ChunkPos.java
T

136 lines
3.3 KiB
Java
Raw Normal View History

2022-07-30 15:21:11 +02:00
/**
* Copyright (C) 2022 FalsePattern
* All Rights Reserved
*
* The above copyright notice, this permission notice and the word "SNEED"
* shall be included in all copies or substantial portions of the Software.
*
* This program 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, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
2022-05-06 17:41:02 +02:00
package com.falsepattern.lib.compat;
2022-05-06 23:53:11 +02:00
import com.falsepattern.lib.StableAPI;
import javax.annotation.concurrent.Immutable;
2022-06-08 16:01:06 +02:00
import lombok.AllArgsConstructor;
import lombok.EqualsAndHashCode;
import lombok.NonNull;
import lombok.val;
import net.minecraft.entity.Entity;
2022-05-06 23:53:11 +02:00
2022-05-31 14:12:17 +01:00
/**
* A functional equivalent to ChunkPos present in Minecraft 1.12.
*/
2022-05-06 23:53:11 +02:00
@Immutable
2022-05-31 14:12:17 +01:00
@EqualsAndHashCode
@AllArgsConstructor
2022-05-06 23:53:11 +02:00
@StableAPI(since = "0.6.0")
2022-05-06 17:41:02 +02:00
public class ChunkPos {
/**
2022-05-31 14:12:17 +01:00
* The x position of the chunk.
2022-05-06 17:41:02 +02:00
*/
public final int x;
/**
2022-05-31 14:12:17 +01:00
* The z position of the chunk.
2022-05-06 17:41:02 +02:00
*/
public final int z;
2022-05-31 14:12:17 +01:00
/**
* Instantiates a new ChunkPos.
*
* @param blockPos the block pos
*/
public ChunkPos(@NonNull BlockPos blockPos) {
x = blockPos.getX() >> 4;
z = blockPos.getZ() >> 4;
2022-05-06 17:41:02 +02:00
}
/**
* Converts the chunk coordinate pair to a long
2022-05-31 14:12:17 +01:00
*
* @param x the chunk x
* @param z the chunk x
2022-06-08 16:01:06 +02:00
*
2022-05-31 14:12:17 +01:00
* @return the unique chunk long
2022-05-06 17:41:02 +02:00
*/
public static long asLong(int x, int z) {
2022-05-31 14:12:17 +01:00
return (long) x & 0xFFFFFFFFL | ((long) z & 0xFFFFFFFFL) << 32;
2022-05-06 17:41:02 +02:00
}
2022-05-31 14:12:17 +01:00
/**
* Gets distance sq.
*
* @param entity the entity
2022-06-08 16:01:06 +02:00
*
2022-05-31 14:12:17 +01:00
* @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;
2022-05-06 17:41:02 +02:00
}
/**
2022-05-31 14:12:17 +01:00
* Get the first x pos inside this chunk.
*
* @return the x start
2022-05-06 17:41:02 +02:00
*/
public int getXStart() {
2022-05-31 14:12:17 +01:00
return x << 4;
2022-05-06 17:41:02 +02:00
}
/**
2022-05-31 14:12:17 +01:00
* Get the first z pos inside this chunk.
*
* @return the z start
2022-05-06 17:41:02 +02:00
*/
public int getZStart() {
2022-05-31 14:12:17 +01:00
return z << 4;
2022-05-06 17:41:02 +02:00
}
/**
2022-05-31 14:12:17 +01:00
* Get the last x pos inside this chunk.
*
* @return the x end
2022-05-06 17:41:02 +02:00
*/
public int getXEnd() {
2022-05-31 14:12:17 +01:00
return (x << 4) + 15;
2022-05-06 17:41:02 +02:00
}
/**
2022-05-31 14:12:17 +01:00
* Get the last z pos inside this chunk.
*
* @return the z end
2022-05-06 17:41:02 +02:00
*/
public int getZEnd() {
2022-05-31 14:12:17 +01:00
return (z << 4) + 15;
2022-05-06 17:41:02 +02:00
}
/**
2022-05-31 14:12:17 +01:00
* Get BlockPos with respect to this chunk.
*
* @param x the x pos
* @param y the y pos
* @param z the z pos
2022-06-08 16:01:06 +02:00
*
2022-05-31 14:12:17 +01:00
* @return the relative block position
2022-05-06 17:41:02 +02:00
*/
public BlockPos getBlock(int x, int y, int z) {
return new BlockPos((this.x << 4) + x, y, (this.z << 4) + z);
}
2022-05-31 14:12:17 +01:00
@Override
2022-05-06 17:41:02 +02:00
public String toString() {
2022-05-31 14:12:17 +01:00
return String.format("[%d, %d]", x, z);
2022-05-06 17:41:02 +02:00
}
}