2020-04-16 04:52:39 +02:00
|
|
|
/*
|
|
|
|
* _____ _ _ _____ _
|
|
|
|
* | __ \| | | | / ____| | |
|
|
|
|
* | |__) | | ___ | |_| (___ __ _ _ _ __ _ _ __ ___ __| |
|
|
|
|
* | ___/| |/ _ \| __|\___ \ / _` | | | |/ _` | '__/ _ \/ _` |
|
|
|
|
* | | | | (_) | |_ ____) | (_| | |_| | (_| | | | __/ (_| |
|
|
|
|
* |_| |_|\___/ \__|_____/ \__, |\__,_|\__,_|_| \___|\__,_|
|
|
|
|
* | |
|
|
|
|
* |_|
|
|
|
|
* PlotSquared plot management system for Minecraft
|
|
|
|
* Copyright (C) 2020 IntellectualSites
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU 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 General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2020-05-13 14:13:14 +02:00
|
|
|
package com.plotsquared.bukkit.util;
|
2018-08-10 17:53:14 +02:00
|
|
|
|
2020-07-10 22:12:37 +02:00
|
|
|
import com.google.inject.Inject;
|
|
|
|
import com.google.inject.Singleton;
|
2020-07-07 12:56:43 +02:00
|
|
|
import com.plotsquared.bukkit.BukkitPlatform;
|
2020-04-15 21:26:54 +02:00
|
|
|
import com.plotsquared.core.generator.AugmentedUtils;
|
|
|
|
import com.plotsquared.core.location.Location;
|
2020-04-16 06:11:03 +02:00
|
|
|
import com.plotsquared.core.location.PlotLoc;
|
2020-04-15 21:26:54 +02:00
|
|
|
import com.plotsquared.core.plot.Plot;
|
|
|
|
import com.plotsquared.core.plot.PlotArea;
|
2020-07-01 22:36:46 +02:00
|
|
|
import com.plotsquared.core.plot.PlotManager;
|
2020-07-23 18:30:23 +02:00
|
|
|
import com.plotsquared.core.queue.GlobalBlockQueue;
|
2020-07-17 16:41:06 +02:00
|
|
|
import com.plotsquared.core.queue.QueueCoordinator;
|
|
|
|
import com.plotsquared.core.queue.ScopedQueueCoordinator;
|
2020-04-16 06:11:03 +02:00
|
|
|
import com.plotsquared.core.util.ChunkManager;
|
2020-05-13 14:13:14 +02:00
|
|
|
import com.plotsquared.core.util.RegionManager;
|
2020-04-15 21:26:54 +02:00
|
|
|
import com.plotsquared.core.util.RegionUtil;
|
2020-07-17 18:21:32 +02:00
|
|
|
import com.plotsquared.core.util.WorldUtil;
|
2020-04-16 06:11:03 +02:00
|
|
|
import com.plotsquared.core.util.entity.EntityCategories;
|
|
|
|
import com.plotsquared.core.util.task.RunnableVal;
|
2020-04-12 19:30:50 +02:00
|
|
|
import com.sk89q.worldedit.bukkit.BukkitAdapter;
|
2018-12-31 18:15:47 +01:00
|
|
|
import com.sk89q.worldedit.bukkit.BukkitWorld;
|
2019-11-04 18:44:23 +01:00
|
|
|
import com.sk89q.worldedit.math.BlockVector2;
|
2020-07-23 18:30:23 +02:00
|
|
|
import com.sk89q.worldedit.math.BlockVector3;
|
2019-11-10 18:47:37 +01:00
|
|
|
import com.sk89q.worldedit.regions.CuboidRegion;
|
2018-12-31 18:15:47 +01:00
|
|
|
import com.sk89q.worldedit.world.block.BaseBlock;
|
2019-11-10 18:47:37 +01:00
|
|
|
import com.sk89q.worldedit.world.block.BlockTypes;
|
2019-09-12 01:04:29 +02:00
|
|
|
import io.papermc.lib.PaperLib;
|
2019-01-16 00:58:21 +01:00
|
|
|
import org.bukkit.Bukkit;
|
|
|
|
import org.bukkit.Chunk;
|
|
|
|
import org.bukkit.World;
|
|
|
|
import org.bukkit.entity.Entity;
|
|
|
|
import org.bukkit.entity.Player;
|
2020-07-12 21:49:05 +02:00
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
2015-07-30 16:25:16 +02:00
|
|
|
|
2019-11-04 18:44:23 +01:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.HashSet;
|
|
|
|
import java.util.List;
|
2019-12-02 23:07:57 +01:00
|
|
|
import java.util.Objects;
|
2019-11-04 18:44:23 +01:00
|
|
|
import java.util.Set;
|
2020-04-08 21:49:41 +02:00
|
|
|
import java.util.concurrent.Semaphore;
|
2016-07-25 07:19:29 +02:00
|
|
|
|
2020-04-16 06:11:03 +02:00
|
|
|
import static com.plotsquared.core.util.entity.EntityCategories.CAP_ANIMAL;
|
|
|
|
import static com.plotsquared.core.util.entity.EntityCategories.CAP_ENTITY;
|
|
|
|
import static com.plotsquared.core.util.entity.EntityCategories.CAP_MISC;
|
|
|
|
import static com.plotsquared.core.util.entity.EntityCategories.CAP_MOB;
|
|
|
|
import static com.plotsquared.core.util.entity.EntityCategories.CAP_MONSTER;
|
|
|
|
import static com.plotsquared.core.util.entity.EntityCategories.CAP_VEHICLE;
|
2019-12-02 23:07:57 +01:00
|
|
|
|
2020-07-17 16:41:06 +02:00
|
|
|
@Singleton
|
|
|
|
public class BukkitRegionManager extends RegionManager {
|
2020-07-10 22:12:37 +02:00
|
|
|
|
2020-07-17 16:41:06 +02:00
|
|
|
private static final Logger logger =
|
|
|
|
LoggerFactory.getLogger("P2/" + BukkitRegionManager.class.getSimpleName());
|
2020-07-23 18:30:23 +02:00
|
|
|
@Inject private WorldUtil worldUtil;
|
|
|
|
@Inject private GlobalBlockQueue blockQueue;
|
2016-03-29 09:43:57 +02:00
|
|
|
|
2019-11-04 18:44:23 +01:00
|
|
|
@Override public Set<BlockVector2> getChunkChunks(String world) {
|
|
|
|
Set<BlockVector2> chunks = super.getChunkChunks(world);
|
2020-04-08 21:49:41 +02:00
|
|
|
if (Bukkit.isPrimaryThread()) {
|
|
|
|
for (Chunk chunk : Objects.requireNonNull(Bukkit.getWorld(world)).getLoadedChunks()) {
|
|
|
|
BlockVector2 loc = BlockVector2.at(chunk.getX() >> 5, chunk.getZ() >> 5);
|
|
|
|
chunks.add(loc);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
final Semaphore semaphore = new Semaphore(1);
|
|
|
|
try {
|
|
|
|
semaphore.acquire();
|
2020-07-17 16:41:06 +02:00
|
|
|
Bukkit.getScheduler()
|
|
|
|
.runTask(BukkitPlatform.getPlugin(BukkitPlatform.class), () -> {
|
|
|
|
for (Chunk chunk : Objects.requireNonNull(Bukkit.getWorld(world))
|
|
|
|
.getLoadedChunks()) {
|
|
|
|
BlockVector2 loc =
|
|
|
|
BlockVector2.at(chunk.getX() >> 5, chunk.getZ() >> 5);
|
|
|
|
chunks.add(loc);
|
|
|
|
}
|
|
|
|
semaphore.release();
|
|
|
|
});
|
2020-04-08 21:49:41 +02:00
|
|
|
semaphore.acquireUninterruptibly();
|
|
|
|
} catch (final Exception e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
2015-02-23 06:29:45 +01:00
|
|
|
}
|
2016-04-05 18:37:11 +02:00
|
|
|
return chunks;
|
|
|
|
}
|
2016-02-14 02:01:18 +01:00
|
|
|
|
2020-07-01 22:36:46 +02:00
|
|
|
@Override public boolean handleClear(Plot plot, Runnable whenDone, PlotManager manager) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-03-15 02:49:33 +01:00
|
|
|
@Override public int[] countEntities(Plot plot) {
|
|
|
|
int[] existing = (int[]) plot.getMeta("EntityCount");
|
|
|
|
if (existing != null && (System.currentTimeMillis() - (long) plot.getMeta("EntityCountTime")
|
|
|
|
< 1000)) {
|
|
|
|
return existing;
|
|
|
|
}
|
|
|
|
PlotArea area = plot.getArea();
|
2020-04-02 14:34:38 +02:00
|
|
|
World world = BukkitUtil.getWorld(area.getWorldName());
|
2019-03-15 02:49:33 +01:00
|
|
|
Location bot = plot.getBottomAbs();
|
|
|
|
Location top = plot.getTopAbs();
|
|
|
|
int bx = bot.getX() >> 4;
|
|
|
|
int bz = bot.getZ() >> 4;
|
|
|
|
|
|
|
|
int tx = top.getX() >> 4;
|
|
|
|
int tz = top.getZ() >> 4;
|
|
|
|
|
|
|
|
int size = tx - bx << 4;
|
|
|
|
|
|
|
|
Set<Chunk> chunks = new HashSet<>();
|
|
|
|
for (int X = bx; X <= tx; X++) {
|
|
|
|
for (int Z = bz; Z <= tz; Z++) {
|
|
|
|
if (world.isChunkLoaded(X, Z)) {
|
|
|
|
chunks.add(world.getChunkAt(X, Z));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
boolean doWhole = false;
|
|
|
|
List<Entity> entities = null;
|
|
|
|
if (size > 200 && chunks.size() > 200) {
|
|
|
|
entities = world.getEntities();
|
|
|
|
if (entities.size() < 16 + size / 8) {
|
|
|
|
doWhole = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int[] count = new int[6];
|
|
|
|
if (doWhole) {
|
|
|
|
for (Entity entity : entities) {
|
|
|
|
org.bukkit.Location location = entity.getLocation();
|
2020-04-30 12:01:52 +02:00
|
|
|
PaperLib.getChunkAtAsync(location).thenAccept(chunk -> {
|
2019-09-12 01:04:29 +02:00
|
|
|
if (chunks.contains(chunk)) {
|
|
|
|
int X = chunk.getX();
|
|
|
|
int Z = chunk.getZ();
|
|
|
|
if (X > bx && X < tx && Z > bz && Z < tz) {
|
2019-03-15 02:49:33 +01:00
|
|
|
count(count, entity);
|
2019-09-12 01:04:29 +02:00
|
|
|
} else {
|
2020-07-17 22:58:10 +02:00
|
|
|
Plot other = area.getPlot(BukkitUtil.adapt(location));
|
2019-09-12 01:04:29 +02:00
|
|
|
if (plot.equals(other)) {
|
|
|
|
count(count, entity);
|
|
|
|
}
|
2019-03-15 02:49:33 +01:00
|
|
|
}
|
|
|
|
}
|
2019-09-12 01:04:29 +02:00
|
|
|
});
|
2019-03-15 02:49:33 +01:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for (Chunk chunk : chunks) {
|
|
|
|
int X = chunk.getX();
|
|
|
|
int Z = chunk.getZ();
|
|
|
|
Entity[] entities1 = chunk.getEntities();
|
|
|
|
for (Entity entity : entities1) {
|
|
|
|
if (X == bx || X == tx || Z == bz || Z == tz) {
|
2020-07-17 22:58:10 +02:00
|
|
|
Plot other = area.getPlot(BukkitUtil.adapt(entity.getLocation()));
|
2019-03-15 02:49:33 +01:00
|
|
|
if (plot.equals(other)) {
|
|
|
|
count(count, entity);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
count(count, entity);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2020-07-18 13:07:56 +02:00
|
|
|
@Override @Inject public boolean regenerateRegion(final Location pos1, final Location pos2,
|
2018-08-10 17:01:10 +02:00
|
|
|
final boolean ignoreAugment, final Runnable whenDone) {
|
2020-07-18 13:07:56 +02:00
|
|
|
final BukkitWorld world = new BukkitWorld((World) pos1.getWorld());
|
2016-04-05 18:37:11 +02:00
|
|
|
|
|
|
|
final int p1x = pos1.getX();
|
|
|
|
final int p1z = pos1.getZ();
|
|
|
|
final int p2x = pos2.getX();
|
|
|
|
final int p2z = pos2.getZ();
|
|
|
|
final int bcx = p1x >> 4;
|
|
|
|
final int bcz = p1z >> 4;
|
|
|
|
final int tcx = p2x >> 4;
|
|
|
|
final int tcz = p2z >> 4;
|
|
|
|
|
2020-07-23 18:30:23 +02:00
|
|
|
final QueueCoordinator queue = blockQueue.getNewQueue(world);
|
|
|
|
queue.setReadRegion(new CuboidRegion(pos1.getBlockVector3(), pos2.getBlockVector3()));
|
|
|
|
queue.setChunkConsumer(chunk -> {
|
|
|
|
|
|
|
|
int x = chunk.getX();
|
|
|
|
int z = chunk.getZ();
|
|
|
|
int xxb = x << 4;
|
|
|
|
int zzb = z << 4;
|
|
|
|
int xxt = xxb + 15;
|
|
|
|
int zzt = zzb + 15;
|
|
|
|
if (xxb >= p1x && xxt <= p2x && zzb >= p1z && zzt <= p2z) {
|
|
|
|
AugmentedUtils
|
|
|
|
.bypass(ignoreAugment, () -> queue.regenChunk(chunk.getX(), chunk.getZ()));
|
|
|
|
return;
|
2015-02-23 06:29:45 +01:00
|
|
|
}
|
2020-07-23 18:30:23 +02:00
|
|
|
boolean checkX1 = false;
|
2016-04-05 18:37:11 +02:00
|
|
|
|
2020-07-23 18:30:23 +02:00
|
|
|
int xxb2;
|
2020-07-18 13:07:56 +02:00
|
|
|
|
2020-07-23 18:30:23 +02:00
|
|
|
if (x == bcx) {
|
|
|
|
xxb2 = p1x - 1;
|
|
|
|
checkX1 = true;
|
|
|
|
} else {
|
|
|
|
xxb2 = xxb;
|
|
|
|
}
|
|
|
|
boolean checkX2 = false;
|
|
|
|
int xxt2;
|
|
|
|
if (x == tcx) {
|
|
|
|
xxt2 = p2x + 1;
|
|
|
|
checkX2 = true;
|
|
|
|
} else {
|
|
|
|
xxt2 = xxt;
|
|
|
|
}
|
|
|
|
boolean checkZ1 = false;
|
|
|
|
int zzb2;
|
|
|
|
if (z == bcz) {
|
|
|
|
zzb2 = p1z - 1;
|
|
|
|
checkZ1 = true;
|
|
|
|
} else {
|
|
|
|
zzb2 = zzb;
|
|
|
|
}
|
|
|
|
boolean checkZ2 = false;
|
|
|
|
int zzt2;
|
|
|
|
if (z == tcz) {
|
|
|
|
zzt2 = p2z + 1;
|
|
|
|
checkZ2 = true;
|
|
|
|
} else {
|
|
|
|
zzt2 = zzt;
|
|
|
|
}
|
|
|
|
final ContentMap map = new ContentMap();
|
|
|
|
if (checkX1) {
|
|
|
|
map.saveRegion(world, xxb, xxb2, zzb2, zzt2); //
|
|
|
|
}
|
|
|
|
if (checkX2) {
|
|
|
|
map.saveRegion(world, xxt2, xxt, zzb2, zzt2); //
|
|
|
|
}
|
|
|
|
if (checkZ1) {
|
|
|
|
map.saveRegion(world, xxb2, xxt2, zzb, zzb2); //
|
|
|
|
}
|
|
|
|
if (checkZ2) {
|
|
|
|
map.saveRegion(world, xxb2, xxt2, zzt2, zzt); //
|
|
|
|
}
|
|
|
|
if (checkX1 && checkZ1) {
|
|
|
|
map.saveRegion(world, xxb, xxb2, zzb, zzb2); //
|
|
|
|
}
|
|
|
|
if (checkX2 && checkZ1) {
|
|
|
|
map.saveRegion(world, xxt2, xxt, zzb, zzb2); // ?
|
|
|
|
}
|
|
|
|
if (checkX1 && checkZ2) {
|
|
|
|
map.saveRegion(world, xxb, xxb2, zzt2, zzt); // ?
|
|
|
|
}
|
|
|
|
if (checkX2 && checkZ2) {
|
|
|
|
map.saveRegion(world, xxt2, xxt, zzt2, zzt); //
|
|
|
|
}
|
|
|
|
CuboidRegion currentPlotClear =
|
|
|
|
RegionUtil.createRegion(pos1.getX(), pos2.getX(), pos1.getZ(), pos2.getZ());
|
|
|
|
map.saveEntitiesOut(Bukkit.getWorld(world.getName()).getChunkAt(x, z),
|
|
|
|
currentPlotClear);
|
|
|
|
AugmentedUtils.bypass(ignoreAugment, () -> ChunkManager
|
|
|
|
.setChunkInPlotArea(null, new RunnableVal<ScopedQueueCoordinator>() {
|
|
|
|
@Override public void run(ScopedQueueCoordinator value) {
|
|
|
|
Location min = value.getMin();
|
|
|
|
int bx = min.getX();
|
|
|
|
int bz = min.getZ();
|
|
|
|
for (int x1 = 0; x1 < 16; x1++) {
|
|
|
|
for (int z1 = 0; z1 < 16; z1++) {
|
|
|
|
PlotLoc plotLoc = new PlotLoc(bx + x1, bz + z1);
|
|
|
|
BaseBlock[] ids = map.allBlocks.get(plotLoc);
|
|
|
|
if (ids != null) {
|
|
|
|
for (int y = 0; y < Math.min(128, ids.length); y++) {
|
|
|
|
BaseBlock id = ids[y];
|
|
|
|
if (id != null) {
|
|
|
|
value.setBlock(x1, y, z1, id);
|
|
|
|
} else {
|
|
|
|
value.setBlock(x1, y, z1,
|
|
|
|
BlockTypes.AIR.getDefaultState());
|
2020-07-18 13:07:56 +02:00
|
|
|
}
|
2020-07-23 18:30:23 +02:00
|
|
|
}
|
|
|
|
for (int y = Math.min(128, ids.length); y < ids.length; y++) {
|
|
|
|
BaseBlock id = ids[y];
|
|
|
|
if (id != null) {
|
|
|
|
value.setBlock(x1, y, z1, id);
|
2016-04-05 18:37:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-07-18 13:07:56 +02:00
|
|
|
}
|
|
|
|
}
|
2020-07-23 18:30:23 +02:00
|
|
|
}
|
|
|
|
}, world.getName(), chunk));
|
|
|
|
//map.restoreBlocks(worldObj, 0, 0);
|
|
|
|
map.restoreEntities(Bukkit.getWorld(world.getName()), 0, 0);
|
|
|
|
});
|
|
|
|
queue.setCompleteTask(whenDone);
|
|
|
|
queue.enqueue();
|
2016-04-05 18:37:11 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-03-15 02:49:33 +01:00
|
|
|
@Override public void clearAllEntities(Location pos1, Location pos2) {
|
2020-07-10 12:21:29 +02:00
|
|
|
String world = pos1.getWorldName();
|
2020-07-17 22:58:10 +02:00
|
|
|
|
|
|
|
final World bukkitWorld = BukkitUtil.getWorld(world);
|
|
|
|
final List<Entity> entities;
|
|
|
|
if (bukkitWorld != null) {
|
|
|
|
entities = new ArrayList<>(bukkitWorld.getEntities());
|
|
|
|
} else {
|
|
|
|
entities = new ArrayList<>();
|
|
|
|
}
|
|
|
|
|
2016-04-05 18:37:11 +02:00
|
|
|
int bx = pos1.getX();
|
|
|
|
int bz = pos1.getZ();
|
|
|
|
int tx = pos2.getX();
|
|
|
|
int tz = pos2.getZ();
|
|
|
|
for (Entity entity : entities) {
|
|
|
|
if (!(entity instanceof Player)) {
|
2016-06-02 17:38:47 +02:00
|
|
|
org.bukkit.Location location = entity.getLocation();
|
2018-08-10 17:01:10 +02:00
|
|
|
if (location.getX() >= bx && location.getX() <= tx && location.getZ() >= bz
|
|
|
|
&& location.getZ() <= tz) {
|
2018-08-19 22:18:58 +02:00
|
|
|
if (entity.hasMetadata("ps-tmp-teleport")) {
|
|
|
|
continue;
|
|
|
|
}
|
2016-04-05 18:37:11 +02:00
|
|
|
entity.remove();
|
2015-02-23 06:29:45 +01:00
|
|
|
}
|
2016-02-14 02:01:18 +01:00
|
|
|
}
|
|
|
|
}
|
2016-04-05 18:37:11 +02:00
|
|
|
}
|
2016-02-14 02:01:18 +01:00
|
|
|
|
2016-04-05 18:37:11 +02:00
|
|
|
private void count(int[] count, Entity entity) {
|
2020-04-12 19:30:50 +02:00
|
|
|
final com.sk89q.worldedit.world.entity.EntityType entityType =
|
|
|
|
BukkitAdapter.adapt(entity.getType());
|
|
|
|
|
|
|
|
if (EntityCategories.PLAYER.contains(entityType)) {
|
|
|
|
return;
|
2020-04-30 12:01:52 +02:00
|
|
|
} else if (EntityCategories.PROJECTILE.contains(entityType) || EntityCategories.OTHER
|
|
|
|
.contains(entityType) || EntityCategories.HANGING.contains(entityType)) {
|
2020-04-12 19:30:50 +02:00
|
|
|
count[CAP_MISC]++;
|
2020-04-30 12:01:52 +02:00
|
|
|
} else if (EntityCategories.ANIMAL.contains(entityType) || EntityCategories.VILLAGER
|
|
|
|
.contains(entityType) || EntityCategories.TAMEABLE.contains(entityType)) {
|
2020-04-12 19:30:50 +02:00
|
|
|
count[CAP_MOB]++;
|
|
|
|
count[CAP_ANIMAL]++;
|
|
|
|
} else if (EntityCategories.VEHICLE.contains(entityType)) {
|
|
|
|
count[CAP_VEHICLE]++;
|
|
|
|
} else if (EntityCategories.HOSTILE.contains(entityType)) {
|
|
|
|
count[CAP_MOB]++;
|
|
|
|
count[CAP_MONSTER]++;
|
2016-04-05 18:37:11 +02:00
|
|
|
}
|
2020-04-12 19:30:50 +02:00
|
|
|
count[CAP_ENTITY]++;
|
2016-04-05 18:37:11 +02:00
|
|
|
}
|
2015-02-23 06:29:45 +01:00
|
|
|
}
|