-
Notifications
You must be signed in to change notification settings - Fork 497
/
Box.java
271 lines (205 loc) · 5.54 KB
/
Box.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
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
/**
* Copyright (c) 2011-2014, SpaceToad and the BuildCraft Team
* http://www.mod-buildcraft.com
*
* BuildCraft is distributed under the terms of the Minecraft Mod Public
* License 1.0, or MMPL. Please check the contents of the license located in
* http://www.mod-buildcraft.com/MMPL-1.0.txt
*/
package buildcraft.core;
import buildcraft.api.core.IAreaProvider;
import buildcraft.api.core.IBox;
import buildcraft.api.core.LaserKind;
import buildcraft.api.core.Position;
import buildcraft.core.network.TileNetworkData;
import buildcraft.core.proxy.CoreProxy;
import buildcraft.core.utils.Utils;
import io.netty.buffer.ByteBuf;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.world.World;
public class Box implements IBox {
public @TileNetworkData
int xMin, yMin, zMin, xMax, yMax, zMax;
public @TileNetworkData
boolean initialized;
private EntityBlock lasers[];
public Box() {
reset();
}
public void reset() {
initialized = false;
xMin = Integer.MAX_VALUE;
yMin = Integer.MAX_VALUE;
zMin = Integer.MAX_VALUE;
xMax = Integer.MAX_VALUE;
yMax = Integer.MAX_VALUE;
zMax = Integer.MAX_VALUE;
}
public boolean isInitialized() {
return initialized;
}
public void initialize(int xMin, int yMin, int zMin, int xMax, int yMax, int zMax) {
this.xMin = xMin;
this.yMin = yMin;
this.zMin = zMin;
this.xMax = xMax;
this.yMax = yMax;
this.zMax = zMax;
initialized = !(xMin == Integer.MAX_VALUE || yMin == Integer.MAX_VALUE || zMin == Integer.MAX_VALUE || xMax == Integer.MAX_VALUE
|| yMax == Integer.MAX_VALUE || zMax == Integer.MAX_VALUE);
}
public void initialize(Box box) {
initialize(box.xMin, box.yMin, box.zMin, box.xMax, box.yMax, box.zMax);
}
public void initialize(IAreaProvider a) {
initialize(a.xMin(), a.yMin(), a.zMin(), a.xMax(), a.yMax(), a.zMax());
}
public void initialize(NBTTagCompound nbttagcompound) {
initialize(nbttagcompound.getInteger("xMin"), nbttagcompound.getInteger("yMin"), nbttagcompound.getInteger("zMin"), nbttagcompound.getInteger("xMax"),
nbttagcompound.getInteger("yMax"), nbttagcompound.getInteger("zMax"));
}
public void initialize(int centerX, int centerY, int centerZ, int size) {
initialize(centerX - size, centerY - size, centerZ - size, centerX + size, centerY + size, centerZ + size);
}
public List<BlockIndex> getBlocksInArea() {
List<BlockIndex> blocks = new ArrayList<BlockIndex>();
for (int x = xMin; x <= xMax; x++) {
for (int y = yMin; y <= yMax; y++) {
for (int z = zMin; z <= zMax; z++) {
blocks.add(new BlockIndex(x, y, z));
}
}
}
return blocks;
}
@Override
public void expand(int amount) {
xMin += amount;
yMin += amount;
zMin += amount;
xMax += amount;
yMax += amount;
zMax += amount;
}
@Override
public void contract(int amount) {
expand(-amount);
}
@Override
public boolean contains(int x, int y, int z) {
if (x >= xMin && x <= xMax && y >= yMin && y <= yMax && z >= zMin && z <= zMax)
return true;
return false;
}
public boolean contains(Position p) {
return contains((int) p.x, (int) p.y, (int) p.z);
}
public boolean contains(BlockIndex i) {
return contains(i.x, i.y, i.z);
}
@Override
public Position pMin() {
return new Position(xMin, yMin, zMin);
}
@Override
public Position pMax() {
return new Position(xMax, yMax, zMax);
}
public int sizeX() {
return xMax - xMin + 1;
}
public int sizeY() {
return yMax - yMin + 1;
}
public int sizeZ() {
return zMax - zMin + 1;
}
public double centerX() {
return xMin + sizeX() / 2.0;
}
public double centerY() {
return yMin + sizeY() / 2.0;
}
public double centerZ() {
return zMin + sizeZ() / 2.0;
}
public Box rotateLeft() {
Box nBox = new Box();
nBox.xMin = (sizeZ() - 1) - zMin;
nBox.yMin = yMin;
nBox.zMin = xMin;
nBox.xMax = (sizeZ() - 1) - zMax;
nBox.yMax = yMax;
nBox.zMax = xMax;
nBox.reorder();
return nBox;
}
public void reorder() {
int tmp;
if (xMin > xMax) {
tmp = xMin;
xMin = xMax;
xMax = tmp;
}
if (yMin > yMax) {
tmp = yMin;
yMin = yMax;
yMax = tmp;
}
if (zMin > zMax) {
tmp = zMin;
zMin = zMax;
zMax = tmp;
}
}
@Override
public void createLasers(World world, LaserKind kind) {
if (lasers == null) {
lasers = Utils.createLaserBox(world, xMin, yMin, zMin, xMax, yMax, zMax, kind);
}
}
@Override
public void deleteLasers() {
if (lasers != null) {
for (EntityBlock b : lasers) {
CoreProxy.proxy.removeEntity(b);
}
lasers = null;
}
}
public void writeToStream(ByteBuf stream) {
stream.writeBoolean(initialized);
stream.writeInt(xMin);
stream.writeInt(yMin);
stream.writeInt(zMin);
stream.writeInt(xMax);
stream.writeInt(yMax);
stream.writeInt(zMax);
}
public void readFromStream(ByteBuf stream) {
initialized = stream.readBoolean();
xMin = stream.readInt();
yMin = stream.readInt();
zMin = stream.readInt();
xMax = stream.readInt();
yMax = stream.readInt();
zMax = stream.readInt();
}
public void writeToNBT(NBTTagCompound nbttagcompound) {
nbttagcompound.setInteger("xMin", xMin);
nbttagcompound.setInteger("yMin", yMin);
nbttagcompound.setInteger("zMin", zMin);
nbttagcompound.setInteger("xMax", xMax);
nbttagcompound.setInteger("yMax", yMax);
nbttagcompound.setInteger("zMax", zMax);
}
@Override
public String toString() {
return "{" + xMin + ", " + xMax + "}, {" + yMin + ", " + yMax + "}, {" + zMin + ", " + zMax + "}";
}
}