From cfd2fcf44389728a68c8d8b14befc4e443eddaf0 Mon Sep 17 00:00:00 2001 From: Matthew Miller Date: Fri, 18 Dec 2020 21:24:21 +1000 Subject: [PATCH] Add API to get Tile Entities in a Chunk by Predicate --- ...ile-Entities-from-a-chunk-without-sn.patch | 41 ++++++++++++++----- ...ile-Entities-from-a-chunk-without-sn.patch | 37 +++++++++++++++++ 2 files changed, 67 insertions(+), 11 deletions(-) diff --git a/Spigot-API-Patches/Ability-to-get-Tile-Entities-from-a-chunk-without-sn.patch b/Spigot-API-Patches/Ability-to-get-Tile-Entities-from-a-chunk-without-sn.patch index 9a88a5c104..b74f35fc79 100644 --- a/Spigot-API-Patches/Ability-to-get-Tile-Entities-from-a-chunk-without-sn.patch +++ b/Spigot-API-Patches/Ability-to-get-Tile-Entities-from-a-chunk-without-sn.patch @@ -8,30 +8,49 @@ diff --git a/src/main/java/org/bukkit/Chunk.java b/src/main/java/org/bukkit/Chun index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644 --- a/src/main/java/org/bukkit/Chunk.java +++ b/src/main/java/org/bukkit/Chunk.java +@@ -0,0 +0,0 @@ + package org.bukkit; + + import java.util.Collection; ++import java.util.function.Predicate; ++ + import org.bukkit.block.Block; + import org.bukkit.block.BlockState; + import org.bukkit.block.data.BlockData; @@ -0,0 +0,0 @@ public interface Chunk extends PersistentDataHolder { @NotNull Entity[] getEntities(); + // Paper start -+ /** -+ * Get a list of all tile entities in the chunk. -+ * -+ * @return The tile entities. -+ */ -+ @NotNull -+ default BlockState[] getTileEntities() { -+ return getTileEntities(true); -+ } -+ /** * Get a list of all tile entities in the chunk. * -+ * @param useSnapshot Take snapshots or direct references * @return The tile entities. */ @NotNull - BlockState[] getTileEntities(); ++ default BlockState[] getTileEntities() { ++ return getTileEntities(true); ++ } ++ ++ /** ++ * Get a list of all tile entities in the chunk. ++ * ++ * @param useSnapshot Take snapshots or direct references ++ * @return The tile entities. ++ */ ++ @NotNull + BlockState[] getTileEntities(boolean useSnapshot); ++ ++ /** ++ * Get a list of all tile entities that match a given predicate in the chunk. ++ * ++ * @param blockPredicate The predicate of blocks to return tile entities for ++ * @param useSnapshot Take snapshots or direct references ++ * @return The tile entities. ++ */ ++ @NotNull ++ Collection getTileEntities(@NotNull Predicate blockPredicate, boolean useSnapshot); + // Paper end /** diff --git a/Spigot-Server-Patches/Ability-to-get-Tile-Entities-from-a-chunk-without-sn.patch b/Spigot-Server-Patches/Ability-to-get-Tile-Entities-from-a-chunk-without-sn.patch index 64e05493b2..c32017fa5b 100644 --- a/Spigot-Server-Patches/Ability-to-get-Tile-Entities-from-a-chunk-without-sn.patch +++ b/Spigot-Server-Patches/Ability-to-get-Tile-Entities-from-a-chunk-without-sn.patch @@ -8,6 +8,17 @@ diff --git a/src/main/java/org/bukkit/craftbukkit/CraftChunk.java b/src/main/jav index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644 --- a/src/main/java/org/bukkit/craftbukkit/CraftChunk.java +++ b/src/main/java/org/bukkit/craftbukkit/CraftChunk.java +@@ -0,0 +0,0 @@ package org.bukkit.craftbukkit; + import com.google.common.base.Preconditions; + import com.google.common.base.Predicates; + import java.lang.ref.WeakReference; ++import java.util.ArrayList; + import java.util.Arrays; + import java.util.Collection; ++import java.util.List; + import java.util.function.Predicate; + import net.minecraft.server.BiomeStorage; + import net.minecraft.server.BlockPosition; @@ -0,0 +0,0 @@ public class CraftChunk implements Chunk { @Override @@ -31,6 +42,32 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000 BlockPosition position = (BlockPosition) obj; - entities[index++] = worldServer.getWorld().getBlockAt(position.getX(), position.getY(), position.getZ()).getState(); + entities[index++] = worldServer.getWorld().getBlockAt(position.getX(), position.getY(), position.getZ()).getState(useSnapshot); // Paper ++ } ++ ++ return entities; ++ } ++ ++ // Paper start ++ @Override ++ public Collection getTileEntities(Predicate blockPredicate, boolean useSnapshot) { ++ Preconditions.checkNotNull(blockPredicate, "blockPredicate"); ++ if (!isLoaded()) { ++ getWorld().getChunkAt(x, z); // Transient load for this tick ++ } ++ net.minecraft.server.Chunk chunk = getHandle(); ++ ++ List entities = new ArrayList<>(); ++ ++ for (BlockPosition position : chunk.tileEntities.keySet()) { ++ Block block = worldServer.getWorld().getBlockAt(position.getX(), position.getY(), position.getZ()); ++ if (blockPredicate.test(block)) { ++ entities.add(block.getState(useSnapshot)); ++ } } return entities; + } ++ // Paper end + + @Override + public boolean isLoaded() {