-
Notifications
You must be signed in to change notification settings - Fork 361
Expand file tree
/
Copy pathMultiblockControllerMachine.java
More file actions
415 lines (371 loc) · 13.8 KB
/
MultiblockControllerMachine.java
File metadata and controls
415 lines (371 loc) · 13.8 KB
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
package com.gregtechceu.gtceu.api.machine.multiblock;
import com.gregtechceu.gtceu.GTCEu;
import com.gregtechceu.gtceu.api.block.MetaMachineBlock;
import com.gregtechceu.gtceu.api.block.property.GTBlockStateProperties;
import com.gregtechceu.gtceu.api.blockentity.BlockEntityCreationInfo;
import com.gregtechceu.gtceu.api.machine.MetaMachine;
import com.gregtechceu.gtceu.api.machine.MultiblockMachineDefinition;
import com.gregtechceu.gtceu.api.machine.feature.multiblock.IMultiPart;
import com.gregtechceu.gtceu.api.machine.property.GTMachineModelProperties;
import com.gregtechceu.gtceu.api.pattern.BlockPattern;
import com.gregtechceu.gtceu.api.pattern.MultiblockState;
import com.gregtechceu.gtceu.api.pattern.MultiblockWorldSavedData;
import com.gregtechceu.gtceu.api.sync_system.annotations.ClientFieldChangeListener;
import com.gregtechceu.gtceu.api.sync_system.annotations.RerenderOnChanged;
import com.gregtechceu.gtceu.api.sync_system.annotations.SaveField;
import com.gregtechceu.gtceu.api.sync_system.annotations.SyncToClient;
import com.gregtechceu.gtceu.client.model.machine.MachineRenderState;
import com.gregtechceu.gtceu.client.renderer.MultiblockInWorldPreviewRenderer;
import com.gregtechceu.gtceu.common.machine.multiblock.part.ParallelHatchPartMachine;
import com.gregtechceu.gtceu.config.ConfigHolder;
import net.minecraft.MethodsReturnNonnullByDefault;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.phys.BlockHitResult;
import lombok.Getter;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.*;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import javax.annotation.ParametersAreNonnullByDefault;
@ParametersAreNonnullByDefault
@MethodsReturnNonnullByDefault
public class MultiblockControllerMachine extends MetaMachine {
private MultiblockState multiblockState;
private final List<IMultiPart> parts = new ArrayList<>();
private @Nullable ParallelHatchPartMachine parallelHatch = null;
@Getter
@SyncToClient
private BlockPos[] partPositions = new BlockPos[0];
/**
* Whether Multiblock Formed.
* <br>
* NOTE: even machine is formed, it doesn't mean to workable!
* Its parts maybe invalid due to chunk unload.
*/
@Getter
@SaveField
@SyncToClient
@RerenderOnChanged
protected boolean isFormed;
@Getter
@SaveField
@SyncToClient
protected boolean isFlipped;
public MultiblockControllerMachine(BlockEntityCreationInfo info) {
super(info);
}
//////////////////////////////////////
// *** Multiblock Lifecycle ***//
//////////////////////////////////////
@Override
public void onLoad() {
super.onLoad();
if (getLevel() instanceof ServerLevel serverLevel) {
MultiblockWorldSavedData.getOrCreate(serverLevel).addAsyncLogic(this);
}
}
@Override
public void onUnload() {
super.onUnload();
if (getLevel() instanceof ServerLevel serverLevel) {
MultiblockWorldSavedData.getOrCreate(serverLevel).removeAsyncLogic(this);
}
}
/**
* Called when structure is formed, have to be called after {@link #checkPattern()}. (server-side / fake scene only)
* <br>
* Trigger points:
* <br>
* 1 - Blocks in structure changed but still formed.
* <br>
* 2 - Literally, structure formed.
*/
public void onStructureFormed() {
isFormed = true;
syncDataHolder.markClientSyncFieldDirty("isFormed");
MachineRenderState renderState = getRenderState();
if (renderState.hasProperty(GTMachineModelProperties.IS_FORMED)) {
setRenderState(renderState.setValue(GTMachineModelProperties.IS_FORMED, true));
}
this.parts.clear();
Set<IMultiPart> set = getMultiblockState().getMatchContext().getOrCreate("parts", Collections::emptySet);
for (IMultiPart part : set) {
if (shouldAddPartToController(part)) {
this.parts.add(part);
}
}
this.parts.sort(getPartSorter());
for (var part : parts) {
if (part instanceof ParallelHatchPartMachine pHatch) {
parallelHatch = pHatch;
}
part.addedToController(this);
}
updatePartPositions();
}
/**
* Called when structure is invalid. (server-side / fake scene only)
* <br>
* Trigger points:
* <br>
* 1 - Blocks in structure changed.
* <br>
* 2 - Before controller machine removed.
*/
public void onStructureInvalid() {
isFormed = false;
MachineRenderState renderState = getRenderState();
if (renderState.hasProperty(GTMachineModelProperties.IS_FORMED)) {
setRenderState(renderState.setValue(GTMachineModelProperties.IS_FORMED, false));
}
for (IMultiPart part : parts) {
part.removedFromController(this);
}
parallelHatch = null;
parts.clear();
updatePartPositions();
}
/**
* Called from part, when part is invalid due to chunk unload or broken.
*/
public void onPartUnload() {
parts.removeIf(part -> part.self().isRemoved());
getMultiblockState().setError(MultiblockState.UNLOAD_ERROR);
if (getLevel() instanceof ServerLevel serverLevel) {
MultiblockWorldSavedData.getOrCreate(serverLevel).addAsyncLogic(this);
}
updatePartPositions();
}
//////////////////////////////////////
// ***** Getters ******//
/// ///////////////////////////////////
@Override
public MultiblockMachineDefinition getDefinition() {
return (MultiblockMachineDefinition) super.getDefinition();
}
/**
* Get MultiblockState. It records all structure-related information.
*/
@NotNull
public MultiblockState getMultiblockState() {
if (multiblockState == null) {
multiblockState = new MultiblockState(getLevel(), getBlockPos());
}
return multiblockState;
}
public @Nullable BlockState getPartAppearance(IMultiPart part, Direction side, BlockState sourceState,
BlockPos sourcePos) {
if (isFormed()) {
return getDefinition().getPartAppearance().apply(this, part, side);
}
return null;
}
public Comparator<IMultiPart> getPartSorter() {
return getDefinition().getPartSorter().apply(this);
}
/**
* Get all parts
*/
public List<IMultiPart> getParts() {
// for the client side, when the chunk unloaded
if (parts.size() != this.partPositions.length) {
parts.clear();
for (var pos : this.partPositions) {
if (getMachine(getLevel(), pos) instanceof IMultiPart part) {
parts.add(part);
}
}
}
return this.parts;
}
/**
* The instance of {@link ParallelHatchPartMachine} attached to this Controller.
* <p>
* Note that this will return a singular instance, and will not account for multiple attached IParallelHatches
*
* @return an {@link Optional} of the attached IParallelHatch, empty if one is not attached
*/
public Optional<ParallelHatchPartMachine> getParallelHatch() {
return Optional.ofNullable(parallelHatch);
}
/**
*
* @return Whether batching is enabled on this multiblock
*/
public boolean isBatchEnabled() {
return false;
}
public void setFlipped(boolean flipped) {
isFlipped = flipped;
syncDataHolder.markClientSyncFieldDirty("isFlipped");
}
@SuppressWarnings("unused")
@ClientFieldChangeListener(fieldName = "partPositions")
protected void onPartsUpdated() {
parts.clear();
for (var pos : partPositions) {
if (getMachine(getLevel(), pos) instanceof IMultiPart part) {
parts.add(part);
}
}
}
protected void updatePartPositions() {
this.partPositions = this.parts.isEmpty() ? new BlockPos[0] :
this.parts.stream().map(part -> part.self().getBlockPos()).toArray(BlockPos[]::new);
syncDataHolder.markClientSyncFieldDirty("partPositions");
}
public void setBatchEnabled(boolean batch) {}
/**
* should add part to the part list.
*/
public boolean shouldAddPartToController(IMultiPart part) {
return true;
}
@Override
public void onRotated(Direction oldFacing, Direction newFacing) {
if (oldFacing != newFacing && getLevel() instanceof ServerLevel serverLevel) {
// invalid structure
this.onStructureInvalid();
var mwsd = MultiblockWorldSavedData.getOrCreate(serverLevel);
mwsd.removeMapping(getMultiblockState());
mwsd.addAsyncLogic(this);
}
}
public boolean allowFlip() {
return getDefinition().isAllowFlip();
}
@Override
public void setUpwardsFacing(@NotNull Direction upwardsFacing) {
if (!getDefinition().isAllowExtendedFacing()) {
return;
}
if (upwardsFacing.getAxis() == Direction.Axis.Y) {
GTCEu.LOGGER.error("Tried to set upwards facing to invalid facing {}! Skipping", upwardsFacing);
return;
}
var blockState = getBlockState();
if (blockState.getBlock() instanceof MetaMachineBlock &&
blockState.getValue(GTBlockStateProperties.UPWARDS_FACING) != upwardsFacing) {
getLevel().setBlockAndUpdate(getBlockPos(),
blockState.setValue(GTBlockStateProperties.UPWARDS_FACING, upwardsFacing));
if (getLevel() != null && !getLevel().isClientSide) {
notifyBlockUpdate();
checkPattern();
}
}
}
@Override
public void setFrontFacing(Direction facing) {
super.setFrontFacing(facing);
if (getLevel() != null && !getLevel().isClientSide) {
checkPattern();
}
}
/**
* Show the preview of structure.
*/
@Override
public InteractionResult onUse(BlockState state, Level world, BlockPos pos, Player player, InteractionHand hand,
BlockHitResult hit) {
if (!isFormed() && player.isShiftKeyDown() && player.getItemInHand(hand).isEmpty()) {
if (world.isClientSide()) {
MultiblockInWorldPreviewRenderer.showPreview(pos, this,
ConfigHolder.INSTANCE.client.inWorldPreviewDuration * 20);
}
return InteractionResult.SUCCESS;
}
return super.onUse(state, world, pos, player, hand, hit);
}
public boolean allowCircuitSlots() {
return true;
}
//////////////////////////////////////
// *** Pattern checking ***//
//////////////////////////////////////
/**
* Get structure pattern.
* You can override it to create dynamic patterns.
*/
public BlockPattern getPattern() {
return getDefinition().getPatternFactory().get();
}
/**
* Get lock for pattern checking.
*/
@Getter
private final Lock patternLock = new ReentrantLock();
/**
* Called in an async thread. It's unsafe, Don't modify anything of world but checking information.
* It will be called per 5 tick.
*
* @param periodID period Tick
*/
public void asyncCheckPattern(long periodID) {
if ((getMultiblockState().hasError() || !isFormed) && (getOffset() + periodID) % 4 == 0 &&
checkPatternWithTryLock()) { // per second
if (getLevel() instanceof ServerLevel serverLevel) {
serverLevel.getServer().execute(() -> {
patternLock.lock();
if (checkPatternWithLock()) { // formed
setFlipped(getMultiblockState().isNeededFlip());
onStructureFormed();
var mwsd = MultiblockWorldSavedData.getOrCreate(serverLevel);
mwsd.addMapping(getMultiblockState());
mwsd.removeAsyncLogic(this);
}
patternLock.unlock();
});
}
}
}
/**
* Check MultiBlock Pattern. Just checking pattern without any other logic.
* You can override it but it's unsafe for calling. because it will also be called in an async thread.
* <br>
* you should always use {@link MultiblockControllerMachine#checkPatternWithLock()} )} and
* {@link MultiblockControllerMachine#checkPatternWithTryLock()} instead.
*
* @return whether it can be formed.
*/
public boolean checkPattern() {
BlockPattern pattern = getPattern();
return pattern != null && pattern.checkPatternAt(getMultiblockState(), false);
}
/**
* Check pattern with a lock.
*/
public boolean checkPatternWithLock() {
var lock = getPatternLock();
lock.lock();
try {
return checkPattern();
} finally {
lock.unlock();
}
}
/**
* Check pattern with a try lock
*
* @return false - checking failed or cant get the lock.
*/
public boolean checkPatternWithTryLock() {
var lock = getPatternLock();
if (lock.tryLock()) {
try {
return checkPattern();
} finally {
lock.unlock();
}
} else {
return false;
}
}
}