Null protection & Readability changes (#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
This commit is contained in:
IThundxr 2024-07-25 06:36:36 -04:00 committed by GitHub
parent 2c98728b20
commit 96a05e70dd
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 37 additions and 41 deletions

View file

@ -619,7 +619,7 @@ public class Train {
public Pair<Train, Vec3> findCollidingTrain(Level level, Vec3 start, Vec3 end, ResourceKey<Level> dimension) {
Vec3 diff = end.subtract(start);
double maxDistanceSqr = Math.pow(AllConfigs.server().trains.maxAssemblyLength.get(), 2.0);
Trains: for (Train train : Create.RAILWAYS.sided(level).trains.values()) {
if (train == this)
continue;
@ -962,6 +962,9 @@ public class Train {
TrackNode node1 = trailingPoint.node1;
TrackNode node2 = trailingPoint.node2;
TrackEdge edge = trailingPoint.edge;
if (edge == null) return;
double position = trailingPoint.position;
EdgeData signalData = edge.getEdgeData();
@ -1219,20 +1222,19 @@ public class Train {
public void determineHonk(Level level) {
if (lowHonk != null)
return;
for (int index = 0; index < carriages.size(); index++) {
Carriage carriage = carriages.get(index);
DimensionalCarriageEntity dimensional = carriage.getDimensionalIfPresent(level.dimension());
if (dimensional == null)
return;
CarriageContraptionEntity entity = dimensional.entity.get();
if (entity == null || !(entity.getContraption()instanceof CarriageContraption otherCC))
break;
Pair<Boolean, Integer> first = otherCC.soundQueue.getFirstWhistle(entity);
if (first != null) {
lowHonk = first.getFirst();
honkPitch = first.getSecond();
}
}
for (Carriage carriage : carriages) {
DimensionalCarriageEntity dimensional = carriage.getDimensionalIfPresent(level.dimension());
if (dimensional == null)
return;
CarriageContraptionEntity entity = dimensional.entity.get();
if (entity == null || !(entity.getContraption() instanceof CarriageContraption otherCC))
break;
Pair<Boolean, Integer> first = otherCC.soundQueue.getFirstWhistle(entity);
if (first != null) {
lowHonk = first.getFirst();
honkPitch = first.getSecond();
}
}
}
public float distanceToLocationSqr(Level level, Vec3 location) {

View file

@ -40,28 +40,24 @@ public class TravellingPoint {
public boolean blocked;
public boolean upsideDown;
public static enum SteerDirection {
public enum SteerDirection {
NONE(0), LEFT(-1), RIGHT(1);
float targetDot;
final float targetDot;
private SteerDirection(float targetDot) {
SteerDirection(float targetDot) {
this.targetDot = targetDot;
}
}
public static interface ITrackSelector
extends BiFunction<TrackGraph, Pair<Boolean, List<Entry<TrackNode, TrackEdge>>>, Entry<TrackNode, TrackEdge>> {
};
public interface ITrackSelector
extends BiFunction<TrackGraph, Pair<Boolean, List<Entry<TrackNode, TrackEdge>>>, Entry<TrackNode, TrackEdge>> { };
public static interface IEdgePointListener extends BiPredicate<Double, Pair<TrackEdgePoint, Couple<TrackNode>>> {
};
public interface IEdgePointListener extends BiPredicate<Double, Pair<TrackEdgePoint, Couple<TrackNode>>> { };
public static interface ITurnListener extends BiConsumer<Double, TrackEdge> {
};
public interface ITurnListener extends BiConsumer<Double, TrackEdge> { };
public static interface IPortalListener extends Predicate<Couple<TrackNodeLocation>> {
};
public interface IPortalListener extends Predicate<Couple<TrackNodeLocation>> { };
public TravellingPoint() {}
@ -78,8 +74,7 @@ public class TravellingPoint {
}
public ITurnListener ignoreTurns() {
return (d, c) -> {
};
return (d, c) -> { };
}
public IPortalListener ignorePortals() {
@ -113,15 +108,14 @@ public class TravellingPoint {
Vector<List<Entry<TrackNode, TrackEdge>>> frontiers = new Vector<>(validTargets.size());
Vector<Set<TrackEdge>> visiteds = new Vector<>(validTargets.size());
for (int j = 0; j < validTargets.size(); j++) {
ArrayList<Entry<TrackNode, TrackEdge>> e = new ArrayList<>();
Entry<TrackNode, TrackEdge> entry = validTargets.get(j);
e.add(entry);
frontiers.add(e);
HashSet<TrackEdge> e2 = new HashSet<>();
e2.add(entry.getValue());
visiteds.add(e2);
}
for (Entry<TrackNode, TrackEdge> validTarget : validTargets) {
ArrayList<Entry<TrackNode, TrackEdge>> e = new ArrayList<>();
e.add(validTarget);
frontiers.add(e);
HashSet<TrackEdge> e2 = new HashSet<>();
e2.add(validTarget.getValue());
visiteds.add(e2);
}
for (int i = 0; i < 20; i++) {
for (int j = 0; j < validTargets.size(); j++) {
@ -233,7 +227,7 @@ public class TravellingPoint {
Double blockedLocation =
edgeTraversedFrom(graph, forward, signalListener, turnListener, prevPos, collectedDistance);
if (blockedLocation != null) {
position = blockedLocation.doubleValue();
position = blockedLocation;
traveled = position - prevPos;
return traveled;
}
@ -289,7 +283,7 @@ public class TravellingPoint {
if (blockedLocation != null) {
traveled -= position;
position = blockedLocation.doubleValue();
position = blockedLocation;
traveled += position;
break;
}
@ -349,7 +343,7 @@ public class TravellingPoint {
if (blockedLocation != null) {
traveled -= position;
position = blockedLocation.doubleValue();
position = blockedLocation;
traveled += position;
break;
}