SteamWar/BauSystem2.0
Archiviert
12
0

Add ViewFlagHolder
Einige Prüfungen sind fehlgeschlagen
SteamWarCI Build failed

Dieser Commit ist enthalten in:
yoyosource 2024-04-07 17:15:54 +02:00
Ursprung 3f9f3341a3
Commit 7cd61fd9a4
2 geänderte Dateien mit 99 neuen und 65 gelöschten Zeilen

Datei anzeigen

@ -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<Player, ViewFlag[]> viewFlagMap = new HashMap<>();
private final Map<Player, ViewFlagHolder> 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);
}
}
@ -137,10 +137,10 @@ public class Trace {
* Renders this traces
*
* @param player The player the trace is rendered to
* @param flags Flags modefieing the rendering
* @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,14 +150,14 @@ 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);
}
/**
@ -165,28 +165,14 @@ public class Trace {
*
* @param server Server to render to
* @param records Records to render
* @param flags Flags modefieing the rendering
* @param viewFlagHolder Flags modefieing the rendering
* @param bundleFilter Filter to determin bundeling of records
*/
private void render(REntityServer server, List<TNTRecord> records, ViewFlag[] flags, BundleFilter bundleFilter) {
private void render(REntityServer server, List<TNTRecord> records, ViewFlagHolder viewFlagHolder, BundleFilter bundleFilter) {
if (records.isEmpty()) return;
List<TNTRecord> workingRecords = records;
//Manage flags and required flags
Set<ViewFlag> 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<ViewFlag> 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);

Datei anzeigen

@ -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 <https://www.gnu.org/licenses/>.
*/
package de.steamwar.bausystem.features.tracer.rendering;
import java.util.*;
public class ViewFlagHolder {
private Map<Class<? extends ViewFlag>, ViewFlag> viewFlags = new HashMap<>();
public ViewFlagHolder(ViewFlag... viewFlags) {
for (ViewFlag viewFlag : viewFlags) {
this.viewFlags.put(viewFlag.getClass(), viewFlag);
}
}
public Set<ViewFlag> getEffectiveViewFlags() {
//Manage flags and required flags
Set<ViewFlag> 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<? extends ViewFlag> clazz) {
return viewFlags.containsKey(clazz);
}
public <T extends ViewFlag> T getViewFlag(Class<T> clazz) {
return (T) viewFlags.get(clazz);
}
public <T extends ViewFlag> T getViewFlagOrDefault(Class<T> clazz, T defaultValue) {
return (T) viewFlags.getOrDefault(clazz, defaultValue);
}
public void addViewFlag(ViewFlag viewFlag) {
viewFlags.put(viewFlag.getClass(), viewFlag);
}
}