-
Notifications
You must be signed in to change notification settings - Fork 754
/
MultiblockStructureData.java
274 lines (244 loc) · 9.15 KB
/
MultiblockStructureData.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
272
273
274
package slimeknights.tconstruct.smeltery.block.entity.multiblock;
import lombok.Getter;
import net.minecraft.core.BlockPos;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.ListTag;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.phys.AABB;
import slimeknights.mantle.block.entity.MantleBlockEntity;
import slimeknights.mantle.util.BlockEntityHelper;
import slimeknights.tconstruct.common.multiblock.IMasterLogic;
import slimeknights.tconstruct.common.multiblock.IServantLogic;
import slimeknights.tconstruct.library.utils.TagUtil;
import slimeknights.tconstruct.smeltery.block.component.SearedBlock;
import javax.annotation.Nullable;
import java.util.Collection;
import java.util.Set;
import java.util.function.Consumer;
import java.util.function.Predicate;
/**
* Data class representing the size and contents of a multiblock
*/
public class MultiblockStructureData {
public static final String TAG_EXTRA_POS = "extra";
public static final String TAG_MIN = "min";
public static final String TAG_MAX = "max";
/** Smallest block position in the structure */
@Getter
private final BlockPos minPos;
/** Largest block position in the structure */
@Getter
private final BlockPos maxPos;
/** Contains all positions not in the standard areas, typically inside */
protected final Set<BlockPos> extra;
/** Booleans to determine bound check parameters */
private final boolean hasCeiling, hasFrame, hasFloor;
/**
* Smallest position inside the structure walls
*/
@Getter
private final BlockPos minInside;
/**
* Largest position inside the structure walls
*/
@Getter
private final BlockPos maxInside;
/** Inside sizes */
@Getter
private final int innerX, innerY, innerZ;
/** Bounding box representing the area inside the structure */
@Getter
private final AABB bounds;
public MultiblockStructureData(BlockPos minPos, BlockPos maxPos, Set<BlockPos> extraPositons, boolean hasFloor, boolean hasFrame, boolean hasCeiling) {
this.minPos = minPos;
this.maxPos = maxPos;
this.extra = extraPositons;
this.hasFloor = hasFloor;
this.hasFrame = hasFrame;
this.hasCeiling = hasCeiling;
// inner positions
minInside = minPos.offset(1, hasFloor ? 1 : 0, 1);
maxInside = maxPos.offset(-1, hasCeiling ? -1 : 0, -1);
innerX = maxInside.getX() - minInside.getX() + 1;
innerY = maxInside.getY() - minInside.getY() + 1;
innerZ = maxInside.getZ() - minInside.getZ() + 1;
bounds = new AABB(minInside, maxInside.offset(1, 1, 1));
}
/**
* Checks if a positon is within the cube made from two other positions
* @param pos Position to check
* @param min Min position
* @param max Max position
* @return True if within the positions
*/
public static boolean isWithin(BlockPos pos, BlockPos min, BlockPos max) {
return pos.getX() >= min.getX() && pos.getY() >= min.getY() && pos.getZ() >= min.getZ()
&& pos.getX() <= max.getX() && pos.getY() <= max.getY() && pos.getZ() <= max.getZ();
}
/**
* Checks if the block position is within the bounds of the structure
* @param pos Position to check
* @return True if the position is within the bounds
*/
public boolean withinBounds(BlockPos pos) {
return isWithin(pos, minPos, maxPos);
}
/**
* Checks if the position is within the inside of the structure
* @param pos Position to check
* @return True if within the central bounds
*/
public boolean isInside(BlockPos pos) {
return isWithin(pos, minInside, maxInside);
}
/**
* Checks if the given block position is part of this structure.
* @param pos Position to check
* @return True if its part of this structure
*/
public boolean contains(BlockPos pos) {
return withinBounds(pos) && containsBase(pos);
}
/**
* Checks if the given block position is part of this structure. Slightly simplier logic assuming the position is within bounds
* @param pos Position to check
* @return True if its part of this structure
*/
private boolean containsBase(BlockPos pos) {
// blocks in the inner region are added to the extra positions, fall back to that
if (!isInside(pos)) {
// if there is a frame, shape is a full cube so the subtraction is all we need
if (hasFrame) {
return true;
}
// otherwise we have to count edges to make sure its not on a frame
// frame is any blocks touching two edges
int edges = 0;
if (pos.getX() == minPos.getX() || pos.getX() == maxPos.getX()) edges++;
if (pos.getZ() == minPos.getZ() || pos.getZ() == maxPos.getZ()) edges++;
if ((hasFloor && pos.getY() == minPos.getY()) ||
(hasCeiling && pos.getX() == maxPos.getX())) edges++;
if (edges < 2) {
return true;
}
}
// inner blocks and frame blocks (no frame) can both be added
// though note checking code does not currently support finding extra frame blocks
return extra.contains(pos);
}
/**
* Checks if the block position is directly above the structure
* @param pos Position to check
* @return True if the position is exactly one block above the structure
*/
public boolean isDirectlyAbove(BlockPos pos) {
return pos.getX() >= minPos.getX() && pos.getZ() >= minPos.getZ()
&& pos.getX() <= maxPos.getX() && pos.getZ() <= maxPos.getZ()
&& pos.getY() == maxPos.getY() + 1;
}
/**
* Iterates over each position contained in this structure
* @param consumer Position consumer, note the position is mutable, so call {@link BlockPos#immutable()} if you have to store it
*/
public void forEachContained(Consumer<BlockPos.MutableBlockPos> consumer) {
BlockPos.MutableBlockPos mutable = new BlockPos.MutableBlockPos();
for (int x = minPos.getX(); x <= maxPos.getX(); x++) {
for (int y = minPos.getY(); y <= maxPos.getY(); y++) {
for (int z = minPos.getZ(); z <= maxPos.getZ(); z++) {
mutable.set(x, y, z);
if (containsBase(mutable)) {
consumer.accept(mutable);
}
}
}
}
}
/** Updates the master at the given position */
private static void updateMaster(Level world, BlockPos pos, IMasterLogic master, boolean add) {
// update the structure property first, this may add or remove the block entity
BlockState state = world.getBlockState(pos);
if (state.hasProperty(SearedBlock.IN_STRUCTURE) && state.getValue(SearedBlock.IN_STRUCTURE) != add) {
world.setBlock(pos, state.setValue(SearedBlock.IN_STRUCTURE, add), Block.UPDATE_CLIENTS);
}
// if the BE is there, set its property
BlockEntityHelper.get(IServantLogic.class, world, pos).ifPresent(
add ? te -> te.setPotentialMaster(master) : te -> te.removeMaster(master));
}
/**
* Assigns the master to all servants in this structure
* @param master Master to assign
* @param oldStructure Previous structure instance. Reduces the number of masters assigned and removes old masters
*/
public <T extends MantleBlockEntity & IMasterLogic> void assignMaster(T master, @Nullable MultiblockStructureData oldStructure) {
Predicate<BlockPos> shouldUpdate;
if (oldStructure == null) {
shouldUpdate = pos -> true;
} else {
shouldUpdate = pos -> !oldStructure.contains(pos);
}
Level world = master.getLevel();
assert world != null;
// assign master to each servant
forEachContained(pos -> {
if (shouldUpdate.test(pos) && world.hasChunkAt(pos)) {
updateMaster(world, pos, master, true);
}
});
// remove master from anything only in the old structure
if (oldStructure != null) {
oldStructure.forEachContained(pos -> {
if (!contains(pos) && world.hasChunkAt(pos)) {
updateMaster(world, pos, master, false);
}
});
}
}
/**
* Clears the master on all blocks in this structure
* @param master Master to remove
*/
public <T extends MantleBlockEntity & IMasterLogic> void clearMaster(T master) {
Level world = master.getLevel();
assert world != null;
forEachContained(pos -> {
if (world.hasChunkAt(pos)) {
updateMaster(world, pos, master, false);
}
});
}
/**
* Writes this structure to NBT for the client, client does not need a full list of positions, just render bounds
* @return structure as NBT
*/
public CompoundTag writeClientTag() {
CompoundTag nbt = new CompoundTag();
nbt.put(TAG_MIN, TagUtil.writePos(minPos));
nbt.put(TAG_MAX, TagUtil.writePos(maxPos));
return nbt;
}
/**
* Writes the full NBT data for writing to disk
* @return structure as NBT
*/
public CompoundTag writeToTag() {
CompoundTag nbt = writeClientTag();
if (!extra.isEmpty()) {
nbt.put(TAG_EXTRA_POS, writePosList(extra));
}
return nbt;
}
/**
* Writes a lit of positions to NBT
* @param collection Position collection
* @return NBT list
*/
protected static ListTag writePosList(Collection<BlockPos> collection) {
ListTag list = new ListTag();
for (BlockPos pos : collection) {
list.add(TagUtil.writePos(pos));
}
return list;
}
}