Skip to content

Commit 96a05e7

Browse files
authored
Null protection & Readability changes (Creators-of-Create#6325)
* Error Null cannot be Null - Add more TrackEdge null protection - Replace 2 for loops with enhanced for loops to improve readability - Remove some unneeded unboxing - Cleanup some interfaces - Remove private from enum constructor, it's already private - set enum value as final - remove static from inner interfaces & enums * Revert new Random() * unused import
1 parent 2c98728 commit 96a05e7

File tree

2 files changed

+37
-41
lines changed

2 files changed

+37
-41
lines changed

src/main/java/com/simibubi/create/content/trains/entity/Train.java

+17-15
Original file line numberDiff line numberDiff line change
@@ -619,7 +619,7 @@ private void collideWithOtherTrains(Level level, Carriage carriage) {
619619
public Pair<Train, Vec3> findCollidingTrain(Level level, Vec3 start, Vec3 end, ResourceKey<Level> dimension) {
620620
Vec3 diff = end.subtract(start);
621621
double maxDistanceSqr = Math.pow(AllConfigs.server().trains.maxAssemblyLength.get(), 2.0);
622-
622+
623623
Trains: for (Train train : Create.RAILWAYS.sided(level).trains.values()) {
624624
if (train == this)
625625
continue;
@@ -962,6 +962,9 @@ public void collectInitiallyOccupiedSignalBlocks() {
962962
TrackNode node1 = trailingPoint.node1;
963963
TrackNode node2 = trailingPoint.node2;
964964
TrackEdge edge = trailingPoint.edge;
965+
966+
if (edge == null) return;
967+
965968
double position = trailingPoint.position;
966969
EdgeData signalData = edge.getEdgeData();
967970

@@ -1219,20 +1222,19 @@ public int countPlayerPassengers() {
12191222
public void determineHonk(Level level) {
12201223
if (lowHonk != null)
12211224
return;
1222-
for (int index = 0; index < carriages.size(); index++) {
1223-
Carriage carriage = carriages.get(index);
1224-
DimensionalCarriageEntity dimensional = carriage.getDimensionalIfPresent(level.dimension());
1225-
if (dimensional == null)
1226-
return;
1227-
CarriageContraptionEntity entity = dimensional.entity.get();
1228-
if (entity == null || !(entity.getContraption()instanceof CarriageContraption otherCC))
1229-
break;
1230-
Pair<Boolean, Integer> first = otherCC.soundQueue.getFirstWhistle(entity);
1231-
if (first != null) {
1232-
lowHonk = first.getFirst();
1233-
honkPitch = first.getSecond();
1234-
}
1235-
}
1225+
for (Carriage carriage : carriages) {
1226+
DimensionalCarriageEntity dimensional = carriage.getDimensionalIfPresent(level.dimension());
1227+
if (dimensional == null)
1228+
return;
1229+
CarriageContraptionEntity entity = dimensional.entity.get();
1230+
if (entity == null || !(entity.getContraption() instanceof CarriageContraption otherCC))
1231+
break;
1232+
Pair<Boolean, Integer> first = otherCC.soundQueue.getFirstWhistle(entity);
1233+
if (first != null) {
1234+
lowHonk = first.getFirst();
1235+
honkPitch = first.getSecond();
1236+
}
1237+
}
12361238
}
12371239

12381240
public float distanceToLocationSqr(Level level, Vec3 location) {

src/main/java/com/simibubi/create/content/trains/entity/TravellingPoint.java

+20-26
Original file line numberDiff line numberDiff line change
@@ -40,28 +40,24 @@ public class TravellingPoint {
4040
public boolean blocked;
4141
public boolean upsideDown;
4242

43-
public static enum SteerDirection {
43+
public enum SteerDirection {
4444
NONE(0), LEFT(-1), RIGHT(1);
4545

46-
float targetDot;
46+
final float targetDot;
4747

48-
private SteerDirection(float targetDot) {
48+
SteerDirection(float targetDot) {
4949
this.targetDot = targetDot;
5050
}
5151
}
5252

53-
public static interface ITrackSelector
54-
extends BiFunction<TrackGraph, Pair<Boolean, List<Entry<TrackNode, TrackEdge>>>, Entry<TrackNode, TrackEdge>> {
55-
};
53+
public interface ITrackSelector
54+
extends BiFunction<TrackGraph, Pair<Boolean, List<Entry<TrackNode, TrackEdge>>>, Entry<TrackNode, TrackEdge>> { };
5655

57-
public static interface IEdgePointListener extends BiPredicate<Double, Pair<TrackEdgePoint, Couple<TrackNode>>> {
58-
};
56+
public interface IEdgePointListener extends BiPredicate<Double, Pair<TrackEdgePoint, Couple<TrackNode>>> { };
5957

60-
public static interface ITurnListener extends BiConsumer<Double, TrackEdge> {
61-
};
58+
public interface ITurnListener extends BiConsumer<Double, TrackEdge> { };
6259

63-
public static interface IPortalListener extends Predicate<Couple<TrackNodeLocation>> {
64-
};
60+
public interface IPortalListener extends Predicate<Couple<TrackNodeLocation>> { };
6561

6662
public TravellingPoint() {}
6763

@@ -78,8 +74,7 @@ public IEdgePointListener ignoreEdgePoints() {
7874
}
7975

8076
public ITurnListener ignoreTurns() {
81-
return (d, c) -> {
82-
};
77+
return (d, c) -> { };
8378
}
8479

8580
public IPortalListener ignorePortals() {
@@ -113,15 +108,14 @@ public ITrackSelector follow(TravellingPoint other, @Nullable Consumer<Boolean>
113108
Vector<List<Entry<TrackNode, TrackEdge>>> frontiers = new Vector<>(validTargets.size());
114109
Vector<Set<TrackEdge>> visiteds = new Vector<>(validTargets.size());
115110

116-
for (int j = 0; j < validTargets.size(); j++) {
117-
ArrayList<Entry<TrackNode, TrackEdge>> e = new ArrayList<>();
118-
Entry<TrackNode, TrackEdge> entry = validTargets.get(j);
119-
e.add(entry);
120-
frontiers.add(e);
121-
HashSet<TrackEdge> e2 = new HashSet<>();
122-
e2.add(entry.getValue());
123-
visiteds.add(e2);
124-
}
111+
for (Entry<TrackNode, TrackEdge> validTarget : validTargets) {
112+
ArrayList<Entry<TrackNode, TrackEdge>> e = new ArrayList<>();
113+
e.add(validTarget);
114+
frontiers.add(e);
115+
HashSet<TrackEdge> e2 = new HashSet<>();
116+
e2.add(validTarget.getValue());
117+
visiteds.add(e2);
118+
}
125119

126120
for (int i = 0; i < 20; i++) {
127121
for (int j = 0; j < validTargets.size(); j++) {
@@ -233,7 +227,7 @@ public double travel(TrackGraph graph, double distance, ITrackSelector trackSele
233227
Double blockedLocation =
234228
edgeTraversedFrom(graph, forward, signalListener, turnListener, prevPos, collectedDistance);
235229
if (blockedLocation != null) {
236-
position = blockedLocation.doubleValue();
230+
position = blockedLocation;
237231
traveled = position - prevPos;
238232
return traveled;
239233
}
@@ -289,7 +283,7 @@ public double travel(TrackGraph graph, double distance, ITrackSelector trackSele
289283

290284
if (blockedLocation != null) {
291285
traveled -= position;
292-
position = blockedLocation.doubleValue();
286+
position = blockedLocation;
293287
traveled += position;
294288
break;
295289
}
@@ -349,7 +343,7 @@ public double travel(TrackGraph graph, double distance, ITrackSelector trackSele
349343

350344
if (blockedLocation != null) {
351345
traveled -= position;
352-
position = blockedLocation.doubleValue();
346+
position = blockedLocation;
353347
traveled += position;
354348
break;
355349
}

0 commit comments

Comments
 (0)