Skip to content

Commit 450425c

Browse files
committed
chore: add and use method for entity removal
1 parent ec3758c commit 450425c

File tree

3 files changed

+23
-13
lines changed

3 files changed

+23
-13
lines changed

worldedit-bukkit/src/main/java/com/sk89q/worldedit/bukkit/BukkitWorld.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,14 @@ public List<com.sk89q.worldedit.entity.Entity> getEntities() {
167167
return list;
168168
}
169169

170+
@Override
171+
public int removeEntities(final Region region) {
172+
List<com.sk89q.worldedit.entity.Entity> entities = getEntities(region);
173+
return TaskManager.taskManager().sync(() -> entities.stream()
174+
.mapToInt(entity -> entity.remove() ? 1 : 0).sum()
175+
);
176+
}
177+
170178
//FAWE: createEntity was moved to IChunkExtent to prevent issues with Async Entity Add.
171179

172180
/**

worldedit-core/src/main/java/com/sk89q/worldedit/command/ClipboardCommands.java

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
import com.fastasyncworldedit.core.util.ImgurUtility;
3737
import com.fastasyncworldedit.core.util.MainUtil;
3838
import com.fastasyncworldedit.core.util.MaskTraverser;
39-
import com.fastasyncworldedit.core.util.TaskManager;
4039
import com.fastasyncworldedit.core.util.task.RunnableVal;
4140
import com.google.common.collect.Lists;
4241
import com.sk89q.worldedit.EditSession;
@@ -51,7 +50,6 @@
5150
import com.sk89q.worldedit.command.util.annotation.Confirm;
5251
import com.sk89q.worldedit.command.util.annotation.Preload;
5352
import com.sk89q.worldedit.command.util.annotation.SynchronousSettingExpected;
54-
import com.sk89q.worldedit.entity.Entity;
5553
import com.sk89q.worldedit.extension.platform.Actor;
5654
import com.sk89q.worldedit.extent.clipboard.BlockArrayClipboard;
5755
import com.sk89q.worldedit.extent.clipboard.Clipboard;
@@ -99,7 +97,6 @@
9997
import java.net.URI;
10098
import java.net.URL;
10199
import java.nio.file.Files;
102-
import java.util.Collection;
103100
import java.util.HashSet;
104101
import java.util.List;
105102
import java.util.Set;
@@ -481,14 +478,7 @@ public void place(
481478
.apply(region.getMaximumPoint().subtract(region.getMinimumPoint()).toVector3())
482479
.toBlockPoint());
483480
if (removeEntities) {
484-
// Collect entities on the current (command) thread, in case it benefits from asynchronous retrieval (unlikely)
485-
final Collection<? extends Entity> entities = editSession.getEntities(new CuboidRegion(realTo, max));
486-
// BukkitEntity#remove is synchronized, but it makes more sense to synchronize here beforehand in case many
487-
// entities are affected. BukkitEntity will not synchronize if it's already called on the main thread.
488-
TaskManager.taskManager().sync(() -> {
489-
entities.forEach(Entity::remove);
490-
return true;
491-
});
481+
editSession.removeEntities(new CuboidRegion(realTo, max));
492482
}
493483
if (selectPasted || onlySelect) {
494484
RegionSelector selector = new CuboidRegionSelector(world, realTo, max);
@@ -578,9 +568,9 @@ public void paste(
578568
Vector3 realTo = to.toVector3().add(transform.apply(clipboardOffset.toVector3()));
579569
Vector3 max = realTo.add(transform.apply(region.getMaximumPoint().subtract(region.getMinimumPoint()).toVector3()));
580570

581-
// FAWE start - entity remova;l
571+
// FAWE start - entity removal
582572
if (removeEntities) {
583-
editSession.getEntities(new CuboidRegion(realTo.toBlockPoint(), max.toBlockPoint())).forEach(Entity::remove);
573+
editSession.removeEntities(new CuboidRegion(realTo.toBlockPoint(), max.toBlockPoint()));
584574
}
585575
if (selectPasted || onlySelect) {
586576
//FAWE end

worldedit-core/src/main/java/com/sk89q/worldedit/extent/Extent.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,18 @@ default Entity createEntity(Location location, BaseEntity entity, UUID uuid) {
176176
default void removeEntity(int x, int y, int z, UUID uuid) {
177177
}
178178

179+
/**
180+
* Removes all entities in the given region.
181+
*
182+
* @param region the region
183+
* @return the number of entities removed
184+
*/
185+
default int removeEntities(Region region) {
186+
return this.getEntities(region).stream()
187+
.mapToInt(entity -> entity.remove() ? 1 : 0)
188+
.sum();
189+
}
190+
179191
/*
180192
Queue based methods
181193
TODO NOT IMPLEMENTED:

0 commit comments

Comments
 (0)