SteamWar/BauSystem2.0
Archiviert
12
0

Remove redundancy in data storage
Alle Prüfungen waren erfolgreich
SteamWarCI Build successful

Dieser Commit ist enthalten in:
yoyosource 2024-04-21 15:55:26 +02:00
Ursprung e931d7157e
Commit 5595f3177c

Datei anzeigen

@ -33,6 +33,7 @@ import org.bukkit.event.player.PlayerQuitEvent;
import java.io.File; import java.io.File;
import java.util.*; import java.util.*;
import java.util.stream.Collectors;
@Linked @Linked
public class TraceManager implements Listener { public class TraceManager implements Listener {
@ -64,8 +65,6 @@ public class TraceManager implements Listener {
/** /**
* List of all current traces * List of all current traces
*/ */
private final Map<Integer, Trace> traces = new HashMap<>();
private final Map<Region, Map<Integer, Trace>> tracesByRegion = new HashMap<>(); private final Map<Region, Map<Integer, Trace>> tracesByRegion = new HashMap<>();
private final Map<Region, Map<Player, PlayerTraceShowData>> showDataPerRegionPerPlayer = new HashMap<>(); private final Map<Region, Map<Player, PlayerTraceShowData>> 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); tracesByRegion.computeIfAbsent(trace.getRegion(), region -> new HashMap<>()).put(nextOpenId, trace);
nextOpenId++; nextOpenId++;
return nextOpenId; return nextOpenId;
@ -103,7 +101,7 @@ public class TraceManager implements Listener {
* @param trace * @param trace
*/ */
public int getId(Trace trace) { public int getId(Trace trace) {
for (Map.Entry<Integer, Trace> entry : traces.entrySet()) { for (Map.Entry<Integer, Trace> entry : tracesByRegion.getOrDefault(trace.getRegion(), Collections.emptyMap()).entrySet()) {
if (entry.getValue() == trace) return entry.getKey(); if (entry.getValue() == trace) return entry.getKey();
} }
return -1; return -1;
@ -134,6 +132,7 @@ public class TraceManager implements Listener {
* @param trace the trace to be removed * @param trace the trace to be removed
*/ */
public boolean remove(Trace trace) { public boolean remove(Trace trace) {
Map<Integer, Trace> traces = tracesByRegion.getOrDefault(trace.getRegion(), Collections.emptyMap());
Integer traceId = traces.entrySet().stream() Integer traceId = traces.entrySet().stream()
.filter(entry -> entry.getValue() == trace) .filter(entry -> entry.getValue() == trace)
.map(Map.Entry::getKey) .map(Map.Entry::getKey)
@ -141,7 +140,6 @@ public class TraceManager implements Listener {
.orElse(null); .orElse(null);
if (traceId == null) return false; if (traceId == null) return false;
traces.remove(traceId); traces.remove(traceId);
tracesByRegion.getOrDefault(trace.getRegion(), Collections.emptyMap()).remove(traceId);
trace.hide(); trace.hide();
return true; return true;
} }
@ -154,7 +152,7 @@ public class TraceManager implements Listener {
.keySet() .keySet()
.forEach(player -> { .forEach(player -> {
Set<Player> players = followerMap.getOrDefault(player, Collections.emptySet()); Set<Player> players = followerMap.getOrDefault(player, Collections.emptySet());
traces.values().forEach(trace -> { tracesByRegion.get(region).values().forEach(trace -> {
trace.hide(player); trace.hide(player);
players.forEach(trace::hide); players.forEach(trace::hide);
}); });
@ -162,7 +160,6 @@ public class TraceManager implements Listener {
tracesByRegion.getOrDefault(region, new HashMap<>()) tracesByRegion.getOrDefault(region, new HashMap<>())
.forEach((i, trace) -> { .forEach((i, trace) -> {
if (trace.getRegion() != region) return; if (trace.getRegion() != region) return;
traces.remove(i);
trace.getMetadataSaveFile().delete(); trace.getMetadataSaveFile().delete();
trace.getRecordsSaveFile().delete(); trace.getRecordsSaveFile().delete();
}); });
@ -186,7 +183,12 @@ public class TraceManager implements Listener {
* @return the trace with given id * @return the trace with given id
*/ */
public Optional<Trace> get(int index) { public Optional<Trace> get(int index) {
return Optional.ofNullable(traces.get(index)); for (Map.Entry<Region, Map<Integer, Trace>> 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 * @return internal list of all current traces
*/ */
public Collection<Trace> getAll() { public Collection<Trace> getAll() {
return traces.values(); return tracesByRegion.values().stream().map(Map::values).flatMap(Collection::stream).collect(Collectors.toList());
} }
/** /**
* @return all ids of active traces * @return all ids of active traces
*/ */
public Set<Integer> getAllIds() { public Set<Integer> getAllIds() {
return traces.keySet(); return tracesByRegion.values().stream().map(Map::keySet).flatMap(Collection::stream).collect(Collectors.toSet());
} }
/** /**