From 2bf3cfbb1d0cafaa84a6ee771d5f849ba8bdaaaa Mon Sep 17 00:00:00 2001 From: D4rkr34lm Date: Tue, 19 Dec 2023 16:17:14 +0100 Subject: [PATCH] Reimplemented trace rendering --- .../src/de/steamwar/bausystem/BauSystem.java | 5 -- .../bausystem/features/tracer2/Recorder.java | 28 ++++++- .../bausystem/features/tracer2/Trace.java | 76 +++++++++++++++++-- .../features/tracer2/TraceEntity.java | 42 ++++++++++ .../features/tracer2/TraceManager.java | 16 ++-- .../bausystem/features/tracer2/ViewFlag.java | 53 +++++++++++++ 6 files changed, 200 insertions(+), 20 deletions(-) create mode 100644 BauSystem_Main/src/de/steamwar/bausystem/features/tracer2/TraceEntity.java create mode 100644 BauSystem_Main/src/de/steamwar/bausystem/features/tracer2/ViewFlag.java diff --git a/BauSystem_Main/src/de/steamwar/bausystem/BauSystem.java b/BauSystem_Main/src/de/steamwar/bausystem/BauSystem.java index 5bf3fb98..218ae32f 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/BauSystem.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/BauSystem.java @@ -51,11 +51,6 @@ public class BauSystem extends JavaPlugin implements Listener { // This should be treated as final! public static Message MESSAGE; - /* - * Plugin wide availeble data - */ - public static final TraceManager TRACES = new TraceManager(); - @Getter private static BauSystem instance; 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 387d753b..c53e6610 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/tracer2/Recorder.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/tracer2/Recorder.java @@ -23,6 +23,7 @@ import de.steamwar.bausystem.BauSystem; import de.steamwar.bausystem.features.tpslimit.TPSUtils; import de.steamwar.bausystem.region.Region; import de.steamwar.linkage.Linked; +import de.steamwar.linkage.LinkedInstance; import org.bukkit.entity.TNTPrimed; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; @@ -35,6 +36,13 @@ import java.util.*; @Linked public class Recorder implements Listener { + + /** + * Linked instance of TraceManager + */ + @LinkedInstance + TraceManager manager; + /** * Map for all traces beeing activly recorded */ @@ -68,7 +76,7 @@ public class Recorder implements Listener { public void startRecording(Region region){ Trace trace = new Trace(region); activeTraces.put(region, trace); - BauSystem.TRACES.add(trace); + manager.add(trace); } /** Stops the recording at the given region @@ -76,9 +84,16 @@ public class Recorder implements Listener { * @param region region to stop recording */ public void stopRecording(Region region){ + Trace trace = activeTraces.get(region); + trace.setRecords(Collections.unmodifiableList(trace.getRecords())); + activeTraces.remove(region); - for(TNTPrimed tnt : trackedTNT.getOrDefault(region, Collections.emptyList())) lastRecordMap.remove(tnt); + for(TNTPrimed tnt : trackedTNT.getOrDefault(region, Collections.emptyList())) + lastRecordMap.remove(tnt); + trackedTNT.put(region, new ArrayList<>()); + + } /** Internal methode to record all tracked TNT Entities @@ -102,7 +117,9 @@ public class Recorder implements Listener { TNTRecord lastRecord = lastRecordMap.getOrDefault(tntPrimed, null); TNTRecord record = new TNTRecord(tntPrimed, tntPrimed.getFuseTicks() == 0, TPSUtils.currentTick.get() - trace.getStartTime(), lastRecord); - if(lastRecord != null) lastRecord.setNext(record); + if(lastRecord != null) + lastRecord.setNext(record); + lastRecordMap.put(tntPrimed, record); trace.add(record); } @@ -114,10 +131,12 @@ public class Recorder implements Listener { @EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true) public void onTNTSpawn(EntitySpawnEvent event){ if(!(event.getEntity() instanceof TNTPrimed)) return; + Region region = Region.getRegion(event.getLocation()); if(activeTraces.containsKey(region)){ //Check whether set for tracking already exists. Creating it if necessary - if(!trackedTNT.containsKey(region)) trackedTNT.put(region, new ArrayList<>()); + if(!trackedTNT.containsKey(region)) + trackedTNT.put(region, new ArrayList<>()); trackedTNT.get(region).add((TNTPrimed) event.getEntity()); tntSpawnRegion.put((TNTPrimed) event.getEntity(), region); @@ -131,6 +150,7 @@ public class Recorder implements Listener { @EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true) public void onTNTExplode(EntityExplodeEvent event){ if(!(event.getEntity() instanceof TNTPrimed)) return; + Region region = tntSpawnRegion.getOrDefault((TNTPrimed) event.getEntity(), null); if(region == null) return; trackedTNT.get(region).remove((TNTPrimed) event.getEntity()); 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 4301c051..4aa4fc7b 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/tracer2/Trace.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/tracer2/Trace.java @@ -21,12 +21,12 @@ package de.steamwar.bausystem.features.tracer2; import de.steamwar.bausystem.features.tpslimit.TPSUtils; import de.steamwar.bausystem.region.Region; +import de.steamwar.entity.REntityServer; import lombok.Getter; +import org.bukkit.Location; +import org.bukkit.entity.Player; -import java.util.ArrayList; -import java.util.HashSet; -import java.util.List; -import java.util.Set; +import java.util.*; public class Trace { /** @@ -44,7 +44,13 @@ public class Trace { /** * Records of TNTs, making up the trace */ - private final List records = new ArrayList<>(); + @Getter + private List records = new ArrayList<>(); + + /** + * A map of players -> REntityServers for rendering traces to a player + */ + private Map serverMap = new HashMap<>(); public Trace (Region region){ this.region = region; @@ -57,4 +63,64 @@ public class Trace { protected void add (TNTRecord record){ records.add(record); } + + /** Internal methode to make records immutable after recording is finished + * + * @param records immutable records list + */ + protected void setRecords(List records){ + this.records = records; + } + + + /** 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){ + List workingRecords = records; + + //Apply filters + for(ViewFlag flag : flags) + workingRecords = flag.filter.apply(workingRecords); + + //Bundle records at unique positions + List> bundles = bundleRecords(workingRecords); + + //Render bundled records + REntityServer server = new REntityServer(); + serverMap.put(player, server); + List entities = new LinkedList<>(); + + for(List bundle : bundles) + entities.add(new TraceEntity(server, bundle.get(0).getLocation(), bundle.get(0).isExplosion(), bundle)); + + //Apply modifiers + for(ViewFlag flag : flags) + flag.modify.accept(server, entities); + } + + /** Bundles the passed TNTRecords based on whether they are at the same location + * + * @param records The TNTRecords that are supposed to be bundled + * @return A list of bundles + */ + private List> bundleRecords(List records){ + Map > entitiesRecords = new HashMap<>(); + + for(TNTRecord record : records) { + if(entitiesRecords.containsKey(record.getLocation())) + entitiesRecords.get(record.getLocation()); + else{ + List entityRecords = new ArrayList<>(); + entityRecords.add(record); + entitiesRecords.put(record.getLocation(), entityRecords); + } + } + + return new LinkedList<>(entitiesRecords.values()); + } + + } diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/tracer2/TraceEntity.java b/BauSystem_Main/src/de/steamwar/bausystem/features/tracer2/TraceEntity.java new file mode 100644 index 00000000..0507c4f9 --- /dev/null +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/tracer2/TraceEntity.java @@ -0,0 +1,42 @@ +/* + * This file is a part of the SteamWar software. + * + * Copyright (C) 2023 SteamWar.de-Serverteam + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +package de.steamwar.bausystem.features.tracer2; + +import de.steamwar.entity.REntityServer; +import de.steamwar.entity.RFallingBlockEntity; +import lombok.Getter; +import org.bukkit.Location; +import org.bukkit.Material; + +import java.util.List; + +public class TraceEntity extends RFallingBlockEntity { + + /** + * The records represented by this REntity + */ + @Getter + private final List records; + + public TraceEntity(REntityServer server, Location location, boolean isExplosion, List records) { + super(server, location, isExplosion ? Material.RED_STAINED_GLASS : Material.TNT); + 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 5d77ebba..1df3bdba 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/tracer2/TraceManager.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/tracer2/TraceManager.java @@ -22,11 +22,9 @@ 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.Location; 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; @@ -43,8 +41,6 @@ public class TraceManager implements Listener { */ private int currOpenId = 0; - - /** Adds a new trace to the global record * * @param trace Trace to be added @@ -70,7 +66,7 @@ public class TraceManager implements Listener { */ public void clear(){ traces.clear(); - currOpenId = 1; + currOpenId = 0; } /** Methode to get all traces in a certain region @@ -101,4 +97,12 @@ public class TraceManager implements Listener { public Set getAll(){ return new HashSet<>(traces.values()); } + + /** Renders the given trace records + * + * @param player The player the trace is rendered to + * @param trace the trace to render + * @param flags Flags modefieing the rendering + */ + } diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/tracer2/ViewFlag.java b/BauSystem_Main/src/de/steamwar/bausystem/features/tracer2/ViewFlag.java new file mode 100644 index 00000000..efc1979d --- /dev/null +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/tracer2/ViewFlag.java @@ -0,0 +1,53 @@ +/* + * This file is a part of the SteamWar software. + * + * Copyright (C) 2023 SteamWar.de-Serverteam + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +package de.steamwar.bausystem.features.tracer2; + +import de.steamwar.entity.REntityServer; + +import java.util.ArrayList; +import java.util.List; +import java.util.function.BiConsumer; +import java.util.function.Consumer; +import java.util.function.UnaryOperator; + +public enum ViewFlag { + EXPLOSION((List records) ->{ + List ret = new ArrayList<>(); + for(TNTRecord record : records) + if(record.isExplosion()) + ret.add(record); + return ret; + }, (REntityServer server , List entities) -> {}); + + /** + * The filtering part of the flag + */ + public final UnaryOperator> filter; + + /** + * The modifieng part of the flag + */ + public final BiConsumer> modify; + + ViewFlag(UnaryOperator> filter, BiConsumer> modify) { + this.filter = filter; + this.modify = modify; + } +}