Skip to content

Commit

Permalink
Implement asynchronous entity check
Browse files Browse the repository at this point in the history
  • Loading branch information
ch4ika committed Feb 23, 2024
1 parent a120e49 commit 9720a91
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 23 deletions.
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)) {
event.setCancelled(true);
}

BukkitEntityUtil.checkEntityAsync(entity, plot.getBasePlot(false)).thenAcceptAsync(toRemove -> {
if(toRemove) {
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 @@ -45,12 +45,14 @@
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.List;
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 @@ -102,7 +104,7 @@ public int[] countEntities(@NonNull Plot plot) {
Chunk chunk = world.getChunkAt(x, z);
final Entity[] entities = chunk.getEntities();
for (Entity entity : entities) {
if (entity instanceof Player) {
if (entity instanceof Player || entity instanceof Item) {
continue;
}

Expand All @@ -119,6 +121,11 @@ public int[] countEntities(@NonNull Plot plot) {
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
4 changes: 4 additions & 0 deletions Core/src/main/java/com/plotsquared/core/plot/Plot.java
Original file line number Diff line number Diff line change
Expand Up @@ -1215,6 +1215,10 @@ public int[] countEntities() {
return this.regionManager.countEntities(this);
}

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

/**
* Returns true if a previous task was running
*
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

0 comments on commit 9720a91

Please sign in to comment.