Add Depth
Dieser Commit ist enthalten in:
Ursprung
2800c8dc01
Commit
988ebcaee6
@ -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 <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
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<Block> 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<Depth> 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<Block> 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()));
|
||||
}
|
||||
|
||||
}
|
@ -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<Region> current = new HashSet<>();
|
||||
public final Map<Region, Vector> minVectors = new HashMap<>();
|
||||
public final Map<Region, Vector> maxVectors = new HashMap<>();
|
||||
public final Map<Region, Set<Depth>> depthMap = new HashMap<>();
|
||||
public final Map<Player, Set<CountMode>> playerSettings = new HashMap<>();
|
||||
|
||||
public void toggleMode(final Player p, final CountMode countMode) {
|
||||
|
@ -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<Depth> 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
|
||||
|
In neuem Issue referenzieren
Einen Benutzer sperren