From 5595f3177ccb34ac958ccbd83902108a8fbfd98d Mon Sep 17 00:00:00 2001 From: yoyosource Date: Sun, 21 Apr 2024 15:55:26 +0200 Subject: [PATCH] Remove redundancy in data storage --- .../features/tracer/TraceManager.java | 22 ++++++++++--------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/tracer/TraceManager.java b/BauSystem_Main/src/de/steamwar/bausystem/features/tracer/TraceManager.java index b4715b79..d735fe8d 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/tracer/TraceManager.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/tracer/TraceManager.java @@ -33,6 +33,7 @@ import org.bukkit.event.player.PlayerQuitEvent; import java.io.File; import java.util.*; +import java.util.stream.Collectors; @Linked public class TraceManager implements Listener { @@ -64,8 +65,6 @@ public class TraceManager implements Listener { /** * List of all current traces */ - private final Map traces = new HashMap<>(); - private final Map> tracesByRegion = new HashMap<>(); private final Map> showDataPerRegionPerPlayer = new HashMap<>(); @@ -91,7 +90,6 @@ public class TraceManager implements Listener { }); }); - traces.put(nextOpenId, trace); tracesByRegion.computeIfAbsent(trace.getRegion(), region -> new HashMap<>()).put(nextOpenId, trace); nextOpenId++; return nextOpenId; @@ -103,7 +101,7 @@ public class TraceManager implements Listener { * @param trace */ public int getId(Trace trace) { - for (Map.Entry entry : traces.entrySet()) { + for (Map.Entry entry : tracesByRegion.getOrDefault(trace.getRegion(), Collections.emptyMap()).entrySet()) { if (entry.getValue() == trace) return entry.getKey(); } return -1; @@ -134,6 +132,7 @@ public class TraceManager implements Listener { * @param trace the trace to be removed */ public boolean remove(Trace trace) { + Map traces = tracesByRegion.getOrDefault(trace.getRegion(), Collections.emptyMap()); Integer traceId = traces.entrySet().stream() .filter(entry -> entry.getValue() == trace) .map(Map.Entry::getKey) @@ -141,7 +140,6 @@ public class TraceManager implements Listener { .orElse(null); if (traceId == null) return false; traces.remove(traceId); - tracesByRegion.getOrDefault(trace.getRegion(), Collections.emptyMap()).remove(traceId); trace.hide(); return true; } @@ -154,7 +152,7 @@ public class TraceManager implements Listener { .keySet() .forEach(player -> { Set players = followerMap.getOrDefault(player, Collections.emptySet()); - traces.values().forEach(trace -> { + tracesByRegion.get(region).values().forEach(trace -> { trace.hide(player); players.forEach(trace::hide); }); @@ -162,7 +160,6 @@ public class TraceManager implements Listener { tracesByRegion.getOrDefault(region, new HashMap<>()) .forEach((i, trace) -> { if (trace.getRegion() != region) return; - traces.remove(i); trace.getMetadataSaveFile().delete(); trace.getRecordsSaveFile().delete(); }); @@ -186,7 +183,12 @@ public class TraceManager implements Listener { * @return the trace with given id */ public Optional get(int index) { - return Optional.ofNullable(traces.get(index)); + for (Map.Entry> intermediate : tracesByRegion.entrySet()) { + if (intermediate.getValue().containsKey(index)) { + return Optional.ofNullable(intermediate.getValue().get(index)); + } + } + return Optional.empty(); } /** @@ -195,14 +197,14 @@ public class TraceManager implements Listener { * @return internal list of all current traces */ public Collection getAll() { - return traces.values(); + return tracesByRegion.values().stream().map(Map::values).flatMap(Collection::stream).collect(Collectors.toList()); } /** * @return all ids of active traces */ public Set getAllIds() { - return traces.keySet(); + return tracesByRegion.values().stream().map(Map::keySet).flatMap(Collection::stream).collect(Collectors.toSet()); } /**