Skip to content

Commit e446b3b

Browse files
authored
Make machine shape matcher api more extensible (#937)
1 parent c093035 commit e446b3b

File tree

11 files changed

+144
-233
lines changed

11 files changed

+144
-233
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 & 2 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;
@@ -175,7 +174,7 @@ private static int buildMultiblock(CommandSourceStack src, BlockPos controllerPo
175174
var be = src.getLevel().getBlockEntity(controllerPos);
176175
if (be instanceof MultiblockMachineBlockEntity multiblock) {
177176
var shape = multiblock.getActiveShape();
178-
var shapeMatcher = new ShapeMatcher(src.getLevel(), controllerPos, multiblock.orientation.facingDirection, shape);
177+
var shapeMatcher = multiblock.createShapeMatcher();
179178
int updatedBlocks = shapeMatcher.buildMultiblock(src.getLevel());
180179

181180
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: 5 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
import aztech.modern_industrialization.machines.multiblocks.ShapeMatcher;
3636
import aztech.modern_industrialization.machines.multiblocks.ShapeTemplate;
3737
import aztech.modern_industrialization.util.Tickable;
38-
import org.jetbrains.annotations.Nullable;
3938

4039
public abstract class AbstractCraftingMultiblockBlockEntity extends MultiblockMachineBlockEntity implements Tickable,
4140
MultiblockInventoryComponentHolder, CrafterComponentHolder {
@@ -56,17 +55,13 @@ public AbstractCraftingMultiblockBlockEntity(BEP bep, String name, OrientationCo
5655
*/
5756
protected abstract CrafterComponent.Behavior getBehavior();
5857

59-
@Nullable
60-
private ShapeMatcher shapeMatcher = null;
6158
private OperatingState operatingState = OperatingState.NOT_MATCHED;
6259

6360
protected final ActiveShapeComponent activeShape;
6461
protected final MultiblockInventoryComponent inventory;
6562
protected final CrafterComponent crafter;
6663
private final IsActiveComponent isActive;
6764

68-
protected abstract void onSuccessfulMatch(ShapeMatcher shapeMatcher);
69-
7065
public ShapeTemplate getActiveShape() {
7166
return activeShape.getActiveShape();
7267
}
@@ -121,36 +116,12 @@ public void tickExtra() {
121116

122117
}
123118

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-
}
146-
}
147-
148119
@Override
149-
public final void unlink() {
150-
if (shapeMatcher != null) {
151-
shapeMatcher.unlinkHatches();
152-
shapeMatcher.unregisterListeners(level);
153-
shapeMatcher = null;
120+
protected void onRematch(ShapeMatcher shapeMatcher) {
121+
operatingState = OperatingState.NOT_MATCHED;
122+
if (shapeMatcher.isMatchSuccessful()) {
123+
inventory.rebuild(shapeMatcher);
124+
operatingState = OperatingState.TRYING_TO_RESUME;
154125
}
155126
}
156127

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,13 @@ public List<EnergyComponent> getEnergyComponents() {
6262
}
6363

6464
@Override
65-
protected void onSuccessfulMatch(ShapeMatcher shapeMatcher) {
66-
energyInputs.clear();
67-
for (HatchBlockEntity hatch : shapeMatcher.getMatchedHatches()) {
68-
hatch.appendEnergyInputs(energyInputs);
65+
protected void onRematch(ShapeMatcher shapeMatcher) {
66+
super.onRematch(shapeMatcher);
67+
if (shapeMatcher.isMatchSuccessful()) {
68+
energyInputs.clear();
69+
for (HatchBlockEntity hatch : shapeMatcher.getMatchedHatches()) {
70+
hatch.appendEnergyInputs(energyInputs);
71+
}
6972
}
7073
}
7174

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

Lines changed: 10 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@
4040
import java.util.ArrayList;
4141
import java.util.List;
4242
import net.minecraft.network.chat.Component;
43-
import org.jetbrains.annotations.Nullable;
4443

4544
public class GeneratorMultiblockBlockEntity extends MultiblockMachineBlockEntity implements Tickable,
4645
EnergyListComponentHolder, MultiblockInventoryComponentHolder {
@@ -64,8 +63,6 @@ public GeneratorMultiblockBlockEntity(BEP bep,
6463
registerGuiComponent(new SlotPanel.Server(this).withRedstoneControl(redstoneControl));
6564
}
6665

67-
@Nullable
68-
private ShapeMatcher shapeMatcher = null;
6966
private boolean allowNormalOperation = false;
7067

7168
private final ActiveShapeComponent activeShape;
@@ -89,13 +86,6 @@ public MultiblockInventoryComponent getMultiblockInventoryComponent() {
8986
return inventory;
9087
}
9188

92-
protected void onSuccessfulMatch(ShapeMatcher shapeMatcher) {
93-
energyOutputs.clear();
94-
for (HatchBlockEntity hatch : shapeMatcher.getMatchedHatches()) {
95-
hatch.appendEnergyOutputs(energyOutputs);
96-
}
97-
}
98-
9989
@Override
10090
public final MIInventory getInventory() {
10191
return MIInventory.EMPTY;
@@ -141,36 +131,17 @@ public long insertEnergy(long value, Simulation simulation) {
141131
return inserted;
142132
}
143133

144-
protected final void link() {
145-
if (shapeMatcher == null) {
146-
shapeMatcher = new ShapeMatcher(level, worldPosition, orientation.facingDirection, getActiveShape());
147-
shapeMatcher.registerListeners(level);
148-
}
149-
if (shapeMatcher.needsRematch()) {
150-
allowNormalOperation = false;
151-
shapeValid.shapeValid = false;
152-
shapeMatcher.rematch(level);
153-
154-
if (shapeMatcher.isMatchSuccessful()) {
155-
inventory.rebuild(shapeMatcher);
156-
157-
onSuccessfulMatch(shapeMatcher);
158-
shapeValid.shapeValid = true;
159-
allowNormalOperation = true;
160-
}
161-
162-
if (shapeValid.update()) {
163-
sync(false);
164-
}
165-
}
166-
}
167-
168134
@Override
169-
public final void unlink() {
170-
if (shapeMatcher != null) {
171-
shapeMatcher.unlinkHatches();
172-
shapeMatcher.unregisterListeners(level);
173-
shapeMatcher = null;
135+
protected void onRematch(ShapeMatcher shapeMatcher) {
136+
allowNormalOperation = false;
137+
if (shapeMatcher.isMatchSuccessful()) {
138+
inventory.rebuild(shapeMatcher);
139+
allowNormalOperation = true;
140+
141+
energyOutputs.clear();
142+
for (HatchBlockEntity hatch : shapeMatcher.getMatchedHatches()) {
143+
hatch.appendEnergyOutputs(energyOutputs);
144+
}
174145
}
175146
}
176147

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

Lines changed: 11 additions & 41 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

@@ -226,35 +222,6 @@ protected MachineModelClientData getMachineModelData() {
226222

227223
}
228224

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-
}
256-
}
257-
258225
@Override
259226
public void tick() {
260227
if (!level.isClientSide) {
@@ -275,14 +242,17 @@ public static long getCapacityFromComponents(int xIndex, int yIndex, int zIndex)
275242
return volume * BUCKET_PER_STRUCTURE_BLOCK * FluidType.BUCKET_VOLUME;
276243
}
277244

278-
private void onMatchSuccessful() {
279-
int index = activeShape.getActiveShapeIndex();
280-
long capacity = getCapacityFromComponents(getXComponent(index), getYComponent(index), getZComponent(index));
281-
fluidStorage.setCapacity(capacity);
282-
283-
for (var hatch : shapeMatcher.getMatchedHatches()) {
284-
if (hatch instanceof LargeTankHatch tankHatch) {
285-
tankHatch.setController(this);
245+
@Override
246+
protected void onRematch(ShapeMatcher shapeMatcher) {
247+
if (shapeMatcher.isMatchSuccessful()) {
248+
int index = activeShape.getActiveShapeIndex();
249+
long capacity = getCapacityFromComponents(getXComponent(index), getYComponent(index), getZComponent(index));
250+
fluidStorage.setCapacity(capacity);
251+
252+
for (var hatch : shapeMatcher.getMatchedHatches()) {
253+
if (hatch instanceof LargeTankHatch tankHatch) {
254+
tankHatch.setController(this);
255+
}
286256
}
287257
}
288258
}

0 commit comments

Comments
 (0)