From c0b35c5da70dac27e9cbca63baba44161aad39f7 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Sun, 2 May 2021 17:08:27 +0200 Subject: [PATCH] Optimize Depth System --- .../testblock/depthcounter/Depth.java | 58 +++++++++++++------ 1 file changed, 39 insertions(+), 19 deletions(-) diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/testblock/depthcounter/Depth.java b/BauSystem_Main/src/de/steamwar/bausystem/features/testblock/depthcounter/Depth.java index b8753a34..af7ed51e 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/testblock/depthcounter/Depth.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/testblock/depthcounter/Depth.java @@ -28,22 +28,26 @@ import org.bukkit.block.Block; import org.bukkit.scheduler.BukkitTask; import org.bukkit.util.Vector; +import java.util.HashSet; import java.util.List; import java.util.Set; public class Depth { + private static BukkitTask bukkitTask = null; + + private Region region; private int tntCount = 1; private Vector minVector = null; private Vector maxVector = null; private long lastUpdate = TPSUtils.currentTick.get(); - private BukkitTask bukkitTask; public Depth(Region region, List blocks) { if (blocks.isEmpty()) { throw new SecurityException(); } + this.region = region; blocks.forEach(block -> { if (minVector == null) { minVector = DepthCounter.blockVector(block.getLocation().toVector()); @@ -53,29 +57,45 @@ public class Depth { } }); - bukkitTask = Bukkit.getScheduler().runTaskTimer(BauSystem.getInstance(), () -> { - if (TPSUtils.currentTick.get() - lastUpdate > 10) { - bukkitTask.cancel(); + if (bukkitTask == null) { + bukkitTask = Bukkit.getScheduler().runTaskTimer(BauSystem.getInstance(), () -> { + Set toRemove = new HashSet<>(); + for (Set value : DepthCounter.depthMap.values()) { + value.forEach(depth -> { + if (TPSUtils.currentTick.get() - depth.lastUpdate < 10) { + return; + } - Vector dimensions = maxVector.subtract(minVector); - dimensions.setX(Math.abs(dimensions.getX())); - dimensions.setY(Math.abs(dimensions.getY())); - dimensions.setZ(Math.abs(dimensions.getZ())); + Vector dimensions = depth.maxVector.subtract(depth.minVector); + dimensions.setX(Math.abs(dimensions.getX())); + dimensions.setY(Math.abs(dimensions.getY())); + dimensions.setZ(Math.abs(dimensions.getZ())); - if (tntCount > 4 && dimensions.getX() != 0 && dimensions.getY() != 0 && dimensions.getZ() != 0) { - RegionUtils.message(region, player -> DepthCounter.getMessage(player, dimensions.getBlockX() + 1, dimensions.getBlockY() + 1, dimensions.getBlockZ() + 1, tntCount)); - } + if (depth.tntCount > 4 && dimensions.getX() != 0 && dimensions.getY() != 0 && dimensions.getZ() != 0) { + RegionUtils.message(depth.region, player -> DepthCounter.getMessage(player, dimensions.getBlockX() + 1, dimensions.getBlockY() + 1, dimensions.getBlockZ() + 1, depth.tntCount)); + } - Set depthSet = DepthCounter.depthMap.get(region); - if (depthSet == null) { - return; + Set depthSet = DepthCounter.depthMap.get(depth.region); + if (depthSet == null) { + return; + } + toRemove.add(depth); + }); } - depthSet.remove(this); - if (depthSet.isEmpty()) { - DepthCounter.depthMap.remove(region); + toRemove.forEach(depth -> { + Set depthSet = DepthCounter.depthMap.get(depth.region); + depthSet.remove(depth); + if (depthSet.isEmpty()) { + DepthCounter.depthMap.remove(depth.region); + } + }); + + if (DepthCounter.depthMap.isEmpty()) { + bukkitTask.cancel(); + bukkitTask = null; } - } - }, 1, 1); + }, 2, 2); + } } public boolean update(List blocks) {