-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
ChunkPos.java
150 lines (137 loc) · 3.55 KB
/
ChunkPos.java
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
/*
* This file is part of FalsePatternLib.
*
* Copyright (C) 2022-2024 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, either version 3 of the License, or
* (at your option) any later version.
*
* 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.StableAPI;
import lombok.AllArgsConstructor;
import lombok.EqualsAndHashCode;
import lombok.NonNull;
import lombok.val;
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 the chunk.
*/
@StableAPI.Expose
public final int x;
/**
* The z position of the chunk.
*/
@StableAPI.Expose
public final int z;
/**
* Instantiates a new ChunkPos.
*
* @param blockPos the block pos
*/
@StableAPI.Expose
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
*/
@StableAPI.Expose
public static long asLong(int x, int z) {
return (long) x & 0xFFFFFFFFL | ((long) z & 0xFFFFFFFFL) << 32;
}
/**
* Gets distance sq.
*
* @param entity the entity
*
* @return the distance sq
*/
@StableAPI.Expose
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
*/
@StableAPI.Expose
public int getXStart() {
return x << 4;
}
/**
* Get the first z pos inside this chunk.
*
* @return the z start
*/
@StableAPI.Expose
public int getZStart() {
return z << 4;
}
/**
* Get the last x pos inside this chunk.
*
* @return the x end
*/
@StableAPI.Expose
public int getXEnd() {
return (x << 4) + 15;
}
/**
* Get the last z pos inside this chunk.
*
* @return the z end
*/
@StableAPI.Expose
public int getZEnd() {
return (z << 4) + 15;
}
/**
* 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
*/
@StableAPI.Expose
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 String.format("[%d, %d]", x, z);
}
}