Fix ProcessingTracesState performance

This commit is contained in:
2024-10-04 20:01:47 +02:00
parent 11a933fede
commit 31bcefce8a

View File

@@ -24,6 +24,7 @@ import de.steamwar.bausystem.BauSystem;
import de.steamwar.bausystem.features.slaves.laufbau.BlockBoundingBox; import de.steamwar.bausystem.features.slaves.laufbau.BlockBoundingBox;
import de.steamwar.bausystem.features.slaves.laufbau.Cuboid; import de.steamwar.bausystem.features.slaves.laufbau.Cuboid;
import de.steamwar.bausystem.features.tracer.TNTPoint; import de.steamwar.bausystem.features.tracer.TNTPoint;
import de.steamwar.bausystem.features.tracer.Trace;
import de.steamwar.bausystem.features.tracer.TraceManager; import de.steamwar.bausystem.features.tracer.TraceManager;
import de.steamwar.bausystem.region.Point; import de.steamwar.bausystem.region.Point;
import de.steamwar.bausystem.utils.FlatteningWrapper; import de.steamwar.bausystem.utils.FlatteningWrapper;
@@ -34,7 +35,6 @@ import org.bukkit.util.Vector;
import java.util.*; import java.util.*;
import java.util.function.BiPredicate; import java.util.function.BiPredicate;
import java.util.stream.Collectors;
public class ProcessingTracesState implements LaufbauState { public class ProcessingTracesState implements LaufbauState {
@@ -45,8 +45,9 @@ public class ProcessingTracesState implements LaufbauState {
private final List<BlockBoundingBox> elements; private final List<BlockBoundingBox> elements;
private final int factor; private final int factor;
private final List<TNTPoint> TNTPoints; private final List<Trace> Traces;
private final int totalTntRecords; private final List<TNTPoint> TNTPoints = new ArrayList<>();
private final int totalTraces;
private final Set<Point> affectedBlocks = new HashSet<>(); private final Set<Point> affectedBlocks = new HashSet<>();
private final Map<Point, Set<Cuboid>> cuboidsPerChunk = new HashMap<>(); private final Map<Point, Set<Cuboid>> cuboidsPerChunk = new HashMap<>();
@@ -58,18 +59,13 @@ public class ProcessingTracesState implements LaufbauState {
this.elements = elements; this.elements = elements;
this.factor = factor; this.factor = factor;
// TODO: Optimize only retrieving traces inside of the affected regions! Traces = new ArrayList<>(TraceManager.instance.getAll());
TNTPoints = TraceManager.instance.getAll() totalTraces = Traces.size();
.stream()
.flatMap(trace -> trace.getHistories().stream())
.flatMap(Collection::stream)
.collect(Collectors.toList());
totalTntRecords = TNTPoints.size();
} }
@Override @Override
public String actionBarMessage(Player p) { public String actionBarMessage(Player p) {
return BauSystem.MESSAGE.parse("LAUFBAU_SIMPLE_PROGRESS", p, BauSystem.MESSAGE.parse("LAUFBAU_STATE_PROCESSING_TRACES", p), totalTntRecords - TNTPoints.size(), totalTntRecords, eta(p, start, totalTntRecords - TNTPoints.size(), totalTntRecords)); return BauSystem.MESSAGE.parse("LAUFBAU_SIMPLE_PROGRESS", p, BauSystem.MESSAGE.parse("LAUFBAU_STATE_PROCESSING_TRACES", p), totalTraces - Traces.size(), totalTraces, eta(p, start, totalTraces - Traces.size(), totalTraces));
} }
private boolean inRegion(Vector location, int expansion) { private boolean inRegion(Vector location, int expansion) {
@@ -78,11 +74,17 @@ public class ProcessingTracesState implements LaufbauState {
@Override @Override
public boolean hasNext() { public boolean hasNext() {
return !TNTPoints.isEmpty(); return !Traces.isEmpty() || !TNTPoints.isEmpty();
} }
@Override @Override
public void next() { public void next() {
if (TNTPoints.isEmpty()) {
Trace trace = Traces.remove(0);
trace.getHistories().stream().flatMap(Collection::stream).forEach(TNTPoints::add);
return;
}
TNTPoint current = TNTPoints.remove(0); TNTPoint current = TNTPoints.remove(0);
if (FlatteningWrapper.impl.inWater(world, current.getLocation().toVector())) return; if (FlatteningWrapper.impl.inWater(world, current.getLocation().toVector())) return;
if (!(inRegion(current.getLocation().toVector(), 1) || (current.getPrevious().isPresent() && inRegion(current.getPrevious().get().getLocation().toVector(), 1)))) if (!(inRegion(current.getLocation().toVector(), 1) || (current.getPrevious().isPresent() && inRegion(current.getPrevious().get().getLocation().toVector(), 1))))