From 7cd61fd9a4e1c6609488bc0098f19c979f4f934c Mon Sep 17 00:00:00 2001 From: yoyosource Date: Sun, 7 Apr 2024 17:15:54 +0200 Subject: [PATCH] Add ViewFlagHolder --- .../bausystem/features/tracer/Trace.java | 95 ++++++------------- .../tracer/rendering/ViewFlagHolder.java | 69 ++++++++++++++ 2 files changed, 99 insertions(+), 65 deletions(-) create mode 100644 BauSystem_Main/src/de/steamwar/bausystem/features/tracer/rendering/ViewFlagHolder.java diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/tracer/Trace.java b/BauSystem_Main/src/de/steamwar/bausystem/features/tracer/Trace.java index 769f7cc0..41ed8a13 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/tracer/Trace.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/tracer/Trace.java @@ -23,6 +23,7 @@ import de.steamwar.bausystem.features.tpslimit.TPSUtils; import de.steamwar.bausystem.features.tracer.rendering.BundleFilter; import de.steamwar.bausystem.features.tracer.rendering.TraceEntity; import de.steamwar.bausystem.features.tracer.rendering.ViewFlag; +import de.steamwar.bausystem.features.tracer.rendering.ViewFlagHolder; import de.steamwar.bausystem.features.tracer.rendering.dynamicflags.AtFlag; import de.steamwar.bausystem.features.tracer.rendering.dynamicflags.IsolateFlag; import de.steamwar.bausystem.region.Region; @@ -31,7 +32,6 @@ import lombok.Getter; import org.bukkit.entity.Player; import java.util.*; -import java.util.stream.Stream; public class Trace { /** @@ -77,7 +77,7 @@ public class Trace { /** * A map saving the view flags for rendered traces, for life rendering */ - private final Map viewFlagMap = new HashMap<>(); + private final Map viewFlagMap = new HashMap<>(); public Trace(Region region) { this.region = region; @@ -103,9 +103,9 @@ public class Trace { for (Player player : serverMap.keySet()) { REntityServer server = serverMap.get(player); BundleFilter bundleFilter = bundleFilterMap.get(player); - ViewFlag[] viewFlags = viewFlagMap.get(player); + ViewFlagHolder viewFlagHolder = viewFlagMap.get(player); - render(server, records, viewFlags, bundleFilter); + render(server, records, viewFlagHolder, bundleFilter); } } @@ -136,11 +136,11 @@ public class Trace { /** * Renders this traces * - * @param player The player the trace is rendered to - * @param flags Flags modefieing the rendering - * @param bundleFilter Filter to determin bundeling of records + * @param player The player the trace is rendered to + * @param viewFlagHolder Flags modefieing the rendering + * @param bundleFilter Filter to determin bundeling of records */ - public void render(Player player, ViewFlag[] flags, BundleFilter bundleFilter) { + public void render(Player player, ViewFlagHolder viewFlagHolder, BundleFilter bundleFilter) { if (serverMap.containsKey(player)) { REntityServer server = serverMap.get(player); server.close(); @@ -150,43 +150,29 @@ public class Trace { bundleFilterMap.remove(player); } - viewFlagMap.put(player, flags); + viewFlagMap.put(player, viewFlagHolder); bundleFilterMap.put(player, bundleFilter); REntityServer server = new REntityServer(); server.addPlayer(player); serverMap.put(player, server); - render(server, records, flags, bundleFilter); + render(server, records, viewFlagHolder, bundleFilter); } /** * Internal methode to render records to a REntityServer * - * @param server Server to render to - * @param records Records to render - * @param flags Flags modefieing the rendering - * @param bundleFilter Filter to determin bundeling of records + * @param server Server to render to + * @param records Records to render + * @param viewFlagHolder Flags modefieing the rendering + * @param bundleFilter Filter to determin bundeling of records */ - private void render(REntityServer server, List records, ViewFlag[] flags, BundleFilter bundleFilter) { + private void render(REntityServer server, List records, ViewFlagHolder viewFlagHolder, BundleFilter bundleFilter) { if (records.isEmpty()) return; List workingRecords = records; - - //Manage flags and required flags - Set flagList = new HashSet<>(Arrays.asList(flags)); - for (ViewFlag flag : flags) { - if (flag.required != null) { - flagList.addAll(Arrays.asList(flag.required)); - } - } - - //Manage inverse flags - ViewFlag.inverseFlags.forEach(viewFlag -> { - if (!flagList.remove(viewFlag)) { - flagList.add(viewFlag); - } - }); + Set flagList = viewFlagHolder.getEffectiveViewFlags(); //Apply filters for (ViewFlag flag : flagList) { @@ -204,7 +190,7 @@ public class Trace { } //Apply modifiers - for (ViewFlag flag : flags) { + for (ViewFlag flag : flagList) { flag.modify(server, entities); } @@ -262,24 +248,12 @@ public class Trace { * @param to end of time interval */ public void renderAt(Player player, int from, int to) { - ViewFlag[] viewFlags = viewFlagMap.get(player); - if (viewFlags == null) return; - - AtFlag atFlag = null; - atFlag = Stream.of(viewFlags) - .filter(AtFlag.class::isInstance) - .map(AtFlag.class::cast) - .findFirst() - .orElse(null); - - if (atFlag != null) { - atFlag.update(from, to); + ViewFlagHolder viewFlags = viewFlagMap.computeIfAbsent(player, p -> new ViewFlagHolder()); + if (viewFlags.hasViewFlag(AtFlag.class)) { + viewFlags.getViewFlag(AtFlag.class).update(from, to); } else { - atFlag = new AtFlag(from, to); - viewFlags = Arrays.copyOf(viewFlags, viewFlags.length + 1); - viewFlags[viewFlags.length - 1] = atFlag; + viewFlags.addViewFlag(new AtFlag(from, to)); } - render(player, viewFlags, BundleFilter.STRICT); } @@ -290,27 +264,18 @@ public class Trace { * @param records the record for which isolation is toggled */ public void isolate(Player player, TNTRecord... records) { - IsolateFlag isolationFlag = null; - - ViewFlag[] viewFlags = viewFlagMap.get(player); - if (viewFlags != null) { - isolationFlag = Stream.of(viewFlags) - .filter(IsolateFlag.class::isInstance) - .map(IsolateFlag.class::cast) - .findFirst() - .orElse(null); + IsolateFlag isolateFlag; + ViewFlagHolder viewFlags = viewFlagMap.get(player); + if (viewFlags != null && viewFlags.hasViewFlag(IsolateFlag.class)) { + isolateFlag = viewFlags.getViewFlag(IsolateFlag.class); + } else { + viewFlags = new ViewFlagHolder(); + isolateFlag = new IsolateFlag(); + viewFlags.addViewFlag(isolateFlag); } - if (viewFlags == null || isolationFlag == null) { - viewFlags = new ViewFlag[1]; - } - if (isolationFlag == null) { - isolationFlag = new IsolateFlag(); - } - viewFlags[0] = isolationFlag; - for (TNTRecord record : records) { - isolationFlag.toggleId(record.getTntId()); + isolateFlag.toggleId(record.getTntId()); } render(player, viewFlags, BundleFilter.STRICT); diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/tracer/rendering/ViewFlagHolder.java b/BauSystem_Main/src/de/steamwar/bausystem/features/tracer/rendering/ViewFlagHolder.java new file mode 100644 index 00000000..9ad156cf --- /dev/null +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/tracer/rendering/ViewFlagHolder.java @@ -0,0 +1,69 @@ +/* + * This file is a part of the SteamWar software. + * + * Copyright (C) 2024 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.tracer.rendering; + +import java.util.*; + +public class ViewFlagHolder { + + private Map, ViewFlag> viewFlags = new HashMap<>(); + + public ViewFlagHolder(ViewFlag... viewFlags) { + for (ViewFlag viewFlag : viewFlags) { + this.viewFlags.put(viewFlag.getClass(), viewFlag); + } + } + + public Set getEffectiveViewFlags() { + //Manage flags and required flags + Set flagList = new HashSet<>(); + for (ViewFlag flag : viewFlags.values()) { + flagList.add(flag); + if (flag.required != null) { + flagList.addAll(Arrays.asList(flag.required)); + } + } + + //Manage inverse flags + ViewFlag.inverseFlags.forEach(viewFlag -> { + if (!flagList.remove(viewFlag)) { + flagList.add(viewFlag); + } + }); + + return flagList; + } + + public boolean hasViewFlag(Class clazz) { + return viewFlags.containsKey(clazz); + } + + public T getViewFlag(Class clazz) { + return (T) viewFlags.get(clazz); + } + + public T getViewFlagOrDefault(Class clazz, T defaultValue) { + return (T) viewFlags.getOrDefault(clazz, defaultValue); + } + + public void addViewFlag(ViewFlag viewFlag) { + viewFlags.put(viewFlag.getClass(), viewFlag); + } +}