From 31bcefce8a243ddd175513e98d432ef3ecde8fd6 Mon Sep 17 00:00:00 2001 From: YoyoNow Date: Fri, 4 Oct 2024 20:01:47 +0200 Subject: [PATCH] Fix ProcessingTracesState performance --- .../laufbau/states/ProcessingTracesState.java | 26 ++++++++++--------- 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/BauSystem/BauSystem_Main/src/de/steamwar/bausystem/features/slaves/laufbau/states/ProcessingTracesState.java b/BauSystem/BauSystem_Main/src/de/steamwar/bausystem/features/slaves/laufbau/states/ProcessingTracesState.java index c2893964..94eb6549 100644 --- a/BauSystem/BauSystem_Main/src/de/steamwar/bausystem/features/slaves/laufbau/states/ProcessingTracesState.java +++ b/BauSystem/BauSystem_Main/src/de/steamwar/bausystem/features/slaves/laufbau/states/ProcessingTracesState.java @@ -24,6 +24,7 @@ import de.steamwar.bausystem.BauSystem; import de.steamwar.bausystem.features.slaves.laufbau.BlockBoundingBox; import de.steamwar.bausystem.features.slaves.laufbau.Cuboid; 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.region.Point; import de.steamwar.bausystem.utils.FlatteningWrapper; @@ -34,7 +35,6 @@ import org.bukkit.util.Vector; import java.util.*; import java.util.function.BiPredicate; -import java.util.stream.Collectors; public class ProcessingTracesState implements LaufbauState { @@ -45,8 +45,9 @@ public class ProcessingTracesState implements LaufbauState { private final List elements; private final int factor; - private final List TNTPoints; - private final int totalTntRecords; + private final List Traces; + private final List TNTPoints = new ArrayList<>(); + private final int totalTraces; private final Set affectedBlocks = new HashSet<>(); private final Map> cuboidsPerChunk = new HashMap<>(); @@ -58,18 +59,13 @@ public class ProcessingTracesState implements LaufbauState { this.elements = elements; this.factor = factor; - // TODO: Optimize only retrieving traces inside of the affected regions! - TNTPoints = TraceManager.instance.getAll() - .stream() - .flatMap(trace -> trace.getHistories().stream()) - .flatMap(Collection::stream) - .collect(Collectors.toList()); - totalTntRecords = TNTPoints.size(); + Traces = new ArrayList<>(TraceManager.instance.getAll()); + totalTraces = Traces.size(); } @Override 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) { @@ -78,11 +74,17 @@ public class ProcessingTracesState implements LaufbauState { @Override public boolean hasNext() { - return !TNTPoints.isEmpty(); + return !Traces.isEmpty() || !TNTPoints.isEmpty(); } @Override 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); 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))))