Skip to content

Commit 99747f3

Browse files
committed
Add a couple helper methods to Path to make it a little clearer what is going on, and remove flowing stack targets when there are no more stacks being sent to a location
1 parent e1ebc6e commit 99747f3

File tree

4 files changed

+46
-27
lines changed

4 files changed

+46
-27
lines changed

src/main/java/mekanism/common/capabilities/item/CursedTransporterItemHandler.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import java.util.function.LongSupplier;
1010
import mekanism.common.content.network.transmitter.LogisticalTransporterBase;
1111
import mekanism.common.content.transporter.TransporterStack;
12-
import mekanism.common.content.transporter.TransporterStack.Path;
1312
import mekanism.common.lib.inventory.TransitRequest;
1413
import mekanism.common.lib.inventory.TransitRequest.TransitResponse;
1514
import net.minecraft.core.BlockPos;
@@ -97,7 +96,7 @@ public ItemStack insertItem(int slot, @NotNull ItemStack itemStack, boolean simu
9796
//Just setting the transporter stack's stack is equivalent to LogisticalTransporterBase#updateTransit when simulating
9897
// as we already know the response is not empty
9998
stack.itemStack = response.getStack();
100-
if (stack.getPathType() != Path.NONE) {
99+
if (stack.getPathType().hasTarget()) {
101100
//If the stack actually has a path add that simulated insert to a list of locally simulated flowing stacks so that
102101
// if the mod simulates against the next slot as well we can give a more accurate result
103102
simulatedFlowingStacks.computeIfAbsent(GlobalPos.of(transporter.getLevel().dimension(), stack.getDest()), k -> new ObjectOpenHashSet<>()).add(stack);

src/main/java/mekanism/common/content/network/transmitter/LogisticalTransporterBase.java

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ public void onUpdateServer() {
148148
for (Int2ObjectMap.Entry<TransporterStack> entry : transit.int2ObjectEntrySet()) {
149149
int stackId = entry.getIntKey();
150150
TransporterStack stack = entry.getValue();
151-
if (!stack.initiatedPath) {
151+
if (!stack.initiatedPath) {//Initiate any paths and remove things that can't go places
152152
if (stack.itemStack.isEmpty() || !recalculate(stackId, stack, null)) {
153153
deletes.add(stackId);
154154
continue;
@@ -168,18 +168,20 @@ public void onUpdateServer() {
168168
BlockPos next = stack.getPath().get(currentIndex - 1);
169169
if (next != null) {
170170
if (!stack.isFinal(this)) {
171+
//If this is not the final transporter try transferring it to the next one
171172
LogisticalTransporterBase transmitter = network.getTransmitter(next);
172173
if (stack.canInsertToTransporter(transmitter, stack.getSide(this), this)) {
173174
transmitter.entityEntering(stack, stack.progress % 100);
174175
deletes.add(stackId);
175176
continue;
176177
}
177178
prevSet = next;
178-
} else if (stack.getPathType() != Path.NONE) {
179+
} else if (stack.getPathType().hasTarget()) {
180+
//Otherwise, try to insert it into the destination inventory
179181
//Get the handler we are trying to insert into from the network's acceptor cache
180182
IItemHandler acceptor = network.getCachedAcceptor(next, stack.getSide(this).getOpposite());
181183
TransitResponse response = TransitRequest.simple(stack.itemStack).addToInventory(getLevel(), next, acceptor, 0,
182-
stack.getPathType() == Path.HOME);
184+
stack.getPathType().isHome());
183185
if (!response.isEmpty()) {
184186
//We were able to add at least part of the stack to the inventory
185187
ItemStack rejected = response.getRejected();
@@ -191,7 +193,7 @@ public void onUpdateServer() {
191193
continue;
192194
}
193195
//Some portion of the stack got rejected; save the remainder and
194-
// let the recalculate below sort out what to do next
196+
// recalculate below to sort out what to do next
195197
stack.itemStack = rejected;
196198
}//else the entire stack got rejected (Note: we don't need to update the stack to point to itself)
197199
prevSet = next;
@@ -209,17 +211,17 @@ public void onUpdateServer() {
209211
boolean tryRecalculate;
210212
if (stack.isFinal(this)) {
211213
Path pathType = stack.getPathType();
212-
if (pathType == Path.DEST || pathType == Path.HOME) {
214+
if (pathType.hasTarget()) {
213215
Direction side = stack.getSide(this);
214216
ConnectionType connectionType = getConnectionType(side);
215217
tryRecalculate = !connectionType.canSendTo() ||
216-
!TransporterUtils.canInsert(getLevel(), stack.getDest(), stack.color, stack.itemStack, side, pathType == Path.HOME);
218+
!TransporterUtils.canInsert(getLevel(), stack.getDest(), stack.color, stack.itemStack, side, pathType.isHome());
217219
} else {
218-
tryRecalculate = pathType == Path.NONE;
220+
tryRecalculate = pathType.isHome();
219221
}
220222
} else {
221223
LogisticalTransporterBase nextTransmitter = network.getTransmitter(stack.getNext(this));
222-
if (nextTransmitter == null && stack.getPathType() == Path.NONE && stack.getPath().size() == 2) {
224+
if (nextTransmitter == null && stack.getPathType().noTarget() && stack.getPath().size() == 2) {
223225
//If there is no next transmitter, and it was an idle path, assume that we are idling
224226
// in a single length transmitter, in which case we only recalculate it at 50 if it won't
225227
// be able to go into that connection type
@@ -363,7 +365,7 @@ public void addStack(int id, TransporterStack s) {
363365

364366
private boolean recalculate(int stackId, TransporterStack stack, BlockPos from) {
365367
//TODO: Why do we skip recalculating the path if it is idle. Is it possible for idle paths to eventually stop being idle or are they just idle forever??
366-
boolean noPath = stack.getPathType() == Path.NONE || stack.recalculatePath(TransitRequest.simple(stack.itemStack), this, 0).isEmpty();
368+
boolean noPath = stack.getPathType().noTarget() || stack.recalculatePath(TransitRequest.simple(stack.itemStack), this, 0).isEmpty();
367369
if (noPath && !stack.calculateIdle(this)) {
368370
TransporterUtils.drop(this, stack);
369371
return false;

src/main/java/mekanism/common/content/transporter/TransporterManager.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
package mekanism.common.content.transporter;
22

33
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
4-
import it.unimi.dsi.fastutil.objects.ObjectOpenHashSet;
54
import java.util.Arrays;
5+
import java.util.HashSet;
66
import java.util.Map;
77
import java.util.Set;
8-
import mekanism.common.content.transporter.TransporterStack.Path;
98
import mekanism.common.lib.inventory.TransitRequest;
109
import mekanism.common.lib.inventory.TransitRequest.ItemData;
1110
import mekanism.common.lib.inventory.TransitRequest.TransitResponse;
@@ -29,12 +28,16 @@ public static void reset() {
2928
}
3029

3130
public static void add(Level world, TransporterStack stack) {
32-
flowingStacks.computeIfAbsent(GlobalPos.of(world.dimension(), stack.getDest()), k -> new ObjectOpenHashSet<>()).add(stack);
31+
flowingStacks.computeIfAbsent(GlobalPos.of(world.dimension(), stack.getDest()), k -> new HashSet<>()).add(stack);
3332
}
3433

3534
public static void remove(Level world, TransporterStack stack) {
36-
if (stack.hasPath() && stack.getPathType() != Path.NONE) {
37-
flowingStacks.get(GlobalPos.of(world.dimension(), stack.getDest())).remove(stack);
35+
if (stack.hasPath() && stack.getPathType().hasTarget()) {
36+
GlobalPos pos = GlobalPos.of(world.dimension(), stack.getDest());
37+
Set<TransporterStack> transporterStacks = flowingStacks.get(pos);
38+
if (transporterStacks.remove(stack) && transporterStacks.isEmpty()) {
39+
flowingStacks.remove(pos);
40+
}
3841
}
3942
}
4043

@@ -205,7 +208,7 @@ private static boolean predictFlowing(GlobalPos position, Direction side, IItemH
205208
Set<TransporterStack> transporterStacks = flowingStacks.get(position);
206209
if (transporterStacks != null) {
207210
for (TransporterStack stack : transporterStacks) {
208-
if (stack != null && stack.getPathType() != Path.NONE) {
211+
if (stack != null && stack.getPathType().hasTarget()) {
209212
//We start by simulating inserting the stack into the handler, regardless of if we
210213
// are interacting with the same side of the target as the stack's path is taking.
211214
// This is so that in cases where the item handler is shared (chests) or some of

src/main/java/mekanism/common/content/transporter/TransporterStack.java

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ public class TransporterStack {
4545
public BlockPos homeLocation;
4646
private BlockPos clientNext;
4747
private BlockPos clientPrev;
48+
@Nullable
4849
private Path pathType;
4950
private List<BlockPos> pathToTarget = new ArrayList<>();
5051

@@ -73,7 +74,7 @@ public void write(FriendlyByteBuf buf, BlockPos pos) {
7374
buf.writeVarInt(TransporterUtils.getColorIndex(color));
7475
buf.writeVarInt(progress);
7576
buf.writeBlockPos(originalLocation);
76-
buf.writeEnum(pathType);
77+
buf.writeEnum(getPathType());
7778
buf.writeNullable(getNext(pos), FriendlyByteBuf::writeBlockPos);
7879
buf.writeBlockPos(getPrev(pos));
7980
buf.writeItem(itemStack);
@@ -98,7 +99,7 @@ public void writeToUpdateTag(LogisticalTransporterBase transporter, CompoundTag
9899
}
99100
updateTag.putInt(NBTConstants.PROGRESS, progress);
100101
updateTag.put(NBTConstants.ORIGINAL_LOCATION, NbtUtils.writeBlockPos(originalLocation));
101-
NBTUtils.writeEnum(updateTag, NBTConstants.PATH_TYPE, pathType);
102+
NBTUtils.writeEnum(updateTag, NBTConstants.PATH_TYPE, getPathType());
102103
BlockPos next = getNext(transporter);
103104
if (next != null) {
104105
updateTag.put(NBTConstants.CLIENT_NEXT, NbtUtils.writeBlockPos(next));
@@ -131,7 +132,9 @@ public void write(CompoundTag nbtTags) {
131132
if (homeLocation != null) {
132133
nbtTags.put(NBTConstants.HOME_LOCATION, NbtUtils.writeBlockPos(homeLocation));
133134
}
134-
NBTUtils.writeEnum(nbtTags, NBTConstants.PATH_TYPE, pathType);
135+
if (pathType != null) {
136+
NBTUtils.writeEnum(nbtTags, NBTConstants.PATH_TYPE, pathType);
137+
}
135138
itemStack.save(nbtTags);
136139
}
137140

@@ -145,30 +148,30 @@ public void read(CompoundTag nbtTags) {
145148
itemStack = ItemStack.of(nbtTags);
146149
}
147150

148-
private void setPath(Level world, List<BlockPos> path, Path type, boolean updateFlowing) {
151+
private void setPath(Level world, @NotNull List<BlockPos> path, @NotNull Path type, boolean updateFlowing) {
149152
//Make sure old path isn't null
150-
if (updateFlowing && pathType != Path.NONE) {
153+
if (updateFlowing && (pathType == null || pathType.hasTarget())) {
151154
//Only update the actual flowing stacks if we want to modify more than our current stack
152155
TransporterManager.remove(world, this);
153156
}
154157
pathToTarget = path;
155158
pathType = type;
156-
if (updateFlowing && pathType != Path.NONE) {
159+
if (updateFlowing && pathType.hasTarget()) {
157160
//Only update the actual flowing stacks if we want to modify more than our current stack
158161
TransporterManager.add(world, this);
159162
}
160163
}
161164

162165
public boolean hasPath() {
163-
return pathToTarget != null && pathToTarget.size() >= 2;
166+
return pathToTarget.size() >= 2;
164167
}
165168

166169
public List<BlockPos> getPath() {
167170
return pathToTarget;
168171
}
169172

170173
public Path getPathType() {
171-
return pathType;
174+
return pathType == null ? Path.NONE : pathType;
172175
}
173176

174177
public TransitResponse recalculatePath(TransitRequest request, LogisticalTransporterBase transporter, int min) {
@@ -220,7 +223,7 @@ public boolean calculateIdle(LogisticalTransporterBase transporter) {
220223
if (newPath == null) {
221224
return false;
222225
}
223-
if (newPath.type() == Path.HOME) {
226+
if (newPath.type().isHome()) {
224227
idleDir = null;
225228
}
226229
setPath(transporter.getLevel(), newPath.path(), newPath.type(), true);
@@ -230,7 +233,7 @@ public boolean calculateIdle(LogisticalTransporterBase transporter) {
230233
}
231234

232235
public boolean isFinal(LogisticalTransporterBase transporter) {
233-
return pathToTarget.indexOf(transporter.getBlockPos()) == (pathType == Path.NONE ? 0 : 1);
236+
return pathToTarget.indexOf(transporter.getBlockPos()) == (getPathType().hasTarget() ? 1 : 0);
234237
}
235238

236239
public BlockPos getNext(LogisticalTransporterBase transporter) {
@@ -317,5 +320,17 @@ public enum Path {
317320
public static Path byIndexStatic(int index) {
318321
return MathUtils.getByIndexMod(PATHS, index);
319322
}
323+
324+
public boolean hasTarget() {
325+
return this != NONE;
326+
}
327+
328+
public boolean noTarget() {
329+
return this == NONE;
330+
}
331+
332+
public boolean isHome() {
333+
return this == HOME;
334+
}
320335
}
321336
}

0 commit comments

Comments
 (0)