From 5c96da9acd0ef54cb537b4e17dbd03e473c441bb Mon Sep 17 00:00:00 2001 From: D4rkr34lm Date: Tue, 20 Feb 2024 23:11:58 +0100 Subject: [PATCH] Fixed bugs with trace rendering Added toString --- .../features/tracer2/BundleFilter.java | 1 - .../bausystem/features/tracer2/Recorder.java | 10 +++--- .../bausystem/features/tracer2/TNTRecord.java | 11 +++++++ .../bausystem/features/tracer2/Trace.java | 31 +++++++++++++++++-- .../features/tracer2/TraceCommand.java | 26 ++++++++++++++-- .../features/tracer2/TraceEntity.java | 1 + .../features/tracer2/TraceManager.java | 2 ++ 7 files changed, 70 insertions(+), 12 deletions(-) diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/tracer2/BundleFilter.java b/BauSystem_Main/src/de/steamwar/bausystem/features/tracer2/BundleFilter.java index 409581bc..08117732 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/tracer2/BundleFilter.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/tracer2/BundleFilter.java @@ -21,7 +21,6 @@ package de.steamwar.bausystem.features.tracer2; import java.util.Optional; import java.util.function.BiFunction; -import java.util.function.BiPredicate; public enum BundleFilter { 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 ff1d1607..f001325d 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/tracer2/Recorder.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/tracer2/Recorder.java @@ -40,7 +40,7 @@ public class Recorder implements Listener { * Linked instance of TraceManager */ @LinkedInstance - TraceManager manager; + public TraceManager manager; /** * Map for all traces beeing activly recorded @@ -63,19 +63,17 @@ public class Recorder implements Listener { private final Map lastRecordMap = new HashMap<>(); public Recorder(){ - BauSystem.runTaskTimer(BauSystem.getInstance(), () ->{ - record(); - }, 0, 1); + BauSystem.runTaskTimer(BauSystem.getInstance(), this::record, 0, 1); } /** Starts a recording at the given region * * @param region region to be recorded */ - public void startRecording(Region region){ + public int startRecording(Region region){ Trace trace = new Trace(region); activeTraces.put(region, trace); - manager.add(trace); + return manager.add(trace); } /** Stops the recording at the given 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 03bb4d71..3392a0c6 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/tracer2/TNTRecord.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/tracer2/TNTRecord.java @@ -78,4 +78,15 @@ public class TNTRecord { protected void setNext(TNTRecord next){ if(this.next == null) this.next = next; } + + @Override + public String toString() { + return "TNTRecord{" + + "explosion=" + explosion + + ", ticksSinceStart=" + ticksSinceStart + + ", fuse=" + fuse + + ", location=" + location + + ", velocity=" + velocity + + '}'; + } } 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 a2d13402..77757a8d 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/tracer2/Trace.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/tracer2/Trace.java @@ -50,7 +50,7 @@ public class Trace { /** * A map of players -> REntityServers for rendering traces to a player */ - private Map serverMap = new HashMap<>(); + private final Map serverMap = new HashMap<>(); public Trace (Region region){ this.region = region; @@ -72,13 +72,21 @@ public class Trace { this.records = records; } - + //TODO default options + //TODO Life rendering /** Renders this traces * * @param player The player the trace is rendered to * @param flags Flags modefieing the rendering */ public void render(Player player, Collection flags, BundleFilter bundleFilter){ + if(serverMap.containsKey(player)){ + REntityServer server = serverMap.get(player); + server.close(); + serverMap.remove(player); + } + + List workingRecords = records; //Apply filters @@ -91,6 +99,7 @@ public class Trace { //Render bundled records REntityServer server = new REntityServer(); serverMap.put(player, server); + server.addPlayer(player); List entities = new LinkedList<>(); for(List bundle : bundles) @@ -107,9 +116,15 @@ public class Trace { * @return A list of bundles */ private List> bundleRecords(List records, BundleFilter filter){ - ArrayList> bundles = new ArrayList<>(); + List> bundles = new ArrayList<>(); recordsLoop : for(TNTRecord record : records) { + if(bundles.isEmpty()){ + List firstBundle = new ArrayList<>(); + firstBundle.add(record); + bundles.add(firstBundle); + } + for(int i = bundles.size() - 1; i >= 0; i--){ List bundle = bundles.get(i); @@ -120,6 +135,7 @@ public class Trace { else{ ArrayList newBundle = new ArrayList<>(); newBundle.add(record); + bundles.add(newBundle); continue recordsLoop; } @@ -139,4 +155,13 @@ public class Trace { server.close(); } + + @Override + public String toString() { + return "Trace{" + + "region=" + region + + ", startTime=" + startTime + + ", records=" + records + + '}'; + } } 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 bd70fb12..d99ed495 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/tracer2/TraceCommand.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/tracer2/TraceCommand.java @@ -25,16 +25,38 @@ import de.steamwar.linkage.Linked; import de.steamwar.linkage.LinkedInstance; import org.bukkit.entity.Player; +import java.util.Collections; +import java.util.HashSet; + @Linked public class TraceCommand extends SWCommand { @LinkedInstance public Recorder recorder; + + int traceId = 0; public TraceCommand(){super("tracetest");} @Register(value = "start") - public void test(@Validator Player player){ + public void test(Player player){ Region region = Region.getRegion(player.getLocation()); - + + traceId = recorder.startRecording(region); + System.out.println(traceId); + } + + @Register(value = "stop") + public void test2(Player player){ + Region region = Region.getRegion(player.getLocation()); + recorder.stopRecording(region); + Trace trace = recorder.manager.get(traceId); + if(trace != null) + System.out.println(trace.getRecords()); + } + + @Register(value = "show") + public void test3(Player player){ + Trace trace = recorder.manager.get(traceId); + trace.render(player, Collections.emptyList(), BundleFilter.DEFAULT); } } diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/tracer2/TraceEntity.java b/BauSystem_Main/src/de/steamwar/bausystem/features/tracer2/TraceEntity.java index 0507c4f9..75b33357 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/tracer2/TraceEntity.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/tracer2/TraceEntity.java @@ -37,6 +37,7 @@ public class TraceEntity extends RFallingBlockEntity { public TraceEntity(REntityServer server, Location location, boolean isExplosion, List records) { super(server, location, isExplosion ? Material.RED_STAINED_GLASS : Material.TNT); + setNoGravity(true); this.records = records; } } 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 f7232e84..5ffc4314 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/tracer2/TraceManager.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/tracer2/TraceManager.java @@ -28,6 +28,8 @@ import java.util.stream.Collectors; @Linked public class TraceManager implements Listener { + //TODO no null returns + /** * Map of all current traces */