From e648b35b95d95c69499c00355d1828cadd6f788c Mon Sep 17 00:00:00 2001 From: MattBDev <4009945+MattBDev@users.noreply.github.com> Date: Tue, 24 Mar 2020 12:15:24 -0400 Subject: [PATCH] Small code quality changes --- .../sk89q/worldedit/bukkit/BukkitAdapter.java | 25 +- .../bukkit/adapter/BukkitImplAdapter.java | 1 + .../bukkit/adapter/BukkitImplLoader.java | 2 +- .../java/com/boydti/fawe/beta/IChunk.java | 3 - .../java/com/boydti/fawe/beta/IChunkGet.java | 12 - .../java/com/boydti/fawe/beta/IChunkSet.java | 9 - .../implementation/queue/QueueHandler.java | 8 +- .../boydti/fawe/function/mask/AirMask.java | 9 +- .../boydti/fawe/function/mask/LiquidMask.java | 11 +- .../fawe/object/collection/BlockSet.java | 12 +- .../fawe/object/collection/MemBlockSet.java | 1 + .../fawe/object/collection/PrimitiveList.java | 338 ------------------ .../function/mask/BlockMaskBuilder.java | 2 +- 13 files changed, 36 insertions(+), 397 deletions(-) delete mode 100644 worldedit-core/src/main/java/com/boydti/fawe/object/collection/PrimitiveList.java diff --git a/worldedit-bukkit/src/main/java/com/sk89q/worldedit/bukkit/BukkitAdapter.java b/worldedit-bukkit/src/main/java/com/sk89q/worldedit/bukkit/BukkitAdapter.java index dc2a46c9e..705618139 100644 --- a/worldedit-bukkit/src/main/java/com/sk89q/worldedit/bukkit/BukkitAdapter.java +++ b/worldedit-bukkit/src/main/java/com/sk89q/worldedit/bukkit/BukkitAdapter.java @@ -41,6 +41,11 @@ import com.sk89q.worldedit.world.block.BlockType; import com.sk89q.worldedit.world.entity.EntityType; import com.sk89q.worldedit.world.gamemode.GameMode; import com.sk89q.worldedit.world.item.ItemType; +import it.unimi.dsi.fastutil.ints.Int2ObjectMap; +import java.util.EnumMap; +import java.util.HashMap; +import java.util.Map; +import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap; import org.bukkit.Material; import org.bukkit.block.Biome; import org.bukkit.block.data.BlockData; @@ -48,6 +53,7 @@ import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; import org.bukkit.inventory.ItemStack; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; /** * Adapts between Bukkit and WorldEdit equivalent objects. @@ -335,12 +341,16 @@ public enum BukkitAdapter { return getAdapter().adapt(entityType); } + private static EnumMap materialBlockTypeCache = new EnumMap<>(Material.class); + private static EnumMap materialItemTypeCache = new EnumMap<>(Material.class); + /** * Converts a Material to a BlockType * * @param material The material * @return The blocktype */ + @Nullable public static BlockType asBlockType(Material material) { return getAdapter().asBlockType(material); } @@ -351,12 +361,13 @@ public enum BukkitAdapter { * @param material The material * @return The itemtype */ + @Nullable public static ItemType asItemType(Material material) { return getAdapter().asItemType(material); } - /* - private static Map blockStateCache = new HashMap<>(); - /* + + private static Int2ObjectMap blockStateCache = new Int2ObjectOpenHashMap<>(); + private static Map blockStateStringCache = new HashMap<>(); /** * Create a WorldEdit BlockState from a Bukkit BlockData @@ -367,9 +378,9 @@ public enum BukkitAdapter { public static BlockState adapt(@NotNull BlockData blockData) { return getAdapter().adapt(blockData); } - /* - private static Map blockDataCache = new HashMap<>(); - */ + + private static Int2ObjectMap blockDataCache = new Int2ObjectOpenHashMap<>(); + /** * Create a Bukkit BlockData from a WorldEdit BlockStateHolder * @@ -409,4 +420,4 @@ public enum BukkitAdapter { public static ItemStack adapt(BaseItemStack item) { return getAdapter().adapt(item); } -} \ No newline at end of file +} diff --git a/worldedit-bukkit/src/main/java/com/sk89q/worldedit/bukkit/adapter/BukkitImplAdapter.java b/worldedit-bukkit/src/main/java/com/sk89q/worldedit/bukkit/adapter/BukkitImplAdapter.java index 8b4c0d35e..7eb54e8f9 100644 --- a/worldedit-bukkit/src/main/java/com/sk89q/worldedit/bukkit/adapter/BukkitImplAdapter.java +++ b/worldedit-bukkit/src/main/java/com/sk89q/worldedit/bukkit/adapter/BukkitImplAdapter.java @@ -187,6 +187,7 @@ public interface BukkitImplAdapter extends IBukkitAdapter { BaseItemStack adapt(ItemStack itemStack); default OptionalInt getInternalBlockStateId(BlockData data) { + // return OptionalInt.empty(); return getInternalBlockStateId(BukkitAdapter.adapt(data)); } diff --git a/worldedit-bukkit/src/main/java/com/sk89q/worldedit/bukkit/adapter/BukkitImplLoader.java b/worldedit-bukkit/src/main/java/com/sk89q/worldedit/bukkit/adapter/BukkitImplLoader.java index dd68d75df..172aaa2e7 100644 --- a/worldedit-bukkit/src/main/java/com/sk89q/worldedit/bukkit/adapter/BukkitImplLoader.java +++ b/worldedit-bukkit/src/main/java/com/sk89q/worldedit/bukkit/adapter/BukkitImplLoader.java @@ -77,7 +77,7 @@ public class BukkitImplLoader { } } - public void addClass(Class cls) { + public void addClass(Class cls) { adapterCandidates.add(0, cls.getName()); } diff --git a/worldedit-core/src/main/java/com/boydti/fawe/beta/IChunk.java b/worldedit-core/src/main/java/com/boydti/fawe/beta/IChunk.java index 1d5aac946..dcd4df154 100644 --- a/worldedit-core/src/main/java/com/boydti/fawe/beta/IChunk.java +++ b/worldedit-core/src/main/java/com/boydti/fawe/beta/IChunk.java @@ -63,9 +63,6 @@ public interface IChunk extends Trimable, IChunkGet, IChunkSet { // */ // void flood(Flood flood, FilterBlockMask mask, ChunkFilterBlock block); - @Override - CompoundTag getTile(int x, int y, int z); - @Override default IChunk reset() { return this; diff --git a/worldedit-core/src/main/java/com/boydti/fawe/beta/IChunkGet.java b/worldedit-core/src/main/java/com/boydti/fawe/beta/IChunkGet.java index eb8917a84..9061dfdf1 100644 --- a/worldedit-core/src/main/java/com/boydti/fawe/beta/IChunkGet.java +++ b/worldedit-core/src/main/java/com/boydti/fawe/beta/IChunkGet.java @@ -26,23 +26,11 @@ public interface IChunkGet extends IBlocks, Trimable, InputExtent, ITileInput { @Override BlockState getBlock(int x, int y, int z); - @Override - Map getTiles(); - - @Override - Set getEntities(); - - @Override - boolean trim(boolean aggressive); - default void optimize() { } > T call(IChunkSet set, Runnable finalize); - @Override - char[] load(int layer); - CompoundTag getEntity(UUID uuid); } diff --git a/worldedit-core/src/main/java/com/boydti/fawe/beta/IChunkSet.java b/worldedit-core/src/main/java/com/boydti/fawe/beta/IChunkSet.java index 81f8f3abc..65b24872c 100644 --- a/worldedit-core/src/main/java/com/boydti/fawe/beta/IChunkSet.java +++ b/worldedit-core/src/main/java/com/boydti/fawe/beta/IChunkSet.java @@ -42,15 +42,6 @@ public interface IChunkSet extends IBlocks, OutputExtent { return getBiomes() != null; } - @Override - BiomeType getBiomeType(int x, int y, int z); - - @Override - Map getTiles(); - - @Override - Set getEntities(); - @Override IChunkSet reset(); diff --git a/worldedit-core/src/main/java/com/boydti/fawe/beta/implementation/queue/QueueHandler.java b/worldedit-core/src/main/java/com/boydti/fawe/beta/implementation/queue/QueueHandler.java index 343d4b484..fc9ef1af8 100644 --- a/worldedit-core/src/main/java/com/boydti/fawe/beta/implementation/queue/QueueHandler.java +++ b/worldedit-core/src/main/java/com/boydti/fawe/beta/implementation/queue/QueueHandler.java @@ -41,11 +41,11 @@ public abstract class QueueHandler implements Trimable, Runnable { private ForkJoinPool forkJoinPoolPrimary = new ForkJoinPool(); private ForkJoinPool forkJoinPoolSecondary = new ForkJoinPool(); private ThreadPoolExecutor blockingExecutor = FaweCache.IMP.newBlockingExecutor(); - private ConcurrentLinkedQueue syncTasks = new ConcurrentLinkedQueue<>(); - private ConcurrentLinkedQueue syncWhenFree = new ConcurrentLinkedQueue<>(); + private final ConcurrentLinkedQueue syncTasks = new ConcurrentLinkedQueue<>(); + private final ConcurrentLinkedQueue syncWhenFree = new ConcurrentLinkedQueue<>(); - private Map>> chunkGetCache = new HashMap<>(); - private CleanableThreadLocal> queuePool = new CleanableThreadLocal<>(QueueHandler.this::create); + private final Map>> chunkGetCache = new HashMap<>(); + private final CleanableThreadLocal> queuePool = new CleanableThreadLocal<>(QueueHandler.this::create); /** * Used to calculate elapsed time in milliseconds and ensure block placement doesn't lag the * server diff --git a/worldedit-core/src/main/java/com/boydti/fawe/function/mask/AirMask.java b/worldedit-core/src/main/java/com/boydti/fawe/function/mask/AirMask.java index 311d46571..75099c30f 100644 --- a/worldedit-core/src/main/java/com/boydti/fawe/function/mask/AirMask.java +++ b/worldedit-core/src/main/java/com/boydti/fawe/function/mask/AirMask.java @@ -21,19 +21,12 @@ package com.boydti.fawe.function.mask; import com.sk89q.worldedit.extent.Extent; import com.sk89q.worldedit.function.mask.BlockMask; -import com.sk89q.worldedit.function.mask.Mask2D; -import javax.annotation.Nullable; public class AirMask extends BlockMask { + public AirMask(Extent extent) { super(extent); add(state -> state.getMaterial().isAir()); } - @Nullable - @Override - public Mask2D toMask2D() { - return null; - } - } diff --git a/worldedit-core/src/main/java/com/boydti/fawe/function/mask/LiquidMask.java b/worldedit-core/src/main/java/com/boydti/fawe/function/mask/LiquidMask.java index 7622aae8e..0eb2f6def 100644 --- a/worldedit-core/src/main/java/com/boydti/fawe/function/mask/LiquidMask.java +++ b/worldedit-core/src/main/java/com/boydti/fawe/function/mask/LiquidMask.java @@ -21,21 +21,12 @@ package com.boydti.fawe.function.mask; import com.sk89q.worldedit.extent.Extent; import com.sk89q.worldedit.function.mask.BlockMask; -import com.sk89q.worldedit.function.mask.Mask2D; -import com.sk89q.worldedit.math.BlockVector3; - -import javax.annotation.Nullable; public class LiquidMask extends BlockMask { + public LiquidMask(Extent extent) { super(extent); add(state -> state.getMaterial().isLiquid()); } - @Nullable - @Override - public Mask2D toMask2D() { - return null; - } - } diff --git a/worldedit-core/src/main/java/com/boydti/fawe/object/collection/BlockSet.java b/worldedit-core/src/main/java/com/boydti/fawe/object/collection/BlockSet.java index 0edf2c6bd..42c3e1588 100644 --- a/worldedit-core/src/main/java/com/boydti/fawe/object/collection/BlockSet.java +++ b/worldedit-core/src/main/java/com/boydti/fawe/object/collection/BlockSet.java @@ -8,6 +8,7 @@ import java.util.Iterator; import java.util.Set; public abstract class BlockSet extends AbstractRegion { + private final int chunkOffsetX; private final int chunkOffsetZ; private final int blockOffsetX; @@ -88,20 +89,23 @@ public abstract class BlockSet extends AbstractRegion { @Override public abstract boolean contains(int x, int y, int z); + public abstract boolean add(int x, int y, int z); + public abstract void set(int x, int y, int z); + public abstract void clear(int x, int y, int z); + public abstract boolean remove(int x, int y, int z); + @Override public abstract Iterator iterator(); + @Override public abstract Set getChunks(); + @Override public abstract Set getChunkCubes(); - @Override - public abstract BlockVector3 getMaximumPoint(); - @Override - public abstract BlockVector3 getMinimumPoint(); @Override public void expand(BlockVector3... changes) throws RegionOperationException { diff --git a/worldedit-core/src/main/java/com/boydti/fawe/object/collection/MemBlockSet.java b/worldedit-core/src/main/java/com/boydti/fawe/object/collection/MemBlockSet.java index 332643e57..dddf6d5d4 100644 --- a/worldedit-core/src/main/java/com/boydti/fawe/object/collection/MemBlockSet.java +++ b/worldedit-core/src/main/java/com/boydti/fawe/object/collection/MemBlockSet.java @@ -77,6 +77,7 @@ public final class MemBlockSet extends BlockSet { return BlockVector3.at(getMinX(), getMinimumY(), getMinZ()); } + @Override public BlockVector3 getMaximumPoint() { return BlockVector3.at(getMaxX(), getMaximumY(), getMaxZ()); } diff --git a/worldedit-core/src/main/java/com/boydti/fawe/object/collection/PrimitiveList.java b/worldedit-core/src/main/java/com/boydti/fawe/object/collection/PrimitiveList.java deleted file mode 100644 index 84b2ccd5e..000000000 --- a/worldedit-core/src/main/java/com/boydti/fawe/object/collection/PrimitiveList.java +++ /dev/null @@ -1,338 +0,0 @@ -package com.boydti.fawe.object.collection; - -import java.lang.reflect.Array; -import java.util.AbstractList; - -public class PrimitiveList extends AbstractList { - private final Class primitive; - private final Type type; - private int length; - private int totalLength; - private Object arr; - - private enum Type { - Byte, - Boolean, - Short, - Character, - Integer, - Float, - Long, - Double - } - - public PrimitiveList(Class type) { - try { - Class boxed; - if (type.isPrimitive()) { - this.primitive = type; - boxed = (Class) Array.get(Array.newInstance(primitive, 1), 0).getClass(); - } else { - this.primitive = (Class) type.getField("TYPE").get(null); - boxed = type; - } - this.type = Type.valueOf(boxed.getSimpleName()); - } catch (Throwable e) { - throw new RuntimeException(e); - } - length = 0; - totalLength = 0; - arr = Array.newInstance(primitive, 0); - } - - public PrimitiveList(T[] arr) { - try { - Class boxed = (Class) arr.getClass().getComponentType(); - this.primitive = (Class) boxed.getField("TYPE").get(null); - this.type = Type.valueOf(boxed.getSimpleName()); - } catch (Throwable e) { - throw new RuntimeException(e); - } - this.arr = Array.newInstance(primitive, arr.length); - for (int i = 0; i < arr.length; i++) { - T val = arr[i]; - if (val != null) setFast(i, val); - } - this.length = arr.length; - this.totalLength = length; - } - - public PrimitiveList(Object arr) { - if (!arr.getClass().isArray()) { - throw new IllegalArgumentException("Argument must be an array!"); - } - this.primitive = arr.getClass().getComponentType(); - Class boxed = (Class) Array.get(Array.newInstance(primitive, 1), 0).getClass(); - this.type = Type.valueOf(boxed.getSimpleName()); - this.arr = arr; - this.length = Array.getLength(arr); - this.totalLength = length; - } - - public Object getArray() { - return arr; - } - - @Override - public T get(int index) { - return (T) getFast(index); - } - - public byte getByte(int index) { - return type == Type.Double ? ((byte[]) arr)[index] : (byte) getFast(index); - } - - public boolean getBoolean(int index) { - return type == Type.Boolean ? ((boolean[]) arr)[index] : (boolean) getFast(index); - } - - public short getShort(int index) { - return type == Type.Short ? ((short[]) arr)[index] : (short) getFast(index); - } - - public char getCharacter(int index) { - return type == Type.Character ? ((char[]) arr)[index] : (char) getFast(index); - } - - public int getInt(int index) { - return type == Type.Integer ? ((int[]) arr)[index] : (int) getFast(index); - } - - public float getFloat(int index) { - return type == Type.Float ? ((float[]) arr)[index] : (float) getFast(index); - } - - public long getLong(int index) { - return type == Type.Long ? ((long[]) arr)[index] : (long) getFast(index); - } - - public double getDouble(int index) { - return type == Type.Double ? ((double[]) arr)[index] : (double) getFast(index); - } - - private final Object getFast(int index) { - switch (type) { - case Byte: - return ((byte[]) arr)[index]; - case Boolean: - return ((boolean[]) arr)[index]; - case Short: - return ((short[]) arr)[index]; - case Character: - return ((char[]) arr)[index]; - case Integer: - return ((int[]) arr)[index]; - case Float: - return ((float[]) arr)[index]; - case Long: - return ((long[]) arr)[index]; - case Double: - return ((double[]) arr)[index]; - } - return null; - } - - @Override - public T set(int index, T element) { - T value = get(index); - setFast(index, element); - return value; - } - - public void set(int index, char value) { - switch (type) { - case Character: - ((char[]) arr)[index] = value; - return; - default: - setFast(index, value); - return; - } - } - - public void set(int index, byte value) { - switch (type) { - case Byte: - ((byte[]) arr)[index] = value; - return; - default: - setFast(index, value); - return; - } - } - - public void set(int index, int value) { - switch (type) { - case Integer: - ((int[]) arr)[index] = value; - return; - case Long: - ((long[]) arr)[index] = (long) value; - return; - case Double: - ((double[]) arr)[index] = (double) value; - return; - default: - setFast(index, value); - return; - } - } - - public void set(int index, long value) { - switch (type) { - case Integer: - ((int[]) arr)[index] = (int) value; - return; - case Long: - ((long[]) arr)[index] = value; - return; - case Double: - ((double[]) arr)[index] = (double) value; - return; - default: - setFast(index, value); - return; - } - } - - public void set(int index, double value) { - switch (type) { - case Float: - ((float[]) arr)[index] = (float) value; - return; - case Long: - ((long[]) arr)[index] = (long) value; - return; - case Double: - ((double[]) arr)[index] = value; - return; - default: - setFast(index, value); - return; - } - } - - public final void setFast(int index, Object element) { - switch (type) { - case Byte: - ((byte[]) arr)[index] = (byte) element; - return; - case Boolean: - ((boolean[]) arr)[index] = (boolean) element; - return; - case Short: - ((short[]) arr)[index] = (short) element; - return; - case Character: - ((char[]) arr)[index] = (char) element; - return; - case Integer: - ((int[]) arr)[index] = (int) element; - return; - case Float: - ((float[]) arr)[index] = (float) element; - return; - case Long: - ((long[]) arr)[index] = (long) element; - return; - case Double: - ((double[]) arr)[index] = (double) element; - return; - } - } - - - @Override - public void add(int index, T element) { - if (index == length) { - if (totalLength == length) { - Object tmp = arr; - totalLength = (length << 1) + 16; - arr = Array.newInstance(primitive, totalLength); - System.arraycopy(tmp, 0, arr, 0, length); - } - setFast(length, element); - length++; - } else { - if (totalLength == length) { - Object tmp = arr; - totalLength = (length << 1) + 16; - arr = Array.newInstance(primitive, totalLength); - System.arraycopy(tmp, 0, arr, 0, index); - } - System.arraycopy(arr, index, arr, index + 1, length - index); - set(index, element); - length++; - } - } - - private void ensureAddCapacity() { - if (totalLength == length) { - Object tmp = arr; - totalLength = (length << 1) + 16; - arr = Array.newInstance(primitive, totalLength); - System.arraycopy(tmp, 0, arr, 0, length); - } - } - - @Override - public boolean add(T element) { - ensureAddCapacity(); - setFast(length++, element); - return true; - } - - public boolean add(int element) { - ensureAddCapacity(); - set(length++, element); - return true; - } - - public boolean add(long element) { - ensureAddCapacity(); - set(length++, element); - return true; - } - - public boolean add(double element) { - ensureAddCapacity(); - set(length++, element); - return true; - } - - public boolean add(byte element) { - ensureAddCapacity(); - set(length++, element); - return true; - } - - public boolean add(char element) { - ensureAddCapacity(); - set(length++, element); - return true; - } - - @Override - public T remove(int index) { - if (index < 0 || index > length) throw new IndexOutOfBoundsException(index + " not in [0, " + length + "]"); - T value = get(index); - if (index != length) { - System.arraycopy(arr, index + 1, arr, index, length - index - 1); - } - length--; - return value; - } - - @Override - public int size() { - return length; - } - - @Override - public void clear() { - if (length != 0) { - this.arr = Array.newInstance(primitive, 0); - } - length = 0; - } -} diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/function/mask/BlockMaskBuilder.java b/worldedit-core/src/main/java/com/sk89q/worldedit/function/mask/BlockMaskBuilder.java index f1eb58e40..76bc67c8a 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/function/mask/BlockMaskBuilder.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/function/mask/BlockMaskBuilder.java @@ -230,7 +230,7 @@ public class BlockMaskBuilder { private void suggest(String input, String property, Collection finalTypes) throws InputParseException { throw new SuggestInputParseException(input + " does not have: " + property, input, () -> { - Set keys = new HashSet<>(); + Set keys = EnumSet.noneOf(PropertyKey.class); finalTypes.forEach(t -> t.getProperties().forEach(p -> keys.add(p.getKey()))); return keys.stream().map(PropertyKey::getId) .filter(p -> StringMan.blockStateMatches(property, p))