Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix-count-entities #4356

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import com.sk89q.worldedit.bukkit.BukkitAdapter;
import com.sk89q.worldedit.world.block.BlockType;
import io.papermc.lib.PaperLib;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.Particle;
import org.bukkit.World;
Expand Down Expand Up @@ -195,9 +196,12 @@ public void creatureSpawnEvent(CreatureSpawnEvent event) {
}
return;
}
if (BukkitEntityUtil.checkEntity(entity, plot.getBasePlot(false))) {
event.setCancelled(true);
}

BukkitEntityUtil.checkEntityAsync(entity, plot.getBasePlot(false)).thenAcceptAsync(toRemove -> {
if(toRemove) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

code style

entity.remove();
}
}, Bukkit.getScheduler().getMainThreadExecutor(BukkitPlatform.getPlugin(BukkitPlatform.class)));
}

@EventHandler(ignoreCancelled = true, priority = EventPriority.HIGHEST)
Expand Down Expand Up @@ -421,13 +425,17 @@ public void onVehicleCreate(VehicleCreateEvent event) {
return;
}
Plot plot = area.getOwnedPlotAbs(location);
if (plot == null || BukkitEntityUtil.checkEntity(entity, plot)) {
entity.remove();
if (plot == null) {
return;
}
if (Settings.Enabled_Components.KILL_ROAD_VEHICLES) {
entity.setMetadata("plot", new FixedMetadataValue((Plugin) PlotSquared.platform(), plot));
}

BukkitEntityUtil.checkEntityAsync(entity, plot.getBasePlot(false)).thenAcceptAsync(toRemove -> {
if (toRemove) {
entity.remove();
} else if (Settings.Enabled_Components.KILL_ROAD_VEHICLES) {
entity.setMetadata("plot", new FixedMetadataValue((Plugin) PlotSquared.platform(), plot));
}
}, Bukkit.getScheduler().getMainThreadExecutor(BukkitPlatform.getPlugin(BukkitPlatform.class)));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/
package com.plotsquared.bukkit.listener;

import com.plotsquared.bukkit.BukkitPlatform;
import com.plotsquared.bukkit.util.BukkitEntityUtil;
import com.plotsquared.bukkit.util.BukkitUtil;
import com.plotsquared.core.PlotSquared;
Expand All @@ -27,6 +28,7 @@
import com.plotsquared.core.plot.PlotArea;
import com.plotsquared.core.plot.flag.implementations.DoneFlag;
import io.papermc.lib.PaperLib;
import org.bukkit.Bukkit;
import org.bukkit.Chunk;
import org.bukkit.World;
import org.bukkit.block.Block;
Expand Down Expand Up @@ -155,9 +157,11 @@ public void creatureSpawnEvent(EntitySpawnEvent event) {
event.setCancelled(true);
}
if (type == EntityType.ENDER_CRYSTAL) {
if (BukkitEntityUtil.checkEntity(entity, plot)) {
event.setCancelled(true);
}
BukkitEntityUtil.checkEntityAsync(entity, plot).thenAcceptAsync(toRemove -> {
if (toRemove) {
entity.remove();
}
}, Bukkit.getScheduler().getMainThreadExecutor(BukkitPlatform.getPlugin(BukkitPlatform.class)));
return;
}
if (type == EntityType.SHULKER) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.destroystokyo.paper.MaterialTags;
import com.google.common.base.Charsets;
import com.google.inject.Inject;
import com.plotsquared.bukkit.BukkitPlatform;
import com.plotsquared.bukkit.player.BukkitPlayer;
import com.plotsquared.bukkit.util.BukkitEntityUtil;
import com.plotsquared.bukkit.util.BukkitUtil;
Expand Down Expand Up @@ -1502,10 +1503,13 @@ public void onHangingPlace(HangingPlaceEvent event) {
return;
}
}
if (BukkitEntityUtil.checkEntity(event.getEntity(), plot)) {
event.setCancelled(true);
}

Entity entity = event.getEntity();
BukkitEntityUtil.checkEntityAsync(entity, plot.getBasePlot(false)).thenAcceptAsync(toRemove -> {
if (toRemove) {
entity.remove();
}
}, Bukkit.getScheduler().getMainThreadExecutor(BukkitPlatform.getPlugin(BukkitPlatform.class)));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
import org.bukkit.projectiles.ProjectileSource;

import java.util.Objects;
import java.util.concurrent.CompletableFuture;

public class BukkitEntityUtil {

Expand Down Expand Up @@ -399,4 +400,8 @@ public static boolean checkEntity(Entity entity, Plot plot) {
return EntityUtil.checkEntity(plot, EntityCapFlag.ENTITY_CAP_UNLIMITED);
}

public static CompletableFuture<Boolean> checkEntityAsync(Entity entity, Plot plot) {
return CompletableFuture.supplyAsync(() -> checkEntity(entity, plot));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -41,19 +41,18 @@
import com.sk89q.worldedit.regions.CuboidRegion;
import com.sk89q.worldedit.world.block.BaseBlock;
import com.sk89q.worldedit.world.block.BlockTypes;
import io.papermc.lib.PaperLib;
import org.bukkit.Bukkit;
import org.bukkit.Chunk;
import org.bukkit.World;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Item;
import org.bukkit.entity.Player;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.CompletableFuture;

import static com.plotsquared.core.util.entity.EntityCategories.CAP_ANIMAL;
import static com.plotsquared.core.util.entity.EntityCategories.CAP_ENTITY;
Expand Down Expand Up @@ -88,79 +87,45 @@ public boolean handleClear(

@Override
public int[] countEntities(@NonNull Plot plot) {
int[] count = new int[6];
int[] existing = (int[]) plot.getMeta("EntityCount");
if (existing != null && (System.currentTimeMillis() - (long) plot.getMeta("EntityCountTime") < 1000)) {
return existing;
}
PlotArea area = plot.getArea();
World world = BukkitUtil.getWorld(area.getWorldName());
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));
}
}
if (world == null) {
return count;
}

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();
PaperLib.getChunkAtAsync(location).thenAccept(chunk -> {
if (chunks.contains(chunk)) {
int X = chunk.getX();
int Z = chunk.getZ();
if (X > bx && X < tx && Z > bz && Z < tz) {
count(count, entity);
} else {
Plot other = area.getPlot(BukkitUtil.adapt(location));
if (plot.equals(other)) {
count(count, entity);
}
for (final CuboidRegion region : plot.getRegions()) {
for (int x = region.getMinimumPoint().getX() >> 4; x <= region.getMaximumPoint().getX() >> 4; x++) {
for (int z = region.getMinimumPoint().getZ() >> 4; z <= region.getMaximumPoint().getZ() >> 4; z++) {
Chunk chunk = world.getChunkAt(x, z);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this method actually be called async?

final Entity[] entities = chunk.getEntities();
for (Entity entity : entities) {
if (entity instanceof Player || entity instanceof Item) {
continue;
}
}
});
}
} 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) {
Plot other = area.getPlot(BukkitUtil.adapt(entity.getLocation()));

org.bukkit.Location location = entity.getLocation();
Plot other = area.getPlot(BukkitUtil.adapt(location));
if (plot.equals(other)) {
count(count, entity);
}
} else {
count(count, entity);
}
}
}
}

return count;
}

@Override
public CompletableFuture<int[]> countEntitiesAsync(final Plot plot) {
return CompletableFuture.supplyAsync(() -> countEntities(plot));
}

@Override
public boolean regenerateRegion(
final @NonNull Location pos1,
Expand Down
17 changes: 9 additions & 8 deletions Core/src/main/java/com/plotsquared/core/command/Caps.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,15 @@ public boolean onCommand(final PlotPlayer<?> player, final String[] args) {
player.sendMessage(TranslatableCaption.of("schematics.schematic_too_large"));
return false;
}
player.sendMessage(TranslatableCaption.of("info.plot_caps_header"));
final int[] countedEntities = plot.countEntities();
sendFormatted(plot, player, MobCapFlag.class, countedEntities, "mobs", CAP_MOB);
sendFormatted(plot, player, HostileCapFlag.class, countedEntities, "hostile", CAP_MONSTER);
sendFormatted(plot, player, AnimalCapFlag.class, countedEntities, "animals", CAP_ANIMAL);
sendFormatted(plot, player, VehicleCapFlag.class, countedEntities, "vehicle", CAP_VEHICLE);
sendFormatted(plot, player, MiscCapFlag.class, countedEntities, "misc", CAP_MISC);
sendFormatted(plot, player, EntityCapFlag.class, countedEntities, "entities", CAP_ENTITY);
plot.countEntitiesAsync().thenAccept(countedEntities -> {
player.sendMessage(TranslatableCaption.of("info.plot_caps_header"));
sendFormatted(plot, player, MobCapFlag.class, countedEntities, "mobs", CAP_MOB);
sendFormatted(plot, player, HostileCapFlag.class, countedEntities, "hostile", CAP_MONSTER);
sendFormatted(plot, player, AnimalCapFlag.class, countedEntities, "animals", CAP_ANIMAL);
sendFormatted(plot, player, VehicleCapFlag.class, countedEntities, "vehicle", CAP_VEHICLE);
sendFormatted(plot, player, MiscCapFlag.class, countedEntities, "misc", CAP_MISC);
sendFormatted(plot, player, EntityCapFlag.class, countedEntities, "entities", CAP_ENTITY);
});
return true;
}

Expand Down
23 changes: 5 additions & 18 deletions Core/src/main/java/com/plotsquared/core/plot/Plot.java
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,6 @@
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Consumer;

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;

/**
* The plot class<br>
* [IMPORTANT]
Expand Down Expand Up @@ -1219,17 +1212,11 @@ public boolean removeFlag(final @NonNull PlotFlag<?, ?> flag) {
* @see RegionManager#countEntities(Plot)
*/
public int[] countEntities() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should probably deprecate the "old" sync methods if moving to async

int[] count = new int[6];
for (Plot current : this.getConnectedPlots()) {
int[] result = this.regionManager.countEntities(current);
count[CAP_ENTITY] += result[CAP_ENTITY];
count[CAP_ANIMAL] += result[CAP_ANIMAL];
count[CAP_MONSTER] += result[CAP_MONSTER];
count[CAP_MOB] += result[CAP_MOB];
count[CAP_VEHICLE] += result[CAP_VEHICLE];
count[CAP_MISC] += result[CAP_MISC];
}
return count;
return this.regionManager.countEntities(this);
}

public CompletableFuture<int[]> countEntitiesAsync() {
return this.regionManager.countEntitiesAsync(this);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
import java.io.File;
import java.util.Collection;
import java.util.Set;
import java.util.concurrent.CompletableFuture;

public abstract class RegionManager {

Expand Down Expand Up @@ -88,6 +89,8 @@ public static BlockVector2 getRegion(Location location) {
*/
public abstract int[] countEntities(Plot plot);

public abstract CompletableFuture<int[]> countEntitiesAsync(Plot plot);

public void deleteRegionFiles(final String world, final Collection<BlockVector2> chunks, final Runnable whenDone) {
TaskManager.runTaskAsync(() -> {
for (BlockVector2 loc : chunks) {
Expand Down
Loading