Skip to content

Commit 95d09ec

Browse files
committed
Clean up multiblock shape matching internals
1 parent 69bd4c1 commit 95d09ec

File tree

10 files changed

+97
-181
lines changed

10 files changed

+97
-181
lines changed

src/client/java/aztech/modern_industrialization/machines/multiblocks/MultiblockMachineBER.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public void render(MultiblockMachineBlockEntity be, float tickDelta, PoseStack m
5454
boolean drawHighlights = isHoldingWrench() && !be.isShapeValid();
5555
HatchType hatchType = getHeldHatchType();
5656
if (drawHighlights || hatchType != null) {
57-
ShapeMatcher matcher = new ShapeMatcher(be.getLevel(), be.getBlockPos(), be.getOrientation().facingDirection, be.getActiveShape());
57+
ShapeMatcher matcher = be.createShapeMatcher();
5858

5959
for (BlockPos pos : matcher.getPositions()) {
6060
matrices.pushPose();

src/main/java/aztech/modern_industrialization/debug/DebugCommands.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
import aztech.modern_industrialization.MIConfig;
3434
import aztech.modern_industrialization.machines.MachineBlockEntity;
3535
import aztech.modern_industrialization.machines.multiblocks.MultiblockMachineBlockEntity;
36-
import aztech.modern_industrialization.machines.multiblocks.ShapeMatcher;
3736
import aztech.modern_industrialization.pipes.MIPipes;
3837
import aztech.modern_industrialization.pipes.api.PipeNetworkType;
3938
import aztech.modern_industrialization.pipes.impl.PipeNetworks;
@@ -174,8 +173,7 @@ private static int dumpStats(ServerPlayer player) {
174173
private static int buildMultiblock(CommandSourceStack src, BlockPos controllerPos) {
175174
var be = src.getLevel().getBlockEntity(controllerPos);
176175
if (be instanceof MultiblockMachineBlockEntity multiblock) {
177-
var shape = multiblock.getActiveShape();
178-
var shapeMatcher = new ShapeMatcher(src.getLevel(), controllerPos, multiblock.orientation.facingDirection, shape);
176+
var shapeMatcher = multiblock.createShapeMatcher();
179177
int updatedBlocks = shapeMatcher.buildMultiblock(src.getLevel());
180178

181179
src.sendSuccess(() -> Component.literal("Successfully built multiblock at position %s. %d blocks updated.".formatted(

src/main/java/aztech/modern_industrialization/machines/blockentities/multiblocks/AbstractCraftingMultiblockBlockEntity.java

Lines changed: 6 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,8 @@
3232
import aztech.modern_industrialization.machines.guicomponents.ReiSlotLocking;
3333
import aztech.modern_industrialization.machines.models.MachineModelClientData;
3434
import aztech.modern_industrialization.machines.multiblocks.MultiblockMachineBlockEntity;
35-
import aztech.modern_industrialization.machines.multiblocks.ShapeMatcher;
3635
import aztech.modern_industrialization.machines.multiblocks.ShapeTemplate;
3736
import aztech.modern_industrialization.util.Tickable;
38-
import org.jetbrains.annotations.Nullable;
3937

4038
public abstract class AbstractCraftingMultiblockBlockEntity extends MultiblockMachineBlockEntity implements Tickable,
4139
MultiblockInventoryComponentHolder, CrafterComponentHolder {
@@ -56,17 +54,13 @@ public AbstractCraftingMultiblockBlockEntity(BEP bep, String name, OrientationCo
5654
*/
5755
protected abstract CrafterComponent.Behavior getBehavior();
5856

59-
@Nullable
60-
private ShapeMatcher shapeMatcher = null;
6157
private OperatingState operatingState = OperatingState.NOT_MATCHED;
6258

6359
protected final ActiveShapeComponent activeShape;
6460
protected final MultiblockInventoryComponent inventory;
6561
protected final CrafterComponent crafter;
6662
private final IsActiveComponent isActive;
6763

68-
protected abstract void onSuccessfulMatch(ShapeMatcher shapeMatcher);
69-
7064
public ShapeTemplate getActiveShape() {
7165
return activeShape.getActiveShape();
7266
}
@@ -121,37 +115,15 @@ public void tickExtra() {
121115

122116
}
123117

124-
protected final void link() {
125-
if (shapeMatcher == null) {
126-
shapeMatcher = new ShapeMatcher(level, worldPosition, orientation.facingDirection, getActiveShape());
127-
shapeMatcher.registerListeners(level);
128-
}
129-
if (shapeMatcher.needsRematch()) {
130-
operatingState = OperatingState.NOT_MATCHED;
131-
shapeValid.shapeValid = false;
132-
shapeMatcher.rematch(level);
133-
134-
if (shapeMatcher.isMatchSuccessful()) {
135-
inventory.rebuild(shapeMatcher);
136-
137-
onSuccessfulMatch(shapeMatcher);
138-
shapeValid.shapeValid = true;
139-
operatingState = OperatingState.TRYING_TO_RESUME;
140-
}
141-
142-
if (shapeValid.update()) {
143-
sync(false);
144-
}
145-
}
118+
@Override
119+
protected void onRematch() {
120+
operatingState = OperatingState.NOT_MATCHED;
146121
}
147122

148123
@Override
149-
public final void unlink() {
150-
if (shapeMatcher != null) {
151-
shapeMatcher.unlinkHatches();
152-
shapeMatcher.unregisterListeners(level);
153-
shapeMatcher = null;
154-
}
124+
protected void onMatchSuccessful() {
125+
inventory.rebuild(shapeMatcher);
126+
operatingState = OperatingState.TRYING_TO_RESUME;
155127
}
156128

157129
private enum OperatingState {

src/main/java/aztech/modern_industrialization/machines/blockentities/multiblocks/AbstractElectricCraftingMultiblockBlockEntity.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
import aztech.modern_industrialization.machines.components.*;
2929
import aztech.modern_industrialization.machines.guicomponents.CraftingMultiblockGui;
3030
import aztech.modern_industrialization.machines.multiblocks.HatchBlockEntity;
31-
import aztech.modern_industrialization.machines.multiblocks.ShapeMatcher;
3231
import aztech.modern_industrialization.machines.multiblocks.ShapeTemplate;
3332
import aztech.modern_industrialization.util.Simulation;
3433
import java.util.ArrayList;
@@ -62,7 +61,9 @@ public List<EnergyComponent> getEnergyComponents() {
6261
}
6362

6463
@Override
65-
protected void onSuccessfulMatch(ShapeMatcher shapeMatcher) {
64+
protected void onMatchSuccessful() {
65+
super.onMatchSuccessful();
66+
6667
energyInputs.clear();
6768
for (HatchBlockEntity hatch : shapeMatcher.getMatchedHatches()) {
6869
hatch.appendEnergyInputs(energyInputs);

src/main/java/aztech/modern_industrialization/machines/blockentities/multiblocks/GeneratorMultiblockBlockEntity.java

Lines changed: 10 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,12 @@
3333
import aztech.modern_industrialization.machines.models.MachineModelClientData;
3434
import aztech.modern_industrialization.machines.multiblocks.HatchBlockEntity;
3535
import aztech.modern_industrialization.machines.multiblocks.MultiblockMachineBlockEntity;
36-
import aztech.modern_industrialization.machines.multiblocks.ShapeMatcher;
3736
import aztech.modern_industrialization.machines.multiblocks.ShapeTemplate;
3837
import aztech.modern_industrialization.util.Simulation;
3938
import aztech.modern_industrialization.util.Tickable;
4039
import java.util.ArrayList;
4140
import java.util.List;
4241
import net.minecraft.network.chat.Component;
43-
import org.jetbrains.annotations.Nullable;
4442

4543
public class GeneratorMultiblockBlockEntity extends MultiblockMachineBlockEntity implements Tickable,
4644
EnergyListComponentHolder, MultiblockInventoryComponentHolder {
@@ -64,8 +62,6 @@ public GeneratorMultiblockBlockEntity(BEP bep,
6462
registerGuiComponent(new SlotPanel.Server(this).withRedstoneControl(redstoneControl));
6563
}
6664

67-
@Nullable
68-
private ShapeMatcher shapeMatcher = null;
6965
private boolean allowNormalOperation = false;
7066

7167
private final ActiveShapeComponent activeShape;
@@ -89,13 +85,6 @@ public MultiblockInventoryComponent getMultiblockInventoryComponent() {
8985
return inventory;
9086
}
9187

92-
protected void onSuccessfulMatch(ShapeMatcher shapeMatcher) {
93-
energyOutputs.clear();
94-
for (HatchBlockEntity hatch : shapeMatcher.getMatchedHatches()) {
95-
hatch.appendEnergyOutputs(energyOutputs);
96-
}
97-
}
98-
9988
@Override
10089
public final MIInventory getInventory() {
10190
return MIInventory.EMPTY;
@@ -143,36 +132,19 @@ public long insertEnergy(long value, Simulation simulation) {
143132
return inserted;
144133
}
145134

146-
protected final void link() {
147-
if (shapeMatcher == null) {
148-
shapeMatcher = new ShapeMatcher(level, worldPosition, orientation.facingDirection, getActiveShape());
149-
shapeMatcher.registerListeners(level);
150-
}
151-
if (shapeMatcher.needsRematch()) {
152-
allowNormalOperation = false;
153-
shapeValid.shapeValid = false;
154-
shapeMatcher.rematch(level);
155-
156-
if (shapeMatcher.isMatchSuccessful()) {
157-
inventory.rebuild(shapeMatcher);
158-
159-
onSuccessfulMatch(shapeMatcher);
160-
shapeValid.shapeValid = true;
161-
allowNormalOperation = true;
162-
}
163-
164-
if (shapeValid.update()) {
165-
sync(false);
166-
}
167-
}
135+
@Override
136+
protected void onRematch() {
137+
allowNormalOperation = false;
168138
}
169139

170140
@Override
171-
public final void unlink() {
172-
if (shapeMatcher != null) {
173-
shapeMatcher.unlinkHatches();
174-
shapeMatcher.unregisterListeners(level);
175-
shapeMatcher = null;
141+
protected void onMatchSuccessful() {
142+
inventory.rebuild(shapeMatcher);
143+
allowNormalOperation = true;
144+
145+
energyOutputs.clear();
146+
for (HatchBlockEntity hatch : shapeMatcher.getMatchedHatches()) {
147+
hatch.appendEnergyOutputs(energyOutputs);
176148
}
177149
}
178150

src/main/java/aztech/modern_industrialization/machines/blockentities/multiblocks/LargeTankMultiblockBlockEntity.java

Lines changed: 2 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@
5454
import net.neoforged.neoforge.fluids.FluidType;
5555
import net.neoforged.neoforge.fluids.capability.IFluidHandler;
5656
import net.neoforged.neoforge.fluids.capability.templates.EmptyFluidHandler;
57-
import org.jetbrains.annotations.Nullable;
5857

5958
public class LargeTankMultiblockBlockEntity extends MultiblockMachineBlockEntity
6059
implements Tickable, FluidStorageComponentHolder {
@@ -148,9 +147,6 @@ private static ShapeTemplate buildShape(int index) {
148147
return templateBuilder.build();
149148
}
150149

151-
@Nullable
152-
private ShapeMatcher shapeMatcher = null;
153-
154150
private final ActiveShapeComponent activeShape;
155151
private final FluidStorageComponent fluidStorage;
156152

@@ -223,36 +219,6 @@ public FluidStorageComponent getFluidStorageComponent() {
223219
@Override
224220
protected MachineModelClientData getMachineModelData() {
225221
return new MachineModelClientData(null, orientation.facingDirection);
226-
227-
}
228-
229-
protected final void link() {
230-
if (shapeMatcher == null) {
231-
shapeMatcher = new ShapeMatcher(level, worldPosition, orientation.facingDirection, getActiveShape());
232-
shapeMatcher.registerListeners(level);
233-
}
234-
if (shapeMatcher.needsRematch()) {
235-
shapeValid.shapeValid = false;
236-
shapeMatcher.rematch(level);
237-
238-
if (shapeMatcher.isMatchSuccessful()) {
239-
shapeValid.shapeValid = true;
240-
onMatchSuccessful();
241-
}
242-
243-
if (shapeValid.update()) {
244-
sync(false);
245-
}
246-
}
247-
}
248-
249-
@Override
250-
public final void unlink() {
251-
if (shapeMatcher != null) {
252-
shapeMatcher.unlinkHatches();
253-
shapeMatcher.unregisterListeners(level);
254-
shapeMatcher = null;
255-
}
256222
}
257223

258224
@Override
@@ -275,7 +241,8 @@ public static long getCapacityFromComponents(int xIndex, int yIndex, int zIndex)
275241
return volume * BUCKET_PER_STRUCTURE_BLOCK * FluidType.BUCKET_VOLUME;
276242
}
277243

278-
private void onMatchSuccessful() {
244+
@Override
245+
protected void onMatchSuccessful() {
279246
int index = activeShape.getActiveShapeIndex();
280247
long capacity = getCapacityFromComponents(getXComponent(index), getYComponent(index), getZComponent(index));
281248
fluidStorage.setCapacity(capacity);

src/main/java/aztech/modern_industrialization/machines/blockentities/multiblocks/NuclearReactorMultiblockBlockEntity.java

Lines changed: 12 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ public class NuclearReactorMultiblockBlockEntity extends MultiblockMachineBlockE
5858
private final RedstoneControlComponent redstoneControl;
5959
private final IsActiveComponent isActive;
6060
private final NuclearEfficiencyHistoryComponent efficiencyHistory;
61-
private ShapeMatcher shapeMatcher;
6261

6362
private NuclearGrid nuclearGrid;
6463
private Supplier<NuclearReactorGui.Data> dataSupplier;
@@ -125,7 +124,18 @@ public void tick() {
125124
}
126125
}
127126

128-
protected void onSuccessfulMatch(ShapeMatcher shapeMatcher) {
127+
@Override
128+
public ShapeTemplate getActiveShape() {
129+
return activeShape.getActiveShape();
130+
}
131+
132+
@Override
133+
protected void onRematch() {
134+
nuclearGrid = null;
135+
}
136+
137+
@Override
138+
protected void onMatchSuccessful() {
129139
shapeValid.shapeValid = true;
130140
int size = gridLayout[activeShape.getActiveShapeIndex()].length;
131141
NuclearHatch[][] hatchesGrid = new NuclearHatch[size][size];
@@ -174,41 +184,6 @@ protected void onSuccessfulMatch(ShapeMatcher shapeMatcher) {
174184
};
175185
}
176186

177-
@Override
178-
public ShapeTemplate getActiveShape() {
179-
return activeShape.getActiveShape();
180-
}
181-
182-
protected final void link() {
183-
if (shapeMatcher == null) {
184-
shapeMatcher = new ShapeMatcher(level, worldPosition, orientation.facingDirection, getActiveShape());
185-
shapeMatcher.registerListeners(level);
186-
}
187-
if (shapeMatcher.needsRematch()) {
188-
shapeValid.shapeValid = false;
189-
nuclearGrid = null;
190-
shapeMatcher.rematch(level);
191-
192-
if (shapeMatcher.isMatchSuccessful()) {
193-
shapeValid.shapeValid = true;
194-
onSuccessfulMatch(shapeMatcher);
195-
}
196-
197-
if (shapeValid.update()) {
198-
sync(false);
199-
}
200-
}
201-
}
202-
203-
@Override
204-
public final void unlink() {
205-
if (shapeMatcher != null) {
206-
shapeMatcher.unlinkHatches();
207-
shapeMatcher.unregisterListeners(level);
208-
shapeMatcher = null;
209-
}
210-
}
211-
212187
public static void registerReiShapes() {
213188
for (int i = 0; i < shapeTemplates.length; ++i) {
214189
ReiMachineRecipes.registerMultiblockShape("nuclear_reactor", shapeTemplates[i], "" + i);

src/main/java/aztech/modern_industrialization/machines/blockentities/multiblocks/SteamBoilerMultiblockBlockEntity.java

Lines changed: 2 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
import aztech.modern_industrialization.machines.guicomponents.TemperatureBar;
3636
import aztech.modern_industrialization.machines.models.MachineModelClientData;
3737
import aztech.modern_industrialization.machines.multiblocks.MultiblockMachineBlockEntity;
38-
import aztech.modern_industrialization.machines.multiblocks.ShapeMatcher;
3938
import aztech.modern_industrialization.machines.multiblocks.ShapeTemplate;
4039
import aztech.modern_industrialization.util.Tickable;
4140
import java.util.List;
@@ -44,7 +43,6 @@
4443

4544
public class SteamBoilerMultiblockBlockEntity extends MultiblockMachineBlockEntity implements Tickable {
4645

47-
private ShapeMatcher shapeMatcher;
4846
private final ShapeTemplate shapeTemplate;
4947
private final IsActiveComponent isActiveComponent;
5048
private final RedstoneControlComponent redstoneControl;
@@ -81,33 +79,9 @@ public SteamBoilerMultiblockBlockEntity(BEP bep, ShapeTemplate shapeTemplate, St
8179

8280
}
8381

84-
protected final void link() {
85-
if (shapeMatcher == null) {
86-
shapeMatcher = new ShapeMatcher(level, worldPosition, orientation.facingDirection, shapeTemplate);
87-
shapeMatcher.registerListeners(level);
88-
}
89-
if (shapeMatcher.needsRematch()) {
90-
shapeValid.shapeValid = false;
91-
shapeMatcher.rematch(level);
92-
93-
if (shapeMatcher.isMatchSuccessful()) {
94-
inventory.rebuild(shapeMatcher);
95-
shapeValid.shapeValid = true;
96-
}
97-
98-
if (shapeValid.update()) {
99-
sync(false);
100-
}
101-
}
102-
}
103-
10482
@Override
105-
public final void unlink() {
106-
if (shapeMatcher != null) {
107-
shapeMatcher.unlinkHatches();
108-
shapeMatcher.unregisterListeners(level);
109-
shapeMatcher = null;
110-
}
83+
protected void onMatchSuccessful() {
84+
inventory.rebuild(shapeMatcher);
11185
}
11286

11387
@Override

0 commit comments

Comments
 (0)