SteamWar/BauSystem2.0
Archiviert
12
0

Trace Refactor #233

Zusammengeführt
YoyoNow hat 121 Commits von TracerGUI nach master 2024-04-21 16:03:26 +02:00 zusammengeführt
6 geänderte Dateien mit 200 neuen und 20 gelöschten Zeilen
Nur Änderungen aus Commit 2bf3cfbb1d werden angezeigt - Alle Commits anzeigen

Datei anzeigen

@ -51,11 +51,6 @@ public class BauSystem extends JavaPlugin implements Listener {
// This should be treated as final! // This should be treated as final!
public static Message MESSAGE; public static Message MESSAGE;
/*
* Plugin wide availeble data
*/
public static final TraceManager TRACES = new TraceManager();
@Getter @Getter
private static BauSystem instance; private static BauSystem instance;

Datei anzeigen

@ -23,6 +23,7 @@ import de.steamwar.bausystem.BauSystem;
import de.steamwar.bausystem.features.tpslimit.TPSUtils; 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 de.steamwar.linkage.LinkedInstance;
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;
@ -35,6 +36,13 @@ import java.util.*;
@Linked @Linked
public class Recorder implements Listener { public class Recorder implements Listener {
/**
* Linked instance of TraceManager
*/
@LinkedInstance
TraceManager manager;
/** /**
* Map for all traces beeing activly recorded * Map for all traces beeing activly recorded
*/ */
@ -68,7 +76,7 @@ public class Recorder implements Listener {
public void startRecording(Region region){ public void startRecording(Region region){
Trace trace = new Trace(region); Trace trace = new Trace(region);
activeTraces.put(region, trace); activeTraces.put(region, trace);
BauSystem.TRACES.add(trace); manager.add(trace);
} }
/** Stops the recording at the given region /** Stops the recording at the given region
@ -76,9 +84,16 @@ public class Recorder implements Listener {
* @param region region to stop recording * @param region region to stop recording
*/ */
public void stopRecording(Region region){ public void stopRecording(Region region){
Trace trace = activeTraces.get(region);
trace.setRecords(Collections.unmodifiableList(trace.getRecords()));
activeTraces.remove(region); 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<>()); trackedTNT.put(region, new ArrayList<>());
} }
/** Internal methode to record all tracked TNT Entities /** Internal methode to record all tracked TNT Entities
@ -102,7 +117,9 @@ public class Recorder implements Listener {
TNTRecord lastRecord = lastRecordMap.getOrDefault(tntPrimed, null); TNTRecord lastRecord = lastRecordMap.getOrDefault(tntPrimed, null);
TNTRecord record = new TNTRecord(tntPrimed, tntPrimed.getFuseTicks() == 0, TPSUtils.currentTick.get() - trace.getStartTime(), lastRecord); 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); lastRecordMap.put(tntPrimed, record);
trace.add(record); trace.add(record);
} }
@ -114,10 +131,12 @@ public class Recorder implements Listener {
@EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true) @EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true)
public void onTNTSpawn(EntitySpawnEvent event){ public void onTNTSpawn(EntitySpawnEvent event){
if(!(event.getEntity() instanceof TNTPrimed)) return; if(!(event.getEntity() instanceof TNTPrimed)) return;
Region region = Region.getRegion(event.getLocation()); Region region = Region.getRegion(event.getLocation());
if(activeTraces.containsKey(region)){ if(activeTraces.containsKey(region)){
//Check whether set for tracking already exists. Creating it if necessary //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()); trackedTNT.get(region).add((TNTPrimed) event.getEntity());
tntSpawnRegion.put((TNTPrimed) event.getEntity(), region); tntSpawnRegion.put((TNTPrimed) event.getEntity(), region);
@ -131,6 +150,7 @@ public class Recorder implements Listener {
@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; if(!(event.getEntity() instanceof TNTPrimed)) return;
Region region = tntSpawnRegion.getOrDefault((TNTPrimed) event.getEntity(), null); Region region = tntSpawnRegion.getOrDefault((TNTPrimed) event.getEntity(), null);
if(region == null) return; if(region == null) return;
trackedTNT.get(region).remove((TNTPrimed) event.getEntity()); trackedTNT.get(region).remove((TNTPrimed) event.getEntity());

Datei anzeigen

@ -21,12 +21,12 @@ package de.steamwar.bausystem.features.tracer2;
import de.steamwar.bausystem.features.tpslimit.TPSUtils; import de.steamwar.bausystem.features.tpslimit.TPSUtils;
import de.steamwar.bausystem.region.Region; import de.steamwar.bausystem.region.Region;
import de.steamwar.entity.REntityServer;
import lombok.Getter; import lombok.Getter;
import org.bukkit.Location;
import org.bukkit.entity.Player;
import java.util.ArrayList; import java.util.*;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class Trace { public class Trace {
/** /**
@ -44,7 +44,13 @@ public class Trace {
/** /**
* Records of TNTs, making up the trace * Records of TNTs, making up the trace
*/ */
private final List<TNTRecord> records = new ArrayList<>(); @Getter
private List<TNTRecord> records = new ArrayList<>();
/**
* A map of players -> REntityServers for rendering traces to a player
*/
private Map<Player, REntityServer> serverMap = new HashMap<>();
public Trace (Region region){ public Trace (Region region){
this.region = region; this.region = region;
@ -57,4 +63,64 @@ public class Trace {
protected void add (TNTRecord record){ protected void add (TNTRecord record){
records.add(record); records.add(record);
} }
/** Internal methode to make records immutable after recording is finished
*
* @param records immutable records list
*/
protected void setRecords(List<TNTRecord> 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<ViewFlag> flags){
List<TNTRecord> workingRecords = records;
//Apply filters
for(ViewFlag flag : flags)
workingRecords = flag.filter.apply(workingRecords);
//Bundle records at unique positions
List<List<TNTRecord>> bundles = bundleRecords(workingRecords);
//Render bundled records
REntityServer server = new REntityServer();
serverMap.put(player, server);
List<TraceEntity> entities = new LinkedList<>();
for(List<TNTRecord> 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<List<TNTRecord>> bundleRecords(List<TNTRecord> records){
Map <Location, List<TNTRecord>> entitiesRecords = new HashMap<>();
for(TNTRecord record : records) {
if(entitiesRecords.containsKey(record.getLocation()))
entitiesRecords.get(record.getLocation());
else{
List <TNTRecord> entityRecords = new ArrayList<>();
entityRecords.add(record);
entitiesRecords.put(record.getLocation(), entityRecords);
}
}
return new LinkedList<>(entitiesRecords.values());
}
} }

Datei anzeigen

@ -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 <https://www.gnu.org/licenses/>.
*/
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<TNTRecord> records;
public TraceEntity(REntityServer server, Location location, boolean isExplosion, List<TNTRecord> records) {
super(server, location, isExplosion ? Material.RED_STAINED_GLASS : Material.TNT);
this.records = records;
}
}

Datei anzeigen

@ -22,11 +22,9 @@ package de.steamwar.bausystem.features.tracer2;
import de.steamwar.bausystem.region.Region; import de.steamwar.bausystem.region.Region;
import de.steamwar.entity.REntityServer; import de.steamwar.entity.REntityServer;
import de.steamwar.linkage.Linked; import de.steamwar.linkage.Linked;
import org.bukkit.Location;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import java.util.*; import java.util.*;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@ -43,8 +41,6 @@ public class TraceManager implements Listener {
*/ */
private int currOpenId = 0; private int currOpenId = 0;
/** Adds a new trace to the global record /** Adds a new trace to the global record
* *
* @param trace Trace to be added * @param trace Trace to be added
@ -70,7 +66,7 @@ public class TraceManager implements Listener {
*/ */
public void clear(){ public void clear(){
traces.clear(); traces.clear();
currOpenId = 1; currOpenId = 0;
} }
/** Methode to get all traces in a certain region /** Methode to get all traces in a certain region
@ -101,4 +97,12 @@ public class TraceManager implements Listener {
public Set<Trace> getAll(){ public Set<Trace> getAll(){
return new HashSet<>(traces.values()); 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
*/
} }

Datei anzeigen

@ -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 <https://www.gnu.org/licenses/>.
*/
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<TNTRecord> records) ->{
List<TNTRecord> ret = new ArrayList<>();
for(TNTRecord record : records)
if(record.isExplosion())
ret.add(record);
return ret;
}, (REntityServer server , List<TraceEntity> entities) -> {});
/**
* The filtering part of the flag
*/
public final UnaryOperator<List<TNTRecord>> filter;
/**
* The modifieng part of the flag
*/
public final BiConsumer<REntityServer, List<TraceEntity>> modify;
ViewFlag(UnaryOperator<List<TNTRecord>> filter, BiConsumer<REntityServer,List<TraceEntity>> modify) {
this.filter = filter;
this.modify = modify;
}
}