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 Sep 11, 2024
1 parent 766a5d6 commit dec5e20
Show file tree
Hide file tree
Showing 37 changed files with 1,135 additions and 64 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_20_R2.nbt.PaperweightLazyCompoundTag;
import com.sk89q.worldedit.internal.Constants;
Expand Down Expand Up @@ -137,11 +138,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 @@ -359,8 +362,7 @@ public CompoundTag getEntity(UUID uuid) {

@Override
public Set<CompoundTag> getEntities() {
ensureLoaded(serverLevel, chunkX, chunkZ);
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 @@ -404,6 +406,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 @@ -43,6 +43,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 Holder<Biome>[][] biomes = null;
Expand All @@ -53,6 +55,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 @@ -90,6 +94,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 @@ -142,6 +151,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()][(y & 12) << 2 | (z & 12) | (x & 12) >> 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
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_20_R3.nbt.PaperweightLazyCompoundTag;
import com.sk89q.worldedit.internal.Constants;
Expand Down Expand Up @@ -136,11 +137,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 @@ -358,8 +361,7 @@ public CompoundTag getEntity(UUID uuid) {

@Override
public Set<CompoundTag> getEntities() {
ensureLoaded(serverLevel, chunkX, chunkZ);
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 @@ -403,6 +405,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 @@ -43,6 +43,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 Holder<Biome>[][] biomes = null;
Expand All @@ -53,6 +55,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 @@ -90,6 +94,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 @@ -142,6 +151,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()][(y & 12) << 2 | (z & 12) | (x & 12) >> 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
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_20_R4.nbt.PaperweightLazyCompoundTag;
import com.sk89q.worldedit.internal.Constants;
Expand Down Expand Up @@ -139,11 +140,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 @@ -361,8 +364,7 @@ public CompoundTag getEntity(UUID uuid) {

@Override
public Set<CompoundTag> getEntities() {
ensureLoaded(serverLevel, chunkX, chunkZ);
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 @@ -406,6 +408,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
Loading

0 comments on commit dec5e20

Please sign in to comment.