SteamWar/BauSystem2.0
Archiviert
12
0

Readded auto-trace
Einige Prüfungen sind fehlgeschlagen
SteamWarCI Build failed

Dieser Commit ist enthalten in:
D4rkr34lm 2024-03-01 23:48:53 +01:00
Ursprung 1094f4a979
Commit 0c37cfc3bb
2 geänderte Dateien mit 42 neuen und 32 gelöschten Zeilen

Datei anzeigen

@ -43,12 +43,12 @@ public class Recorder implements Listener {
public TraceManager manager; public TraceManager manager;
/** /**
* Map for all traces beeing activly recorded * Map for all traces being actively recorded
*/ */
private final Map<Region, Trace> activeTraces = new HashMap<>(); private final Map<Region, Trace> activeTraces = new HashMap<>();
/** /**
* Map for all tnts being traced, by region * Map for all TNTs being traced, by region
*/ */
private final Map<Region, List<TNTPrimed>> trackedTNT = new HashMap<>(); private final Map<Region, List<TNTPrimed>> trackedTNT = new HashMap<>();
@ -58,22 +58,39 @@ public class Recorder implements Listener {
private final Map<TNTPrimed, Region> tntSpawnRegion = new HashMap<>(); private final Map<TNTPrimed, Region> tntSpawnRegion = new HashMap<>();
/** /**
* Maps a tracked tnt entity to the last record taken of it * Maps a tracked tnt entity to its entire recording history
*/ */
private final Map<TNTPrimed, List<TNTRecord>> historyMap = new HashMap<>(); private final Map<TNTPrimed, List<TNTRecord>> historyMap = new HashMap<>();
/**
* Regions where auto-trace is enabled
*/
private final Set<Region> autoTraceRegions = new HashSet<>();
public Recorder(){ public Recorder(){
BauSystem.runTaskTimer(BauSystem.getInstance(), this::record, 0, 1); BauSystem.runTaskTimer(BauSystem.getInstance(), this::record, 0, 1);
} }
/** Toggles auto trace for the given region
*
* @param region
*/
public void toggleAutoTrace(Region region) {
if (autoTraceRegions.contains(region))
autoTraceRegions.remove(region);
else
autoTraceRegions.add(region);
}
/** Starts a recording at the given region /** Starts a recording at the given region
* *
* @param region region to be recorded * @param region region to be recorded
* @
*/ */
public int startRecording(Region region){ public void startRecording(Region region){
if(activeTraces.containsKey(region)) return;
Trace trace = new Trace(region); Trace trace = new Trace(region);
activeTraces.put(region, trace); activeTraces.put(region, trace);
return manager.add(trace);
} }
/** Stops the recording at the given region /** Stops the recording at the given region
@ -81,7 +98,8 @@ public class Recorder implements Listener {
* @param region region to stop recording * @param region region to stop recording
*/ */
public void stopRecording(Region region){ public void stopRecording(Region region){
Trace trace = activeTraces.get(region); Trace trace = activeTraces.getOrDefault(region, null);
if (trace == null) return;
trace.setRecords(Collections.unmodifiableList(trace.getRecords())); trace.setRecords(Collections.unmodifiableList(trace.getRecords()));
activeTraces.remove(region); activeTraces.remove(region);
@ -136,6 +154,10 @@ public class Recorder implements Listener {
if(!(event.getEntity() instanceof TNTPrimed)) return; if(!(event.getEntity() instanceof TNTPrimed)) return;
Region region = Region.getRegion(event.getLocation()); Region region = Region.getRegion(event.getLocation());
if(autoTraceRegions.contains(region) && !activeTraces.containsKey(region))
startRecording(region);
if(activeTraces.containsKey(region)){ if(activeTraces.containsKey(region)){
//Check whether set for tracking already exists. Creating it if necessary //Check whether set for tracking already exists. Creating it if necessary
if(!trackedTNT.containsKey(region)) if(!trackedTNT.containsKey(region))

Datei anzeigen

@ -29,33 +29,24 @@ import java.util.stream.Collectors;
@Linked @Linked
public class TraceManager implements Listener { public class TraceManager implements Listener {
/** /**
* Map of all current traces * List of all current traces
*/ */
private final Map<Integer, Trace> traces = new HashMap<>(); private final List<Trace> traces = new ArrayList<>();
/**
* Utility variable to keep track of the next open trace id
*/
private int currOpenId = 0;
/** Adds a new trace to the global record /** Adds a new trace to the global record
* *
* @param trace Trace to be added * @param trace Trace to be added
* @return id of the newly added trace
*/ */
protected int add(Trace trace){ protected void add(Trace trace){
int id = currOpenId; traces.add(trace);
traces.put(id, trace);
currOpenId++;
return id;
} }
/** Removes the trace with the given id /** Removes the trace with the given id
* *
* @param id Id of the trace to be removed * @param index index of the trace to be removed
*/ */
public void remove(int id){ public void remove(int index){
traces.remove(id); traces.remove(index);
} }
/** Clears all traces /** Clears all traces
@ -63,7 +54,6 @@ public class TraceManager implements Listener {
*/ */
public void clear(){ public void clear(){
traces.clear(); traces.clear();
currOpenId = 0;
} }
/** Methode to get all traces in a certain region /** Methode to get all traces in a certain region
@ -72,21 +62,19 @@ public class TraceManager implements Listener {
* @return All traces recorded in the given Region * @return All traces recorded in the given Region
*/ */
public Set<Trace> get(Region region){ public Set<Trace> get(Region region){
return traces.values() return traces.stream()
.stream()
.filter((Trace trace) -> trace.getRegion() == region) .filter((Trace trace) -> trace.getRegion() == region)
.collect(Collectors.toSet()); .collect(Collectors.toSet());
} }
/** Methode to get the trace with specific id /** Methode to get the trace with specific id
* *
* @param id id of the trace * @param index index of the trace
* @return the trace with given id or null if no trace with id is found * @return the trace with given id or null if no trace with id is found
*/ */
public Optional<Trace> get(int id){ public Optional<Trace> get(int index){
Trace ret = traces.get(id); if(index < traces.size())
if(traces.containsKey(id)) return Optional.of(traces.get(index));
return Optional.of(traces.get(id));
return Optional.empty(); return Optional.empty();
} }
@ -94,7 +82,7 @@ public class TraceManager implements Listener {
* *
* @return fresh set of all current traces * @return fresh set of all current traces
*/ */
public Set<Trace> getAll(){ public List<Trace> getAll(){
return new HashSet<>(traces.values()); return new ArrayList<>(traces);
} }
} }