Small code quality changes

This commit is contained in:
MattBDev 2020-03-24 12:15:24 -04:00
parent 1577ef61d4
commit e648b35b95
13 changed files with 36 additions and 397 deletions

View File

@ -41,6 +41,11 @@ import com.sk89q.worldedit.world.block.BlockType;
import com.sk89q.worldedit.world.entity.EntityType; import com.sk89q.worldedit.world.entity.EntityType;
import com.sk89q.worldedit.world.gamemode.GameMode; import com.sk89q.worldedit.world.gamemode.GameMode;
import com.sk89q.worldedit.world.item.ItemType; 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.Material;
import org.bukkit.block.Biome; import org.bukkit.block.Biome;
import org.bukkit.block.data.BlockData; import org.bukkit.block.data.BlockData;
@ -48,6 +53,7 @@ import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
/** /**
* Adapts between Bukkit and WorldEdit equivalent objects. * Adapts between Bukkit and WorldEdit equivalent objects.
@ -335,12 +341,16 @@ public enum BukkitAdapter {
return getAdapter().adapt(entityType); return getAdapter().adapt(entityType);
} }
private static EnumMap<Material, BlockType> materialBlockTypeCache = new EnumMap<>(Material.class);
private static EnumMap<Material, ItemType> materialItemTypeCache = new EnumMap<>(Material.class);
/** /**
* Converts a Material to a BlockType * Converts a Material to a BlockType
* *
* @param material The material * @param material The material
* @return The blocktype * @return The blocktype
*/ */
@Nullable
public static BlockType asBlockType(Material material) { public static BlockType asBlockType(Material material) {
return getAdapter().asBlockType(material); return getAdapter().asBlockType(material);
} }
@ -351,12 +361,13 @@ public enum BukkitAdapter {
* @param material The material * @param material The material
* @return The itemtype * @return The itemtype
*/ */
@Nullable
public static ItemType asItemType(Material material) { public static ItemType asItemType(Material material) {
return getAdapter().asItemType(material); return getAdapter().asItemType(material);
} }
/*
private static Map<String, BlockState> blockStateCache = new HashMap<>(); private static Int2ObjectMap<BlockState> blockStateCache = new Int2ObjectOpenHashMap<>();
/* private static Map<String, BlockState> blockStateStringCache = new HashMap<>();
/** /**
* Create a WorldEdit BlockState from a Bukkit BlockData * Create a WorldEdit BlockState from a Bukkit BlockData
@ -367,9 +378,9 @@ public enum BukkitAdapter {
public static BlockState adapt(@NotNull BlockData blockData) { public static BlockState adapt(@NotNull BlockData blockData) {
return getAdapter().adapt(blockData); return getAdapter().adapt(blockData);
} }
/*
private static Map<String, BlockData> blockDataCache = new HashMap<>(); private static Int2ObjectMap<BlockData> blockDataCache = new Int2ObjectOpenHashMap<>();
*/
/** /**
* Create a Bukkit BlockData from a WorldEdit BlockStateHolder * Create a Bukkit BlockData from a WorldEdit BlockStateHolder
* *
@ -409,4 +420,4 @@ public enum BukkitAdapter {
public static ItemStack adapt(BaseItemStack item) { public static ItemStack adapt(BaseItemStack item) {
return getAdapter().adapt(item); return getAdapter().adapt(item);
} }
} }

View File

@ -187,6 +187,7 @@ public interface BukkitImplAdapter<T> extends IBukkitAdapter {
BaseItemStack adapt(ItemStack itemStack); BaseItemStack adapt(ItemStack itemStack);
default OptionalInt getInternalBlockStateId(BlockData data) { default OptionalInt getInternalBlockStateId(BlockData data) {
// return OptionalInt.empty();
return getInternalBlockStateId(BukkitAdapter.adapt(data)); return getInternalBlockStateId(BukkitAdapter.adapt(data));
} }

View File

@ -77,7 +77,7 @@ public class BukkitImplLoader {
} }
} }
public void addClass(Class cls) { public void addClass(Class<?> cls) {
adapterCandidates.add(0, cls.getName()); adapterCandidates.add(0, cls.getName());
} }

View File

@ -63,9 +63,6 @@ public interface IChunk extends Trimable, IChunkGet, IChunkSet {
// */ // */
// void flood(Flood flood, FilterBlockMask mask, ChunkFilterBlock block); // void flood(Flood flood, FilterBlockMask mask, ChunkFilterBlock block);
@Override
CompoundTag getTile(int x, int y, int z);
@Override @Override
default IChunk reset() { default IChunk reset() {
return this; return this;

View File

@ -26,23 +26,11 @@ public interface IChunkGet extends IBlocks, Trimable, InputExtent, ITileInput {
@Override @Override
BlockState getBlock(int x, int y, int z); BlockState getBlock(int x, int y, int z);
@Override
Map<BlockVector3, CompoundTag> getTiles();
@Override
Set<CompoundTag> getEntities();
@Override
boolean trim(boolean aggressive);
default void optimize() { default void optimize() {
} }
<T extends Future<T>> T call(IChunkSet set, Runnable finalize); <T extends Future<T>> T call(IChunkSet set, Runnable finalize);
@Override
char[] load(int layer);
CompoundTag getEntity(UUID uuid); CompoundTag getEntity(UUID uuid);
} }

View File

@ -42,15 +42,6 @@ public interface IChunkSet extends IBlocks, OutputExtent {
return getBiomes() != null; return getBiomes() != null;
} }
@Override
BiomeType getBiomeType(int x, int y, int z);
@Override
Map<BlockVector3, CompoundTag> getTiles();
@Override
Set<CompoundTag> getEntities();
@Override @Override
IChunkSet reset(); IChunkSet reset();

View File

@ -41,11 +41,11 @@ public abstract class QueueHandler implements Trimable, Runnable {
private ForkJoinPool forkJoinPoolPrimary = new ForkJoinPool(); private ForkJoinPool forkJoinPoolPrimary = new ForkJoinPool();
private ForkJoinPool forkJoinPoolSecondary = new ForkJoinPool(); private ForkJoinPool forkJoinPoolSecondary = new ForkJoinPool();
private ThreadPoolExecutor blockingExecutor = FaweCache.IMP.newBlockingExecutor(); private ThreadPoolExecutor blockingExecutor = FaweCache.IMP.newBlockingExecutor();
private ConcurrentLinkedQueue<FutureTask> syncTasks = new ConcurrentLinkedQueue<>(); private final ConcurrentLinkedQueue<FutureTask> syncTasks = new ConcurrentLinkedQueue<>();
private ConcurrentLinkedQueue<FutureTask> syncWhenFree = new ConcurrentLinkedQueue<>(); private final ConcurrentLinkedQueue<FutureTask> syncWhenFree = new ConcurrentLinkedQueue<>();
private Map<World, WeakReference<IChunkCache<IChunkGet>>> chunkGetCache = new HashMap<>(); private final Map<World, WeakReference<IChunkCache<IChunkGet>>> chunkGetCache = new HashMap<>();
private CleanableThreadLocal<IQueueExtent<IQueueChunk>> queuePool = new CleanableThreadLocal<>(QueueHandler.this::create); private final CleanableThreadLocal<IQueueExtent<IQueueChunk>> queuePool = new CleanableThreadLocal<>(QueueHandler.this::create);
/** /**
* Used to calculate elapsed time in milliseconds and ensure block placement doesn't lag the * Used to calculate elapsed time in milliseconds and ensure block placement doesn't lag the
* server * server

View File

@ -21,19 +21,12 @@ package com.boydti.fawe.function.mask;
import com.sk89q.worldedit.extent.Extent; import com.sk89q.worldedit.extent.Extent;
import com.sk89q.worldedit.function.mask.BlockMask; import com.sk89q.worldedit.function.mask.BlockMask;
import com.sk89q.worldedit.function.mask.Mask2D;
import javax.annotation.Nullable;
public class AirMask extends BlockMask { public class AirMask extends BlockMask {
public AirMask(Extent extent) { public AirMask(Extent extent) {
super(extent); super(extent);
add(state -> state.getMaterial().isAir()); add(state -> state.getMaterial().isAir());
} }
@Nullable
@Override
public Mask2D toMask2D() {
return null;
}
} }

View File

@ -21,21 +21,12 @@ package com.boydti.fawe.function.mask;
import com.sk89q.worldedit.extent.Extent; import com.sk89q.worldedit.extent.Extent;
import com.sk89q.worldedit.function.mask.BlockMask; 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 class LiquidMask extends BlockMask {
public LiquidMask(Extent extent) { public LiquidMask(Extent extent) {
super(extent); super(extent);
add(state -> state.getMaterial().isLiquid()); add(state -> state.getMaterial().isLiquid());
} }
@Nullable
@Override
public Mask2D toMask2D() {
return null;
}
} }

View File

@ -8,6 +8,7 @@ import java.util.Iterator;
import java.util.Set; import java.util.Set;
public abstract class BlockSet extends AbstractRegion { public abstract class BlockSet extends AbstractRegion {
private final int chunkOffsetX; private final int chunkOffsetX;
private final int chunkOffsetZ; private final int chunkOffsetZ;
private final int blockOffsetX; private final int blockOffsetX;
@ -88,20 +89,23 @@ public abstract class BlockSet extends AbstractRegion {
@Override @Override
public abstract boolean contains(int x, int y, int z); public abstract boolean contains(int x, int y, int z);
public abstract boolean add(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 set(int x, int y, int z);
public abstract void clear(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); public abstract boolean remove(int x, int y, int z);
@Override @Override
public abstract Iterator<BlockVector3> iterator(); public abstract Iterator<BlockVector3> iterator();
@Override @Override
public abstract Set<BlockVector2> getChunks(); public abstract Set<BlockVector2> getChunks();
@Override @Override
public abstract Set<BlockVector3> getChunkCubes(); public abstract Set<BlockVector3> getChunkCubes();
@Override
public abstract BlockVector3 getMaximumPoint();
@Override
public abstract BlockVector3 getMinimumPoint();
@Override @Override
public void expand(BlockVector3... changes) throws RegionOperationException { public void expand(BlockVector3... changes) throws RegionOperationException {

View File

@ -77,6 +77,7 @@ public final class MemBlockSet extends BlockSet {
return BlockVector3.at(getMinX(), getMinimumY(), getMinZ()); return BlockVector3.at(getMinX(), getMinimumY(), getMinZ());
} }
@Override
public BlockVector3 getMaximumPoint() { public BlockVector3 getMaximumPoint() {
return BlockVector3.at(getMaxX(), getMaximumY(), getMaxZ()); return BlockVector3.at(getMaxX(), getMaximumY(), getMaxZ());
} }

View File

@ -1,338 +0,0 @@
package com.boydti.fawe.object.collection;
import java.lang.reflect.Array;
import java.util.AbstractList;
public class PrimitiveList<T> extends AbstractList<T> {
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<T> type) {
try {
Class<T> boxed;
if (type.isPrimitive()) {
this.primitive = type;
boxed = (Class<T>) 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<T> boxed = (Class<T>) 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<T> boxed = (Class<T>) 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;
}
}

View File

@ -230,7 +230,7 @@ public class BlockMaskBuilder {
private void suggest(String input, String property, Collection<BlockType> finalTypes) throws InputParseException { private void suggest(String input, String property, Collection<BlockType> finalTypes) throws InputParseException {
throw new SuggestInputParseException(input + " does not have: " + property, input, () -> { throw new SuggestInputParseException(input + " does not have: " + property, input, () -> {
Set<PropertyKey> keys = new HashSet<>(); Set<PropertyKey> keys = EnumSet.noneOf(PropertyKey.class);
finalTypes.forEach(t -> t.getProperties().forEach(p -> keys.add(p.getKey()))); finalTypes.forEach(t -> t.getProperties().forEach(p -> keys.add(p.getKey())));
return keys.stream().map(PropertyKey::getId) return keys.stream().map(PropertyKey::getId)
.filter(p -> StringMan.blockStateMatches(property, p)) .filter(p -> StringMan.blockStateMatches(property, p))