Avoid NPE during navigation write (#4975)

- Guard for unexpected null entries in train navigation during serialisation
This commit is contained in:
IThundxr 2023-07-03 10:10:44 -04:00 committed by GitHub
parent b7a082c408
commit f91afb2975
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -735,6 +735,20 @@ public class Navigation {
CompoundTag tag = new CompoundTag();
if (destination == null)
return tag;
// Remove null values in train navigation fixing a rare crash that could occur
List<Couple<TrackNode>> toRemove = new ArrayList<>();
for (Couple<TrackNode> couple : currentPath) {
if (couple == null || couple.getFirst() == null || couple.getSecond() == null)
toRemove.add(couple);
}
if (toRemove.size() > 0) {
Create.LOGGER.error("Found null values in path of train with name: "+train.name.getString()+", id: "+train.id.toString());
}
for (Couple<TrackNode> brokenCouple : toRemove) {
currentPath.remove(brokenCouple);
}
tag.putUUID("Destination", destination.id);
tag.putDouble("DistanceToDestination", distanceToDestination);
tag.putDouble("DistanceStartedAt", distanceStartedAt);