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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
|
package gtPlusPlus.api.objects.minecraft;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;
import net.minecraft.block.Block;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.world.World;
import net.minecraftforge.common.DimensionManager;
import gregtech.api.interfaces.tileentity.IGregTechTileEntity;
public class BlockPos implements Serializable {
private static final long serialVersionUID = -7271947491316682006L;
public final int xPos;
public final int yPos;
public final int zPos;
public final int dim;
public final transient World world;
public static BlockPos generateBlockPos(String sUUID) {
String[] s2 = sUUID.split("@");
return new BlockPos(s2);
}
public BlockPos(String[] s) {
this(Integer.parseInt(s[1]), Integer.parseInt(s[2]), Integer.parseInt(s[3]), Integer.parseInt(s[0]));
}
public BlockPos(int x, int y, int z) {
this(x, y, z, 0);
}
public BlockPos(int x, int y, int z, int dim) {
this(x, y, z, DimensionManager.getWorld(dim));
}
public BlockPos(int x, int y, int z, World dim) {
this.xPos = x;
this.yPos = y;
this.zPos = z;
if (dim != null) {
this.dim = dim.provider.dimensionId;
this.world = dim;
} else {
this.dim = 0;
this.world = null;
}
}
public BlockPos(IGregTechTileEntity b) {
this(b.getXCoord(), b.getYCoord(), b.getZCoord(), b.getWorld());
}
public BlockPos(TileEntity b) {
this(b.xCoord, b.yCoord, b.zCoord, b.getWorldObj());
}
public String getLocationString() {
return "[X: " + this.xPos + "][Y: " + this.yPos + "][Z: " + this.zPos + "][Dim: " + this.dim + "]";
}
public String getUniqueIdentifier() {
String S = "" + this.dim + "@" + this.xPos + "@" + this.yPos + "@" + this.zPos;
return S;
}
@Override
public int hashCode() {
int hash = 5;
hash += (13 * this.xPos);
hash += (19 * this.yPos);
hash += (31 * this.zPos);
hash += (17 * this.dim);
return hash;
}
@Override
public boolean equals(Object other) {
if (other == null) {
return false;
}
if (other == this) {
return true;
}
if (!(other instanceof BlockPos otherPoint)) {
return false;
}
return this.xPos == otherPoint.xPos && this.yPos == otherPoint.yPos
&& this.zPos == otherPoint.zPos
&& this.dim == otherPoint.dim;
}
public int distanceFrom(BlockPos target) {
if (target.dim != this.dim) {
return Short.MIN_VALUE;
}
return distanceFrom(target.xPos, target.yPos, target.zPos);
}
/**
*
* @param x X coordinate of target.
* @param y Y coordinate of target.
* @param z Z coordinate of target.
* @return square of distance
*/
public int distanceFrom(int x, int y, int z) {
int distanceX = this.xPos - x;
int distanceY = this.yPos - y;
int distanceZ = this.zPos - z;
return distanceX * distanceX + distanceY * distanceY + distanceZ * distanceZ;
}
public boolean isWithinRange(BlockPos target, int range) {
if (target.dim != this.dim) {
return false;
}
return isWithinRange(target.xPos, target.yPos, target.zPos, range);
}
public boolean isWithinRange(int x, int y, int z, int range) {
return distanceFrom(x, y, z) <= (range * range);
}
public BlockPos getUp() {
return new BlockPos(this.xPos, this.yPos + 1, this.zPos, this.dim);
}
public BlockPos getDown() {
return new BlockPos(this.xPos, this.yPos - 1, this.zPos, this.dim);
}
public BlockPos getXPos() {
return new BlockPos(this.xPos + 1, this.yPos, this.zPos, this.dim);
}
public BlockPos getXNeg() {
return new BlockPos(this.xPos - 1, this.yPos, this.zPos, this.dim);
}
public BlockPos getZPos() {
return new BlockPos(this.xPos, this.yPos, this.zPos + 1, this.dim);
}
public BlockPos getZNeg() {
return new BlockPos(this.xPos, this.yPos, this.zPos - 1, this.dim);
}
public ArrayList<BlockPos> getSurroundingBlocks() {
ArrayList<BlockPos> sides = new ArrayList<>();
sides.add(getUp());
sides.add(getDown());
sides.add(getXPos());
sides.add(getXNeg());
sides.add(getZPos());
sides.add(getZNeg());
return sides;
}
public Block getBlockAtPos() {
return getBlockAtPos(this);
}
public Block getBlockAtPos(BlockPos pos) {
return getBlockAtPos(world, pos);
}
public Block getBlockAtPos(World world, BlockPos pos) {
return world.getBlock(pos.xPos, pos.yPos, pos.zPos);
}
public int getMetaAtPos() {
return getMetaAtPos(this);
}
public int getMetaAtPos(BlockPos pos) {
return getMetaAtPos(world, pos);
}
public int getMetaAtPos(World world, BlockPos pos) {
return world.getBlockMetadata(pos.xPos, pos.yPos, pos.zPos);
}
public boolean hasSimilarNeighbour() {
return hasSimilarNeighbour(false);
}
/**
* @param strict - Does this check Meta Data?
* @return - Does this block have a neighbour that is the same?
*/
public boolean hasSimilarNeighbour(boolean strict) {
for (BlockPos g : getSurroundingBlocks()) {
if (getBlockAtPos(g) == getBlockAtPos()) {
if (!strict) {
return true;
} else {
if (getMetaAtPos() == getMetaAtPos(g)) {
return true;
}
}
}
}
return false;
}
public ArrayList<BlockPos> getSimilarNeighbour() {
return getSimilarNeighbour(false);
}
/**
* @param strict - Does this check Meta Data?
* @return - Does this block have a neighbour that is the same?
*/
public ArrayList<BlockPos> getSimilarNeighbour(boolean strict) {
ArrayList<BlockPos> sides = new ArrayList<>();
for (BlockPos g : getSurroundingBlocks()) {
if (getBlockAtPos(g) == getBlockAtPos()) {
if (!strict) {
sides.add(g);
} else {
if (getMetaAtPos() == getMetaAtPos(g)) {
sides.add(g);
}
}
}
}
return sides;
}
public Set<BlockPos> getValidNeighboursAndSelf() {
ArrayList<BlockPos> h = getSimilarNeighbour(true);
h.add(this);
return new HashSet<>(h);
}
}
|