more structure

This commit is contained in:
Jesse Boyd
2019-04-29 01:44:59 +10:00
parent 6adf0e6435
commit 57b5be84f4
45 changed files with 1317 additions and 870 deletions

View File

@ -31,22 +31,22 @@ import static com.google.common.base.Preconditions.checkNotNull;
/**
* [ WorldEdit action]
* |
* |
* \|/
* [ EditSession ] - The change is processed (area restrictions, change limit, block type)
* |
* |
* \|/
* [Block change] - A block change from some location
* |
* |
* \|/
* [ Set Queue ] - The SetQueue manages the implementation specific queue
* |
* |
* \|/
* [ Fawe Queue] - A queue of chunks - check if the queue has the chunk for a change
* |
* |
* \|/
* [ Fawe Chunk Implementation ] - Otherwise create a new FaweChunk object which is a wrapper around the Chunk object
* |
* |
* \|/
* [ Execution ] - When done, the queue then sets the blocks for the chunk, performs lighting updates and sends the chunk packet to the clients
* <p>

View File

@ -1,5 +1,6 @@
package com.boydti.fawe;
import com.boydti.fawe.beta.Trimable;
import com.boydti.fawe.jnbt.anvil.BitArray4096;
import com.boydti.fawe.object.collection.IterableThreadLocal;
import com.boydti.fawe.util.MathMan;
@ -11,29 +12,21 @@ import com.sk89q.worldedit.world.block.BlockTypes;
import java.lang.reflect.Field;
import java.util.*;
public class FaweCache {
public static final IterableThreadLocal<char[]> BLOCK_TO_PALETTE_CHAR = new IterableThreadLocal<char[]>() {
@Override
public char[] init() {
char[] result = new char[BlockTypes.states.length];
Arrays.fill(result, Character.MAX_VALUE);
return result;
}
};
public class FaweCache implements Trimable {
public static final IterableThreadLocal<char[]> PALETTE_TO_BLOCK_CHAR = new IterableThreadLocal<char[]>() {
@Override
public char[] init() {
return new char[Character.MAX_VALUE];
}
};
/*
Palette buffers / cache
*/
public static final IterableThreadLocal<char[]> SECTION_BLOCKS_CHAR = new IterableThreadLocal<char[]>() {
@Override
public char[] init() {
return new char[4096];
}
};
@Override
public boolean trim(boolean aggressive) {
BLOCK_TO_PALETTE.clean();
PALETTE_TO_BLOCK.clean();
BLOCK_STATES.clean();
SECTION_BLOCKS.clean();
PALETTE_CACHE.clean();
return false;
}
public static final IterableThreadLocal<int[]> BLOCK_TO_PALETTE = new IterableThreadLocal<int[]>() {
@Override
@ -75,6 +68,121 @@ public class FaweCache {
return map;
}
private static final class Palette {
public int paletteToBlockLength;
/**
* Reusable buffer array, MUST check paletteToBlockLength for actual length
*/
public int[] paletteToBlock;
public int blockstatesLength;
/**
* Reusable buffer array, MUST check blockstatesLength for actual length
*/
public long[] blockstates;
}
private static final IterableThreadLocal<Palette> PALETTE_CACHE = new IterableThreadLocal<Palette>() {
@Override
public Palette init() {
return new Palette();
}
};
/**
* Convert raw char array to palette
* @param layer
* @param blocks
* @return palette
*/
public static Palette toPalette(int layer, char[] blocks) {
return toPalette(layer, null, blocks);
}
/**
* Convert raw int array to palette
* @param layer
* @param blocks
* @return palette
*/
public static Palette toPalette(int layer, int[] blocks) {
return toPalette(layer, blocks, null);
}
private static Palette toPalette(int layer, int[] blocksInts, char[] blocksChars) {
int[] blockToPalette = BLOCK_TO_PALETTE.get();
int[] paletteToBlock = PALETTE_TO_BLOCK.get();
long[] blockstates = BLOCK_STATES.get();
int[] blocksCopy = SECTION_BLOCKS.get();
int blockIndexStart = layer << 12;
int blockIndexEnd = blockIndexStart + 4096;
int num_palette = 0;
try {
if (blocksChars != null) {
for (int i = blockIndexStart, j = 0; i < blockIndexEnd; i++, j++) {
int ordinal = blocksChars[i];
int palette = blockToPalette[ordinal];
if (palette == Integer.MAX_VALUE) {
BlockState state = BlockTypes.states[ordinal];
blockToPalette[ordinal] = palette = num_palette;
paletteToBlock[num_palette] = ordinal;
num_palette++;
}
blocksCopy[j] = palette;
}
} else if (blocksInts != null) {
for (int i = blockIndexStart, j = 0; i < blockIndexEnd; i++, j++) {
int ordinal = blocksInts[i];
int palette = blockToPalette[ordinal];
if (palette == Integer.MAX_VALUE) {
BlockState state = BlockTypes.states[ordinal];
blockToPalette[ordinal] = palette = num_palette;
paletteToBlock[num_palette] = ordinal;
num_palette++;
}
blocksCopy[j] = palette;
}
} else {
throw new IllegalArgumentException();
}
for (int i = 0; i < num_palette; i++) {
blockToPalette[paletteToBlock[i]] = Integer.MAX_VALUE;
}
// BlockStates
int bitsPerEntry = MathMan.log2nlz(num_palette - 1);
int blockBitArrayEnd = (bitsPerEntry * 4096) >> 6;
if (num_palette == 1) {
// Set a value, because minecraft needs it for some reason
blockstates[0] = 0;
blockBitArrayEnd = 1;
} else {
BitArray4096 bitArray = new BitArray4096(blockstates, bitsPerEntry);
bitArray.fromRaw(blocksCopy);
}
// Construct palette
Palette palette = PALETTE_CACHE.get();
palette.paletteToBlockLength = num_palette;
palette.paletteToBlock = paletteToBlock;
palette.blockstatesLength = blockBitArrayEnd;
palette.blockstates = blockstates;
return palette;
} catch (Throwable e) {
Arrays.fill(blockToPalette, Integer.MAX_VALUE);
e.printStackTrace();
throw e;
}
}
/*
Conversion methods between JNBT tags and raw values
*/
public static ShortTag asTag(short value) {
return new ShortTag(value);
}
@ -205,56 +313,4 @@ public class FaweCache {
if (clazz == null) clazz = EndTag.class;
return new ListTag(clazz, list);
}
private static final class Palette {
}
public void toPalette(int layer, char[] blocks) {
int[] blockToPalette = FaweCache.BLOCK_TO_PALETTE.get();
int[] paletteToBlock = FaweCache.PALETTE_TO_BLOCK.get();
long[] blockstates = FaweCache.BLOCK_STATES.get();
int[] blocksCopy = FaweCache.SECTION_BLOCKS.get();
int blockIndexStart = layer << 12;
int blockIndexEnd = blockIndexStart + 4096;
int num_palette = 0;
try {
for (int i = blockIndexStart, j = 0; i < blockIndexEnd; i++, j++) {
int ordinal = blocks[i];
int palette = blockToPalette[ordinal];
if (palette == Integer.MAX_VALUE) {
BlockState state = BlockTypes.states[ordinal];
blockToPalette[ordinal] = palette = num_palette;
paletteToBlock[num_palette] = ordinal;
num_palette++;
}
blocksCopy[j] = palette;
}
for (int i = 0; i < num_palette; i++) {
blockToPalette[paletteToBlock[i]] = Integer.MAX_VALUE;
}
// BlockStates
int bitsPerEntry = MathMan.log2nlz(num_palette - 1);
int blockBitArrayEnd = (bitsPerEntry * 4096) >> 6;
if (num_palette == 1) {
// Set a value, because minecraft needs it for some reason
blockstates[0] = 0;
blockBitArrayEnd = 1;
} else {
BitArray4096 bitArray = new BitArray4096(blockstates, bitsPerEntry);
bitArray.fromRaw(blocksCopy);
}
// num_palette
// paletteToBlock
// blockstates (range: blockBitArrayEnd)
} catch (Throwable e) {
Arrays.fill(blockToPalette, Integer.MAX_VALUE);
e.printStackTrace();
throw e;
}
}
}

View File

@ -0,0 +1,50 @@
package com.boydti.fawe.beta;
import com.sk89q.worldedit.world.block.BaseBlock;
public interface Filter {
/**
* Check whether a chunk should be read
*
* @param cx
* @param cz
* @return
*/
default boolean appliesChunk(final int cx, final int cz) {
return true;
}
/**
* Do something with the IChunk<br>
* - Return null if you don't want to filter blocks<br>
* - Return the chunk if you do want to filter blocks<br>
*
* @param chunk
* @return
*/
default IChunk applyChunk(final IChunk chunk) {
return chunk;
}
/**
* Make changes to the block here<br>
* - e.g. block.setId(...)<br>
* - Note: Performance is critical here<br>
*
* @param x
* @param y
* @param z
* @param block
*/
default void applyBlock(final int x, final int y, final int z, final BaseBlock block) {
}
/**
* Do something with the IChunk after block filtering<br>
*
* @param chunk
* @return
*/
default void finishChunk(final IChunk chunk) {
}
}

View File

@ -0,0 +1,5 @@
package com.boydti.fawe.beta;
public interface IBlocks {
}

View File

@ -0,0 +1,34 @@
package com.boydti.fawe.beta;
import com.sk89q.worldedit.world.biome.BiomeType;
import com.sk89q.worldedit.world.block.BaseBlock;
import com.sk89q.worldedit.world.block.BlockState;
import com.sk89q.worldedit.world.block.BlockStateHolder;
public interface IChunk<T, V extends IQueueExtent> {
/* set */
boolean setBiome(int x, int y, int z, BiomeType biome);
boolean setBlock(int x, int y, int z, BlockStateHolder block);
/* get */
BiomeType getBiome(int x, int z);
BlockState getBlock(int x, int y, int z);
BaseBlock getFullBlock(int x, int y, int z);
void init(V extent, int X, int Z);
T apply();
int getX();
int getZ();
default IChunk getRoot() {
return this;
}
void filter(Filter filter);
}

View File

@ -0,0 +1,79 @@
package com.boydti.fawe.beta;
import com.boydti.fawe.beta.implementation.SingleThreadQueueExtent;
import com.sk89q.worldedit.world.biome.BiomeType;
import com.sk89q.worldedit.world.block.BaseBlock;
import com.sk89q.worldedit.world.block.BlockState;
import com.sk89q.worldedit.world.block.BlockStateHolder;
public interface IDelegateChunk<T, V extends IQueueExtent, U extends IChunk<T, V>> extends IChunk<T, V> {
U getParent();
default IChunk getRoot() {
IChunk root = getParent();
while (root instanceof IDelegateChunk) {
root = ((IDelegateChunk) root).getParent();
}
return root;
}
@Override
default boolean setBiome(final int x, final int y, final int z, final BiomeType biome) {
return getParent().setBiome(x, y, z, biome);
}
@Override
default boolean setBlock(final int x, final int y, final int z, final BlockStateHolder holder) {
return getParent().setBlock(x, y, z, holder);
}
@Override
default BiomeType getBiome(final int x, final int z) {
return getParent().getBiome(x, z);
}
@Override
default BlockState getBlock(final int x, final int y, final int z) {
return getParent().getBlock(x, y, z);
}
@Override
default BaseBlock getFullBlock(final int x, final int y, final int z) {
return getParent().getFullBlock(x, y, z);
}
@Override
default void init(final V extent, final int X, final int Z) {
getParent().init(extent, X, Z);
}
@Override
default int getX() {
return getParent().getX();
}
@Override
default int getZ() {
return getParent().getZ();
}
@Override
default T apply() {
return getParent().apply();
}
default <T extends IChunk> T findParent(final Class<T> clazz) {
IChunk root = getParent();
if (clazz.isAssignableFrom(root.getClass())) return (T) root;
while (root instanceof IDelegateChunk) {
root = ((IDelegateChunk) root).getParent();
if (clazz.isAssignableFrom(root.getClass())) return (T) root;
}
return null;
}
@Override
default void filter(Filter filter) {
getParent().filter(filter);
}
}

View File

@ -0,0 +1,44 @@
package com.boydti.fawe.beta;
import com.boydti.fawe.beta.implementation.WorldChunkCache;
import java.util.concurrent.Future;
public interface IDelegateQueueExtent extends IQueueExtent {
IQueueExtent getParent();
@Override
default void init(WorldChunkCache cache) {
getParent().init(cache);
}
@Override
default IChunk getCachedChunk(int X, int Z) {
return getParent().getCachedChunk(X, Z);
}
@Override
default <T> Future<T> submit(IChunk<T, ?> chunk) {
return getParent().submit(chunk);
}
@Override
default IChunk create(boolean full) {
return getParent().create(full);
}
@Override
default IChunk wrap(IChunk root) {
return getParent().wrap(root);
}
@Override
default void flush() {
getParent().flush();
}
@Override
default boolean trim(boolean aggressive) {
return getParent().trim(aggressive);
}
}

View File

@ -0,0 +1,15 @@
package com.boydti.fawe.beta;
import com.sk89q.worldedit.world.biome.BiomeType;
import com.sk89q.worldedit.world.block.BaseBlock;
import com.sk89q.worldedit.world.block.BlockState;
public interface IGetBlocks extends IBlocks {
BaseBlock getFullBlock(int x, int y, int z);
BiomeType getBiome(int x, int z);
BlockState getBlock(int x, int y, int z);
void trim();
}

View File

@ -0,0 +1,56 @@
package com.boydti.fawe.beta;
import com.boydti.fawe.beta.implementation.WorldChunkCache;
import com.sk89q.worldedit.world.biome.BiomeType;
import com.sk89q.worldedit.world.block.BlockState;
import com.sk89q.worldedit.world.block.BlockStateHolder;
import java.io.Flushable;
import java.util.concurrent.Future;
public interface IQueueExtent extends Flushable, Trimable {
void init(WorldChunkCache world);
IChunk getCachedChunk(int X, int Z);
<T> Future<T> submit(IChunk<T, ?> chunk);
default boolean setBlock(final int x, final int y, final int z, final BlockStateHolder state) {
final IChunk chunk = getCachedChunk(x >> 4, z >> 4);
return chunk.setBlock(x & 15, y, z & 15, state);
}
default boolean setBiome(final int x, final int y, final int z, final BiomeType biome) {
final IChunk chunk = getCachedChunk(x >> 4, z >> 4);
return chunk.setBiome(x & 15, y, z & 15, biome);
}
default BlockState getBlock(final int x, final int y, final int z) {
final IChunk chunk = getCachedChunk(x >> 4, z >> 4);
return chunk.getBlock(x & 15, y, z & 15);
}
default BiomeType getBiome(final int x, final int z) {
final IChunk chunk = getCachedChunk(x >> 4, z >> 4);
return chunk.getBiome(x & 15, z & 15);
}
/**
* Return the IChunk
* @param full
* @return
*/
IChunk create(boolean full);
/**
* Wrap the chunk object (i.e. for region restrictions etc.)
* @param root
* @return wrapped chunk
*/
default IChunk wrap(IChunk root) {
return root;
}
@Override
void flush();
}

View File

@ -0,0 +1,10 @@
package com.boydti.fawe.beta;
import com.sk89q.worldedit.world.biome.BiomeType;
import com.sk89q.worldedit.world.block.BlockStateHolder;
public interface ISetBlocks extends IBlocks {
boolean setBiome(int x, int y, int z, BiomeType biome);
boolean setBlock(int x, int y, int z, BlockStateHolder holder);
}

View File

@ -0,0 +1,5 @@
package com.boydti.fawe.beta;
public interface Trimable {
boolean trim(boolean aggressive);
}

View File

@ -0,0 +1,122 @@
package com.boydti.fawe.beta.implementation;
import com.boydti.fawe.beta.Filter;
import com.boydti.fawe.beta.IQueueExtent;
import com.boydti.fawe.beta.Trimable;
import com.boydti.fawe.object.collection.IterableThreadLocal;
import com.sk89q.worldedit.regions.Region;
import com.sk89q.worldedit.world.World;
import java.lang.ref.WeakReference;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
public abstract class QueueHandler implements Trimable {
private Map<World, WeakReference<WorldChunkCache>> chunkCache = new HashMap<>();
private IterableThreadLocal<IQueueExtent> pool = new IterableThreadLocal<IQueueExtent>() {
@Override
public IQueueExtent init() {
return create();
}
};
public WorldChunkCache getOrCreate(final World world) {
synchronized (chunkCache) {
final WeakReference<WorldChunkCache> ref = chunkCache.get(world);
if (ref != null) {
final WorldChunkCache cached = ref.get();
if (cached != null) {
return cached;
}
}
final WorldChunkCache created = new WorldChunkCache(world);
chunkCache.put(world, new WeakReference<>(created));
return created;
}
}
public abstract IQueueExtent create();
public boolean trim(final boolean aggressive) {
boolean result = true;
synchronized (chunkCache) {
final Iterator<Map.Entry<World, WeakReference<WorldChunkCache>>> iter = chunkCache.entrySet().iterator();
while (iter.hasNext()) {
final Map.Entry<World, WeakReference<WorldChunkCache>> entry = iter.next();
final WeakReference<WorldChunkCache> value = entry.getValue();
final WorldChunkCache cache = value.get();
if (cache == null || cache.size() == 0 || cache.trim(aggressive)) {
iter.remove();
continue;
}
result = false;
}
}
return result;
}
public static void apply(final Region region, final Filter filter) { // TODO not MCAFilter, but another similar class
// // The chunks positions to iterate over
// final Set<BlockVector2> chunks = region.getChunks();
// final Iterator<BlockVector2> chunksIter = chunks.iterator();
//
// // Get a pool, to operate on the chunks in parallel
// final ForkJoinPool pool = TaskManager.IMP.getPublicForkJoinPool();
// final int size = Math.min(chunks.size(), Settings.IMP.QUEUE.PARALLEL_THREADS);
// final ForkJoinTask[] tasks = new ForkJoinTask[size];
//
// for (int i = 0; i < size; i++) {
// tasks[i] = pool.submit(new Runnable() {
// @Override
// public void run() {
// // Create a chunk that we will reuse/reset for each operation
// IChunk chunk = create(true);
//
// while (true) {
// // Get the next chunk pos
// final BlockVector2 pos;
// synchronized (chunksIter) {
// if (!chunksIter.hasNext()) return;
// pos = chunksIter.next();
// }
// final int X = pos.getX();
// final int Z = pos.getZ();
// final long pair = MathMan.pairInt(X, Z);
//
// // Initialize
// chunk.init(SingleThreadQueueExtent.this, X, Z);
//
// { // Start set
// lastPair = pair;
// lastChunk = chunk;
// }
// try {
// if (!filter.appliesChunk(X, Z)) {
// continue;
// }
// chunk = filter.applyChunk(chunk);
//
// if (chunk == null) continue;
//
// chunk.filter(filter);
//
// filter.finishChunk(chunk);
//
// chunk.apply();
// } finally
// { // End set
// lastPair = Long.MAX_VALUE;
// lastChunk = null;
// }
// }
// }
// });
// }
//
// // Join the tasks
// for (final ForkJoinTask task : tasks) {
// task.join();
// }
}
}

View File

@ -0,0 +1,154 @@
package com.boydti.fawe.beta.implementation;
import com.boydti.fawe.beta.IChunk;
import com.boydti.fawe.beta.IQueueExtent;
import com.boydti.fawe.beta.implementation.holder.ReferenceChunk;
import com.boydti.fawe.config.Settings;
import com.boydti.fawe.util.MathMan;
import com.boydti.fawe.util.MemUtil;
import com.boydti.fawe.util.TaskManager;
import it.unimi.dsi.fastutil.longs.Long2ObjectLinkedOpenHashMap;
import java.util.concurrent.Callable;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.ForkJoinTask;
import static com.google.common.base.Preconditions.checkNotNull;
public abstract class SingleThreadQueueExtent implements IQueueExtent {
private WorldChunkCache cache;
private Thread currentThread;
private void checkThread() {
if (Thread.currentThread() != currentThread && currentThread != null) {
throw new UnsupportedOperationException("This class must be used from a single thread. Use multiple queues for concurrent operations");
}
}
public WorldChunkCache getCache() {
return cache;
}
protected synchronized void reset() {
checkThread();
cache = null;
if (!chunks.isEmpty()) {
for (IChunk chunk : chunks.values()) {
chunk = chunk.getRoot();
if (chunk != null) {
chunkPool.add(chunk);
}
}
chunks.clear();
}
lastChunk = null;
lastPair = Long.MAX_VALUE;
currentThread = null;
}
@Override
public synchronized void init(final WorldChunkCache cache) {
if (cache != null) {
reset();
}
currentThread = Thread.currentThread();
checkNotNull(cache);
this.cache = cache;
}
private IChunk lastChunk;
private long lastPair = Long.MAX_VALUE;
private final Long2ObjectLinkedOpenHashMap<IChunk> chunks = new Long2ObjectLinkedOpenHashMap<>();
private final ConcurrentLinkedQueue<IChunk> chunkPool = new ConcurrentLinkedQueue<>();
@Override
public <T> ForkJoinTask<T> submit(final IChunk<T, ?> tmp) {
final ForkJoinPool pool = TaskManager.IMP.getPublicForkJoinPool();
return pool.submit(new Callable<T>() {
@Override
public T call() {
IChunk<T, ?> chunk = tmp;
T result = chunk.apply();
chunk = chunk.getRoot();
if (chunk != null) {
chunkPool.add(chunk);
}
return result;
}
});
}
@Override
public synchronized boolean trim(boolean aggressive) {
chunkPool.clear();
if (Thread.currentThread() == currentThread) {
lastChunk = null;
lastPair = Long.MAX_VALUE;
return chunks.isEmpty();
}
synchronized (this) {
return currentThread == null;
}
}
private IChunk pool(final int X, final int Z) {
IChunk next = chunkPool.poll();
if (next == null) next = create(false);
next.init(this, X, Z);
return next;
}
public final IChunk getCachedChunk(final int X, final int Z) {
final long pair = MathMan.pairInt(X, Z);
if (pair == lastPair) {
return lastChunk;
}
IChunk chunk = chunks.get(pair);
if (chunk instanceof ReferenceChunk) {
chunk = ((ReferenceChunk) (chunk)).getParent();
}
if (chunk != null) {
lastPair = pair;
lastChunk = chunk;
}
if (chunk != null) return chunk;
checkThread();
final int size = chunks.size();
if (size > Settings.IMP.QUEUE.TARGET_SIZE || MemUtil.isMemoryLimited()) {
if (size > Settings.IMP.QUEUE.PARALLEL_THREADS * 2 + 16) {
chunk = chunks.removeFirst();
submit(chunk);
}
}
chunk = pool(X, Z);
chunk = wrap(chunk);
chunks.put(pair, chunk);
lastPair = pair;
lastChunk = chunk;
return chunk;
}
@Override
public synchronized void flush() {
checkThread();
if (!chunks.isEmpty()) {
final ForkJoinTask[] tasks = new ForkJoinTask[chunks.size()];
int i = 0;
for (final IChunk chunk : chunks.values()) {
tasks[i++] = submit(chunk);
}
chunks.clear();
for (final ForkJoinTask task : tasks) {
if (task != null) task.join();
}
}
reset();
}
}

View File

@ -0,0 +1,62 @@
package com.boydti.fawe.beta.implementation;
import com.boydti.fawe.beta.IGetBlocks;
import com.boydti.fawe.beta.Trimable;
import com.sk89q.worldedit.world.World;
import it.unimi.dsi.fastutil.longs.Long2ObjectLinkedOpenHashMap;
import it.unimi.dsi.fastutil.longs.Long2ObjectMap;
import it.unimi.dsi.fastutil.objects.ObjectIterator;
import java.lang.ref.WeakReference;
import java.util.function.Supplier;
public class WorldChunkCache implements Trimable {
protected final Long2ObjectLinkedOpenHashMap<WeakReference<IGetBlocks>> getCache;
private final World world;
protected WorldChunkCache(final World world) {
this.world = world;
this.getCache = new Long2ObjectLinkedOpenHashMap<>();
}
public World getWorld() {
return world;
}
public synchronized int size() {
return getCache.size();
}
public synchronized IGetBlocks get(final long index, final Supplier<IGetBlocks> provider) {
final WeakReference<IGetBlocks> ref = getCache.get(index);
if (ref != null) {
final IGetBlocks blocks = ref.get();
if (blocks != null) return blocks;
}
final IGetBlocks blocks = provider.get();
getCache.put(index, new WeakReference<>(blocks));
return blocks;
}
@Override
public synchronized boolean trim(final boolean aggressive) {
boolean result = true;
if (!getCache.isEmpty()) {
final ObjectIterator<Long2ObjectMap.Entry<WeakReference<IGetBlocks>>> iter = getCache.long2ObjectEntrySet().fastIterator();
while (iter.hasNext()) {
final Long2ObjectMap.Entry<WeakReference<IGetBlocks>> entry = iter.next();
final WeakReference<IGetBlocks> value = entry.getValue();
final IGetBlocks igb = value.get();
if (igb == null) iter.remove();
else {
result = false;
if (!aggressive) return result;
synchronized (igb) {
igb.trim();
}
}
}
}
return result;
}
}

View File

@ -0,0 +1,7 @@
package com.boydti.fawe.beta.implementation.blocks;
import com.boydti.fawe.beta.IBlocks;
public class CharBlocks implements IBlocks {
protected char[][] blocks;
}

View File

@ -0,0 +1,23 @@
package com.boydti.fawe.beta.implementation.blocks;
import com.boydti.fawe.beta.IGetBlocks;
import com.sk89q.worldedit.world.biome.BiomeType;
import com.sk89q.worldedit.world.block.BaseBlock;
import com.sk89q.worldedit.world.block.BlockState;
public class CharGetBlocks extends CharBlocks implements IGetBlocks {
@Override
public BaseBlock getFullBlock(final int x, final int y, final int z) {
return null;
}
@Override
public BiomeType getBiome(final int x, final int z) {
}
@Override
public BlockState getBlock(final int x, final int y, final int z) {
return null;
}
}

View File

@ -0,0 +1,15 @@
package com.boydti.fawe.beta.implementation.blocks;
import com.boydti.fawe.beta.ISetBlocks;
import com.sk89q.jnbt.CompoundTag;
import java.util.HashMap;
import java.util.HashSet;
import java.util.UUID;
public class CharSetBlocks extends CharBlocks implements ISetBlocks {
private byte[] biomes;
private HashMap<Short, CompoundTag> tiles;
private HashSet<CompoundTag> entities;
private HashSet<UUID> entityRemoves;
}

View File

@ -0,0 +1,8 @@
package com.boydti.fawe.beta.implementation.blocks;
import com.boydti.fawe.beta.IBlocks;
public class FullCharBlocks implements IBlocks {
public final boolean[] hasSections = new boolean[16];
public final char[] blocks = new char[65536];
}

View File

@ -0,0 +1,243 @@
package com.boydti.fawe.beta.implementation.holder;
import com.boydti.fawe.beta.implementation.blocks.CharGetBlocks;
import com.boydti.fawe.beta.implementation.blocks.CharSetBlocks;
import com.boydti.fawe.beta.IChunk;
import com.boydti.fawe.beta.IGetBlocks;
import com.boydti.fawe.beta.ISetBlocks;
import com.boydti.fawe.beta.implementation.SingleThreadQueueExtent;
import com.boydti.fawe.beta.implementation.WorldChunkCache;
import com.boydti.fawe.util.MathMan;
import com.sk89q.worldedit.world.biome.BiomeType;
import com.sk89q.worldedit.world.block.BaseBlock;
import com.sk89q.worldedit.world.block.BlockState;
import com.sk89q.worldedit.world.block.BlockStateHolder;
import java.util.function.Supplier;
public abstract class ChunkHolder<T, V extends SingleThreadQueueExtent> implements IChunk<T, V>, Supplier<IGetBlocks> {
private IGetBlocks get;
private ISetBlocks set;
private IBlockDelegate delegate;
private SingleThreadQueueExtent extent;
private int X,Z;
public ChunkHolder() {
this.delegate = NULL;
}
public ChunkHolder(IBlockDelegate delegate) {
this.delegate = delegate;
}
public final IGetBlocks cachedGet() {
if (get == null) get = newGet();
return get;
}
public final ISetBlocks cachedSet() {
if (set == null) set = set();
return set;
}
public ISetBlocks set() {
return new CharSetBlocks();
}
private IGetBlocks newGet() {
WorldChunkCache cache = extent.getCache();
cache.get(MathMan.pairInt(X, Z), this);
return new CharGetBlocks();
}
public void init(final SingleThreadQueueExtent extent, final int X, final int Z) {
this.extent = extent;
this.X = X;
this.Z = Z;
set = null;
if (delegate == BOTH) {
delegate = GET;
} else if (delegate == SET) {
delegate = NULL;
}
}
public V getExtent() {
return (V) extent;
}
@Override
public int getX() {
return X;
}
@Override
public int getZ() {
return Z;
}
@Override
public boolean setBiome(final int x, final int y, final int z, final BiomeType biome) {
return delegate.setBiome(this, x, y, z, biome);
}
@Override
public boolean setBlock(final int x, final int y, final int z, final BlockStateHolder block) {
return delegate.setBlock(this, x, y, z, block);
}
@Override
public BiomeType getBiome(final int x, final int z) {
return delegate.getBiome(this, x, z);
}
@Override
public BlockState getBlock(final int x, final int y, final int z) {
return delegate.getBlock(this, x, y, z);
}
@Override
public BaseBlock getFullBlock(final int x, final int y, final int z) {
return delegate.getFullBlock(this, x, y, z);
}
public interface IBlockDelegate {
boolean setBiome(final ChunkHolder chunk, final int x, final int y, final int z, final BiomeType biome);
boolean setBlock(final ChunkHolder chunk, final int x, final int y, final int z, final BlockStateHolder holder);
BiomeType getBiome(final ChunkHolder chunk, final int x, final int z);
BlockState getBlock(final ChunkHolder chunk, final int x, final int y, final int z);
BaseBlock getFullBlock(final ChunkHolder chunk, final int x, final int y, final int z);
}
public static final IBlockDelegate NULL = new IBlockDelegate() {
@Override
public boolean setBiome(final ChunkHolder chunk, final int x, final int y, final int z, final BiomeType biome) {
chunk.cachedSet();
chunk.delegate = SET;
return chunk.setBiome(x, y, z, biome);
}
@Override
public boolean setBlock(final ChunkHolder chunk, final int x, final int y, final int z, final BlockStateHolder block) {
chunk.cachedSet();
chunk.delegate = SET;
return chunk.setBlock(x, y, z, block);
}
@Override
public BiomeType getBiome(final ChunkHolder chunk, final int x, final int z) {
chunk.cachedGet();
chunk.delegate = GET;
return chunk.getBiome(x, z);
}
@Override
public BlockState getBlock(final ChunkHolder chunk, final int x, final int y, final int z) {
chunk.cachedGet();
chunk.delegate = GET;
return chunk.getBlock(x, y, z);
}
@Override
public BaseBlock getFullBlock(final ChunkHolder chunk, final int x, final int y, final int z) {
chunk.cachedGet();
chunk.delegate = GET;
return chunk.getFullBlock(x, y, z);
}
};
public static final IBlockDelegate GET = new IBlockDelegate() {
@Override
public boolean setBiome(final ChunkHolder chunk, final int x, final int y, final int z, final BiomeType biome) {
chunk.cachedSet();
chunk.delegate = BOTH;
return chunk.setBiome(x, y, z, biome);
}
@Override
public boolean setBlock(final ChunkHolder chunk, final int x, final int y, final int z, final BlockStateHolder block) {
chunk.cachedSet();
chunk.delegate = BOTH;
return chunk.setBlock(x, y, z, block);
}
@Override
public BiomeType getBiome(final ChunkHolder chunk, final int x, final int z) {
return chunk.get.getBiome(x, z);
}
@Override
public BlockState getBlock(final ChunkHolder chunk, final int x, final int y, final int z) {
return chunk.get.getBlock(x, y, z);
}
@Override
public BaseBlock getFullBlock(final ChunkHolder chunk, final int x, final int y, final int z) {
return chunk.get.getFullBlock(x, y, z);
}
};
public static final IBlockDelegate SET = new IBlockDelegate() {
@Override
public boolean setBiome(final ChunkHolder chunk, final int x, final int y, final int z, final BiomeType biome) {
return chunk.set.setBiome(x, y, z, biome);
}
@Override
public boolean setBlock(final ChunkHolder chunk, final int x, final int y, final int z, final BlockStateHolder block) {
return chunk.set.setBlock(x, y, z, block);
}
@Override
public BiomeType getBiome(final ChunkHolder chunk, final int x, final int z) {
chunk.cachedGet();
chunk.delegate = BOTH;
return chunk.getBiome(x, z);
}
@Override
public BlockState getBlock(final ChunkHolder chunk, final int x, final int y, final int z) {
chunk.cachedGet();
chunk.delegate = BOTH;
return chunk.getBlock(x, y, z);
}
@Override
public BaseBlock getFullBlock(final ChunkHolder chunk, final int x, final int y, final int z) {
chunk.cachedGet();
chunk.delegate = BOTH;
return chunk.getFullBlock(x, y, z);
}
};
public static final IBlockDelegate BOTH = new IBlockDelegate() {
@Override
public boolean setBiome(final ChunkHolder chunk, final int x, final int y, final int z, final BiomeType biome) {
return chunk.set.setBiome(x, y, z, biome);
}
@Override
public boolean setBlock(final ChunkHolder chunk, final int x, final int y, final int z, final BlockStateHolder block) {
return chunk.set.setBlock(x, y, z, block);
}
@Override
public BiomeType getBiome(final ChunkHolder chunk, final int x, final int z) {
return chunk.get.getBiome(x, z);
}
@Override
public BlockState getBlock(final ChunkHolder chunk, final int x, final int y, final int z) {
return chunk.get.getBlock(x, y, z);
}
@Override
public BaseBlock getFullBlock(final ChunkHolder chunk, final int x, final int y, final int z) {
return chunk.get.getFullBlock(x, y, z);
}
};
}

View File

@ -0,0 +1,20 @@
package com.boydti.fawe.beta.implementation.holder;
import com.boydti.fawe.beta.IChunk;
import com.boydti.fawe.beta.IDelegateChunk;
public class DelegateChunk<T extends IChunk> implements IDelegateChunk {
private T parent;
public DelegateChunk(final T parent) {
this.parent = parent;
}
public final T getParent() {
return parent;
}
public final void setParent(final T parent) {
this.parent = parent;
}
}

View File

@ -0,0 +1,30 @@
package com.boydti.fawe.beta.implementation.holder;
import com.boydti.fawe.beta.IQueueExtent;
import com.boydti.fawe.beta.IChunk;
/**
* Used by {@link ReferenceChunk} to allow the chunk to be garbage collected
* - When the object is finalized, add it to the queue
*/
public class FinalizedChunk extends DelegateChunk {
private final IQueueExtent queueExtent;
public FinalizedChunk(final IChunk parent, IQueueExtent queueExtent) {
super(parent);
this.queueExtent = queueExtent;
}
/**
* Submit the chunk to the queue
* @throws Throwable
*/
@Override
protected void finalize() throws Throwable {
if (getParent() != null) {
apply();
setParent(null);
}
super.finalize();
}
}

View File

@ -0,0 +1,23 @@
package com.boydti.fawe.beta.implementation.holder;
import com.boydti.fawe.beta.IChunk;
import com.boydti.fawe.beta.IDelegateChunk;
import com.boydti.fawe.beta.IQueueExtent;
import java.lang.ref.Reference;
public abstract class ReferenceChunk implements IDelegateChunk {
private final Reference<FinalizedChunk> ref;
public ReferenceChunk(final IChunk parent, IQueueExtent queueExtent) {
this.ref = toRef(new FinalizedChunk(parent, queueExtent));
}
protected abstract Reference<FinalizedChunk> toRef(FinalizedChunk parent);
@Override
public IChunk getParent() {
final FinalizedChunk finalized = ref.get();
return finalized != null ? finalized.getParent() : null;
}
}

View File

@ -0,0 +1,19 @@
package com.boydti.fawe.beta.implementation.holder;
import com.boydti.fawe.beta.IChunk;
import com.boydti.fawe.beta.IQueueExtent;
import java.lang.ref.Reference;
import java.lang.ref.SoftReference;
public class SoftChunk extends ReferenceChunk {
public SoftChunk(final IChunk parent, IQueueExtent queueExtent) {
super(parent, queueExtent);
}
@Override
protected Reference<FinalizedChunk> toRef(final FinalizedChunk parent) {
return new SoftReference<>(parent);
}
}

View File

@ -0,0 +1,17 @@
package com.boydti.fawe.beta.implementation.holder;
import com.boydti.fawe.beta.IChunk;
import java.lang.ref.Reference;
import java.lang.ref.WeakReference;
public class WeakChunk extends ReferenceChunk {
public WeakChunk(final IChunk parent) {
super(parent);
}
@Override
protected Reference<FinalizedChunk> toRef(final FinalizedChunk parent) {
return new WeakReference<>(parent);
}
}