Trace Refactor #233
@ -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<Integer, Trace> traces = new HashMap<>();
|
||||
|
||||
private final Map<Region, Map<Integer, Trace>> tracesByRegion = 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);
|
||||
nextOpenId++;
|
||||
return nextOpenId;
|
||||
@ -103,7 +101,7 @@ public class TraceManager implements Listener {
|
||||
* @param 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();
|
||||
}
|
||||
return -1;
|
||||
@ -134,6 +132,7 @@ public class TraceManager implements Listener {
|
||||
* @param trace the trace to be removed
|
||||
*/
|
||||
public boolean remove(Trace trace) {
|
||||
Map<Integer, Trace> 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<Player> 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<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
|
||||
*/
|
||||
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
|
||||
*/
|
||||
public Set<Integer> getAllIds() {
|
||||
return traces.keySet();
|
||||
return tracesByRegion.values().stream().map(Map::keySet).flatMap(Collection::stream).collect(Collectors.toSet());
|
||||
}
|
||||
|
||||
/**
|
||||
|
In neuem Issue referenzieren
Einen Benutzer sperren