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 520b1234..45f697a6 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/tracer2/Recorder.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/tracer2/Recorder.java @@ -20,22 +20,21 @@ package de.steamwar.bausystem.features.tracer2; import de.steamwar.bausystem.BauSystem; +import de.steamwar.bausystem.features.tpslimit.TPSUtils; import de.steamwar.bausystem.region.Region; import de.steamwar.linkage.Linked; import org.bukkit.entity.TNTPrimed; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; +import org.bukkit.event.Listener; import org.bukkit.event.block.TNTPrimeEvent; import org.bukkit.event.entity.EntityExplodeEvent; import org.bukkit.event.entity.EntitySpawnEvent; -import java.util.HashMap; -import java.util.HashSet; -import java.util.Map; -import java.util.Set; +import java.util.*; @Linked -public class Recorder { +public class Recorder implements Listener { /** * Map for all traces beeing activly recorded */ @@ -67,22 +66,50 @@ public class Recorder { */ public void stopRecording(Region region){ activeTraces.remove(region); + for(TNTPrimed tnt : trackedTNT.getOrDefault(region, Collections.emptySet())) lastRecordMap.remove(tnt); + trackedTNT.put(region, new HashSet<>()); } /** Internal methode to record all tracked TNT Entities * */ private void record(){ + for(Region region : activeTraces.keySet()){ + Trace trace = activeTraces.get(region); + for(TNTPrimed tnt : trackedTNT.getOrDefault(region, Collections.emptySet())){ + TNTRecord record = new TNTRecord(tnt, false, TPSUtils.currentTick.get() - trace.getStartTime(), lastRecordMap.getOrDefault(tnt, null)); + lastRecordMap.put(tnt, record); + trace.add(record); + } + } + } + /** Internal methode to record exploded tnt + * + * @param tntPrimed tnt exploding + */ + private void record(TNTPrimed tntPrimed){ + Region region = Region.getRegion(tntPrimed.getLocation()); + Trace trace = activeTraces.get(region); + + TNTRecord record = new TNTRecord(tntPrimed, true, TPSUtils.currentTick.get() - trace.getStartTime(), lastRecordMap.getOrDefault(tntPrimed, null)); + trace.add(record); } /** Eventhandler for TNTs beeing spawn. - * Registers newly spawn to be traced if reqired + * Registers newly spawned TNT to be traced if reqired * @param event */ @EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true) - public void onTNTPrimed(EntitySpawnEvent event){ + public void onTNTSpawn(EntitySpawnEvent event){ + if(!(event.getEntity() instanceof TNTPrimed)) return; + Region region = Region.getRegion(event.getLocation()); + if(activeTraces.keySet().contains(region)){ + //Check whether set for tracking already exists. Creating it if necessary + if(!trackedTNT.keySet().contains(region)) trackedTNT.put(region, new HashSet<>()); + trackedTNT.get(region).add((TNTPrimed) event.getEntity()); + } } /** Event for TNTs exploding @@ -91,6 +118,9 @@ public class Recorder { */ @EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true) public void onTNTExplode(EntityExplodeEvent event){ - + if(!(event.getEntity() instanceof TNTPrimed)) return; + Region region = Region.getRegion(event.getLocation()); + trackedTNT.get(region).remove((TNTPrimed) event.getEntity()); + record((TNTPrimed) event.getEntity()); } } 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 84adc3de..32f29c7f 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/tracer2/TNTRecord.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/tracer2/TNTRecord.java @@ -34,7 +34,7 @@ public class TNTRecord { /** * Tick offset, from this record being taken to the start of the trace */ - private final int ticksSinceStart; + private final long ticksSinceStart; /** * Fuse ticks of the recorded tnt (0 if this is an explosion) @@ -56,7 +56,7 @@ public class TNTRecord { */ private final TNTRecord previous; - public TNTRecord(TNTPrimed tnt, boolean explosion, int ticksSinceStart, TNTRecord previous){ + public TNTRecord(TNTPrimed tnt, boolean explosion, long ticksSinceStart, TNTRecord previous){ this.explosion = explosion; this.ticksSinceStart = ticksSinceStart; fuse = tnt.getFuseTicks(); diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/tracer2/Trace.java b/BauSystem_Main/src/de/steamwar/bausystem/features/tracer2/Trace.java index 69bda8ec..a821b473 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/tracer2/Trace.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/tracer2/Trace.java @@ -19,6 +19,7 @@ package de.steamwar.bausystem.features.tracer2; +import de.steamwar.bausystem.features.tpslimit.TPSUtils; import de.steamwar.bausystem.region.Region; import lombok.Getter; @@ -32,6 +33,12 @@ public class Trace { @Getter private final Region region; + /** + * Tick the recording started at + */ + @Getter + private final long startTime = TPSUtils.currentTick.get(); + /** * Records of TNTs, making up the trace */