Skip to content

Commit

Permalink
feat: implement "unloaded-entity" operations
Browse files Browse the repository at this point in the history
 - Add new extent that does an action on chunk GET load
 - closes #1826
  • Loading branch information
dordsor21 committed Jul 14, 2023
1 parent a680c7c commit 6abf7f3
Show file tree
Hide file tree
Showing 41 changed files with 1,146 additions and 89 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.sk89q.jnbt.StringTag;
import com.sk89q.jnbt.Tag;
import com.sk89q.worldedit.bukkit.BukkitAdapter;
import com.sk89q.worldedit.bukkit.BukkitEntity;
import com.sk89q.worldedit.bukkit.WorldEditPlugin;
import com.sk89q.worldedit.bukkit.adapter.impl.fawe.v1_17_R1_2.nbt.PaperweightLazyCompoundTag;
import com.sk89q.worldedit.internal.Constants;
Expand Down Expand Up @@ -57,6 +58,7 @@
import org.bukkit.World;
import org.bukkit.craftbukkit.v1_17_R1.CraftWorld;
import org.bukkit.craftbukkit.v1_17_R1.block.CraftBlock;
import org.bukkit.craftbukkit.v1_17_R1.entity.CraftEntity;
import org.bukkit.event.entity.CreatureSpawnEvent;

import javax.annotation.Nonnull;
Expand Down Expand Up @@ -124,11 +126,13 @@ public PaperweightGetBlocks(ServerLevel serverLevel, int chunkX, int chunkZ) {
this.blockLight = new DataLayer[getSectionCount()];
}

public int getChunkX() {
@Override
public int getX() {
return chunkX;
}

public int getChunkZ() {
@Override
public int getZ() {
return chunkZ;
}

Expand Down Expand Up @@ -338,7 +342,7 @@ public CompoundTag getEntity(UUID uuid) {

@Override
public Set<CompoundTag> getEntities() {
List<Entity> entities = PaperweightPlatformAdapter.getEntities(getChunk());
List<Entity> entities = PaperweightPlatformAdapter.getEntities(ensureLoaded(serverLevel, chunkX, chunkZ));
if (entities.isEmpty()) {
return Collections.emptySet();
}
Expand Down Expand Up @@ -382,6 +386,53 @@ public Iterator<CompoundTag> iterator() {
};
}

@Override
public Set<com.sk89q.worldedit.entity.Entity> getFullEntities() {
getSections(true);
getChunk();
List<Entity> entities = PaperweightPlatformAdapter.getEntities(ensureLoaded(serverLevel, chunkX, chunkZ));
if (entities.isEmpty()) {
return Collections.emptySet();
}
int size = entities.size();
return new AbstractSet<>() {
@Override
public int size() {
return size;
}

@Override
public boolean isEmpty() {
return false;
}

@Override
public boolean contains(Object get) {
if (!(get instanceof com.sk89q.worldedit.entity.Entity e)) {
return false;
}
UUID getUUID = e.getState().getNbtData().getUUID();
for (Entity entity : entities) {
UUID uuid = entity.getUUID();
if (uuid.equals(getUUID)) {
return true;
}
}
return false;
}

@Nonnull
@Override
public Iterator<com.sk89q.worldedit.entity.Entity> iterator() {
Iterable<com.sk89q.worldedit.entity.Entity> result = entities
.stream()
.map(input -> new BukkitEntity(input.getBukkitEntity()))
.collect(Collectors.toList());
return result.iterator();
}
};
}

private void removeEntity(Entity entity) {
entity.discard();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ public class PaperweightGetBlocks_Copy implements IChunkGet {
private final char[][] blocks;
private final int minHeight;
private final int maxHeight;
private final int chunkX;
private final int chunkZ;
final ServerLevel serverLevel;
final LevelChunk levelChunk;
private ChunkBiomeContainer chunkBiomeContainer;
Expand All @@ -46,6 +48,8 @@ protected PaperweightGetBlocks_Copy(LevelChunk levelChunk) {
this.minHeight = serverLevel.getMinBuildHeight();
this.maxHeight = serverLevel.getMaxBuildHeight() - 1; // Minecraft max limit is exclusive.
this.blocks = new char[getSectionCount()][];
this.chunkX = levelChunk.locX;
this.chunkZ = levelChunk.locZ;
}

protected void storeTile(BlockEntity blockEntity) {
Expand Down Expand Up @@ -83,6 +87,11 @@ public Set<CompoundTag> getEntities() {
return this.entities;
}

@Override
public Set<com.sk89q.worldedit.entity.Entity> getFullEntities() {
throw new UnsupportedOperationException("Cannot get full entities from GET copy.");
}

@Override
public CompoundTag getEntity(UUID uuid) {
for (CompoundTag tag : entities) {
Expand Down Expand Up @@ -134,6 +143,16 @@ public int getMinSectionPosition() {
return minHeight >> 4;
}

@Override
public int getX() {
return chunkX;
}

@Override
public int getZ() {
return chunkZ;
}

protected void storeBiomes(ChunkBiomeContainer chunkBiomeContainer) {
// The to do one line below is pre-paperweight and needs to be revised
// TODO revisit last parameter, BiomeStorage[] *would* be more efficient
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public Extent construct(final Extent child) {

@Override
public ProcessorScope getScope() {
return ProcessorScope.READING_SET_BLOCKS;
return ProcessorScope.READING_BLOCKS;
}

private boolean wasAdjacentToWater(char[] get, char[] set, int i, int x, int y, int z) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@
import com.fastasyncworldedit.core.util.MathMan;
import com.fastasyncworldedit.core.util.collection.AdaptedMap;
import com.google.common.base.Suppliers;
import com.google.common.collect.Iterables;
import com.sk89q.jnbt.CompoundTag;
import com.sk89q.jnbt.ListTag;
import com.sk89q.jnbt.StringTag;
import com.sk89q.jnbt.Tag;
import com.sk89q.worldedit.bukkit.BukkitAdapter;
import com.sk89q.worldedit.bukkit.BukkitEntity;
import com.sk89q.worldedit.bukkit.WorldEditPlugin;
import com.sk89q.worldedit.bukkit.adapter.impl.fawe.v1_18_R2.nbt.PaperweightLazyCompoundTag;
import com.sk89q.worldedit.internal.Constants;
Expand Down Expand Up @@ -60,13 +60,13 @@
import org.bukkit.World;
import org.bukkit.craftbukkit.v1_18_R2.CraftWorld;
import org.bukkit.craftbukkit.v1_18_R2.block.CraftBlock;
import org.bukkit.craftbukkit.v1_18_R2.entity.CraftEntity;
import org.bukkit.event.entity.CreatureSpawnEvent;

import javax.annotation.Nonnull;
import java.util.AbstractSet;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
Expand All @@ -82,7 +82,6 @@
import java.util.concurrent.locks.ReentrantReadWriteLock;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

public class PaperweightGetBlocks extends CharGetBlocks implements BukkitGetBlocks {

Expand Down Expand Up @@ -132,11 +131,13 @@ public PaperweightGetBlocks(ServerLevel serverLevel, int chunkX, int chunkZ) {
this.biomeHolderIdMap = biomeRegistry.asHolderIdMap();
}

public int getChunkX() {
@Override
public int getX() {
return chunkX;
}

public int getChunkZ() {
@Override
public int getZ() {
return chunkZ;
}

Expand Down Expand Up @@ -332,7 +333,7 @@ public CompoundTag getEntity(UUID uuid) {

@Override
public Set<CompoundTag> getEntities() {
List<Entity> entities = PaperweightPlatformAdapter.getEntities(getChunk());
List<Entity> entities = PaperweightPlatformAdapter.getEntities(ensureLoaded(serverLevel, chunkX, chunkZ));
if (entities.isEmpty()) {
return Collections.emptySet();
}
Expand Down Expand Up @@ -376,6 +377,51 @@ public Iterator<CompoundTag> iterator() {
};
}

@Override
public Set<com.sk89q.worldedit.entity.Entity> getFullEntities() {
List<Entity> entities = PaperweightPlatformAdapter.getEntities(ensureLoaded(serverLevel, chunkX, chunkZ));
if (entities.isEmpty()) {
return Collections.emptySet();
}
int size = entities.size();
return new AbstractSet<>() {
@Override
public int size() {
return size;
}

@Override
public boolean isEmpty() {
return false;
}

@Override
public boolean contains(Object get) {
if (!(get instanceof com.sk89q.worldedit.entity.Entity e)) {
return false;
}
UUID getUUID = e.getState().getNbtData().getUUID();
for (Entity entity : entities) {
UUID uuid = entity.getUUID();
if (uuid.equals(getUUID)) {
return true;
}
}
return false;
}

@Nonnull
@Override
public Iterator<com.sk89q.worldedit.entity.Entity> iterator() {
Iterable<com.sk89q.worldedit.entity.Entity> result = entities
.stream()
.map(input -> new BukkitEntity(input.getBukkitEntity()))
.collect(Collectors.toList());
return result.iterator();
}
};
}

private void removeEntity(Entity entity) {
entity.discard();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ public class PaperweightGetBlocks_Copy implements IChunkGet {
private final char[][] blocks;
private final int minHeight;
private final int maxHeight;
private final int chunkX;
private final int chunkZ;
final ServerLevel serverLevel;
final LevelChunk levelChunk;
private PalettedContainer<Holder<Biome>>[] biomes = null;
Expand All @@ -48,6 +50,8 @@ protected PaperweightGetBlocks_Copy(LevelChunk levelChunk) {
this.minHeight = serverLevel.getMinBuildHeight();
this.maxHeight = serverLevel.getMaxBuildHeight() - 1; // Minecraft max limit is exclusive.
this.blocks = new char[getSectionCount()][];
this.chunkX = levelChunk.locX;
this.chunkZ = levelChunk.locZ;
}

protected void storeTile(BlockEntity blockEntity) {
Expand Down Expand Up @@ -85,6 +89,11 @@ public Set<CompoundTag> getEntities() {
return this.entities;
}

@Override
public Set<com.sk89q.worldedit.entity.Entity> getFullEntities() {
throw new UnsupportedOperationException("Cannot get full entities from GET copy.");
}

@Override
public CompoundTag getEntity(UUID uuid) {
for (CompoundTag tag : entities) {
Expand Down Expand Up @@ -136,6 +145,16 @@ public int getMinSectionPosition() {
return minHeight >> 4;
}

@Override
public int getX() {
return chunkX;
}

@Override
public int getZ() {
return chunkZ;
}

@Override
public BiomeType getBiomeType(int x, int y, int z) {
Holder<Biome> biome = biomes[(y >> 4) - getMinSectionPosition()].get(x >> 2, (y & 15) >> 2, z >> 2);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public Extent construct(final Extent child) {

@Override
public ProcessorScope getScope() {
return ProcessorScope.READING_SET_BLOCKS;
return ProcessorScope.READING_BLOCKS;
}

private boolean wasAdjacentToWater(char[] get, char[] set, int i, int x, int y, int z) {
Expand Down
Loading

0 comments on commit 6abf7f3

Please sign in to comment.