From 1f2b6a0290e443a992155b8e2e74fe92aceded5b Mon Sep 17 00:00:00 2001 From: Lixfel Date: Sun, 30 Aug 2020 07:13:11 +0200 Subject: [PATCH 1/4] Simplify Tracer #1 --- .../bausystem/tracer/TNTTracer_12.java | 90 +------------------ .../bausystem/tracer/TNTTracer_15.java | 87 +----------------- .../bausystem/tracer/TracerGuiUpdater.java | 4 - .../src/de/steamwar/bausystem/BauSystem.java | 27 ++---- .../steamwar/bausystem/tracer/MoveEvent.java | 17 ---- .../bausystem/tracer/TNTListener.java | 42 --------- .../steamwar/bausystem/tracer/TNTTracer.java | 89 ++++++++++++++---- .../bausystem/tracer/TNTTracerGUI_15.java | 0 .../bausystem/tracer/TraceActionbar.java | 1 + .../steamwar/bausystem/tracer/TraceCache.java | 0 .../bausystem/tracer/TraceListener.java | 33 +++++++ .../steamwar/bausystem/tracer/TraceUtils.java | 11 +-- .../bausystem/tracer/TracerUpdater.java | 0 .../tracer/recorder/RecordManager.java | 6 +- 14 files changed, 125 insertions(+), 282 deletions(-) delete mode 100644 BauSystem_API/src/de/steamwar/bausystem/tracer/TracerGuiUpdater.java delete mode 100644 BauSystem_Main/src/de/steamwar/bausystem/tracer/MoveEvent.java delete mode 100644 BauSystem_Main/src/de/steamwar/bausystem/tracer/TNTListener.java rename {BauSystem_15 => BauSystem_Main}/src/de/steamwar/bausystem/tracer/TNTTracerGUI_15.java (100%) rename {BauSystem_API => BauSystem_Main}/src/de/steamwar/bausystem/tracer/TraceCache.java (100%) create mode 100644 BauSystem_Main/src/de/steamwar/bausystem/tracer/TraceListener.java rename {BauSystem_API => BauSystem_Main}/src/de/steamwar/bausystem/tracer/TraceUtils.java (86%) rename {BauSystem_API => BauSystem_Main}/src/de/steamwar/bausystem/tracer/TracerUpdater.java (100%) diff --git a/BauSystem_12/src/de/steamwar/bausystem/tracer/TNTTracer_12.java b/BauSystem_12/src/de/steamwar/bausystem/tracer/TNTTracer_12.java index 018245c..0f5d0f3 100644 --- a/BauSystem_12/src/de/steamwar/bausystem/tracer/TNTTracer_12.java +++ b/BauSystem_12/src/de/steamwar/bausystem/tracer/TNTTracer_12.java @@ -1,95 +1,11 @@ package de.steamwar.bausystem.tracer; -import net.md_5.bungee.api.ChatMessageType; -import net.md_5.bungee.api.chat.TextComponent; -import org.bukkit.*; -import org.bukkit.block.Block; -import org.bukkit.block.BlockFace; -import org.bukkit.entity.Player; -import org.bukkit.entity.TNTPrimed; -import org.bukkit.material.Step; -import org.bukkit.plugin.Plugin; +import org.bukkit.Material; -import java.util.*; - -import static de.steamwar.bausystem.tracer.TraceUtils.actionBar; - -@SuppressWarnings("deprecation") class TNTTracer_12 { - private TNTTracer_12(){} - private static TracerUpdater tracerUpdater; - private static TraceCache traceCache = new TraceCache(); - - private static class Synchronizer {} - private static final Synchronizer synchronizer = new Synchronizer(); - - private static World world; - static void worldInit() { - if (world != null) return; - world = Bukkit.getWorlds().get(0); + static Material getMaterial(){ + return Material.STAINED_GLASS; } - - static void init(Plugin plugin, TracerUpdater tracerUpdater) { - Bukkit.getScheduler().runTaskTimer(plugin, () -> { - synchronized (synchronizer) { - synchronizer.notifyAll(); - } - }, 1, 20); - Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> { - while (true) { - synchronized (synchronizer) { - try { - synchronizer.wait(); - } catch (InterruptedException e) { - Thread.currentThread().interrupt(); - } - } - run(); - } - }); - - TNTTracer_12.tracerUpdater = tracerUpdater; - } - - private static void run() { - for (Player p : Bukkit.getOnlinePlayers()) { - if (actionBar) { - 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) { - continue; - } - Set toHide = traceCache.update(p, tracerUpdater, dirty); - Set toShow = traceCache.get(p); - - hideBlockTraces(toHide, p); - showTraces(toShow, tracerUpdater.getDisplayType(p), p); - } - } - - private static void hideBlockTraces(Set locs, Player player) { - if (locs.isEmpty()) return; - - for (TraceCache.Loc l : locs) { - TraceUtils.hideBlock(player, l.x, l.y - 0.49F, l.z); - } - } - - private static void showTraces(Set locs, TracerUpdater.DisplayMode displayMode, Player player) { - if (locs.isEmpty()) return; - - for (TraceCache.Loc l : locs) { - if (displayMode == TracerUpdater.DisplayMode.Block) { - TraceUtils.showBlock(player, l.x, l.y - 0.49F, l.z, Material.STAINED_GLASS, (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)); - } - } - } - } diff --git a/BauSystem_15/src/de/steamwar/bausystem/tracer/TNTTracer_15.java b/BauSystem_15/src/de/steamwar/bausystem/tracer/TNTTracer_15.java index 63a545d..553e1ea 100644 --- a/BauSystem_15/src/de/steamwar/bausystem/tracer/TNTTracer_15.java +++ b/BauSystem_15/src/de/steamwar/bausystem/tracer/TNTTracer_15.java @@ -1,94 +1,11 @@ package de.steamwar.bausystem.tracer; -import net.md_5.bungee.api.ChatMessageType; -import net.md_5.bungee.api.chat.TextComponent; -import org.bukkit.Bukkit; import org.bukkit.Material; -import org.bukkit.Particle; -import org.bukkit.World; -import org.bukkit.entity.Player; -import org.bukkit.plugin.Plugin; -import java.util.Set; - -import static de.steamwar.bausystem.tracer.TraceUtils.actionBar; - -@SuppressWarnings("deprecation") class TNTTracer_15 { - private TNTTracer_15(){} - private static TracerUpdater tracerUpdater; - private static TraceCache traceCache = new TraceCache(); - - private static class Synchronizer {} - private static final Synchronizer synchronizer = new Synchronizer(); - - private static World world; - static void worldInit() { - if (world != null) return; - world = Bukkit.getWorlds().get(0); + static Material getMaterial(boolean updatePoint){ + return updatePoint ? Material.LIME_STAINED_GLASS : Material.RED_STAINED_GLASS; } - - static void init(Plugin plugin, TracerUpdater tracerUpdater) { - Bukkit.getScheduler().runTaskTimer(plugin, () -> { - synchronized (synchronizer) { - synchronizer.notifyAll(); - } - }, 1, 20); - Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> { - while (true) { - synchronized (synchronizer) { - try { - synchronizer.wait(); - } catch (InterruptedException e) { - Thread.currentThread().interrupt(); - } - } - run(); - } - }); - - TNTTracer_15.tracerUpdater = tracerUpdater; - } - - private static void run() { - for (Player p : Bukkit.getOnlinePlayers()) { - if (actionBar) { - 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) { - continue; - } - Set toHide = traceCache.update(p, tracerUpdater, dirty); - Set toShow = traceCache.get(p); - - hideBlockTraces(toHide, p); - showTraces(toShow, tracerUpdater.getDisplayType(p), p); - } - } - - private static void hideBlockTraces(Set locs, Player player) { - if (locs.isEmpty()) return; - - for (TraceCache.Loc l : locs) { - TraceUtils.hideBlock(player, l.x, l.y - 0.49F, l.z); - } - } - - private static void showTraces(Set locs, TracerUpdater.DisplayMode displayMode, Player player) { - if (locs.isEmpty()) return; - - for (TraceCache.Loc l : locs) { - if (displayMode == TracerUpdater.DisplayMode.Block) { - TraceUtils.showBlock(player, l.x, l.y - 0.49F, l.z, (l.updatePoint ? Material.LIME_STAINED_GLASS : Material.RED_STAINED_GLASS), (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)); - } - } - } - } diff --git a/BauSystem_API/src/de/steamwar/bausystem/tracer/TracerGuiUpdater.java b/BauSystem_API/src/de/steamwar/bausystem/tracer/TracerGuiUpdater.java deleted file mode 100644 index 1f3dd47..0000000 --- a/BauSystem_API/src/de/steamwar/bausystem/tracer/TracerGuiUpdater.java +++ /dev/null @@ -1,4 +0,0 @@ -package de.steamwar.bausystem.tracer; - -public class TracerGuiUpdater { -} diff --git a/BauSystem_Main/src/de/steamwar/bausystem/BauSystem.java b/BauSystem_Main/src/de/steamwar/bausystem/BauSystem.java index 5625da9..093c3bd 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/BauSystem.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/BauSystem.java @@ -1,12 +1,9 @@ package de.steamwar.bausystem; import de.steamwar.bausystem.commands.*; -import de.steamwar.bausystem.tracer.MoveEvent; -import de.steamwar.bausystem.tracer.TNTListener; +import de.steamwar.bausystem.tracer.TraceListener; import de.steamwar.bausystem.tracer.TNTTracer; -import de.steamwar.bausystem.tracer.TraceUtils; import de.steamwar.bausystem.tracer.manager.ShowManager; -import de.steamwar.bausystem.tracer.recorder.RecordManager; import de.steamwar.bausystem.world.AFKStopper; import de.steamwar.bausystem.world.ArenaSection; import de.steamwar.bausystem.world.BauScoreboard; @@ -104,15 +101,10 @@ public class BauSystem extends JavaPlugin implements Listener { Bukkit.getPluginManager().registerEvents(this, this); Bukkit.getPluginManager().registerEvents(new RegionListener(), this); - Bukkit.getPluginManager().registerEvents(new TNTListener(), this); Bukkit.getPluginManager().registerEvents(new BauScoreboard(), this); - Bukkit.getPluginManager().registerEvents(new MoveEvent(), this); + Bukkit.getPluginManager().registerEvents(new TraceListener(), this); new AFKStopper(); - if (Core.getVersion() == 15) { - TNTTracer.init15(); - } else { - TNTTracer.init12(); - } + TNTTracer.init(); autoShutdown = Bukkit.getScheduler().runTaskLater(this, Bukkit::shutdown, 1200); } @@ -277,16 +269,10 @@ public class BauSystem extends JavaPlugin implements Listener { attachment.setPermission("worldedit.calc", true); attachment.setPermission("worldedit.fill", true);*/ - RecordManager.worldInit(); - TNTTracer.worldInit(); - TraceUtils.locationInit(); - ShowManager.add(p); - if (Core.getVersion() == 15){ - ShowManager.add(p); + if (Core.getVersion() == 15) Bukkit.getWorlds().get(0).setGameRule(GameRule.REDUCED_DEBUG_INFO, false); - } } @EventHandler @@ -301,8 +287,9 @@ public class BauSystem extends JavaPlugin implements Listener { @EventHandler public void onInventoryClick(InventoryClickEvent e) { ItemStack stack = e.getCursor(); - if (stack == null) return; - if (!stack.hasItemMeta()) return; + if (stack == null || !stack.hasItemMeta()) + return; + assert stack.getItemMeta() != null; if (stack.getItemMeta().hasEnchants()) { for (Enchantment en : Enchantment.values()) { if (stack.getEnchantmentLevel(en) > en.getMaxLevel()) diff --git a/BauSystem_Main/src/de/steamwar/bausystem/tracer/MoveEvent.java b/BauSystem_Main/src/de/steamwar/bausystem/tracer/MoveEvent.java deleted file mode 100644 index 3cfb0dc..0000000 --- a/BauSystem_Main/src/de/steamwar/bausystem/tracer/MoveEvent.java +++ /dev/null @@ -1,17 +0,0 @@ -package de.steamwar.bausystem.tracer; - -import de.steamwar.bausystem.tracer.manager.ShowManager; -import org.bukkit.entity.Player; -import org.bukkit.event.EventHandler; -import org.bukkit.event.Listener; -import org.bukkit.event.player.PlayerMoveEvent; - -public class MoveEvent implements Listener { - - @EventHandler - public void playerMove(PlayerMoveEvent event) { - Player p = event.getPlayer(); - ShowManager.get(p).move(p); - } - -} diff --git a/BauSystem_Main/src/de/steamwar/bausystem/tracer/TNTListener.java b/BauSystem_Main/src/de/steamwar/bausystem/tracer/TNTListener.java deleted file mode 100644 index 8499435..0000000 --- a/BauSystem_Main/src/de/steamwar/bausystem/tracer/TNTListener.java +++ /dev/null @@ -1,42 +0,0 @@ -package de.steamwar.bausystem.tracer; - -import de.steamwar.bausystem.tracer.manager.TNTManager; -import de.steamwar.bausystem.tracer.recorder.RecordManager; -import de.steamwar.bausystem.tracer.recorder.TNTRecorder; -import de.steamwar.core.Core; -import org.bukkit.entity.TNTPrimed; -import org.bukkit.event.EventHandler; -import org.bukkit.event.Listener; -import org.bukkit.event.entity.EntityExplodeEvent; - -public class TNTListener implements Listener { - - @EventHandler - public void onEntityExplode(EntityExplodeEvent event) { - 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()); - } - - /*private void onEntityExplode12(EntityExplodeEvent event) { - if(TNTTracer12.getStatus() != TNTTracer12.Status.RECORD) return; - if(!(event.getEntity() instanceof TNTPrimed)) return; - TNTPrimed entity = (TNTPrimed) event.getEntity(); - TNTTracer12.remove(entity); - } - - private void onEntityExplode15(EntityExplodeEvent event) { - if(!(event.getEntity() instanceof TNTPrimed)) return; - if (TraceRecorder.getStatus() == TraceRecorder.Status.IDLE_AUTO || TraceRecorder.getStatus() == TraceRecorder.Status.RECORD_AUTO) { - TraceRecorder.start(); - } - TraceRecorder.explode((TNTPrimed) event.getEntity()); - }*/ -} diff --git a/BauSystem_Main/src/de/steamwar/bausystem/tracer/TNTTracer.java b/BauSystem_Main/src/de/steamwar/bausystem/tracer/TNTTracer.java index 3d77d5e..3de47f9 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/tracer/TNTTracer.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/tracer/TNTTracer.java @@ -4,15 +4,26 @@ 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; +import org.bukkit.Bukkit; +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; -public class TNTTracer { +import static de.steamwar.bausystem.tracer.TraceUtils.DEBUG; - private static TracerUpdater tracerUpdater = new TracerUpdater() { +public class TNTTracer { + private 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 updateLocations(Player player) { Iterator traces = ShowManager.get(player).getTraces().descendingIterator(); @@ -61,23 +72,71 @@ public class TNTTracer { } }; - public static void init15() { - TNTTracer_15.init(BauSystem.getPlugin(), tracerUpdater); + public static void init(){ + Bukkit.getScheduler().runTaskTimer(BauSystem.getPlugin(), () -> { + synchronized (synchronizer) { + synchronizer.notifyAll(); + } + }, 1, 20); + Bukkit.getScheduler().runTaskAsynchronously(BauSystem.getPlugin(), () -> { + while (true) { + synchronized (synchronizer) { + try { + synchronizer.wait(); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + } + } + run(); + } + }); } - public static void init12() { - TNTTracer_12.init(BauSystem.getPlugin(), tracerUpdater); - } + private static void run(){ + for (Player p : Bukkit.getOnlinePlayers()) { + if (DEBUG) { + String actionBar = tracerUpdater.actionBar(p); + p.spigot().sendMessage(ChatMessageType.ACTION_BAR, TextComponent.fromLegacyText(actionBar)); + } - public static void worldInit() { - switch (Core.getVersion()) { - case 15: - TNTTracer_15.worldInit(); - break; - default: - TNTTracer_12.worldInit(); - break; + boolean dirty = tracerUpdater.needsUpdate(p); + if (tracerUpdater.getDisplayType(p) == TracerUpdater.DisplayMode.Block && !dirty) { + continue; + } + Set toHide = traceCache.update(p, tracerUpdater, dirty); + Set toShow = traceCache.get(p); + + hideBlockTraces(toHide, p); + showTraces(toShow, tracerUpdater.getDisplayType(p), p); } } + private static void hideBlockTraces(Set locs, Player player) { + if (locs.isEmpty()) return; + + for (TraceCache.Loc l : locs) { + TraceUtils.hideBlock(player, l.x, l.y - 0.49F, l.z); + } + } + + private static void showTraces(Set locs, TracerUpdater.DisplayMode displayMode, Player player) { + if (locs.isEmpty()) return; + + for (TraceCache.Loc l : locs) { + if (displayMode == TracerUpdater.DisplayMode.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)); + } + } + } + + private static Material getMaterial(TraceCache.Loc l){ + switch(Core.getVersion()){ + case 15: + return TNTTracer_15.getMaterial(l.updatePoint); + default: + return TNTTracer_12.getMaterial(); + } + } } diff --git a/BauSystem_15/src/de/steamwar/bausystem/tracer/TNTTracerGUI_15.java b/BauSystem_Main/src/de/steamwar/bausystem/tracer/TNTTracerGUI_15.java similarity index 100% rename from BauSystem_15/src/de/steamwar/bausystem/tracer/TNTTracerGUI_15.java rename to BauSystem_Main/src/de/steamwar/bausystem/tracer/TNTTracerGUI_15.java diff --git a/BauSystem_Main/src/de/steamwar/bausystem/tracer/TraceActionbar.java b/BauSystem_Main/src/de/steamwar/bausystem/tracer/TraceActionbar.java index 86f71ea..7fb8984 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/tracer/TraceActionbar.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/tracer/TraceActionbar.java @@ -9,6 +9,7 @@ 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); diff --git a/BauSystem_API/src/de/steamwar/bausystem/tracer/TraceCache.java b/BauSystem_Main/src/de/steamwar/bausystem/tracer/TraceCache.java similarity index 100% rename from BauSystem_API/src/de/steamwar/bausystem/tracer/TraceCache.java rename to BauSystem_Main/src/de/steamwar/bausystem/tracer/TraceCache.java diff --git a/BauSystem_Main/src/de/steamwar/bausystem/tracer/TraceListener.java b/BauSystem_Main/src/de/steamwar/bausystem/tracer/TraceListener.java new file mode 100644 index 0000000..137bd63 --- /dev/null +++ b/BauSystem_Main/src/de/steamwar/bausystem/tracer/TraceListener.java @@ -0,0 +1,33 @@ +package de.steamwar.bausystem.tracer; + +import de.steamwar.bausystem.tracer.manager.ShowManager; +import de.steamwar.bausystem.tracer.manager.TNTManager; +import de.steamwar.bausystem.tracer.recorder.RecordManager; +import org.bukkit.entity.Player; +import org.bukkit.entity.TNTPrimed; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.entity.EntityExplodeEvent; +import org.bukkit.event.player.PlayerMoveEvent; + +public class TraceListener implements Listener { + + @EventHandler + public void onEntityExplode(EntityExplodeEvent event) { + 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()); + } + + + @EventHandler + public void playerMove(PlayerMoveEvent event) { + Player p = event.getPlayer(); + ShowManager.get(p).move(p); + } +} diff --git a/BauSystem_API/src/de/steamwar/bausystem/tracer/TraceUtils.java b/BauSystem_Main/src/de/steamwar/bausystem/tracer/TraceUtils.java similarity index 86% rename from BauSystem_API/src/de/steamwar/bausystem/tracer/TraceUtils.java rename to BauSystem_Main/src/de/steamwar/bausystem/tracer/TraceUtils.java index bcb4404..492f990 100644 --- a/BauSystem_API/src/de/steamwar/bausystem/tracer/TraceUtils.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/tracer/TraceUtils.java @@ -7,14 +7,11 @@ import org.bukkit.Particle; import org.bukkit.entity.Player; public class TraceUtils { + private TraceUtils(){} - private static Location location; + public static final boolean DEBUG = false; - public static boolean actionBar = false; - - public static void locationInit() { - location = new Location(Bukkit.getWorlds().get(0), 0, 0, 0); - } + private static Location location = new Location(Bukkit.getWorlds().get(0), 0, 0, 0); public static void showCorner(Player player, float x, float y, float z, Particle particle) { player.spawnParticle(particle, makeLocation(x + 0.00F, y + 0.00F, z + 0.00F), 1, 0F, 0F, 0F, 0.001); @@ -40,7 +37,7 @@ public class TraceUtils { } } - public static Location makeLocation(float x, float y, float z) { + private static Location makeLocation(float x, float y, float z) { location.setX(x); location.setY(y); location.setZ(z); diff --git a/BauSystem_API/src/de/steamwar/bausystem/tracer/TracerUpdater.java b/BauSystem_Main/src/de/steamwar/bausystem/tracer/TracerUpdater.java similarity index 100% rename from BauSystem_API/src/de/steamwar/bausystem/tracer/TracerUpdater.java rename to BauSystem_Main/src/de/steamwar/bausystem/tracer/TracerUpdater.java diff --git a/BauSystem_Main/src/de/steamwar/bausystem/tracer/recorder/RecordManager.java b/BauSystem_Main/src/de/steamwar/bausystem/tracer/recorder/RecordManager.java index b04e3c7..a7b22b1 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/tracer/recorder/RecordManager.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/tracer/recorder/RecordManager.java @@ -15,11 +15,7 @@ public class RecordManager { static Status status = Status.IDLE; public static Integer activeFrameID = null; - static World world; - public static void worldInit() { - if (world != null) return; - world = Bukkit.getWorlds().get(0); - } + static final World world = Bukkit.getWorlds().get(0); public static void tracer(Player player, String[] args) { switch (args[0].toLowerCase()) { -- 2.39.2 From 6831ada70f4878a61377d15eff8ba9dc357e1f01 Mon Sep 17 00:00:00 2001 From: Lixfel Date: Sun, 30 Aug 2020 07:45:32 +0200 Subject: [PATCH 2/4] Untangle spagetti code part 1 --- .../steamwar/bausystem/tracer/TNTTracer.java | 66 ++----------------- .../bausystem/tracer/TraceActionbar.java | 53 --------------- .../steamwar/bausystem/tracer/TraceCache.java | 31 ++++----- .../bausystem/tracer/TraceListener.java | 30 ++++++++- .../bausystem/tracer/TracerUpdater.java | 45 ++++++++++--- .../tracer/manager/FrameManager.java | 2 +- .../bausystem/tracer/manager/IDManager.java | 2 +- .../bausystem/tracer/manager/ShowManager.java | 24 +++---- .../bausystem/tracer/manager/TNTManager.java | 33 ---------- .../tracer/manager/TraceManager.java | 4 +- .../tracer/recorder/TNTRecorder.java | 4 +- .../bausystem/tracer/trace/TNTTrace.java | 41 ++++-------- 12 files changed, 117 insertions(+), 218 deletions(-) delete mode 100644 BauSystem_Main/src/de/steamwar/bausystem/tracer/TraceActionbar.java delete mode 100644 BauSystem_Main/src/de/steamwar/bausystem/tracer/manager/TNTManager.java diff --git a/BauSystem_Main/src/de/steamwar/bausystem/tracer/TNTTracer.java b/BauSystem_Main/src/de/steamwar/bausystem/tracer/TNTTracer.java index 3de47f9..c12cc21 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/tracer/TNTTracer.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/tracer/TNTTracer.java @@ -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 updateLocations(Player player) { - Iterator traces = ShowManager.get(player).getTraces().descendingIterator(); - Set 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 updatePoints(Player player) { - Iterator traces = ShowManager.get(player).getTraces().descendingIterator(); - Set 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 toHide = traceCache.update(p, tracerUpdater, dirty); + Set toHide = traceCache.update(p, dirty); Set 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 locs, TracerUpdater.DisplayMode displayMode, Player player) { + private static void showTraces(Set 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)); diff --git a/BauSystem_Main/src/de/steamwar/bausystem/tracer/TraceActionbar.java b/BauSystem_Main/src/de/steamwar/bausystem/tracer/TraceActionbar.java deleted file mode 100644 index 7fb8984..0000000 --- a/BauSystem_Main/src/de/steamwar/bausystem/tracer/TraceActionbar.java +++ /dev/null @@ -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 traceIDs = TraceManager.get(); - long bitTraces = traceIDs.size() * 32L; - long bitTracesIntern = 0; - Iterator 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"; - } - -} diff --git a/BauSystem_Main/src/de/steamwar/bausystem/tracer/TraceCache.java b/BauSystem_Main/src/de/steamwar/bausystem/tracer/TraceCache.java index 7634d37..fd69602 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/tracer/TraceCache.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/tracer/TraceCache.java @@ -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 empty = new HashSet(); + private static final Set empty = new HashSet<>(); private Map> playerMap = new HashMap<>(); - private Map playerDisplayMap = new HashMap<>(); + private Map playerDisplayMap = new HashMap<>(); public Set get(Player player) { return playerMap.getOrDefault(player.getUniqueId().toString(), empty); } - public Set update(Player player, TracerUpdater tracerUpdater, boolean dirty) { + Set update(Player player, boolean dirty) { if (!dirty) return empty; String key = player.getUniqueId().toString(); Set 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 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) { diff --git a/BauSystem_Main/src/de/steamwar/bausystem/tracer/TraceListener.java b/BauSystem_Main/src/de/steamwar/bausystem/tracer/TraceListener.java index 137bd63..52db229 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/tracer/TraceListener.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/tracer/TraceListener.java @@ -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 tntMap = new HashMap<>(); + + public static void add(Stream 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(); } diff --git a/BauSystem_Main/src/de/steamwar/bausystem/tracer/TracerUpdater.java b/BauSystem_Main/src/de/steamwar/bausystem/tracer/TracerUpdater.java index efa73aa..b8c32e1 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/tracer/TracerUpdater.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/tracer/TracerUpdater.java @@ -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 updateLocations(Player player) { + Iterator traces = ShowManager.get(player).getTraces().descendingIterator(); + Set 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 updateLocations(Player player); - Set updatePoints(Player player); - boolean needsUpdate(Player player); - DisplayMode getDisplayType(Player player); + static Set updatePoints(Player player) { + Iterator traces = ShowManager.get(player).getTraces().descendingIterator(); + Set 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; + } - String actionBar(Player player); + 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"; + } } diff --git a/BauSystem_Main/src/de/steamwar/bausystem/tracer/manager/FrameManager.java b/BauSystem_Main/src/de/steamwar/bausystem/tracer/manager/FrameManager.java index a3fc515..22bb52b 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/tracer/manager/FrameManager.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/tracer/manager/FrameManager.java @@ -45,7 +45,7 @@ public class FrameManager { if (!frameMap.get(frameID).isEmpty()) { return; } - frameMap.remove((Integer) frameID); + frameMap.remove(frameID); } } diff --git a/BauSystem_Main/src/de/steamwar/bausystem/tracer/manager/IDManager.java b/BauSystem_Main/src/de/steamwar/bausystem/tracer/manager/IDManager.java index 6f75dff..3a133a4 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/tracer/manager/IDManager.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/tracer/manager/IDManager.java @@ -4,7 +4,7 @@ public class IDManager { private static int currentID = 0; - public static synchronized int generateID() { + static synchronized int generateID() { return currentID++; } diff --git a/BauSystem_Main/src/de/steamwar/bausystem/tracer/manager/ShowManager.java b/BauSystem_Main/src/de/steamwar/bausystem/tracer/manager/ShowManager.java index ebe08a8..a2e0b86 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/tracer/manager/ShowManager.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/tracer/manager/ShowManager.java @@ -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 entry : showMap.entrySet()) { entry.getValue().removeSelection(id); } } - public static void globalDirty() { + static void globalDirty() { for (Map.Entry entry : showMap.entrySet()) { entry.getValue().dirty = true; } diff --git a/BauSystem_Main/src/de/steamwar/bausystem/tracer/manager/TNTManager.java b/BauSystem_Main/src/de/steamwar/bausystem/tracer/manager/TNTManager.java deleted file mode 100644 index 10a8af0..0000000 --- a/BauSystem_Main/src/de/steamwar/bausystem/tracer/manager/TNTManager.java +++ /dev/null @@ -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 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 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()); - }); - } - -} diff --git a/BauSystem_Main/src/de/steamwar/bausystem/tracer/manager/TraceManager.java b/BauSystem_Main/src/de/steamwar/bausystem/tracer/manager/TraceManager.java index 75dedd7..4c49725 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/tracer/manager/TraceManager.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/tracer/manager/TraceManager.java @@ -17,14 +17,14 @@ public class TraceManager { traceMap.put(id, tntTrace); } - public static void remove() { + private static void remove() { Iterator 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; } diff --git a/BauSystem_Main/src/de/steamwar/bausystem/tracer/recorder/TNTRecorder.java b/BauSystem_Main/src/de/steamwar/bausystem/tracer/recorder/TNTRecorder.java index f906bb1..9d98f63 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/tracer/recorder/TNTRecorder.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/tracer/recorder/TNTRecorder.java @@ -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(); diff --git a/BauSystem_Main/src/de/steamwar/bausystem/tracer/trace/TNTTrace.java b/BauSystem_Main/src/de/steamwar/bausystem/tracer/trace/TNTTrace.java index 9f5de80..95b003b 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/tracer/trace/TNTTrace.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/tracer/trace/TNTTrace.java @@ -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; - } - - } -- 2.39.2 From dda4ad7f997ae337a05817124437ab866d348401 Mon Sep 17 00:00:00 2001 From: Lixfel Date: Sun, 30 Aug 2020 08:01:51 +0200 Subject: [PATCH 3/4] Untangle spagetti code #2 --- .../tracer/{trace => }/TNTTrace.java | 24 +++------- .../steamwar/bausystem/tracer/TNTTracer.java | 43 +++++++++++++++-- ...TNTTracerGUI_15.java => TNTTracerGUI.java} | 2 +- .../bausystem/tracer/TraceListener.java | 1 - .../bausystem/tracer/TraceSidebar.java | 22 --------- .../steamwar/bausystem/tracer/TraceUtils.java | 47 ------------------- .../bausystem/tracer/TracerUpdater.java | 1 - .../bausystem/tracer/manager/ShowManager.java | 2 +- .../tracer/manager/TraceManager.java | 4 +- .../tracer/recorder/RecordManager.java | 6 +-- .../bausystem/world/BauScoreboard.java | 21 ++++++--- 11 files changed, 67 insertions(+), 106 deletions(-) rename BauSystem_Main/src/de/steamwar/bausystem/tracer/{trace => }/TNTTrace.java (82%) rename BauSystem_Main/src/de/steamwar/bausystem/tracer/{TNTTracerGUI_15.java => TNTTracerGUI.java} (98%) delete mode 100644 BauSystem_Main/src/de/steamwar/bausystem/tracer/TraceSidebar.java delete mode 100644 BauSystem_Main/src/de/steamwar/bausystem/tracer/TraceUtils.java diff --git a/BauSystem_Main/src/de/steamwar/bausystem/tracer/trace/TNTTrace.java b/BauSystem_Main/src/de/steamwar/bausystem/tracer/TNTTrace.java similarity index 82% rename from BauSystem_Main/src/de/steamwar/bausystem/tracer/trace/TNTTrace.java rename to BauSystem_Main/src/de/steamwar/bausystem/tracer/TNTTrace.java index 95b003b..45cb02b 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/tracer/trace/TNTTrace.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/tracer/TNTTrace.java @@ -1,6 +1,5 @@ -package de.steamwar.bausystem.tracer.trace; +package de.steamwar.bausystem.tracer; -import de.steamwar.bausystem.tracer.TraceCache; import de.steamwar.bausystem.tracer.manager.ShowManager; import org.bukkit.Location; @@ -11,7 +10,6 @@ import java.util.Set; public class TNTTrace { private final int frameID; - private int tntID; private int index = 0; private float[] positions = new float[240]; @@ -20,11 +18,11 @@ public class TNTTrace { private Set posYSet = new HashSet<>(); private Set posXYZSet = new HashSet<>(); - public TNTTrace(int frameID) { + TNTTrace(int frameID) { this.frameID = frameID; } - public void addLocation(Location location) { + void addLocation(Location location) { if (index >= positions.length) { positions = Arrays.copyOf(positions, positions.length + 3); } @@ -42,23 +40,15 @@ public class TNTTrace { return size(); } - public int realLength() { + private int realLength() { return positions.length; } - public void setTntID(int tntID) { - this.tntID = tntID; - } - - public int getTntID() { - return tntID; - } - public int getFrameID() { return frameID; } - public void cleanUp() { + void cleanUp() { positions = Arrays.copyOf(positions, index); ShowManager.traceAdd(); @@ -88,11 +78,11 @@ public class TNTTrace { } } - public Set locs() { + Set locs() { return posSet; } - public Set locsUpdate(ShowManager.DisplayMode displayMode) { + Set locsUpdate(ShowManager.DisplayMode displayMode) { if (displayMode == ShowManager.DisplayMode.NONE) return new HashSet<>(); if (displayMode == ShowManager.DisplayMode.Y_AXIS) return posYSet; return posXYZSet; diff --git a/BauSystem_Main/src/de/steamwar/bausystem/tracer/TNTTracer.java b/BauSystem_Main/src/de/steamwar/bausystem/tracer/TNTTracer.java index c12cc21..10f2a74 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/tracer/TNTTracer.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/tracer/TNTTracer.java @@ -6,17 +6,18 @@ import de.steamwar.core.Core; import net.md_5.bungee.api.ChatMessageType; import net.md_5.bungee.api.chat.TextComponent; import org.bukkit.Bukkit; +import org.bukkit.Location; import org.bukkit.Material; import org.bukkit.Particle; import org.bukkit.entity.Player; import java.util.Set; -import static de.steamwar.bausystem.tracer.TraceUtils.DEBUG; - public class TNTTracer { private TNTTracer(){} + static final boolean DEBUG = false; + private static final Object synchronizer = new Object(); private static final TraceCache traceCache = new TraceCache(); @@ -63,7 +64,7 @@ public class TNTTracer { if (locs.isEmpty()) return; for (TraceCache.Loc l : locs) { - TraceUtils.hideBlock(player, l.x, l.y - 0.49F, l.z); + hideBlock(player, l.x, l.y - 0.49F, l.z); } } @@ -72,9 +73,9 @@ public class TNTTracer { for (TraceCache.Loc l : locs) { if (displayMode == ShowManager.DisplayType.Block) { - TraceUtils.showBlock(player, l.x, l.y - 0.49F, l.z, getMaterial(l), (l.updatePoint ? (byte) 5 : (byte) 14)); + 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)); + showCorner(player, l.x - 0.49F, l.y, l.z - 0.49F, (l.updatePoint ? Particle.FLAME : Particle.VILLAGER_HAPPY)); } } } @@ -87,4 +88,36 @@ public class TNTTracer { return TNTTracer_12.getMaterial(); } } + + private static void showCorner(Player player, float x, float y, float z, Particle particle) { + player.spawnParticle(particle, makeLocation(x + 0.00F, y + 0.00F, z + 0.00F), 1, 0F, 0F, 0F, 0.001); + player.spawnParticle(particle, makeLocation(x + 0.98F, y + 0.00F, z + 0.00F), 1, 0F, 0F, 0F, 0.001); + player.spawnParticle(particle, makeLocation(x + 0.00F, y + 0.00F, z + 0.98F), 1, 0F, 0F, 0F, 0.001); + player.spawnParticle(particle, makeLocation(x + 0.98F, y + 0.00F, z + 0.98F), 1, 0F, 0F, 0F, 0.001); + + player.spawnParticle(particle, makeLocation(x + 0.00F, y + 0.98F, z + 0.00F), 1, 0F, 0F, 0F, 0.001); + player.spawnParticle(particle, makeLocation(x + 0.98F, y + 0.98F, z + 0.00F), 1, 0F, 0F, 0F, 0.001); + player.spawnParticle(particle, makeLocation(x + 0.00F, y + 0.98F, z + 0.98F), 1, 0F, 0F, 0F, 0.001); + player.spawnParticle(particle, makeLocation(x + 0.98F, y + 0.98F, z + 0.98F), 1, 0F, 0F, 0F, 0.001); + } + + private static void showBlock(Player p, float x, float y, float z, Material block, byte b) { + if (makeLocation(x, y, z).getBlock().getType() == Material.AIR) { + p.sendBlockChange(makeLocation(x, y, z), block, b); + } + } + + private static void hideBlock(Player p, float x, float y, float z) { + if (makeLocation(x, y, z).getBlock().getType() == Material.AIR) { + p.sendBlockChange(makeLocation(x, y, z), Material.AIR, (byte) 0); + } + } + + private static final Location location = new Location(Bukkit.getWorlds().get(0), 0, 0, 0); + private static Location makeLocation(float x, float y, float z) { + location.setX(x); + location.setY(y); + location.setZ(z); + return location; + } } diff --git a/BauSystem_Main/src/de/steamwar/bausystem/tracer/TNTTracerGUI_15.java b/BauSystem_Main/src/de/steamwar/bausystem/tracer/TNTTracerGUI.java similarity index 98% rename from BauSystem_Main/src/de/steamwar/bausystem/tracer/TNTTracerGUI_15.java rename to BauSystem_Main/src/de/steamwar/bausystem/tracer/TNTTracerGUI.java index 750e69f..c7426a5 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/tracer/TNTTracerGUI_15.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/tracer/TNTTracerGUI.java @@ -1,6 +1,6 @@ package de.steamwar.bausystem.tracer; -public class TNTTracerGUI_15 { +public class TNTTracerGUI { /*private static GuiCallbackTNTFrame guiCallbackTNTFrame; private static GuiCallbackTNTTrace guiCallbackTNTTrace; diff --git a/BauSystem_Main/src/de/steamwar/bausystem/tracer/TraceListener.java b/BauSystem_Main/src/de/steamwar/bausystem/tracer/TraceListener.java index 52db229..7a3848f 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/tracer/TraceListener.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/tracer/TraceListener.java @@ -3,7 +3,6 @@ package de.steamwar.bausystem.tracer; import de.steamwar.bausystem.tracer.manager.ShowManager; 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; diff --git a/BauSystem_Main/src/de/steamwar/bausystem/tracer/TraceSidebar.java b/BauSystem_Main/src/de/steamwar/bausystem/tracer/TraceSidebar.java deleted file mode 100644 index b67d0f1..0000000 --- a/BauSystem_Main/src/de/steamwar/bausystem/tracer/TraceSidebar.java +++ /dev/null @@ -1,22 +0,0 @@ -package de.steamwar.bausystem.tracer; - -import de.steamwar.bausystem.tracer.manager.FrameManager; -import de.steamwar.bausystem.tracer.recorder.RecordManager; -import de.steamwar.bausystem.tracer.recorder.TNTRecorder; - -public class TraceSidebar { - - public static long traceStart() { - return TNTRecorder.recordStart; - } - - public static long traceTicks() { - return (System.currentTimeMillis() - TNTRecorder.recordStart) / 50; - } - - public static int traceSize() { - if (RecordManager.activeFrameID == null) return 0; - return FrameManager.get(RecordManager.activeFrameID).size(); - } - -} diff --git a/BauSystem_Main/src/de/steamwar/bausystem/tracer/TraceUtils.java b/BauSystem_Main/src/de/steamwar/bausystem/tracer/TraceUtils.java deleted file mode 100644 index 492f990..0000000 --- a/BauSystem_Main/src/de/steamwar/bausystem/tracer/TraceUtils.java +++ /dev/null @@ -1,47 +0,0 @@ -package de.steamwar.bausystem.tracer; - -import org.bukkit.Bukkit; -import org.bukkit.Location; -import org.bukkit.Material; -import org.bukkit.Particle; -import org.bukkit.entity.Player; - -public class TraceUtils { - private TraceUtils(){} - - public static final boolean DEBUG = false; - - private static Location location = new Location(Bukkit.getWorlds().get(0), 0, 0, 0); - - public static void showCorner(Player player, float x, float y, float z, Particle particle) { - player.spawnParticle(particle, makeLocation(x + 0.00F, y + 0.00F, z + 0.00F), 1, 0F, 0F, 0F, 0.001); - player.spawnParticle(particle, makeLocation(x + 0.98F, y + 0.00F, z + 0.00F), 1, 0F, 0F, 0F, 0.001); - player.spawnParticle(particle, makeLocation(x + 0.00F, y + 0.00F, z + 0.98F), 1, 0F, 0F, 0F, 0.001); - player.spawnParticle(particle, makeLocation(x + 0.98F, y + 0.00F, z + 0.98F), 1, 0F, 0F, 0F, 0.001); - - player.spawnParticle(particle, makeLocation(x + 0.00F, y + 0.98F, z + 0.00F), 1, 0F, 0F, 0F, 0.001); - player.spawnParticle(particle, makeLocation(x + 0.98F, y + 0.98F, z + 0.00F), 1, 0F, 0F, 0F, 0.001); - player.spawnParticle(particle, makeLocation(x + 0.00F, y + 0.98F, z + 0.98F), 1, 0F, 0F, 0F, 0.001); - player.spawnParticle(particle, makeLocation(x + 0.98F, y + 0.98F, z + 0.98F), 1, 0F, 0F, 0F, 0.001); - } - - public static void showBlock(Player p, float x, float y, float z, Material block, byte b) { - if (makeLocation(x, y, z).getBlock().getType() == Material.AIR) { - p.sendBlockChange(makeLocation(x, y, z), block, b); - } - } - - public static void hideBlock(Player p, float x, float y, float z) { - if (makeLocation(x, y, z).getBlock().getType() == Material.AIR) { - p.sendBlockChange(makeLocation(x, y, z), Material.AIR, (byte) 0); - } - } - - private static Location makeLocation(float x, float y, float z) { - location.setX(x); - location.setY(y); - location.setZ(z); - return location; - } - -} diff --git a/BauSystem_Main/src/de/steamwar/bausystem/tracer/TracerUpdater.java b/BauSystem_Main/src/de/steamwar/bausystem/tracer/TracerUpdater.java index b8c32e1..5c26149 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/tracer/TracerUpdater.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/tracer/TracerUpdater.java @@ -2,7 +2,6 @@ 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; diff --git a/BauSystem_Main/src/de/steamwar/bausystem/tracer/manager/ShowManager.java b/BauSystem_Main/src/de/steamwar/bausystem/tracer/manager/ShowManager.java index a2e0b86..572b57a 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/tracer/manager/ShowManager.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/tracer/manager/ShowManager.java @@ -2,7 +2,7 @@ package de.steamwar.bausystem.tracer.manager; import de.steamwar.bausystem.BauSystem; import de.steamwar.bausystem.tracer.TraceCache; -import de.steamwar.bausystem.tracer.trace.TNTTrace; +import de.steamwar.bausystem.tracer.TNTTrace; import org.bukkit.Location; import org.bukkit.entity.Player; diff --git a/BauSystem_Main/src/de/steamwar/bausystem/tracer/manager/TraceManager.java b/BauSystem_Main/src/de/steamwar/bausystem/tracer/manager/TraceManager.java index 4c49725..a88510c 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/tracer/manager/TraceManager.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/tracer/manager/TraceManager.java @@ -1,18 +1,18 @@ package de.steamwar.bausystem.tracer.manager; import de.steamwar.bausystem.BauSystem; -import de.steamwar.bausystem.tracer.trace.TNTTrace; +import de.steamwar.bausystem.tracer.TNTTrace; import org.bukkit.entity.Player; import java.util.*; public class TraceManager { + private TraceManager(){} private static Map traceMap = new HashMap<>(); public static void add(TNTTrace tntTrace) { int id = IDManager.generateID(); - tntTrace.setTntID(id); FrameManager.add(tntTrace.getFrameID(), id); traceMap.put(id, tntTrace); } diff --git a/BauSystem_Main/src/de/steamwar/bausystem/tracer/recorder/RecordManager.java b/BauSystem_Main/src/de/steamwar/bausystem/tracer/recorder/RecordManager.java index a7b22b1..61bf96b 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/tracer/recorder/RecordManager.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/tracer/recorder/RecordManager.java @@ -39,7 +39,7 @@ public class RecordManager { } } - public static void toggleAuto() { + private static void toggleAuto() { switch (status) { case IDLE: status = Status.IDLE_AUTO; @@ -62,7 +62,7 @@ public class RecordManager { return status; } - public static void start() { + private static void start() { if (activeFrameID != null) return; status = Status.RECORD; generateFrameID(); @@ -85,7 +85,7 @@ public class RecordManager { update(); } - public static void stop() { + private static void stop() { if (activeFrameID == null) return; status = Status.IDLE; stopRecording(); diff --git a/BauSystem_Main/src/de/steamwar/bausystem/world/BauScoreboard.java b/BauSystem_Main/src/de/steamwar/bausystem/world/BauScoreboard.java index 7ae7963..52ef4e8 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/world/BauScoreboard.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/world/BauScoreboard.java @@ -3,8 +3,9 @@ package de.steamwar.bausystem.world; import de.steamwar.bausystem.commands.CommandFreeze; import de.steamwar.bausystem.commands.CommandInfo; import de.steamwar.bausystem.commands.CommandTNT; -import de.steamwar.bausystem.tracer.TraceSidebar; +import de.steamwar.bausystem.tracer.manager.FrameManager; import de.steamwar.bausystem.tracer.recorder.RecordManager; +import de.steamwar.bausystem.tracer.recorder.TNTRecorder; import de.steamwar.scoreboard.SWScoreboard; import de.steamwar.scoreboard.ScoreboardCallback; import org.bukkit.entity.Player; @@ -55,11 +56,9 @@ public class BauScoreboard implements Listener { if (RecordManager.getStatus().isTracing()) { strings.add("§3"); - strings.add("§eTrace-Start§8: §7" + new SimpleDateFormat("HH:mm:ss").format(new Date(TraceSidebar.traceStart()))); - strings.add("§eTicks§8: §7" + TraceSidebar.traceTicks()); - strings.add("§eAnzahl TNT§8: §7" + TraceSidebar.traceSize()); - } else if (AutoLoader.hasLoader(p)) { - // strings.add("§3"); + strings.add("§eTrace-Start§8: §7" + new SimpleDateFormat("HH:mm:ss").format(new Date(TNTRecorder.recordStart))); + strings.add("§eTicks§8: §7" + traceTicks()); + strings.add("§eAnzahl TNT§8: §7" + traceSize()); } strings.add("§4"); @@ -67,4 +66,14 @@ public class BauScoreboard implements Listener { return strings; } + private long traceTicks() { + return (System.currentTimeMillis() - TNTRecorder.recordStart) / 50; + } + + private int traceSize() { + if (RecordManager.activeFrameID == null) + return 0; + return FrameManager.get(RecordManager.activeFrameID).size(); + } + } -- 2.39.2 From 5e539a875602d0a8fb851089fb0ec69cd23c8c0e Mon Sep 17 00:00:00 2001 From: Lixfel Date: Sun, 30 Aug 2020 12:01:47 +0200 Subject: [PATCH 4/4] Untangle spagetti code #3 (might not work in this state) --- .../src/de/steamwar/bausystem/BauSystem.java | 2 +- .../bausystem/commands/CommandTrace.java | 163 ++++++++- .../bausystem/tracer/ShowManager.java | 206 +++++++++++ .../steamwar/bausystem/tracer/TNTTrace.java | 3 +- .../steamwar/bausystem/tracer/TNTTracer.java | 9 +- .../steamwar/bausystem/tracer/TraceCache.java | 34 +- .../bausystem/tracer/TraceListener.java | 9 +- .../bausystem/tracer/TraceManager.java | 80 +++++ .../bausystem/tracer/TracerUpdater.java | 47 --- .../tracer/manager/FrameManager.java | 51 --- .../bausystem/tracer/manager/IDManager.java | 11 - .../bausystem/tracer/manager/ShowManager.java | 336 ------------------ .../tracer/manager/TraceManager.java | 61 ---- .../tracer/recorder/RecordManager.java | 30 +- .../tracer/recorder/TNTRecorder.java | 2 +- .../bausystem/world/BauScoreboard.java | 11 +- 16 files changed, 484 insertions(+), 571 deletions(-) create mode 100644 BauSystem_Main/src/de/steamwar/bausystem/tracer/ShowManager.java create mode 100644 BauSystem_Main/src/de/steamwar/bausystem/tracer/TraceManager.java delete mode 100644 BauSystem_Main/src/de/steamwar/bausystem/tracer/TracerUpdater.java delete mode 100644 BauSystem_Main/src/de/steamwar/bausystem/tracer/manager/FrameManager.java delete mode 100644 BauSystem_Main/src/de/steamwar/bausystem/tracer/manager/IDManager.java delete mode 100644 BauSystem_Main/src/de/steamwar/bausystem/tracer/manager/ShowManager.java delete mode 100644 BauSystem_Main/src/de/steamwar/bausystem/tracer/manager/TraceManager.java diff --git a/BauSystem_Main/src/de/steamwar/bausystem/BauSystem.java b/BauSystem_Main/src/de/steamwar/bausystem/BauSystem.java index 093c3bd..7b9c541 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/BauSystem.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/BauSystem.java @@ -3,7 +3,7 @@ package de.steamwar.bausystem; import de.steamwar.bausystem.commands.*; import de.steamwar.bausystem.tracer.TraceListener; import de.steamwar.bausystem.tracer.TNTTracer; -import de.steamwar.bausystem.tracer.manager.ShowManager; +import de.steamwar.bausystem.tracer.ShowManager; import de.steamwar.bausystem.world.AFKStopper; import de.steamwar.bausystem.world.ArenaSection; import de.steamwar.bausystem.world.BauScoreboard; diff --git a/BauSystem_Main/src/de/steamwar/bausystem/commands/CommandTrace.java b/BauSystem_Main/src/de/steamwar/bausystem/commands/CommandTrace.java index 95ac43d..680f6bc 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/commands/CommandTrace.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/commands/CommandTrace.java @@ -2,8 +2,8 @@ package de.steamwar.bausystem.commands; import de.steamwar.bausystem.BauSystem; import de.steamwar.bausystem.Permission; -import de.steamwar.bausystem.tracer.manager.ShowManager; -import de.steamwar.bausystem.tracer.manager.TraceManager; +import de.steamwar.bausystem.tracer.ShowManager; +import de.steamwar.bausystem.tracer.TraceManager; import de.steamwar.bausystem.tracer.recorder.RecordManager; import de.steamwar.bausystem.world.Welt; import org.bukkit.command.Command; @@ -27,12 +27,6 @@ public class CommandTrace implements CommandExecutor { player.sendMessage("§7Optionale Parameter mit §8<>§7, Benötigte Parameter mit §8[]"); } - @Override - public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { - if(!(sender instanceof Player)) return false; - return tracer((Player) sender, args); - } - private boolean permissionCheck(Player player) { if(Welt.noPermission(player, Permission.world)){ player.sendMessage(BauSystem.PREFIX + "§cDu darfst hier nicht den TNT-Tracer nutzen"); @@ -41,7 +35,11 @@ public class CommandTrace implements CommandExecutor { return true; } - private boolean tracer(Player player, String[] args) { + @Override + public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { + if(!(sender instanceof Player)) return false; + Player player = (Player) sender; + if(args.length == 0){ help(player); return false; @@ -61,7 +59,7 @@ public class CommandTrace implements CommandExecutor { case "toggleshow": case "interpolate": case "distance": - if (ShowManager.tracer(player, args)) { + if (tracer(player, args)) { help(player); } break; @@ -70,7 +68,7 @@ public class CommandTrace implements CommandExecutor { // player.sendMessage(BauSystem.PREFIX + "§cNoch in Bau"); break; case "delete": - if (TraceManager.delete(player, args)) { + if (delete(player, args)) { help(player); } break; @@ -80,4 +78,147 @@ public class CommandTrace implements CommandExecutor { return false; } + private boolean delete(Player player, String[] args) { + if (args.length == 2) { + try { + TraceManager.delete(Integer.parseInt(args[1])); + player.sendMessage(BauSystem.PREFIX + "§cTNT-Positionen mit ID " + args[1] + " gelöscht"); + ShowManager.globalDirty(); + return false; + } catch (NumberFormatException e) { + return true; + } + } + TraceManager.deleteAll(); + player.sendMessage(BauSystem.PREFIX + "§cAlle TNT-Positionen gelöscht"); + ShowManager.globalDirty(); + return false; + } + + + public boolean tracer(Player player, String[] args) { + ShowManager.ShowStatus showStatus = ShowManager.showMap.get(player.getUniqueId().toString()); + showStatus.dirty = true; + switch (args[0].toLowerCase()) { + case "show": + showStatus.show(); + if (args.length == 2) { + return tracerShow(player, showStatus, args); + } + player.sendMessage(BauSystem.PREFIX + "§aAlle TNT-Positionen angezeigt"); + break; + case "distance": + if (args.length != 2) { + return true; + } + if (args[1].equalsIgnoreCase("default")) { + ShowManager.get(player).setSlope(7.0); + player.sendMessage(BauSystem.PREFIX + "§aAnzeige Distanz auf " + ShowManager.get(player).slopeHeight + " gesetzt"); + return false; + } + try { + double radius = Double.parseDouble(args[1]); + radius = (double)(int)(radius * 10) / 10; + ShowManager.get(player).setSlope(radius); + player.sendMessage(BauSystem.PREFIX + "§aAnzeige Distanz auf " + ShowManager.get(player).slopeHeight + " gesetzt"); + return false; + } catch (NumberFormatException e) { + return true; + } + case "hide": + showStatus.hide(); + if (args.length == 2) { + return traceID(player, args[1], showStatus::removeSelection, new String[]{BauSystem.PREFIX + "§aTNT-Positionen des TNT mit ID {} versteckt"}); + } + player.sendMessage(BauSystem.PREFIX + "§aAlle TNT-Positionen versteckt"); + break; + case "toggleshow": + if (args.length == 2) { + return traceID(player, args[1], showStatus::toggleSelection, new String[]{BauSystem.PREFIX + "§aTNT-Positionen des TNT mit ID {} angezeigt"}, new String[]{BauSystem.PREFIX + "§aTNT-Positionen des TNT mit ID {} versteckt"}); + } + return true; + case "interpolate": + if (args.length == 2) { + return tracerInterpolate(player, showStatus, args); + } + return true; + default: + break; + } + return false; + } + + private boolean tracerShow(Player player, ShowManager.ShowStatus showStatus, String[] args) { + switch (args[1].toLowerCase()) { + case "block": + case "blocks": + showStatus.displayType = ShowManager.DisplayType.Block; + player.sendMessage(BauSystem.PREFIX + "§aAlle TNT-Positionen, als Blöcke, angezeigt"); + break; + case "particle": + case "particles": + case "default": + showStatus.displayType = ShowManager.DisplayType.Particle; + player.sendMessage(BauSystem.PREFIX + "§aAlle TNT-Positionen, als Partikel, angezeigt"); + break; + default: + return traceID(player, args[1], showStatus::addSelection, new String[]{BauSystem.PREFIX + "§aTNT-Positionen des TNT mit ID {} angezeigt"}); + } + return false; + } + + private boolean tracerInterpolate(Player player, ShowManager.ShowStatus showStatus, String[] args) { + switch (args[1].toLowerCase()) { + case "none": + showStatus.displayMode = ShowManager.DisplayMode.NONE; + player.sendMessage(BauSystem.PREFIX + "§aInterpolation auf §enone §agesetzt"); + break; + case "yaxis": + case "y": + showStatus.displayMode = ShowManager.DisplayMode.Y_AXIS; + player.sendMessage(BauSystem.PREFIX + "§aInterpolation auf §eyaxis §agesetzt"); + break; + case "all": + case "allaxis": + case "xyz": + case "default": + showStatus.displayMode = ShowManager.DisplayMode.ALL; + player.sendMessage(BauSystem.PREFIX + "§aInterpolation auf §eall §agesetzt"); + break; + default: + return true; + } + return false; + } + + + private boolean traceID(Player player, String number, ShowManager.Mode mode, String[] messages) { + try { + mode.run(Integer.parseInt(number)); + for (String s : messages) { + player.sendMessage(s.replace("{}", number)); + } + } catch (NumberFormatException e) { + return true; + } + return false; + } + + private boolean traceID(Player player, String number, ShowManager.ModeChoose mode, String[] messagesTrue, String[] messagesFalse) { + try { + boolean result = mode.run(Integer.parseInt(number)); + if (result) { + for (String s : messagesTrue) { + player.sendMessage(s.replace("{}", number)); + } + } else { + for (String s : messagesFalse) { + player.sendMessage(s.replace("{}", number)); + } + } + } catch (NumberFormatException e) { + return true; + } + return false; + } } diff --git a/BauSystem_Main/src/de/steamwar/bausystem/tracer/ShowManager.java b/BauSystem_Main/src/de/steamwar/bausystem/tracer/ShowManager.java new file mode 100644 index 0000000..83f8237 --- /dev/null +++ b/BauSystem_Main/src/de/steamwar/bausystem/tracer/ShowManager.java @@ -0,0 +1,206 @@ +package de.steamwar.bausystem.tracer; + +import org.bukkit.Location; +import org.bukkit.entity.Player; + +import java.util.*; + +public class ShowManager { + + public enum DisplayMode { + NONE, + Y_AXIS, + ALL + } + + public enum DisplayType { + Particle, + Block + } + + public enum ShowSelection { + NONE, + SELECTIVE, + ALL + } + + + public interface Mode { + void run(int tntID); + } + + public interface ModeChoose { + boolean run(int tntID); + } + + + public static class ShowStatus { + + private ShowSelection showSelection = ShowSelection.NONE; + public DisplayType displayType = DisplayType.Particle; + public DisplayMode displayMode = DisplayMode.ALL; + + public boolean dirty = true; + private int count = 0; + + private Set selected = new HashSet<>(); + + private void clear() { + if (showSelection == ShowSelection.NONE) return; + selected.clear(); + } + + public void show() { + showSelection = ShowSelection.ALL; + } + + public void hide() { + clear(); + showSelection = ShowSelection.NONE; + } + + public void addSelection(int id) { + if (showSelection == ShowSelection.ALL) return; + if (showSelection == ShowSelection.NONE) showSelection = ShowSelection.SELECTIVE; + selected.addAll(TraceManager.getFrame(id)); + if (selected.size() == TraceManager.getAllTraces().size()) { + showSelection = ShowSelection.ALL; + clear(); + } + } + + public void removeSelection(int id) { + if (showSelection == ShowSelection.NONE) return; + if (showSelection == ShowSelection.ALL) selected = TraceManager.getAllTraces(); + selected.removeAll(TraceManager.getFrame(id)); + showSelection = ShowSelection.SELECTIVE; + if (selected.isEmpty()) showSelection = ShowSelection.NONE; + } + + public boolean toggleSelection(int id) { + if (showSelection == ShowSelection.NONE) { + addSelection(id); + return true; + } + if (showSelection == ShowSelection.ALL) { + removeSelection(id); + return false; + } + if (selected.contains(id)) { + removeSelection(id); + return false; + } else { + addSelection(id); + return true; + } + } + + public DisplayType getDisplayType() { + return displayType; + } + + public DisplayMode getDisplayMode() { + return displayMode; + } + + public LinkedList getTraces() { + if (showSelection == ShowSelection.NONE) return new LinkedList<>(); + Set tntIDs; + if (showSelection == ShowSelection.ALL) { + tntIDs = TraceManager.getAllTraces(); + } else { + tntIDs = selected; + } + + LinkedList traces = new LinkedList<>(); + for (int traceId : tntIDs) { + TNTTrace trace = TraceManager.getTrace(traceId); + if (trace == null) continue; + traces.add(trace); + } + return traces; + } + + public boolean isDirty() { + if (displayType == DisplayType.Block) { + count++; + } + if (count >= 10) { + count = 0; + return true; + } + if (dirty) { + dirty = false; + return true; + } + return false; + } + + public double slopeHeight = 7.0; + + private int size = 0; + + private TraceCache.Loc loc = null; + + public double getShowRadius() { + double maxRadius = 80.0; + if (showSelection == ShowSelection.NONE) return maxRadius; + if (showSelection == ShowSelection.ALL) size = TraceManager.getAllTraces().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) { + double minSlope = 3.0; + double maxSlope = 11.0; + slopeHeight = Math.min(Math.max(slope, minSlope), maxSlope); + } + + public void move(Player player) { + if (loc != null && !loc.remove(player, 4)) { + return; + } + Location location = player.getLocation(); + loc = new TraceCache.Loc((float) location.getX(), (float) location.getY(), (float) location.getZ()); + dirty = true; + } + + } + + public static Map showMap = new HashMap<>(); + + public static void add(Player p) { + showMap.put(p.getUniqueId().toString(), new ShowStatus()); + } + + public static ShowStatus get(Player p) { + if (!showMap.containsKey(p.getUniqueId().toString())) add(p); + return showMap.get(p.getUniqueId().toString()); + } + + public static void traceAdd() { + for (Map.Entry entry : showMap.entrySet()) { + if (entry.getValue().showSelection == ShowSelection.ALL) { + entry.getValue().dirty = true; + } + } + } + + public static void traceRemove(int id) { + for (Map.Entry entry : showMap.entrySet()) { + entry.getValue().removeSelection(id); + } + } + + public static void globalDirty() { + for (Map.Entry entry : showMap.entrySet()) { + entry.getValue().dirty = true; + } + } + +} diff --git a/BauSystem_Main/src/de/steamwar/bausystem/tracer/TNTTrace.java b/BauSystem_Main/src/de/steamwar/bausystem/tracer/TNTTrace.java index 45cb02b..eb11d85 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/tracer/TNTTrace.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/tracer/TNTTrace.java @@ -1,6 +1,5 @@ package de.steamwar.bausystem.tracer; -import de.steamwar.bausystem.tracer.manager.ShowManager; import org.bukkit.Location; import java.util.Arrays; @@ -44,7 +43,7 @@ public class TNTTrace { return positions.length; } - public int getFrameID() { + int getFrameID() { return frameID; } diff --git a/BauSystem_Main/src/de/steamwar/bausystem/tracer/TNTTracer.java b/BauSystem_Main/src/de/steamwar/bausystem/tracer/TNTTracer.java index 10f2a74..d5c5574 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/tracer/TNTTracer.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/tracer/TNTTracer.java @@ -1,7 +1,6 @@ package de.steamwar.bausystem.tracer; import de.steamwar.bausystem.BauSystem; -import de.steamwar.bausystem.tracer.manager.ShowManager; import de.steamwar.core.Core; import net.md_5.bungee.api.ChatMessageType; import net.md_5.bungee.api.chat.TextComponent; @@ -44,19 +43,19 @@ public class TNTTracer { private static void run(){ for (Player p : Bukkit.getOnlinePlayers()) { if (DEBUG) { - String actionBar = TracerUpdater.actionBar(p); + String actionBar = "§e" + TraceManager.getAllTraces().size() + " §cTraces §e" + ShowManager.get(p).getShowRadius() + " §cRadius"; p.spigot().sendMessage(ChatMessageType.ACTION_BAR, TextComponent.fromLegacyText(actionBar)); } - boolean dirty = TracerUpdater.needsUpdate(p); - if (TracerUpdater.getDisplayType(p) == ShowManager.DisplayType.Block && !dirty) { + boolean dirty = ShowManager.get(p).isDirty(); + if (ShowManager.get(p).getDisplayType() == ShowManager.DisplayType.Block && !dirty) { continue; } Set toHide = traceCache.update(p, dirty); Set toShow = traceCache.get(p); hideBlockTraces(toHide, p); - showTraces(toShow, TracerUpdater.getDisplayType(p), p); + showTraces(toShow, ShowManager.get(p).getDisplayType(), p); } } diff --git a/BauSystem_Main/src/de/steamwar/bausystem/tracer/TraceCache.java b/BauSystem_Main/src/de/steamwar/bausystem/tracer/TraceCache.java index fd69602..27bedf0 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/tracer/TraceCache.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/tracer/TraceCache.java @@ -1,6 +1,5 @@ package de.steamwar.bausystem.tracer; -import de.steamwar.bausystem.tracer.manager.ShowManager; import org.bukkit.entity.Player; import java.util.*; @@ -22,24 +21,24 @@ public class TraceCache { ShowManager.DisplayType displayMode; if (!playerMap.containsKey(key)) { locOld = new HashSet<>(); - displayMode = TracerUpdater.getDisplayType(player); + displayMode = getDisplayType(player); } else { locOld = playerMap.get(key); displayMode = playerDisplayMap.get(key); } Set locSet = new HashSet<>(); - TracerUpdater.updatePoints(player).forEach(loc -> { + updatePoints(player).forEach(loc -> { loc.updatePoint = true; locSet.add(loc); }); - TracerUpdater.updateLocations(player).forEach(loc -> { + updateLocations(player).forEach(loc -> { loc.updatePoint = false; locSet.add(loc); }); playerMap.put(key, locSet); - ShowManager.DisplayType currentMode = TracerUpdater.getDisplayType(player); + ShowManager.DisplayType currentMode = getDisplayType(player); playerDisplayMap.put(key, currentMode); if (currentMode == ShowManager.DisplayType.Particle && displayMode == ShowManager.DisplayType.Block) return locOld; @@ -124,4 +123,29 @@ public class TraceCache { return locOld; } + private Set updateLocations(Player player) { + Iterator traces = ShowManager.get(player).getTraces().descendingIterator(); + Set 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; + } + + private Set updatePoints(Player player) { + Iterator traces = ShowManager.get(player).getTraces().descendingIterator(); + Set 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; + } + + private ShowManager.DisplayType getDisplayType(Player player) { + return ShowManager.get(player).getDisplayType(); + } } diff --git a/BauSystem_Main/src/de/steamwar/bausystem/tracer/TraceListener.java b/BauSystem_Main/src/de/steamwar/bausystem/tracer/TraceListener.java index 7a3848f..abd106a 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/tracer/TraceListener.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/tracer/TraceListener.java @@ -1,7 +1,5 @@ package de.steamwar.bausystem.tracer; -import de.steamwar.bausystem.tracer.manager.ShowManager; -import de.steamwar.bausystem.tracer.manager.TraceManager; import de.steamwar.bausystem.tracer.recorder.RecordManager; import org.bukkit.entity.Player; import org.bukkit.entity.TNTPrimed; @@ -18,12 +16,11 @@ public class TraceListener implements Listener { private static final Map tntMap = new HashMap<>(); - public static void add(Stream tntPrimedStream, int frameID) { + public static void onTick(Stream tntPrimedStream) { tntPrimedStream.forEach(tnt -> { TNTTrace trace; if (!tntMap.containsKey(tnt)) { - trace = new TNTTrace(frameID); - TraceManager.add(trace); + trace = TraceManager.createTrace(); tntMap.put(tnt, trace); } else { trace = tntMap.get(tnt); @@ -42,7 +39,7 @@ public class TraceListener implements Listener { if (RecordManager.getStatus() == RecordManager.Status.RECORD_AUTO) RecordManager.updateAuto(); - TNTTrace trace = tntMap.get((TNTPrimed) event.getEntity()); + TNTTrace trace = tntMap.remove((TNTPrimed) event.getEntity()); if (trace != null) trace.cleanUp(); } diff --git a/BauSystem_Main/src/de/steamwar/bausystem/tracer/TraceManager.java b/BauSystem_Main/src/de/steamwar/bausystem/tracer/TraceManager.java new file mode 100644 index 0000000..84f9184 --- /dev/null +++ b/BauSystem_Main/src/de/steamwar/bausystem/tracer/TraceManager.java @@ -0,0 +1,80 @@ +package de.steamwar.bausystem.tracer; + +import java.util.*; + +public class TraceManager { + private TraceManager(){} + + private static Map> frameMap = new HashMap<>(); + private static Map traceMap = new HashMap<>(); + + private static int currentFrame; + + private static int currentID = 0; + private static synchronized int generateID() { + return currentID++; + } + + public static void startFrame() { + currentFrame = generateID(); + frameMap.put(currentFrame, new ArrayList<>()); + } + + public static TNTTrace createTrace() { + TNTTrace trace = new TNTTrace(currentFrame); + int id = generateID(); + traceMap.put(id, trace); + frameMap.get(currentFrame).add(id); + return trace; + } + + public static TNTTrace getTrace(int id) { + return traceMap.get(id); + } + + public static Set getAllTraces() { + return traceMap.keySet(); + } + + public static List getFrame(int frameID) { + return new ArrayList<>(frameMap.getOrDefault(frameID, new ArrayList<>())); + } + + public static int currentFrameSize(){ + return frameMap.get(currentFrame).size(); + } + + public static void stopFrame() { + testRemoveFrame(currentFrame); + } + + private static void testRemoveFrame(int frame){ + if (!frameMap.containsKey(frame) || !frameMap.get(frame).isEmpty()) + return; + frameMap.remove(frame); + } + + public static void deleteAll() { + for (Integer integer : new HashSet<>(traceMap.keySet())) { + delete(integer); + } + } + + public static void delete(int id) { + TNTTrace trace = traceMap.remove(id); + if(trace == null){ + List frame = frameMap.get(id); + if(frame == null) + return; + + for(int t : new HashSet<>(frame)){ + delete(t); + } + return; + } + int frameID = trace.getFrameID(); + ShowManager.traceRemove(id); + frameMap.get(frameID).remove((Integer) id); + testRemoveFrame(frameID); + } +} diff --git a/BauSystem_Main/src/de/steamwar/bausystem/tracer/TracerUpdater.java b/BauSystem_Main/src/de/steamwar/bausystem/tracer/TracerUpdater.java deleted file mode 100644 index 5c26149..0000000 --- a/BauSystem_Main/src/de/steamwar/bausystem/tracer/TracerUpdater.java +++ /dev/null @@ -1,47 +0,0 @@ -package de.steamwar.bausystem.tracer; - -import de.steamwar.bausystem.tracer.manager.ShowManager; -import de.steamwar.bausystem.tracer.manager.TraceManager; -import org.bukkit.entity.Player; - -import java.util.HashSet; -import java.util.Iterator; -import java.util.Set; - -class TracerUpdater { - private TracerUpdater(){} - - static Set updateLocations(Player player) { - Iterator traces = ShowManager.get(player).getTraces().descendingIterator(); - Set 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; - } - - static Set updatePoints(Player player) { - Iterator traces = ShowManager.get(player).getTraces().descendingIterator(); - Set 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"; - } -} diff --git a/BauSystem_Main/src/de/steamwar/bausystem/tracer/manager/FrameManager.java b/BauSystem_Main/src/de/steamwar/bausystem/tracer/manager/FrameManager.java deleted file mode 100644 index 22bb52b..0000000 --- a/BauSystem_Main/src/de/steamwar/bausystem/tracer/manager/FrameManager.java +++ /dev/null @@ -1,51 +0,0 @@ -package de.steamwar.bausystem.tracer.manager; - -import java.util.*; - -public class FrameManager { - - private static Map> frameMap = new HashMap<>(); - - public static int createFrame() { - int id = IDManager.generateID(); - frameMap.put(id, new ArrayList<>()); - return id; - } - - static void add(int frameID, int tntID) { - if (!frameMap.containsKey(frameID)) { - return; - } - frameMap.get(frameID).add(tntID); - } - - public static List get(int frameID) { - if (!frameMap.containsKey(frameID)) { - return Arrays.asList(frameID); - } - return new ArrayList<>(frameMap.get(frameID)); - } - - public static List get() { - return new LinkedList<>(frameMap.keySet()); - } - - static void remove(int frameID, int traceID) { - if (!frameMap.containsKey(frameID)) { - return; - } - frameMap.get(frameID).remove((Integer) traceID); - cleanUp(frameID); - } - - public static void cleanUp(int frameID) { - if (!frameMap.containsKey(frameID)) { - return; - } - if (!frameMap.get(frameID).isEmpty()) { - return; - } - frameMap.remove(frameID); - } - -} diff --git a/BauSystem_Main/src/de/steamwar/bausystem/tracer/manager/IDManager.java b/BauSystem_Main/src/de/steamwar/bausystem/tracer/manager/IDManager.java deleted file mode 100644 index 3a133a4..0000000 --- a/BauSystem_Main/src/de/steamwar/bausystem/tracer/manager/IDManager.java +++ /dev/null @@ -1,11 +0,0 @@ -package de.steamwar.bausystem.tracer.manager; - -public class IDManager { - - private static int currentID = 0; - - static synchronized int generateID() { - return currentID++; - } - -} diff --git a/BauSystem_Main/src/de/steamwar/bausystem/tracer/manager/ShowManager.java b/BauSystem_Main/src/de/steamwar/bausystem/tracer/manager/ShowManager.java deleted file mode 100644 index 572b57a..0000000 --- a/BauSystem_Main/src/de/steamwar/bausystem/tracer/manager/ShowManager.java +++ /dev/null @@ -1,336 +0,0 @@ -package de.steamwar.bausystem.tracer.manager; - -import de.steamwar.bausystem.BauSystem; -import de.steamwar.bausystem.tracer.TraceCache; -import de.steamwar.bausystem.tracer.TNTTrace; -import org.bukkit.Location; -import org.bukkit.entity.Player; - -import java.util.HashMap; -import java.util.Iterator; -import java.util.LinkedList; -import java.util.Map; - -public class ShowManager { - - public enum DisplayMode { - NONE, - Y_AXIS, - ALL - } - - public enum DisplayType { - Particle, - Block - } - - public enum ShowSelection { - NONE, - SELECTIVE, - ALL - } - - public static boolean tracer(Player player, String[] args) { - ShowStatus showStatus = showMap.get(player.getUniqueId().toString()); - showStatus.dirty = true; - switch (args[0].toLowerCase()) { - case "show": - showStatus.show(); - if (args.length == 2) { - return tracerShow(player, showStatus, args); - } - player.sendMessage(BauSystem.PREFIX + "§aAlle TNT-Positionen angezeigt"); - break; - case "distance": - if (args.length != 2) { - return true; - } - if (args[1].equalsIgnoreCase("default")) { - ShowManager.get(player).setSlope(7.0); - player.sendMessage(BauSystem.PREFIX + "§aAnzeige Distanz auf " + ShowManager.get(player).slopeHeight + " gesetzt"); - return false; - } - try { - double radius = Double.parseDouble(args[1]); - radius = (double)(int)(radius * 10) / 10; - ShowManager.get(player).setSlope(radius); - player.sendMessage(BauSystem.PREFIX + "§aAnzeige Distanz auf " + ShowManager.get(player).slopeHeight + " gesetzt"); - return false; - } catch (NumberFormatException e) { - return true; - } - case "hide": - showStatus.hide(); - if (args.length == 2) { - return traceID(player, args[1], showStatus::removeSelection, new String[]{BauSystem.PREFIX + "§aTNT-Positionen des TNT mit ID {} versteckt"}); - } - player.sendMessage(BauSystem.PREFIX + "§aAlle TNT-Positionen versteckt"); - break; - case "toggleshow": - if (args.length == 2) { - return traceID(player, args[1], showStatus::toggleSelection, new String[]{BauSystem.PREFIX + "§aTNT-Positionen des TNT mit ID {} angezeigt"}, new String[]{BauSystem.PREFIX + "§aTNT-Positionen des TNT mit ID {} versteckt"}); - } - return true; - case "interpolate": - if (args.length == 2) { - return tracerInterpolate(player, showStatus, args); - } - return true; - default: - break; - } - return false; - } - - private static boolean tracerShow(Player player, ShowStatus showStatus, String[] args) { - switch (args[1].toLowerCase()) { - case "block": - case "blocks": - showStatus.displayType = DisplayType.Block; - player.sendMessage(BauSystem.PREFIX + "§aAlle TNT-Positionen, als Blöcke, angezeigt"); - break; - case "particle": - case "particles": - case "default": - showStatus.displayType = DisplayType.Particle; - player.sendMessage(BauSystem.PREFIX + "§aAlle TNT-Positionen, als Partikel, angezeigt"); - break; - default: - return traceID(player, args[1], showStatus::addSelection, new String[]{BauSystem.PREFIX + "§aTNT-Positionen des TNT mit ID {} angezeigt"}); - } - return false; - } - - private static boolean tracerInterpolate(Player player, ShowStatus showStatus, String[] args) { - switch (args[1].toLowerCase()) { - case "none": - showStatus.displayMode = DisplayMode.NONE; - player.sendMessage(BauSystem.PREFIX + "§aInterpolation auf §enone §agesetzt"); - break; - case "yaxis": - case "y": - showStatus.displayMode = DisplayMode.Y_AXIS; - player.sendMessage(BauSystem.PREFIX + "§aInterpolation auf §eyaxis §agesetzt"); - break; - case "all": - case "allaxis": - case "xyz": - case "default": - showStatus.displayMode = DisplayMode.ALL; - player.sendMessage(BauSystem.PREFIX + "§aInterpolation auf §eall §agesetzt"); - break; - default: - return true; - } - return false; - } - - private interface Mode { - void run(int tntID); - } - - private interface ModeChoose { - boolean run(int tntID); - } - - private static boolean traceID(Player player, String number, Mode mode, String[] messages) { - try { - mode.run(Integer.parseInt(number)); - for (String s : messages) { - player.sendMessage(s.replace("{}", number)); - } - } catch (NumberFormatException e) { - return true; - } - return false; - } - - private static boolean traceID(Player player, String number, ModeChoose mode, String[] messagesTrue, String[] messagesFalse) { - try { - boolean result = mode.run(Integer.parseInt(number)); - if (result) { - for (String s : messagesTrue) { - player.sendMessage(s.replace("{}", number)); - } - } else { - for (String s : messagesFalse) { - player.sendMessage(s.replace("{}", number)); - } - } - } catch (NumberFormatException e) { - return true; - } - return false; - } - - public static class ShowStatus { - - private ShowSelection showSelection = ShowSelection.NONE; - private DisplayType displayType = DisplayType.Particle; - private DisplayMode displayMode = DisplayMode.ALL; - - private boolean dirty = true; - private int count = 0; - - private LinkedList selected = new LinkedList<>(); - - private void clear() { - if (showSelection == ShowSelection.NONE) return; - selected.clear(); - } - - void show() { - showSelection = ShowSelection.ALL; - } - - void hide() { - clear(); - showSelection = ShowSelection.NONE; - } - - void addSelection(int id) { - if (showSelection == ShowSelection.ALL) return; - if (showSelection == ShowSelection.NONE) showSelection = ShowSelection.SELECTIVE; - selected.addAll(FrameManager.get(id)); - if (selected.size() == TraceManager.get().size()) { - showSelection = ShowSelection.ALL; - clear(); - } - } - - void removeSelection(int id) { - if (showSelection == ShowSelection.NONE) return; - if (showSelection == ShowSelection.ALL) selected = TraceManager.get(); - selected.removeAll(FrameManager.get(id)); - showSelection = ShowSelection.SELECTIVE; - if (selected.isEmpty()) showSelection = ShowSelection.NONE; - } - - boolean toggleSelection(int id) { - if (showSelection == ShowSelection.NONE) { - addSelection(id); - return true; - } - if (showSelection == ShowSelection.ALL) { - removeSelection(id); - return false; - } - if (selected.contains(id)) { - removeSelection(id); - return false; - } else { - addSelection(id); - return true; - } - } - - public DisplayType getDisplayType() { - return displayType; - } - - public DisplayMode getDisplayMode() { - return displayMode; - } - - public LinkedList getTraces() { - if (showSelection == ShowSelection.NONE) return new LinkedList<>(); - LinkedList tntIDs; - if (showSelection == ShowSelection.ALL) { - tntIDs = TraceManager.get(); - } else { - tntIDs = selected; - } - - LinkedList traces = new LinkedList<>(); - Iterator iterator = tntIDs.descendingIterator(); - while (iterator.hasNext()) { - TNTTrace trace = TraceManager.get(iterator.next()); - if (trace == null) continue; - traces.add(trace); - } - return traces; - } - - public boolean isDirty() { - if (displayType == DisplayType.Block) { - count++; - } - if (count >= 10) { - count = 0; - return true; - } - if (dirty) { - dirty = false; - return true; - } - return false; - } - - private double slopeHeight = 7.0; - - private int size = 0; - - 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); - } - - void setSlope(double slope) { - double minSlope = 3.0; - double maxSlope = 11.0; - slopeHeight = Math.min(Math.max(slope, minSlope), maxSlope); - } - - public void move(Player player) { - if (loc != null && !loc.remove(player, 4)) { - return; - } - Location location = player.getLocation(); - loc = new TraceCache.Loc((float) location.getX(), (float) location.getY(), (float) location.getZ()); - dirty = true; - } - - } - - private static Map showMap = new HashMap<>(); - - public static void add(Player p) { - showMap.put(p.getUniqueId().toString(), new ShowStatus()); - } - - public static ShowStatus get(Player p) { - if (!showMap.containsKey(p.getUniqueId().toString())) add(p); - return showMap.get(p.getUniqueId().toString()); - } - - public static void traceAdd() { - for (Map.Entry entry : showMap.entrySet()) { - if (entry.getValue().showSelection == ShowSelection.ALL) { - entry.getValue().dirty = true; - } - } - } - - static void traceRemove(int id) { - for (Map.Entry entry : showMap.entrySet()) { - entry.getValue().removeSelection(id); - } - } - - static void globalDirty() { - for (Map.Entry entry : showMap.entrySet()) { - entry.getValue().dirty = true; - } - } - -} diff --git a/BauSystem_Main/src/de/steamwar/bausystem/tracer/manager/TraceManager.java b/BauSystem_Main/src/de/steamwar/bausystem/tracer/manager/TraceManager.java deleted file mode 100644 index a88510c..0000000 --- a/BauSystem_Main/src/de/steamwar/bausystem/tracer/manager/TraceManager.java +++ /dev/null @@ -1,61 +0,0 @@ -package de.steamwar.bausystem.tracer.manager; - -import de.steamwar.bausystem.BauSystem; -import de.steamwar.bausystem.tracer.TNTTrace; -import org.bukkit.entity.Player; - -import java.util.*; - -public class TraceManager { - private TraceManager(){} - - private static Map traceMap = new HashMap<>(); - - public static void add(TNTTrace tntTrace) { - int id = IDManager.generateID(); - FrameManager.add(tntTrace.getFrameID(), id); - traceMap.put(id, tntTrace); - } - - private static void remove() { - Iterator iterator = get().descendingIterator(); - while (iterator.hasNext()) { - remove(iterator.next()); - } - } - - private static void remove(int id) { - if (!traceMap.containsKey(id)) { - return; - } - int frameID = traceMap.remove(id).getFrameID(); - ShowManager.traceRemove(id); - FrameManager.remove(frameID, id); - } - - public static TNTTrace get(int id) { - return traceMap.get(id); - } - - public static LinkedList get() { - return new LinkedList<>(traceMap.keySet()); - } - - public static boolean delete(Player player, String[] args) { - if (args.length == 2) { - try { - remove(Integer.parseInt(args[1])); - player.sendMessage(BauSystem.PREFIX + "§cTNT-Positionen mit ID " + args[1] + " gelöscht"); - ShowManager.globalDirty(); - return false; - } catch (NumberFormatException e) { - return true; - } - } - remove(); - player.sendMessage(BauSystem.PREFIX + "§cAlle TNT-Positionen gelöscht"); - ShowManager.globalDirty(); - return false; - } - -} diff --git a/BauSystem_Main/src/de/steamwar/bausystem/tracer/recorder/RecordManager.java b/BauSystem_Main/src/de/steamwar/bausystem/tracer/recorder/RecordManager.java index 61bf96b..7e80b98 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/tracer/recorder/RecordManager.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/tracer/recorder/RecordManager.java @@ -1,7 +1,7 @@ package de.steamwar.bausystem.tracer.recorder; import de.steamwar.bausystem.BauSystem; -import de.steamwar.bausystem.tracer.manager.FrameManager; +import de.steamwar.bausystem.tracer.TraceManager; import net.md_5.bungee.api.ChatMessageType; import net.md_5.bungee.api.chat.TextComponent; import org.bukkit.Bukkit; @@ -13,7 +13,6 @@ import static de.steamwar.bausystem.tracer.recorder.TNTRecorder.*; public class RecordManager { static Status status = Status.IDLE; - public static Integer activeFrameID = null; static final World world = Bukkit.getWorlds().get(0); @@ -63,52 +62,33 @@ public class RecordManager { } private static void start() { - if (activeFrameID != null) return; status = Status.RECORD; - generateFrameID(); + TraceManager.startFrame(); startRecording(); } public static void startAuto() { - if (activeFrameID != null) { - update(); - return; - } status = Status.RECORD_AUTO; Bukkit.getOnlinePlayers().forEach(player -> player.spigot().sendMessage(ChatMessageType.ACTION_BAR, TextComponent.fromLegacyText("§7Automatischer TNT-Tracer §8- §aAufnahme gestartet"))); - generateFrameID(); + TraceManager.startFrame(); startRecording(); } public static void updateAuto() { - if (activeFrameID == null) return; update(); } private static void stop() { - if (activeFrameID == null) return; status = Status.IDLE; stopRecording(); - cleanUPFrameID(); + TraceManager.stopFrame(); } static void stopAuto() { - if (activeFrameID == null) return; status = Status.IDLE_AUTO; Bukkit.getOnlinePlayers().forEach(player -> player.spigot().sendMessage(ChatMessageType.ACTION_BAR, TextComponent.fromLegacyText("§7Automatischer TNT-Tracer §8- §aAufnahme gestoppt"))); stopRecording(); - cleanUPFrameID(); - } - - private static void generateFrameID() { - if (activeFrameID != null) return;; - activeFrameID = FrameManager.createFrame(); - } - - private static void cleanUPFrameID() { - if (activeFrameID == null) return; - FrameManager.cleanUp(activeFrameID); - activeFrameID = null; + TraceManager.stopFrame(); } public enum Status { diff --git a/BauSystem_Main/src/de/steamwar/bausystem/tracer/recorder/TNTRecorder.java b/BauSystem_Main/src/de/steamwar/bausystem/tracer/recorder/TNTRecorder.java index 9d98f63..0d47a1e 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/tracer/recorder/TNTRecorder.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/tracer/recorder/TNTRecorder.java @@ -41,7 +41,7 @@ public class TNTRecorder { .stream() .filter(e -> e instanceof TNTPrimed) .map(e -> (TNTPrimed)e); - TraceListener.add(tntPrimedStream, RecordManager.activeFrameID); + TraceListener.onTick(tntPrimedStream); if (RecordManager.status == RecordManager.Status.RECORD_AUTO && System.currentTimeMillis() - lastExplosion > 4500) { stopAuto(); diff --git a/BauSystem_Main/src/de/steamwar/bausystem/world/BauScoreboard.java b/BauSystem_Main/src/de/steamwar/bausystem/world/BauScoreboard.java index 52ef4e8..433b51e 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/world/BauScoreboard.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/world/BauScoreboard.java @@ -3,7 +3,7 @@ package de.steamwar.bausystem.world; import de.steamwar.bausystem.commands.CommandFreeze; import de.steamwar.bausystem.commands.CommandInfo; import de.steamwar.bausystem.commands.CommandTNT; -import de.steamwar.bausystem.tracer.manager.FrameManager; +import de.steamwar.bausystem.tracer.TraceManager; import de.steamwar.bausystem.tracer.recorder.RecordManager; import de.steamwar.bausystem.tracer.recorder.TNTRecorder; import de.steamwar.scoreboard.SWScoreboard; @@ -58,7 +58,7 @@ public class BauScoreboard implements Listener { strings.add("§3"); strings.add("§eTrace-Start§8: §7" + new SimpleDateFormat("HH:mm:ss").format(new Date(TNTRecorder.recordStart))); strings.add("§eTicks§8: §7" + traceTicks()); - strings.add("§eAnzahl TNT§8: §7" + traceSize()); + strings.add("§eAnzahl TNT§8: §7" + TraceManager.currentFrameSize()); } strings.add("§4"); @@ -69,11 +69,4 @@ public class BauScoreboard implements Listener { private long traceTicks() { return (System.currentTimeMillis() - TNTRecorder.recordStart) / 50; } - - private int traceSize() { - if (RecordManager.activeFrameID == null) - return 0; - return FrameManager.get(RecordManager.activeFrameID).size(); - } - } -- 2.39.2