SteamWar/BauSystem2.0
Archiviert
12
0

Add DepthCounter

Dieser Commit ist enthalten in:
yoyosource 2021-05-01 15:07:42 +02:00
Ursprung b43620741e
Commit 8f802fbf1a

Datei anzeigen

@ -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 <https://www.gnu.org/licenses/>.
*/
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<Region> current = new HashSet<>();
private final Map<Region, Vector> minVectors = new HashMap<>();
private final Map<Region, Vector> 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<Double> 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()));
}
}