Skip to content

Commit e8e40f0

Browse files
authored
Fix #144: Crash with Immersive Engineering fluid pipes (#147)
1 parent 56a49e2 commit e8e40f0

File tree

2 files changed

+26
-9
lines changed

2 files changed

+26
-9
lines changed

src/main/java/dev/technici4n/moderndynamics/network/NodeHost.java

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -147,16 +147,33 @@ public final void refreshSelf() {
147147
@Nullable
148148
public abstract Object getApiInstance(BlockCapability<?, Direction> lookup, @Nullable Direction side);
149149

150+
/**
151+
* Returns null if the node is not available.
152+
* Throws an exception if the host is not on the logical server.
153+
*/
150154
@SuppressWarnings("unchecked")
155+
// TODO: consider making this not nullable
151156
@Nullable
152-
protected final <H extends NodeHost, C extends NetworkCache<H, C>> NetworkNode<H, C> findNode() {
153-
// TODO: not the best unchecked cast...
157+
protected final <H extends NodeHost, C extends NetworkCache<H, C>> NetworkNode<H, C> findNodeOnServer() {
154158
return getManager().findNode((ServerLevel) pipe.getLevel(), pipe.getBlockPos());
155159
}
156160

161+
/**
162+
* Returns null on the client side or if the node is not available.
163+
*/
164+
@SuppressWarnings("unchecked")
165+
@Nullable
166+
protected final <H extends NodeHost, C extends NetworkCache<H, C>> NetworkNode<H, C> findNode() {
167+
if (pipe.getLevel() instanceof ServerLevel serverLevel) {
168+
return getManager().findNode(serverLevel, pipe.getBlockPos());
169+
} else {
170+
return null;
171+
}
172+
}
173+
157174
public final void separateNetwork() {
158175
@Nullable
159-
NetworkNode<?, ?> node = findNode();
176+
NetworkNode<?, ?> node = findNodeOnServer();
160177

161178
if (node != null && node.getHost() == this) {
162179
node.getNetworkCache().separate();
@@ -181,7 +198,7 @@ public final void scheduleUpdate() {
181198
if (!needsUpdate) {
182199
needsUpdate = true;
183200
@Nullable
184-
NetworkNode node = findNode();
201+
NetworkNode node = findNodeOnServer();
185202

186203
if (node != null) {
187204
node.getNetworkCache().scheduleHostUpdate(this);

src/main/java/dev/technici4n/moderndynamics/network/item/ItemHost.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ private IItemHandler buildExternalNetworkInjectStorage(Direction side) {
113113
double speedupFactor = getAttachment(side) instanceof ItemAttachedIo io ? io.getItemSpeedupFactor() : 1;
114114
return cache.insertList(node, paths, resource, maxAmount, simulate, speedupFactor, null);
115115
} else {
116-
// The node can be null if the pipe was just placed, and not initialized yet.
116+
// The node can be null on the client or if the pipe was just placed and not initialized yet.
117117
return 0;
118118
}
119119
});
@@ -125,7 +125,7 @@ private IItemHandler buildExternalNetworkInjectStorage(Direction side) {
125125
private InsertionOnlyItemHandler buildExtractorNetworkInjectStorage(Direction side, ItemAttachedIo extractor,
126126
@Nullable MaxParticipant maxIndexParticipant) {
127127
double speedupFactor = extractor.getItemSpeedupFactor();
128-
NetworkNode<ItemHost, ItemCache> node = findNode();
128+
NetworkNode<ItemHost, ItemCache> node = findNodeOnServer();
129129
var cache = node.getNetworkCache();
130130
var paths = rearrangePaths(cache.pathCache.getPaths(node, side.getOpposite()), extractor);
131131
return new InsertionOnlyItemHandler((resource, maxAmount, simulate) -> {
@@ -233,7 +233,7 @@ public void tickAttractor(Direction side, ItemAttachedIo attractor) {
233233
if (!insertTarget.hasStorage())
234234
return;
235235

236-
NetworkNode<ItemHost, ItemCache> thisNode = findNode();
236+
NetworkNode<ItemHost, ItemCache> thisNode = findNodeOnServer();
237237
var cache = thisNode.getNetworkCache();
238238
var pathCache = cache.pathCache;
239239
var paths = rearrangePaths(pathCache.getPaths(thisNode, side.getOpposite()), attractor);
@@ -356,7 +356,7 @@ public void tickMovingItems() {
356356

357357
@Nullable
358358
ItemHost adjacentItemHost = null;
359-
NetworkNode<ItemHost, ItemCache> ownNode = findNode();
359+
NetworkNode<ItemHost, ItemCache> ownNode = findNodeOnServer();
360360
for (var connection : ownNode.getConnections()) {
361361
if (connection.direction() == adjPipeDirection) {
362362
adjacentItemHost = connection.target().getHost();
@@ -486,7 +486,7 @@ public void gatherCapabilities() {
486486

487487
if (oldConnections != inventoryConnections) {
488488
pipe.sync();
489-
NetworkNode<ItemHost, ItemCache> node = findNode();
489+
NetworkNode<ItemHost, ItemCache> node = findNodeOnServer();
490490
node.getNetworkCache().pathCache.invalidate();
491491
}
492492
}

0 commit comments

Comments
 (0)