From f94aeeda0191fe458b08e53209ac861411eb23d4 Mon Sep 17 00:00:00 2001 From: D4rkr34lm Date: Sun, 26 Nov 2023 17:00:02 +0100 Subject: [PATCH] Added next link in tnt record --- .../bausystem/features/tracer2/Recorder.java | 29 ++++++++++++------- .../bausystem/features/tracer2/TNTRecord.java | 14 +++++++++ .../features/tracer2/TraceCommand.java | 17 ++++++++++- .../features/tracer2/TraceManager.java | 12 +++++++- 4 files changed, 60 insertions(+), 12 deletions(-) diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/tracer2/Recorder.java b/BauSystem_Main/src/de/steamwar/bausystem/features/tracer2/Recorder.java index 3fcd4998..387d753b 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/tracer2/Recorder.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/tracer2/Recorder.java @@ -45,6 +45,11 @@ public class Recorder implements Listener { */ private final Map> trackedTNT = new HashMap<>(); + /** Map from TNT to Region the TNT spawned in + * + */ + private final Map tntSpawnRegion = new HashMap<>(); + /** * Maps a tracked tnt entity to the last record taken of it */ @@ -83,9 +88,7 @@ public class Recorder implements Listener { for(Region region : activeTraces.keySet()){ Trace trace = activeTraces.get(region); for(TNTPrimed tnt : trackedTNT.getOrDefault(region, Collections.emptyList())){ - TNTRecord record = new TNTRecord(tnt, false, TPSUtils.currentTick.get() - trace.getStartTime(), lastRecordMap.getOrDefault(tnt, null)); - lastRecordMap.put(tnt, record); - trace.add(record); + record(tnt, trace); } } } @@ -93,16 +96,18 @@ public class Recorder implements Listener { /** Internal methode to record exploded tnt * * @param tntPrimed tnt exploding + * @param trace trace to record the tnt for */ - private void record(TNTPrimed tntPrimed){ - Region region = Region.getRegion(tntPrimed.getLocation()); - Trace trace = activeTraces.get(region); + private void record(TNTPrimed tntPrimed, Trace trace){ + TNTRecord lastRecord = lastRecordMap.getOrDefault(tntPrimed, null); - TNTRecord record = new TNTRecord(tntPrimed, true, TPSUtils.currentTick.get() - trace.getStartTime(), lastRecordMap.getOrDefault(tntPrimed, null)); + TNTRecord record = new TNTRecord(tntPrimed, tntPrimed.getFuseTicks() == 0, TPSUtils.currentTick.get() - trace.getStartTime(), lastRecord); + if(lastRecord != null) lastRecord.setNext(record); + lastRecordMap.put(tntPrimed, record); trace.add(record); } - /** Eventhandler for TNTs beeing spawn. + /** Event for TNTs beeing spawn. * Registers newly spawned TNT to be traced if reqired * @param event */ @@ -115,6 +120,7 @@ public class Recorder implements Listener { if(!trackedTNT.containsKey(region)) trackedTNT.put(region, new ArrayList<>()); trackedTNT.get(region).add((TNTPrimed) event.getEntity()); + tntSpawnRegion.put((TNTPrimed) event.getEntity(), region); } } @@ -125,8 +131,11 @@ public class Recorder implements Listener { @EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true) public void onTNTExplode(EntityExplodeEvent event){ if(!(event.getEntity() instanceof TNTPrimed)) return; - Region region = Region.getRegion(event.getLocation()); + Region region = tntSpawnRegion.getOrDefault((TNTPrimed) event.getEntity(), null); + if(region == null) return; trackedTNT.get(region).remove((TNTPrimed) event.getEntity()); - record((TNTPrimed) event.getEntity()); + tntSpawnRegion.remove((TNTPrimed) event.getEntity()); + + record((TNTPrimed) event.getEntity(), activeTraces.get(region)); } } diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/tracer2/TNTRecord.java b/BauSystem_Main/src/de/steamwar/bausystem/features/tracer2/TNTRecord.java index 32f29c7f..03bb4d71 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/tracer2/TNTRecord.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/tracer2/TNTRecord.java @@ -56,6 +56,11 @@ public class TNTRecord { */ private final TNTRecord previous; + /** + * 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){ this.explosion = explosion; this.ticksSinceStart = ticksSinceStart; @@ -64,4 +69,13 @@ public class TNTRecord { velocity = tnt.getVelocity(); this.previous = previous; } + + + /** + * One call only function to set next + * @param next + */ + protected void setNext(TNTRecord next){ + if(this.next == null) this.next = next; + } } diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/tracer2/TraceCommand.java b/BauSystem_Main/src/de/steamwar/bausystem/features/tracer2/TraceCommand.java index 1fdfaf2f..ab82eb3e 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/tracer2/TraceCommand.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/tracer2/TraceCommand.java @@ -19,5 +19,20 @@ package de.steamwar.bausystem.features.tracer2; -public class TraceCommand { +import de.steamwar.command.SWCommand; +import de.steamwar.linkage.Linked; +import de.steamwar.linkage.LinkedInstance; +import org.bukkit.entity.Player; + +@Linked +public class TraceCommand extends SWCommand { + + @LinkedInstance + public Recorder recorder; + public TraceCommand(){super("tracetest");} + + @Register + public void test(@Validator Player player){ + + } } diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/tracer2/TraceManager.java b/BauSystem_Main/src/de/steamwar/bausystem/features/tracer2/TraceManager.java index 47f8778d..5d77ebba 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/tracer2/TraceManager.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/tracer2/TraceManager.java @@ -20,11 +20,19 @@ package de.steamwar.bausystem.features.tracer2; import de.steamwar.bausystem.region.Region; +import de.steamwar.entity.REntityServer; +import de.steamwar.linkage.Linked; +import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.player.PlayerJoinEvent; +import org.bukkit.event.player.PlayerQuitEvent; import java.util.*; import java.util.stream.Collectors; -public class TraceManager { +@Linked +public class TraceManager implements Listener { /** * Map of all current traces */ @@ -35,6 +43,8 @@ public class TraceManager { */ private int currOpenId = 0; + + /** Adds a new trace to the global record * * @param trace Trace to be added