diff --git a/worldedit-bukkit/adapters/adapter-1_20_2/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_20_R2/PaperweightGetBlocks.java b/worldedit-bukkit/adapters/adapter-1_20_2/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_20_R2/PaperweightGetBlocks.java index 8251ee0e3..0d716e9b7 100644 --- a/worldedit-bukkit/adapters/adapter-1_20_2/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_20_R2/PaperweightGetBlocks.java +++ b/worldedit-bukkit/adapters/adapter-1_20_2/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_20_R2/PaperweightGetBlocks.java @@ -2,6 +2,7 @@ package com.sk89q.worldedit.bukkit.adapter.impl.fawe.v1_20_R2; 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; @@ -377,44 +378,11 @@ public class PaperweightGetBlocks extends CharGetBlocks implements BukkitGetBloc 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; - } - - @Nonnull - @Override - public Iterator iterator() { - Iterable result = entities.stream().map(input -> { - CompoundTag tag = new CompoundTag(); - input.save(tag); - return FaweCompoundTag.of((LinCompoundTag) adapter.toNativeLin(tag)); - })::iterator; - return result.iterator(); - } - }; + 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)); + }); } @Override @@ -423,43 +391,7 @@ public class PaperweightGetBlocks extends CharGetBlocks implements BukkitGetBloc 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 iterator() { - Iterable result = entities - .stream() - .map(input -> new BukkitEntity(input.getBukkitEntity())) - .collect(Collectors.toList()); - return result.iterator(); - } - }; + return new NativeEntityFunctionSet<>(entities, Entity::getUUID, e -> new BukkitEntity(e.getBukkitEntity())); } private void removeEntity(Entity entity) { diff --git a/worldedit-bukkit/adapters/adapter-1_20_4/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_20_R3/PaperweightGetBlocks.java b/worldedit-bukkit/adapters/adapter-1_20_4/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_20_R3/PaperweightGetBlocks.java index a04f7d2d4..399eacefb 100644 --- a/worldedit-bukkit/adapters/adapter-1_20_4/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_20_R3/PaperweightGetBlocks.java +++ b/worldedit-bukkit/adapters/adapter-1_20_4/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_20_R3/PaperweightGetBlocks.java @@ -2,6 +2,7 @@ package com.sk89q.worldedit.bukkit.adapter.impl.fawe.v1_20_R3; 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; @@ -377,44 +378,11 @@ public class PaperweightGetBlocks extends CharGetBlocks implements BukkitGetBloc 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; - } - - @Nonnull - @Override - public Iterator iterator() { - Iterable 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(); - } - }; + 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)); + }); } @Override @@ -423,43 +391,7 @@ public class PaperweightGetBlocks extends CharGetBlocks implements BukkitGetBloc 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 iterator() { - Iterable result = entities - .stream() - .map(input -> new BukkitEntity(input.getBukkitEntity())) - .collect(Collectors.toList()); - return result.iterator(); - } - }; + return new NativeEntityFunctionSet<>(entities, Entity::getUUID, e -> new BukkitEntity(e.getBukkitEntity())); } private void removeEntity(Entity entity) { diff --git a/worldedit-bukkit/adapters/adapter-1_20_5/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_20_R4/PaperweightGetBlocks.java b/worldedit-bukkit/adapters/adapter-1_20_5/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_20_R4/PaperweightGetBlocks.java index 453801190..42fec53a6 100644 --- a/worldedit-bukkit/adapters/adapter-1_20_5/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_20_R4/PaperweightGetBlocks.java +++ b/worldedit-bukkit/adapters/adapter-1_20_5/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_20_R4/PaperweightGetBlocks.java @@ -2,6 +2,7 @@ package com.sk89q.worldedit.bukkit.adapter.impl.fawe.v1_20_R4; 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; @@ -378,44 +379,11 @@ public class PaperweightGetBlocks extends CharGetBlocks implements BukkitGetBloc 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; - } - - @Nonnull - @Override - public Iterator iterator() { - Iterable 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(); - } - }; + 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)); + }); } @Override @@ -424,43 +392,7 @@ public class PaperweightGetBlocks extends CharGetBlocks implements BukkitGetBloc 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 iterator() { - Iterable result = entities - .stream() - .map(input -> new BukkitEntity(input.getBukkitEntity())) - .collect(Collectors.toList()); - return result.iterator(); - } - }; + return new NativeEntityFunctionSet<>(entities, Entity::getUUID, e -> new BukkitEntity(e.getBukkitEntity())); } private void removeEntity(Entity entity) { diff --git a/worldedit-bukkit/adapters/adapter-1_21/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_21_R1/PaperweightGetBlocks.java b/worldedit-bukkit/adapters/adapter-1_21/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_21_R1/PaperweightGetBlocks.java index 095b17b63..4bc91ea86 100644 --- a/worldedit-bukkit/adapters/adapter-1_21/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_21_R1/PaperweightGetBlocks.java +++ b/worldedit-bukkit/adapters/adapter-1_21/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_21_R1/PaperweightGetBlocks.java @@ -2,6 +2,7 @@ package com.sk89q.worldedit.bukkit.adapter.impl.fawe.v1_21_R1; 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; @@ -378,45 +379,11 @@ public class PaperweightGetBlocks extends CharGetBlocks implements BukkitGetBloc 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; - } - - @Nonnull - @Override - public Iterator iterator() { - Iterable result = entities.stream().map(input -> { - CompoundTag tag = new CompoundTag(); - input.save(tag); - return FaweCompoundTag.of((LinCompoundTag) adapter.toNativeLin(tag)); - })::iterator; - return result.iterator(); - } - }; - + 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)); + }); } @Override @@ -427,43 +394,7 @@ public class PaperweightGetBlocks extends CharGetBlocks implements BukkitGetBloc 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 iterator() { - Iterable result = entities - .stream() - .map(input -> new BukkitEntity(input.getBukkitEntity())) - .collect(Collectors.toList()); - return result.iterator(); - } - }; + return new NativeEntityFunctionSet<>(entities, Entity::getUUID, e -> new BukkitEntity(e.getBukkitEntity())); } private void removeEntity(Entity entity) { diff --git a/worldedit-bukkit/src/main/java/com/fastasyncworldedit/bukkit/adapter/NativeEntityFunctionSet.java b/worldedit-bukkit/src/main/java/com/fastasyncworldedit/bukkit/adapter/NativeEntityFunctionSet.java new file mode 100644 index 000000000..67196ccbb --- /dev/null +++ b/worldedit-bukkit/src/main/java/com/fastasyncworldedit/bukkit/adapter/NativeEntityFunctionSet.java @@ -0,0 +1,60 @@ +package com.fastasyncworldedit.bukkit.adapter; + +import javax.annotation.Nonnull; +import java.util.AbstractSet; +import java.util.Iterator; +import java.util.List; +import java.util.UUID; +import java.util.function.Function; + +public class NativeEntityFunctionSet extends AbstractSet { + + private final List nativeEntities; + private final Function uuidGetter; + private final Function resultFunction; + + public NativeEntityFunctionSet( + List nativeEntities, + Function uuidGetter, + Function resultFunction + ) { + this.nativeEntities = nativeEntities; + this.uuidGetter = uuidGetter; + this.resultFunction = resultFunction; + } + + @Override + public int size() { + return nativeEntities.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 (NativeEntity entity : nativeEntities) { + UUID uuid = uuidGetter.apply(entity); + if (uuid.equals(getUUID)) { + return true; + } + } + return false; + } + + @Nonnull + @Override + public Iterator iterator() { + return nativeEntities + .stream() + .map(resultFunction) + .iterator(); + } + +} diff --git a/worldedit-core/src/main/java/com/fastasyncworldedit/core/extent/OncePerChunkExtent.java b/worldedit-core/src/main/java/com/fastasyncworldedit/core/extent/OncePerChunkExtent.java index 5125ad9bc..4335d5948 100644 --- a/worldedit-core/src/main/java/com/fastasyncworldedit/core/extent/OncePerChunkExtent.java +++ b/worldedit-core/src/main/java/com/fastasyncworldedit/core/extent/OncePerChunkExtent.java @@ -32,7 +32,6 @@ public class OncePerChunkExtent extends AbstractDelegateExtent implements IBatch private final IQueueExtent queue; private Consumer task; private volatile long lastPair = Long.MAX_VALUE; - private volatile boolean isProcessing; /** * Create a new instance. @@ -59,16 +58,12 @@ public class OncePerChunkExtent extends AbstractDelegateExtent implements IBatch } lastPair = pair; synchronized (set) { - if (!set.contains(chunkX, chunkZ)) { - set.add(chunkX, chunkZ); - return true; - } + return set.add(chunkX, chunkZ); } - return false; } private void checkAndRun(int chunkX, int chunkZ) { - if (!isProcessing && shouldRun(chunkX, chunkZ)) { + if (shouldRun(chunkX, chunkZ)) { task.accept(queue.getCachedGet(chunkX, chunkZ)); } } @@ -88,7 +83,6 @@ public class OncePerChunkExtent extends AbstractDelegateExtent implements IBatch @Override public IChunkGet processGet(final IChunkGet get) { - isProcessing = true; if (shouldRun(get.getX(), get.getZ())) { task.accept(get); } diff --git a/worldedit-core/src/main/java/com/fastasyncworldedit/core/math/LocalBlockVector2Set.java b/worldedit-core/src/main/java/com/fastasyncworldedit/core/math/LocalBlockVector2Set.java index bfc80ee91..9ad9cf251 100644 --- a/worldedit-core/src/main/java/com/fastasyncworldedit/core/math/LocalBlockVector2Set.java +++ b/worldedit-core/src/main/java/com/fastasyncworldedit/core/math/LocalBlockVector2Set.java @@ -72,11 +72,11 @@ public class LocalBlockVector2Set implements Set { if (offsetX == Integer.MAX_VALUE) { return false; } - short sx = (short) (x - offsetX); - short sz = (short) (z - offsetZ); - if (sx > 32767 || sx < -32767 || sz > 32767 || sz < -32767) { + if (x > 32767 || x < -32769 || z > 32769 || z < -32767) { return false; } + short sx = (short) (x - offsetX); + short sz = (short) (z - offsetZ); try { return set.get(MathMan.pairSearchCoords(sx, sz)); } catch (IndexOutOfBoundsException e) {