From 988ebcaee6649eb7d18238db943abd20da8f8541 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Sat, 1 May 2021 19:36:02 +0200 Subject: [PATCH] Add Depth --- .../testblock/depthcounter/Depth.java | 108 ++++++++++++++++++ .../testblock/depthcounter/DepthCounter.java | 19 +-- .../depthcounter/DepthCounterListener.java | 47 ++------ 3 files changed, 123 insertions(+), 51 deletions(-) create mode 100644 BauSystem_Main/src/de/steamwar/bausystem/features/testblock/depthcounter/Depth.java 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 new file mode 100644 index 00000000..761f4e4a --- /dev/null +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/testblock/depthcounter/Depth.java @@ -0,0 +1,108 @@ +/* + * This file is a part of the SteamWar software. + * + * Copyright (C) 2021 SteamWar.de-Serverteam + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +package de.steamwar.bausystem.features.testblock.depthcounter; + +import de.steamwar.bausystem.BauSystem; +import de.steamwar.bausystem.features.tpslimit.TPSUtils; +import de.steamwar.bausystem.region.Region; +import de.steamwar.bausystem.region.RegionUtils; +import org.bukkit.Bukkit; +import org.bukkit.block.Block; +import org.bukkit.scheduler.BukkitTask; +import org.bukkit.util.Vector; + +import java.util.List; +import java.util.Set; + +public class Depth { + + 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(); + } + blocks.forEach(block -> { + if (minVector == null) { + minVector = DepthCounter.blockVector(block.getLocation().toVector()); + maxVector = DepthCounter.blockVector(block.getLocation().toVector()); + } else { + internalUpdate(block); + } + }); + + bukkitTask = Bukkit.getScheduler().runTaskTimer(BauSystem.getInstance(), () -> { + if (TPSUtils.currentTick.get() - lastUpdate > 10) { + bukkitTask.cancel(); + + Vector dimensions = maxVector.subtract(minVector); + dimensions.setX(Math.abs(dimensions.getX())); + dimensions.setY(Math.abs(dimensions.getY())); + dimensions.setZ(Math.abs(dimensions.getZ())); + + if (dimensions.getX() != 0 && dimensions.getY() != 0 && dimensions.getZ() != 0) { + RegionUtils.message(region, player -> DepthCounter.getMessage(player, dimensions.getBlockX(), dimensions.getBlockY(), dimensions.getBlockZ())); + } + + Set depthSet = DepthCounter.depthMap.get(region); + if (depthSet == null) { + return; + } + depthSet.remove(this); + if (depthSet.isEmpty()) { + DepthCounter.depthMap.remove(region); + } + } + }, 2, 2); + } + + public boolean update(List blocks) { + boolean expand = blocks.stream().anyMatch(block -> { + Vector vector = DepthCounter.blockVector(block.getLocation().toVector()); + return vector.getX() >= minVector.getX() + && vector.getY() >= minVector.getY() + && vector.getZ() >= minVector.getZ() + && vector.getX() <= maxVector.getX() + && vector.getY() <= maxVector.getY() + && vector.getZ() <= maxVector.getZ(); + }); + if (!expand) { + return false; + } + lastUpdate = TPSUtils.currentTick.get(); + blocks.forEach(this::internalUpdate); + return true; + } + + private void internalUpdate(Block block) { + minVector.setX(Math.min(minVector.getX(), block.getX())); + minVector.setY(Math.min(minVector.getY(), block.getY())); + minVector.setZ(Math.min(minVector.getZ(), block.getZ())); + + maxVector.setX(Math.max(maxVector.getX(), block.getX())); + maxVector.setY(Math.max(maxVector.getY(), block.getY())); + maxVector.setZ(Math.max(maxVector.getZ(), block.getZ())); + } + +} diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/testblock/depthcounter/DepthCounter.java b/BauSystem_Main/src/de/steamwar/bausystem/features/testblock/depthcounter/DepthCounter.java index 59728971..cefa7856 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/testblock/depthcounter/DepthCounter.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/testblock/depthcounter/DepthCounter.java @@ -21,32 +21,19 @@ package de.steamwar.bausystem.features.testblock.depthcounter; import de.steamwar.bausystem.BauSystem; import de.steamwar.bausystem.config.ColorConfig; -import de.steamwar.bausystem.linkage.LinkageType; -import de.steamwar.bausystem.linkage.Linked; import de.steamwar.bausystem.region.Region; -import de.steamwar.bausystem.region.RegionUtils; -import de.steamwar.bausystem.region.utils.RegionExtensionType; -import de.steamwar.bausystem.region.utils.RegionType; -import java.util.*; -import java.util.concurrent.CountDownLatch; -import java.util.stream.Collectors; import lombok.experimental.UtilityClass; -import org.bukkit.Bukkit; -import org.bukkit.Material; import org.bukkit.entity.Player; -import org.bukkit.event.EventHandler; -import org.bukkit.event.Listener; -import org.bukkit.event.entity.EntityExplodeEvent; import org.bukkit.util.Vector; +import java.util.*; import java.util.function.DoubleBinaryOperator; +import java.util.stream.Collectors; @UtilityClass public class DepthCounter { - public final Set current = new HashSet<>(); - public final Map minVectors = new HashMap<>(); - public final Map maxVectors = new HashMap<>(); + public final Map> depthMap = new HashMap<>(); public final Map> playerSettings = new HashMap<>(); public void toggleMode(final Player p, final CountMode countMode) { diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/testblock/depthcounter/DepthCounterListener.java b/BauSystem_Main/src/de/steamwar/bausystem/features/testblock/depthcounter/DepthCounterListener.java index c6276fd4..99778878 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/testblock/depthcounter/DepthCounterListener.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/testblock/depthcounter/DepthCounterListener.java @@ -19,21 +19,19 @@ package de.steamwar.bausystem.features.testblock.depthcounter; -import de.steamwar.bausystem.BauSystem; import de.steamwar.bausystem.linkage.LinkageType; import de.steamwar.bausystem.linkage.Linked; import de.steamwar.bausystem.region.Region; -import de.steamwar.bausystem.region.RegionUtils; import de.steamwar.bausystem.region.utils.RegionExtensionType; import de.steamwar.bausystem.region.utils.RegionType; -import org.bukkit.Bukkit; -import org.bukkit.Material; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; import org.bukkit.event.entity.EntityExplodeEvent; import org.bukkit.event.player.PlayerJoinEvent; import org.bukkit.event.player.PlayerQuitEvent; -import org.bukkit.util.Vector; + +import java.util.HashSet; +import java.util.Set; @Linked(LinkageType.LISTENER) @@ -46,39 +44,18 @@ public class DepthCounterListener implements Listener { if (!testblock) { return; } - Vector minVector = DepthCounter.blockVector(event.getEntity().getLocation().toVector()); - Vector maxVector = DepthCounter.blockVector(event.getEntity().getLocation().toVector()); - event.blockList().forEach(block -> { - if (block.getType() == Material.AIR) { + + if (!DepthCounter.depthMap.containsKey(region)) { + DepthCounter.depthMap.put(region, new HashSet<>()); + } + + Set depthSet = DepthCounter.depthMap.get(region); + for (Depth depth : depthSet) { + if (depth.update(event.blockList())) { return; } - minVector.setX(Math.min(minVector.getX(), block.getX())); - minVector.setY(Math.min(minVector.getY(), block.getY())); - minVector.setZ(Math.min(minVector.getZ(), block.getZ())); - - maxVector.setX(Math.max(maxVector.getX(), block.getX())); - maxVector.setY(Math.max(maxVector.getY(), block.getY())); - maxVector.setZ(Math.max(maxVector.getZ(), block.getZ())); - }); - - DepthCounter.compute(DepthCounter.minVectors.computeIfAbsent(region, region1 -> minVector), minVector, Math::min); - DepthCounter.compute(DepthCounter.maxVectors.computeIfAbsent(region, region1 -> maxVector), maxVector, Math::max); - - if (!DepthCounter.current.contains(region)) { - DepthCounter.current.add(region); - Bukkit.getScheduler().runTaskLater(BauSystem.getInstance(), () -> { - Vector minVectorRemoved = DepthCounter.minVectors.remove(region); - Vector maxVectorRemoved = DepthCounter.maxVectors.remove(region); - - Vector dimensions = maxVectorRemoved.subtract(minVectorRemoved); - dimensions.setX(Math.abs(dimensions.getX())); - dimensions.setY(Math.abs(dimensions.getY())); - dimensions.setZ(Math.abs(dimensions.getZ())); - - RegionUtils.message(region, player -> DepthCounter.getMessage(player, dimensions.getBlockX(), dimensions.getBlockY(), dimensions.getBlockZ())); - DepthCounter.current.remove(region); - }, 20); } + depthSet.add(new Depth(region, event.blockList())); } @EventHandler