SteamWar/BauSystem
Archiviert
13
0

Untangle spagetti code part 1

Dieser Commit ist enthalten in:
Lixfel 2020-08-30 07:45:32 +02:00
Ursprung 1f2b6a0290
Commit 6831ada70f
12 geänderte Dateien mit 117 neuen und 218 gelöschten Zeilen

Datei anzeigen

@ -2,7 +2,6 @@ package de.steamwar.bausystem.tracer;
import de.steamwar.bausystem.BauSystem;
import de.steamwar.bausystem.tracer.manager.ShowManager;
import de.steamwar.bausystem.tracer.trace.TNTTrace;
import de.steamwar.core.Core;
import net.md_5.bungee.api.ChatMessageType;
import net.md_5.bungee.api.chat.TextComponent;
@ -11,8 +10,6 @@ import org.bukkit.Material;
import org.bukkit.Particle;
import org.bukkit.entity.Player;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import static de.steamwar.bausystem.tracer.TraceUtils.DEBUG;
@ -23,55 +20,6 @@ public class TNTTracer {
private static final Object synchronizer = new Object();
private static final TraceCache traceCache = new TraceCache();
private static final TracerUpdater tracerUpdater = new TracerUpdater() {
@Override
public Set<TraceCache.Loc> updateLocations(Player player) {
Iterator<TNTTrace> traces = ShowManager.get(player).getTraces().descendingIterator();
Set<TraceCache.Loc> locSet = new HashSet<>();
while (traces.hasNext()) {
locSet.addAll(traces.next().locs());
}
double radius = ShowManager.get(player).getShowRadius();
locSet.removeIf(loc -> loc.remove(player, radius));
return locSet;
}
@Override
public Set<TraceCache.Loc> updatePoints(Player player) {
Iterator<TNTTrace> traces = ShowManager.get(player).getTraces().descendingIterator();
Set<TraceCache.Loc> locSet = new HashSet<>();
while (traces.hasNext()) {
locSet.addAll(traces.next().locsUpdate(ShowManager.get(player).getDisplayMode()));
}
double radius = ShowManager.get(player).getShowRadius();
locSet.removeIf(loc -> loc.remove(player, radius));
return locSet;
}
@Override
public boolean needsUpdate(Player player) {
return ShowManager.get(player).isDirty();
}
@Override
public DisplayMode getDisplayType(Player player) {
switch (ShowManager.get(player).getDisplayType()) {
case Block:
return DisplayMode.Block;
case Particle:
return DisplayMode.Particle;
default:
break;
}
return DisplayMode.Particle;
}
@Override
public String actionBar(Player player) {
return TraceActionbar.traceMemory() + " " + TraceActionbar.traceSize() + " " + TraceActionbar.showRadius(player);
}
};
public static void init(){
Bukkit.getScheduler().runTaskTimer(BauSystem.getPlugin(), () -> {
synchronized (synchronizer) {
@ -95,19 +43,19 @@ public class TNTTracer {
private static void run(){
for (Player p : Bukkit.getOnlinePlayers()) {
if (DEBUG) {
String actionBar = tracerUpdater.actionBar(p);
String actionBar = TracerUpdater.actionBar(p);
p.spigot().sendMessage(ChatMessageType.ACTION_BAR, TextComponent.fromLegacyText(actionBar));
}
boolean dirty = tracerUpdater.needsUpdate(p);
if (tracerUpdater.getDisplayType(p) == TracerUpdater.DisplayMode.Block && !dirty) {
boolean dirty = TracerUpdater.needsUpdate(p);
if (TracerUpdater.getDisplayType(p) == ShowManager.DisplayType.Block && !dirty) {
continue;
}
Set<TraceCache.Loc> toHide = traceCache.update(p, tracerUpdater, dirty);
Set<TraceCache.Loc> toHide = traceCache.update(p, dirty);
Set<TraceCache.Loc> toShow = traceCache.get(p);
hideBlockTraces(toHide, p);
showTraces(toShow, tracerUpdater.getDisplayType(p), p);
showTraces(toShow, TracerUpdater.getDisplayType(p), p);
}
}
@ -119,11 +67,11 @@ public class TNTTracer {
}
}
private static void showTraces(Set<TraceCache.Loc> locs, TracerUpdater.DisplayMode displayMode, Player player) {
private static void showTraces(Set<TraceCache.Loc> locs, ShowManager.DisplayType displayMode, Player player) {
if (locs.isEmpty()) return;
for (TraceCache.Loc l : locs) {
if (displayMode == TracerUpdater.DisplayMode.Block) {
if (displayMode == ShowManager.DisplayType.Block) {
TraceUtils.showBlock(player, l.x, l.y - 0.49F, l.z, getMaterial(l), (l.updatePoint ? (byte) 5 : (byte) 14));
} else {
TraceUtils.showCorner(player, l.x - 0.49F, l.y, l.z - 0.49F, (l.updatePoint ? Particle.FLAME : Particle.VILLAGER_HAPPY));

Datei anzeigen

@ -1,53 +0,0 @@
package de.steamwar.bausystem.tracer;
import de.steamwar.bausystem.tracer.manager.FrameManager;
import de.steamwar.bausystem.tracer.manager.ShowManager;
import de.steamwar.bausystem.tracer.manager.TraceManager;
import org.bukkit.entity.Player;
import java.util.Iterator;
import java.util.LinkedList;
public class TraceActionbar {
private TraceActionbar(){}
private static double round(double toRound, int digits) {
int x = (int)Math.pow(10, digits);
double r = (toRound * x);
double t = r - (int) r;
if (t >= 0.5) {
return (((double)(int)r) + 1) / x;
} else {
return (((double)(int)r) + 0) / x;
}
}
public static String traceMemory() {
try {
long bitFrames = FrameManager.get().size() * 32L;
LinkedList<Integer> traceIDs = TraceManager.get();
long bitTraces = traceIDs.size() * 32L;
long bitTracesIntern = 0;
Iterator<Integer> iterator = traceIDs.descendingIterator();
while (iterator.hasNext()) {
bitTracesIntern += TraceManager.get(iterator.next()).realLength() * 32L;
}
double mByte = (bitFrames + bitTraces + bitTracesIntern) / 8.0 / 1024;
return "§e" + round(mByte, 4) + " §cKB";
} catch (Exception e) {
}
return "§e??? §cKB";
}
public static String traceSize() {
return "§e" + TraceManager.get().size() + " §cTraces";
}
public static String showRadius(Player p) {
return "§e" + ShowManager.get(p).getShowRadius() + " §cRadius";
}
}

Datei anzeigen

@ -1,61 +1,62 @@
package de.steamwar.bausystem.tracer;
import de.steamwar.bausystem.tracer.manager.ShowManager;
import org.bukkit.entity.Player;
import java.util.*;
public class TraceCache {
private static final Set<Loc> empty = new HashSet();
private static final Set<Loc> empty = new HashSet<>();
private Map<String, Set<Loc>> playerMap = new HashMap<>();
private Map<String, TracerUpdater.DisplayMode> playerDisplayMap = new HashMap<>();
private Map<String, ShowManager.DisplayType> playerDisplayMap = new HashMap<>();
public Set<Loc> get(Player player) {
return playerMap.getOrDefault(player.getUniqueId().toString(), empty);
}
public Set<Loc> update(Player player, TracerUpdater tracerUpdater, boolean dirty) {
Set<Loc> update(Player player, boolean dirty) {
if (!dirty) return empty;
String key = player.getUniqueId().toString();
Set<Loc> locOld;
TracerUpdater.DisplayMode displayMode;
ShowManager.DisplayType displayMode;
if (!playerMap.containsKey(key)) {
locOld = new HashSet<>();
displayMode = tracerUpdater.getDisplayType(player);
displayMode = TracerUpdater.getDisplayType(player);
} else {
locOld = playerMap.get(key);
displayMode = playerDisplayMap.get(key);
}
Set<Loc> locSet = new HashSet<>();
tracerUpdater.updatePoints(player).forEach(loc -> {
TracerUpdater.updatePoints(player).forEach(loc -> {
loc.updatePoint = true;
locSet.add(loc);
});
tracerUpdater.updateLocations(player).forEach(loc -> {
TracerUpdater.updateLocations(player).forEach(loc -> {
loc.updatePoint = false;
locSet.add(loc);
});
playerMap.put(key, locSet);
TracerUpdater.DisplayMode currentMode = tracerUpdater.getDisplayType(player);
ShowManager.DisplayType currentMode = TracerUpdater.getDisplayType(player);
playerDisplayMap.put(key, currentMode);
if (currentMode == TracerUpdater.DisplayMode.Particle && displayMode == TracerUpdater.DisplayMode.Block) return locOld;
if (currentMode == TracerUpdater.DisplayMode.Block) return diff(locOld, locSet);
if (currentMode == ShowManager.DisplayType.Particle && displayMode == ShowManager.DisplayType.Block) return locOld;
if (currentMode == ShowManager.DisplayType.Block) return diff(locOld, locSet);
return empty;
}
public static class Loc {
public final float x;
public final float y;
public final float z;
final float x;
final float y;
final float z;
private final float dx;
private final float dy;
private final float dz;
public boolean updatePoint = false;
boolean updatePoint = false;
public Loc(float x, float y, float z) {
this.x = x;
@ -66,8 +67,8 @@ public class TraceCache {
this.dz = round(z);
}
private static int roundNumber = 10;
private static float round(float toRound) {
final int roundNumber = 10;
float r = (toRound * roundNumber);
float t = r - (int) r;
if (t >= 0.5) {

Datei anzeigen

@ -1,8 +1,9 @@
package de.steamwar.bausystem.tracer;
import de.steamwar.bausystem.tracer.manager.ShowManager;
import de.steamwar.bausystem.tracer.manager.TNTManager;
import de.steamwar.bausystem.tracer.manager.TraceManager;
import de.steamwar.bausystem.tracer.recorder.RecordManager;
import de.steamwar.bausystem.tracer.trace.TNTTrace;
import org.bukkit.entity.Player;
import org.bukkit.entity.TNTPrimed;
import org.bukkit.event.EventHandler;
@ -10,18 +11,41 @@ import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityExplodeEvent;
import org.bukkit.event.player.PlayerMoveEvent;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Stream;
public class TraceListener implements Listener {
private static final Map<TNTPrimed, TNTTrace> tntMap = new HashMap<>();
public static void add(Stream<TNTPrimed> tntPrimedStream, int frameID) {
tntPrimedStream.forEach(tnt -> {
TNTTrace trace;
if (!tntMap.containsKey(tnt)) {
trace = new TNTTrace(frameID);
TraceManager.add(trace);
tntMap.put(tnt, trace);
} else {
trace = tntMap.get(tnt);
}
trace.addLocation(tnt.getLocation());
});
}
@EventHandler
public void onEntityExplode(EntityExplodeEvent event) {
if (!(event.getEntity() instanceof TNTPrimed)) return;
if (!(event.getEntity() instanceof TNTPrimed))
return;
if (RecordManager.getStatus() == RecordManager.Status.IDLE_AUTO)
RecordManager.startAuto();
if (RecordManager.getStatus() == RecordManager.Status.RECORD_AUTO)
RecordManager.updateAuto();
TNTManager.explode((TNTPrimed) event.getEntity());
TNTTrace trace = tntMap.get((TNTPrimed) event.getEntity());
if (trace != null)
trace.cleanUp();
}

Datei anzeigen

@ -1,21 +1,48 @@
package de.steamwar.bausystem.tracer;
import de.steamwar.bausystem.tracer.manager.ShowManager;
import de.steamwar.bausystem.tracer.manager.TraceManager;
import de.steamwar.bausystem.tracer.trace.TNTTrace;
import org.bukkit.entity.Player;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
public interface TracerUpdater {
class TracerUpdater {
private TracerUpdater(){}
enum DisplayMode {
Block,
Particle,
static Set<TraceCache.Loc> updateLocations(Player player) {
Iterator<TNTTrace> traces = ShowManager.get(player).getTraces().descendingIterator();
Set<TraceCache.Loc> locSet = new HashSet<>();
while (traces.hasNext()) {
locSet.addAll(traces.next().locs());
}
double radius = ShowManager.get(player).getShowRadius();
locSet.removeIf(loc -> loc.remove(player, radius));
return locSet;
}
Set<TraceCache.Loc> updateLocations(Player player);
Set<TraceCache.Loc> updatePoints(Player player);
boolean needsUpdate(Player player);
DisplayMode getDisplayType(Player player);
String actionBar(Player player);
static Set<TraceCache.Loc> updatePoints(Player player) {
Iterator<TNTTrace> traces = ShowManager.get(player).getTraces().descendingIterator();
Set<TraceCache.Loc> locSet = new HashSet<>();
while (traces.hasNext()) {
locSet.addAll(traces.next().locsUpdate(ShowManager.get(player).getDisplayMode()));
}
double radius = ShowManager.get(player).getShowRadius();
locSet.removeIf(loc -> loc.remove(player, radius));
return locSet;
}
static boolean needsUpdate(Player player) {
return ShowManager.get(player).isDirty();
}
static ShowManager.DisplayType getDisplayType(Player player) {
return ShowManager.get(player).getDisplayType();
}
static String actionBar(Player player) {
return "§e" + TraceManager.get().size() + " §cTraces §e" + ShowManager.get(player).getShowRadius() + " §cRadius";
}
}

Datei anzeigen

@ -45,7 +45,7 @@ public class FrameManager {
if (!frameMap.get(frameID).isEmpty()) {
return;
}
frameMap.remove((Integer) frameID);
frameMap.remove(frameID);
}
}

Datei anzeigen

@ -4,7 +4,7 @@ public class IDManager {
private static int currentID = 0;
public static synchronized int generateID() {
static synchronized int generateID() {
return currentID++;
}

Datei anzeigen

@ -179,16 +179,16 @@ public class ShowManager {
selected.clear();
}
public void show() {
void show() {
showSelection = ShowSelection.ALL;
}
public void hide() {
void hide() {
clear();
showSelection = ShowSelection.NONE;
}
public void addSelection(int id) {
void addSelection(int id) {
if (showSelection == ShowSelection.ALL) return;
if (showSelection == ShowSelection.NONE) showSelection = ShowSelection.SELECTIVE;
selected.addAll(FrameManager.get(id));
@ -198,7 +198,7 @@ public class ShowManager {
}
}
public void removeSelection(int id) {
void removeSelection(int id) {
if (showSelection == ShowSelection.NONE) return;
if (showSelection == ShowSelection.ALL) selected = TraceManager.get();
selected.removeAll(FrameManager.get(id));
@ -206,7 +206,7 @@ public class ShowManager {
if (selected.isEmpty()) showSelection = ShowSelection.NONE;
}
public boolean toggleSelection(int id) {
boolean toggleSelection(int id) {
if (showSelection == ShowSelection.NONE) {
addSelection(id);
return true;
@ -266,10 +266,6 @@ public class ShowManager {
return false;
}
private static double maxRadius = 80.0;
private static double minRadius = 20.0;
private static double minSlope = 3.0;
private static double maxSlope = 11.0;
private double slopeHeight = 7.0;
private int size = 0;
@ -277,17 +273,21 @@ public class ShowManager {
private TraceCache.Loc loc = null;
public double getShowRadius() {
double maxRadius = 80.0;
if (showSelection == ShowSelection.NONE) return maxRadius;
if (showSelection == ShowSelection.ALL) size = TraceManager.get().size();
if (showSelection == ShowSelection.SELECTIVE) size = selected.size();
if (size == 0) return maxRadius;
double minRadius = 20.0;
if (size >= 950) return minRadius;
double slope = -(size / slopeHeight) + 85;
return Math.min(Math.max(slope, minRadius), maxRadius);
}
public void setSlope(double slope) {
void setSlope(double slope) {
double minSlope = 3.0;
double maxSlope = 11.0;
slopeHeight = Math.min(Math.max(slope, minSlope), maxSlope);
}
@ -321,13 +321,13 @@ public class ShowManager {
}
}
public static void traceRemove(int id) {
static void traceRemove(int id) {
for (Map.Entry<String, ShowStatus> entry : showMap.entrySet()) {
entry.getValue().removeSelection(id);
}
}
public static void globalDirty() {
static void globalDirty() {
for (Map.Entry<String, ShowStatus> entry : showMap.entrySet()) {
entry.getValue().dirty = true;
}

Datei anzeigen

@ -1,33 +0,0 @@
package de.steamwar.bausystem.tracer.manager;
import de.steamwar.bausystem.tracer.trace.TNTTrace;
import org.bukkit.entity.TNTPrimed;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Stream;
public class TNTManager {
private static Map<TNTPrimed, Integer> tntMap = new HashMap<>();
public static void explode(TNTPrimed tntPrimed) {
if (!tntMap.containsKey(tntPrimed)) return;
TraceManager.get(tntMap.get(tntPrimed)).cleanUp();
}
public static void add(Stream<TNTPrimed> tntPrimedStream, int frameID) {
tntPrimedStream.forEach(tnt -> {
TNTTrace trace;
if (!tntMap.containsKey(tnt)) {
trace = new TNTTrace(frameID);
TraceManager.add(trace);
tntMap.put(tnt, trace.getTntID());
} else {
trace = TraceManager.get(tntMap.get(tnt));
}
trace.addLocation(tnt.getLocation());
});
}
}

Datei anzeigen

@ -17,14 +17,14 @@ public class TraceManager {
traceMap.put(id, tntTrace);
}
public static void remove() {
private static void remove() {
Iterator<Integer> iterator = get().descendingIterator();
while (iterator.hasNext()) {
remove(iterator.next());
}
}
public static void remove(int id) {
private static void remove(int id) {
if (!traceMap.containsKey(id)) {
return;
}

Datei anzeigen

@ -1,7 +1,7 @@
package de.steamwar.bausystem.tracer.recorder;
import de.steamwar.bausystem.BauSystem;
import de.steamwar.bausystem.tracer.manager.TNTManager;
import de.steamwar.bausystem.tracer.TraceListener;
import org.bukkit.Bukkit;
import org.bukkit.entity.TNTPrimed;
import org.bukkit.scheduler.BukkitTask;
@ -41,7 +41,7 @@ public class TNTRecorder {
.stream()
.filter(e -> e instanceof TNTPrimed)
.map(e -> (TNTPrimed)e);
TNTManager.add(tntPrimedStream, RecordManager.activeFrameID);
TraceListener.add(tntPrimedStream, RecordManager.activeFrameID);
if (RecordManager.status == RecordManager.Status.RECORD_AUTO && System.currentTimeMillis() - lastExplosion > 4500) {
stopAuto();

Datei anzeigen

@ -10,7 +10,6 @@ import java.util.Set;
public class TNTTrace {
private final long startTime = System.currentTimeMillis();
private final int frameID;
private int tntID;
@ -26,20 +25,16 @@ public class TNTTrace {
}
public void addLocation(Location location) {
addLocation(location.getX(), location.getY(), location.getZ());
}
public void addLocation(double x, double y, double z) {
if (index >= positions.length) {
positions = Arrays.copyOf(positions, positions.length + 240);
positions = Arrays.copyOf(positions, positions.length + 3);
}
positions[index + 0] = (float)x;
positions[index + 1] = (float)y;
positions[index + 2] = (float)z;
positions[index] = (float)location.getX();
positions[index + 1] = (float)location.getY();
positions[index + 2] = (float)location.getZ();
index += 3;
}
public int size() {
private int size() {
return index / 3;
}
@ -51,10 +46,6 @@ public class TNTTrace {
return positions.length;
}
public long getStartTime() {
return startTime;
}
public void setTntID(int tntID) {
this.tntID = tntID;
}
@ -72,11 +63,11 @@ public class TNTTrace {
ShowManager.traceAdd();
for (int i = 0; i < length(); i++) {
posSet.add(new TraceCache.Loc(positions[i * 3 + 0], positions[i * 3 + 1], positions[i * 3 + 2]));
posSet.add(new TraceCache.Loc(positions[i * 3], positions[i * 3 + 1], positions[i * 3 + 2]));
}
for (int i = 0; i < realLength() - 3; i += 3) {
float x1 = positions[i + 0];
float x1 = positions[i];
float y1 = positions[i + 1];
float z1 = positions[i + 2];
@ -84,15 +75,15 @@ public class TNTTrace {
float y2 = positions[i + 4];
float z2 = positions[i + 5];
if (!isEqual(y2, y1)) {
if (isDifferent(y2, y1)) {
TraceCache.Loc loc = new TraceCache.Loc(x1, y2, z1);
posYSet.add(loc);
posXYZSet.add(loc);
}
if (abs(x2 - x1) > abs(z2 - z1)) {
if (!isEqual(x2, x1)) posXYZSet.add(new TraceCache.Loc(x2, y2, z1));
if (Math.abs(x2 - x1) > Math.abs(z2 - z1)) {
if (isDifferent(x2, x1)) posXYZSet.add(new TraceCache.Loc(x2, y2, z1));
} else {
if (!isEqual(z2, z1)) posXYZSet.add(new TraceCache.Loc(x1, y2, z2));
if (isDifferent(z2, z1)) posXYZSet.add(new TraceCache.Loc(x1, y2, z2));
}
}
}
@ -107,13 +98,7 @@ public class TNTTrace {
return posXYZSet;
}
private static boolean isEqual(float d1, float d2) {
return (d2 - d1) * (d2 - d1) < 0.01;
private static boolean isDifferent(float d1, float d2) {
return (d2 - d1) * (d2 - d1) >= 0.01;
}
private static float abs(float x) {
return x < 0 ? -x : x;
}
}