From 8f802fbf1a588e6df1b589d80a16299debb2e307 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Sat, 1 May 2021 15:07:42 +0200 Subject: [PATCH] Add DepthCounter --- .../features/testblock/DepthCounter.java | 109 ++++++++++++++++++ 1 file changed, 109 insertions(+) create mode 100644 BauSystem_Main/src/de/steamwar/bausystem/features/testblock/DepthCounter.java diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/testblock/DepthCounter.java b/BauSystem_Main/src/de/steamwar/bausystem/features/testblock/DepthCounter.java new file mode 100644 index 00000000..592104f4 --- /dev/null +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/testblock/DepthCounter.java @@ -0,0 +1,109 @@ +/* + * 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; + +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.util.Vector; + +import java.util.HashMap; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; +import java.util.function.BinaryOperator; + +@Linked(LinkageType.LISTENER) +public class DepthCounter implements Listener { + + private final Set current = new HashSet<>(); + private final Map minVectors = new HashMap<>(); + private final Map maxVectors = new HashMap<>(); + + @EventHandler + public void onEntityExplode(EntityExplodeEvent event) { + Region region = Region.getRegion(event.getLocation()); + boolean testblock = event.blockList().stream().anyMatch(block -> region.inRegion(block.getLocation(), RegionType.TESTBLOCK, RegionExtensionType.NORMAL)); + if (!testblock) { + return; + } + Vector minVector = blockVector(event.getEntity().getLocation().toVector()); + Vector maxVector = blockVector(event.getEntity().getLocation().toVector()); + event.blockList().forEach(block -> { + if (block.getType() == Material.AIR) { + 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())); + }); + + compute(minVectors.computeIfAbsent(region, region1 -> minVector), minVector, Math::min); + compute(maxVectors.computeIfAbsent(region, region1 -> maxVector), maxVector, Math::max); + + if (!current.contains(region)) { + current.add(region); + Bukkit.getScheduler().runTaskLater(BauSystem.getInstance(), () -> { + Vector minVectorRemoved = minVectors.remove(region); + Vector maxVectorRemoved = 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())); + + if (dimensions.getX() > dimensions.getY() && dimensions.getX() > dimensions.getZ()) { + RegionUtils.message(region, BauSystem.PREFIX + "Tiefe X: " + dimensions.getX()); + } + if (dimensions.getY() > dimensions.getX() && dimensions.getY() > dimensions.getZ()) { + RegionUtils.message(region, BauSystem.PREFIX + "Tiefe Y: " + dimensions.getY()); + } + if (dimensions.getZ() > dimensions.getX() && dimensions.getZ() > dimensions.getY()) { + RegionUtils.message(region, BauSystem.PREFIX + "Tiefe Z: " + dimensions.getZ()); + } + current.remove(region); + }, 5); + } + } + + private Vector blockVector(Vector vector) { + return new Vector(vector.getBlockX(), vector.getBlockY(), vector.getBlockZ()); + } + + private void compute(Vector v1, Vector v2, BinaryOperator function) { + v1.setX(function.apply(v1.getX(), v2.getX())); + v1.setY(function.apply(v1.getY(), v2.getY())); + v1.setZ(function.apply(v1.getZ(), v2.getZ())); + } + +}