SteamWar/BauSystem2.0
Archiviert
12
0

Optimize Depth System

Dieser Commit ist enthalten in:
yoyosource 2021-05-02 17:08:27 +02:00
Ursprung 59a9a0dcfc
Commit c0b35c5da7

Datei anzeigen

@ -28,22 +28,26 @@ import org.bukkit.block.Block;
import org.bukkit.scheduler.BukkitTask; import org.bukkit.scheduler.BukkitTask;
import org.bukkit.util.Vector; import org.bukkit.util.Vector;
import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
public class Depth { public class Depth {
private static BukkitTask bukkitTask = null;
private Region region;
private int tntCount = 1; private int tntCount = 1;
private Vector minVector = null; private Vector minVector = null;
private Vector maxVector = null; private Vector maxVector = null;
private long lastUpdate = TPSUtils.currentTick.get(); private long lastUpdate = TPSUtils.currentTick.get();
private BukkitTask bukkitTask;
public Depth(Region region, List<Block> blocks) { public Depth(Region region, List<Block> blocks) {
if (blocks.isEmpty()) { if (blocks.isEmpty()) {
throw new SecurityException(); throw new SecurityException();
} }
this.region = region;
blocks.forEach(block -> { blocks.forEach(block -> {
if (minVector == null) { if (minVector == null) {
minVector = DepthCounter.blockVector(block.getLocation().toVector()); minVector = DepthCounter.blockVector(block.getLocation().toVector());
@ -53,29 +57,45 @@ public class Depth {
} }
}); });
bukkitTask = Bukkit.getScheduler().runTaskTimer(BauSystem.getInstance(), () -> { if (bukkitTask == null) {
if (TPSUtils.currentTick.get() - lastUpdate > 10) { bukkitTask = Bukkit.getScheduler().runTaskTimer(BauSystem.getInstance(), () -> {
bukkitTask.cancel(); Set<Depth> toRemove = new HashSet<>();
for (Set<Depth> value : DepthCounter.depthMap.values()) {
value.forEach(depth -> {
if (TPSUtils.currentTick.get() - depth.lastUpdate < 10) {
return;
}
Vector dimensions = maxVector.subtract(minVector); Vector dimensions = depth.maxVector.subtract(depth.minVector);
dimensions.setX(Math.abs(dimensions.getX())); dimensions.setX(Math.abs(dimensions.getX()));
dimensions.setY(Math.abs(dimensions.getY())); dimensions.setY(Math.abs(dimensions.getY()));
dimensions.setZ(Math.abs(dimensions.getZ())); dimensions.setZ(Math.abs(dimensions.getZ()));
if (tntCount > 4 && dimensions.getX() != 0 && dimensions.getY() != 0 && dimensions.getZ() != 0) { if (depth.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)); RegionUtils.message(depth.region, player -> DepthCounter.getMessage(player, dimensions.getBlockX() + 1, dimensions.getBlockY() + 1, dimensions.getBlockZ() + 1, depth.tntCount));
} }
Set<Depth> depthSet = DepthCounter.depthMap.get(region); Set<Depth> depthSet = DepthCounter.depthMap.get(depth.region);
if (depthSet == null) { if (depthSet == null) {
return; return;
}
toRemove.add(depth);
});
} }
depthSet.remove(this); toRemove.forEach(depth -> {
if (depthSet.isEmpty()) { Set<Depth> depthSet = DepthCounter.depthMap.get(depth.region);
DepthCounter.depthMap.remove(region); depthSet.remove(depth);
if (depthSet.isEmpty()) {
DepthCounter.depthMap.remove(depth.region);
}
});
if (DepthCounter.depthMap.isEmpty()) {
bukkitTask.cancel();
bukkitTask = null;
} }
} }, 2, 2);
}, 1, 1); }
} }
public boolean update(List<Block> blocks) { public boolean update(List<Block> blocks) {