SteamWar/BauSystem2.0
Archiviert
12
0

Added methods to record tnt to Recorder

Dieser Commit ist enthalten in:
D4rkr34lm 2023-11-06 02:05:17 +01:00
Ursprung 84150aadd2
Commit 979428d493
3 geänderte Dateien mit 47 neuen und 10 gelöschten Zeilen

Datei anzeigen

@ -20,22 +20,21 @@
package de.steamwar.bausystem.features.tracer2; package de.steamwar.bausystem.features.tracer2;
import de.steamwar.bausystem.BauSystem; import de.steamwar.bausystem.BauSystem;
import de.steamwar.bausystem.features.tpslimit.TPSUtils;
import de.steamwar.bausystem.region.Region; import de.steamwar.bausystem.region.Region;
import de.steamwar.linkage.Linked; import de.steamwar.linkage.Linked;
import org.bukkit.entity.TNTPrimed; import org.bukkit.entity.TNTPrimed;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority; import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.block.TNTPrimeEvent; import org.bukkit.event.block.TNTPrimeEvent;
import org.bukkit.event.entity.EntityExplodeEvent; import org.bukkit.event.entity.EntityExplodeEvent;
import org.bukkit.event.entity.EntitySpawnEvent; import org.bukkit.event.entity.EntitySpawnEvent;
import java.util.HashMap; import java.util.*;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
@Linked @Linked
public class Recorder { public class Recorder implements Listener {
/** /**
* Map for all traces beeing activly recorded * Map for all traces beeing activly recorded
*/ */
@ -67,22 +66,50 @@ public class Recorder {
*/ */
public void stopRecording(Region region){ public void stopRecording(Region region){
activeTraces.remove(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 /** Internal methode to record all tracked TNT Entities
* *
*/ */
private void record(){ 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. /** Eventhandler for TNTs beeing spawn.
* Registers newly spawn to be traced if reqired * Registers newly spawned TNT to be traced if reqired
* @param event * @param event
*/ */
@EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true) @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 /** Event for TNTs exploding
@ -91,6 +118,9 @@ public class Recorder {
*/ */
@EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true) @EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true)
public void onTNTExplode(EntityExplodeEvent event){ 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());
} }
} }

Datei anzeigen

@ -34,7 +34,7 @@ public class TNTRecord {
/** /**
* Tick offset, from this record being taken to the start of the trace * 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) * Fuse ticks of the recorded tnt (0 if this is an explosion)
@ -56,7 +56,7 @@ public class TNTRecord {
*/ */
private final TNTRecord previous; 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.explosion = explosion;
this.ticksSinceStart = ticksSinceStart; this.ticksSinceStart = ticksSinceStart;
fuse = tnt.getFuseTicks(); fuse = tnt.getFuseTicks();

Datei anzeigen

@ -19,6 +19,7 @@
package de.steamwar.bausystem.features.tracer2; package de.steamwar.bausystem.features.tracer2;
import de.steamwar.bausystem.features.tpslimit.TPSUtils;
import de.steamwar.bausystem.region.Region; import de.steamwar.bausystem.region.Region;
import lombok.Getter; import lombok.Getter;
@ -32,6 +33,12 @@ public class Trace {
@Getter @Getter
private final Region region; private final Region region;
/**
* Tick the recording started at
*/
@Getter
private final long startTime = TPSUtils.currentTick.get();
/** /**
* Records of TNTs, making up the trace * Records of TNTs, making up the trace
*/ */