From e71ccc31606d8e57e5309267cf0cae5a58eedb0a Mon Sep 17 00:00:00 2001 From: D4rkr34lm Date: Fri, 1 Mar 2024 11:40:24 +0100 Subject: [PATCH] Replaced doubly linked list structure with ArrayList refrence, for tnt history --- .../bausystem/features/tracer/Recorder.java | 17 +++++----- .../bausystem/features/tracer/TNTRecord.java | 31 ++++++++++--------- 2 files changed, 24 insertions(+), 24 deletions(-) diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/tracer/Recorder.java b/BauSystem_Main/src/de/steamwar/bausystem/features/tracer/Recorder.java index d47dda8c..0e9e1d0a 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/tracer/Recorder.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/tracer/Recorder.java @@ -60,7 +60,7 @@ public class Recorder implements Listener { /** * Maps a tracked tnt entity to the last record taken of it */ - private final Map lastRecordMap = new HashMap<>(); + private final Map> historyMap = new HashMap<>(); public Recorder(){ BauSystem.runTaskTimer(BauSystem.getInstance(), this::record, 0, 1); @@ -86,11 +86,9 @@ public class Recorder implements Listener { activeTraces.remove(region); for(TNTPrimed tnt : trackedTNT.getOrDefault(region, Collections.emptyList())) - lastRecordMap.remove(tnt); + historyMap.remove(tnt); trackedTNT.put(region, new ArrayList<>()); - - } /** Internal methode to record all tracked TNT Entities @@ -112,13 +110,14 @@ public class Recorder implements Listener { * @param trace trace to record the tnt for */ private void record(TNTPrimed tntPrimed, Trace trace){ - TNTRecord lastRecord = lastRecordMap.getOrDefault(tntPrimed, null); + List history = historyMap.getOrDefault(tntPrimed, new ArrayList<>()); - TNTRecord record = new TNTRecord(tntPrimed, tntPrimed.getFuseTicks() == 0, TPSUtils.currentTick.get() - trace.getStartTime(), lastRecord); - if(lastRecord != null) - lastRecord.setNext(record); + if(historyMap.size() == 0) + historyMap.put(tntPrimed, history); + + TNTRecord record = new TNTRecord(tntPrimed, tntPrimed.getFuseTicks() == 0, TPSUtils.currentTick.get() - trace.getStartTime(), history); + history.add(record); - lastRecordMap.put(tntPrimed, record); trace.add(record); } diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/tracer/TNTRecord.java b/BauSystem_Main/src/de/steamwar/bausystem/features/tracer/TNTRecord.java index 5969fcc8..2efc01e4 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/tracer/TNTRecord.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/tracer/TNTRecord.java @@ -24,6 +24,9 @@ import org.bukkit.Location; import org.bukkit.entity.TNTPrimed; import org.bukkit.util.Vector; +import java.util.List; +import java.util.Optional; + @Getter public class TNTRecord { /** @@ -52,31 +55,29 @@ public class TNTRecord { private final Vector velocity; /** - * Reference to the last record having been taken of the tnt represented by this record + * List of all tnt records, that are represent the same tnt */ - private final TNTRecord previous; + private final List history; - /** - * Reference to the next record of the tnt represented by this record - */ - private TNTRecord next; - - public TNTRecord(TNTPrimed tnt, boolean explosion, long ticksSinceStart, TNTRecord previous){ + public TNTRecord(TNTPrimed tnt, boolean explosion, long ticksSinceStart, List history){ this.explosion = explosion; this.ticksSinceStart = ticksSinceStart; fuse = tnt.getFuseTicks(); location = tnt.getLocation(); velocity = tnt.getVelocity(); - this.previous = previous; + this.history = history; } + public Optional getNext(){ + int index = history.indexOf(this); - /** - * One call only function to set next - * @param next - */ - protected void setNext(TNTRecord next){ - if(this.next == null) this.next = next; + return index == history.size() - 1 ? Optional.empty() : Optional.of(history.get(index + 1)); + } + + public Optional getPrev(){ + int index = history.indexOf(this); + + return index == 0 ? Optional.empty() : Optional.of(history.get(index - 1)); } @Override