Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: improved entity operations #2356

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.fastasyncworldedit.bukkit.adapter.BukkitGetBlocks;
import com.fastasyncworldedit.bukkit.adapter.DelegateSemaphore;
import com.fastasyncworldedit.bukkit.adapter.NativeEntityFunctionSet;
import com.fastasyncworldedit.core.Fawe;
import com.fastasyncworldedit.core.FaweCache;
import com.fastasyncworldedit.core.configuration.Settings;
Expand All @@ -17,6 +18,7 @@
import com.fastasyncworldedit.core.util.NbtUtils;
import com.fastasyncworldedit.core.util.collection.AdaptedMap;
import com.sk89q.worldedit.bukkit.BukkitAdapter;
import com.sk89q.worldedit.bukkit.BukkitEntity;
import com.sk89q.worldedit.bukkit.WorldEditPlugin;
import com.sk89q.worldedit.internal.Constants;
import com.sk89q.worldedit.internal.util.LogManagerCompat;
Expand Down Expand Up @@ -69,6 +71,7 @@
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.AbstractCollection;
import java.util.AbstractSet;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
Expand All @@ -88,6 +91,7 @@
import java.util.concurrent.locks.ReentrantLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import java.util.function.Function;
import java.util.stream.Collectors;

import static net.minecraft.core.registries.Registries.BIOME;

Expand Down Expand Up @@ -145,11 +149,13 @@ public PaperweightGetBlocks(ServerLevel serverLevel, int chunkX, int chunkZ) {
this.chunkPos = new IntPair(chunkX, chunkZ);
}

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

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

Expand Down Expand Up @@ -368,49 +374,24 @@ public int[] getHeightMap(HeightMapType type) {

@Override
public Collection<FaweCompoundTag> entities() {
ensureLoaded(serverLevel, chunkX, chunkZ);
List<Entity> entities = PaperweightPlatformAdapter.getEntities(getChunk());
List<Entity> entities = PaperweightPlatformAdapter.getEntities(ensureLoaded(serverLevel, chunkX, chunkZ));
if (entities.isEmpty()) {
return Collections.emptyList();
}
int size = entities.size();
return new AbstractCollection<>() {
@Override
public int size() {
return size;
}

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

@Override
public boolean contains(Object get) {
if (!(get instanceof FaweCompoundTag getTag)) {
return false;
}
UUID getUUID = NbtUtils.uuid(getTag);
for (Entity entity : entities) {
UUID uuid = entity.getUUID();
if (uuid.equals(getUUID)) {
return true;
}
}
return false;
}
return new NativeEntityFunctionSet<>(entities, Entity::getUUID, e -> {
net.minecraft.nbt.CompoundTag tag = new net.minecraft.nbt.CompoundTag();
e.save(tag);
return FaweCompoundTag.of(() -> (LinCompoundTag) adapter.toNativeLin(tag));
});
}

@Nonnull
@Override
public Iterator<FaweCompoundTag> iterator() {
Iterable<FaweCompoundTag> result = entities.stream().map(input -> {
CompoundTag tag = new CompoundTag();
input.save(tag);
return FaweCompoundTag.of((LinCompoundTag) adapter.toNativeLin(tag));
})::iterator;
return result.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();
}
return new NativeEntityFunctionSet<>(entities, Entity::getUUID, e -> new BukkitEntity(e.getBukkitEntity()));
}

private void removeEntity(Entity entity) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,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 @@ -56,6 +58,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 @@ -94,6 +98,11 @@ public Collection<FaweCompoundTag> entities() {
return null;
}

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

@Override
public boolean isCreateCopy() {
return false;
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()][(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 @@ -2,6 +2,7 @@

import com.fastasyncworldedit.bukkit.adapter.BukkitGetBlocks;
import com.fastasyncworldedit.bukkit.adapter.DelegateSemaphore;
import com.fastasyncworldedit.bukkit.adapter.NativeEntityFunctionSet;
import com.fastasyncworldedit.core.Fawe;
import com.fastasyncworldedit.core.FaweCache;
import com.fastasyncworldedit.core.configuration.Settings;
Expand All @@ -17,6 +18,7 @@
import com.fastasyncworldedit.core.util.NbtUtils;
import com.fastasyncworldedit.core.util.collection.AdaptedMap;
import com.sk89q.worldedit.bukkit.BukkitAdapter;
import com.sk89q.worldedit.bukkit.BukkitEntity;
import com.sk89q.worldedit.bukkit.WorldEditPlugin;
import com.sk89q.worldedit.internal.Constants;
import com.sk89q.worldedit.internal.util.LogManagerCompat;
Expand Down Expand Up @@ -69,6 +71,7 @@
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.AbstractCollection;
import java.util.AbstractSet;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
Expand All @@ -88,6 +91,7 @@
import java.util.concurrent.locks.ReentrantLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import java.util.function.Function;
import java.util.stream.Collectors;

import static net.minecraft.core.registries.Registries.BIOME;

Expand Down Expand Up @@ -145,11 +149,13 @@ public PaperweightGetBlocks(ServerLevel serverLevel, int chunkX, int chunkZ) {
this.chunkPos = new IntPair(chunkX, chunkZ);
}

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

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

Expand Down Expand Up @@ -368,49 +374,24 @@ public int[] getHeightMap(HeightMapType type) {

@Override
public Collection<FaweCompoundTag> entities() {
ensureLoaded(serverLevel, chunkX, chunkZ);
List<Entity> entities = PaperweightPlatformAdapter.getEntities(getChunk());
List<Entity> entities = PaperweightPlatformAdapter.getEntities(ensureLoaded(serverLevel, chunkX, chunkZ));
if (entities.isEmpty()) {
return Collections.emptyList();
}
int size = entities.size();
return new AbstractCollection<>() {
@Override
public int size() {
return size;
}

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

@Override
public boolean contains(Object get) {
if (!(get instanceof FaweCompoundTag getTag)) {
return false;
}
UUID getUUID = NbtUtils.uuid(getTag);
for (Entity entity : entities) {
UUID uuid = entity.getUUID();
if (uuid.equals(getUUID)) {
return true;
}
}
return false;
}
return new NativeEntityFunctionSet<>(entities, Entity::getUUID, e -> {
net.minecraft.nbt.CompoundTag tag = new net.minecraft.nbt.CompoundTag();
e.save(tag);
return FaweCompoundTag.of(() -> (LinCompoundTag) adapter.toNativeLin(tag));
});
}

@Nonnull
@Override
public Iterator<FaweCompoundTag> iterator() {
Iterable<FaweCompoundTag> result = entities.stream().map(input -> {
net.minecraft.nbt.CompoundTag tag = new net.minecraft.nbt.CompoundTag();
input.save(tag);
return FaweCompoundTag.of((LinCompoundTag) adapter.toNativeLin(tag));
})::iterator;
return result.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();
}
return new NativeEntityFunctionSet<>(entities, Entity::getUUID, e -> new BukkitEntity(e.getBukkitEntity()));
}

private void removeEntity(Entity entity) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,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 @@ -56,6 +58,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 @@ -94,6 +98,11 @@ public Collection<FaweCompoundTag> entities() {
return null;
}

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

@Override
public boolean isCreateCopy() {
return false;
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()][(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
Loading
Loading