2018-08-12 14:03:07 +00:00
|
|
|
package com.boydti.fawe;
|
|
|
|
|
2019-04-28 15:44:59 +00:00
|
|
|
import com.boydti.fawe.beta.Trimable;
|
2019-10-23 14:35:04 +00:00
|
|
|
import com.boydti.fawe.config.BBC;
|
2019-05-01 15:45:18 +00:00
|
|
|
import com.boydti.fawe.config.Settings;
|
2019-07-18 20:49:29 +00:00
|
|
|
import com.boydti.fawe.object.collection.BitArray4096;
|
2019-10-23 04:23:52 +00:00
|
|
|
import com.boydti.fawe.object.collection.CleanableThreadLocal;
|
2019-10-23 14:35:04 +00:00
|
|
|
import com.boydti.fawe.object.exception.FaweBlockBagException;
|
|
|
|
import com.boydti.fawe.object.exception.FaweChunkLoadException;
|
|
|
|
import com.boydti.fawe.object.exception.FaweException;
|
2019-08-18 01:09:09 +00:00
|
|
|
import com.boydti.fawe.util.IOUtil;
|
2019-04-27 01:15:08 +00:00
|
|
|
import com.boydti.fawe.util.MathMan;
|
2019-07-18 20:49:29 +00:00
|
|
|
import com.sk89q.jnbt.ByteArrayTag;
|
|
|
|
import com.sk89q.jnbt.ByteTag;
|
|
|
|
import com.sk89q.jnbt.CompoundTag;
|
|
|
|
import com.sk89q.jnbt.DoubleTag;
|
|
|
|
import com.sk89q.jnbt.EndTag;
|
|
|
|
import com.sk89q.jnbt.FloatTag;
|
|
|
|
import com.sk89q.jnbt.IntArrayTag;
|
|
|
|
import com.sk89q.jnbt.IntTag;
|
|
|
|
import com.sk89q.jnbt.ListTag;
|
|
|
|
import com.sk89q.jnbt.LongArrayTag;
|
|
|
|
import com.sk89q.jnbt.LongTag;
|
|
|
|
import com.sk89q.jnbt.ShortTag;
|
|
|
|
import com.sk89q.jnbt.StringTag;
|
|
|
|
import com.sk89q.jnbt.Tag;
|
2019-05-28 20:31:22 +00:00
|
|
|
import com.sk89q.worldedit.math.MutableBlockVector3;
|
|
|
|
import com.sk89q.worldedit.math.MutableVector3;
|
2019-04-27 01:15:08 +00:00
|
|
|
import com.sk89q.worldedit.world.block.BlockState;
|
2019-04-13 06:44:23 +00:00
|
|
|
import com.sk89q.worldedit.world.block.BlockTypes;
|
2018-08-12 14:03:07 +00:00
|
|
|
|
|
|
|
import java.lang.reflect.Field;
|
2019-07-18 20:49:29 +00:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Arrays;
|
|
|
|
import java.util.Collection;
|
|
|
|
import java.util.HashMap;
|
2019-08-18 01:09:09 +00:00
|
|
|
import java.util.IdentityHashMap;
|
2019-07-18 20:49:29 +00:00
|
|
|
import java.util.List;
|
|
|
|
import java.util.Map;
|
2019-05-01 15:45:18 +00:00
|
|
|
import java.util.concurrent.ArrayBlockingQueue;
|
2019-10-30 11:26:52 +00:00
|
|
|
import java.util.concurrent.CancellationException;
|
2019-08-18 01:09:09 +00:00
|
|
|
import java.util.concurrent.ConcurrentLinkedQueue;
|
2019-10-30 11:26:52 +00:00
|
|
|
import java.util.concurrent.ExecutionException;
|
2019-05-01 15:45:18 +00:00
|
|
|
import java.util.concurrent.Executors;
|
2019-10-30 11:26:52 +00:00
|
|
|
import java.util.concurrent.Future;
|
2019-05-01 15:45:18 +00:00
|
|
|
import java.util.concurrent.ThreadPoolExecutor;
|
|
|
|
import java.util.concurrent.TimeUnit;
|
2019-10-30 11:58:21 +00:00
|
|
|
import java.util.concurrent.atomic.AtomicBoolean;
|
2019-08-18 01:09:09 +00:00
|
|
|
import java.util.function.Supplier;
|
2018-08-12 14:03:07 +00:00
|
|
|
|
2019-08-18 01:09:09 +00:00
|
|
|
import static com.google.common.base.Preconditions.checkNotNull;
|
2019-07-18 12:42:04 +00:00
|
|
|
|
2019-08-18 01:09:09 +00:00
|
|
|
public enum FaweCache implements Trimable {
|
|
|
|
IMP
|
|
|
|
; // singleton
|
2019-07-18 12:42:04 +00:00
|
|
|
|
2019-08-18 01:09:09 +00:00
|
|
|
public final int BLOCKS_PER_LAYER = 4096;
|
|
|
|
public final int CHUNK_LAYERS = 16;
|
|
|
|
public final int WORLD_HEIGHT = CHUNK_LAYERS << 4;
|
|
|
|
public final int WORLD_MAX_Y = WORLD_HEIGHT - 1;
|
|
|
|
|
|
|
|
public final char[] EMPTY_CHAR_4096 = new char[4096];
|
|
|
|
|
2019-10-23 04:23:52 +00:00
|
|
|
private final IdentityHashMap<Class, CleanableThreadLocal> REGISTERED_SINGLETONS = new IdentityHashMap<>();
|
2019-08-18 01:09:09 +00:00
|
|
|
private final IdentityHashMap<Class, Pool> REGISTERED_POOLS = new IdentityHashMap<>();
|
|
|
|
|
|
|
|
public interface Pool<T> {
|
|
|
|
T poll();
|
|
|
|
default boolean offer(T recycle) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
default void clear() {}
|
|
|
|
}
|
|
|
|
|
|
|
|
public class QueuePool<T> extends ConcurrentLinkedQueue<T> implements Pool<T> {
|
|
|
|
private final Supplier<T> supplier;
|
|
|
|
|
|
|
|
public QueuePool(Supplier<T> supplier) {
|
|
|
|
this.supplier = supplier;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean offer(T t) {
|
|
|
|
return super.offer(t);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public T poll() {
|
|
|
|
T result = super.poll();
|
|
|
|
if (result == null) {
|
|
|
|
return supplier.get();
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void clear() {
|
|
|
|
if (!isEmpty()) super.clear();
|
|
|
|
}
|
|
|
|
}
|
2019-04-27 01:15:08 +00:00
|
|
|
|
2019-04-28 15:44:59 +00:00
|
|
|
/*
|
|
|
|
Palette buffers / cache
|
|
|
|
*/
|
2019-04-27 01:15:08 +00:00
|
|
|
|
2019-04-28 15:44:59 +00:00
|
|
|
@Override
|
2019-08-18 01:09:09 +00:00
|
|
|
public synchronized boolean trim(boolean aggressive) {
|
2019-10-30 11:58:21 +00:00
|
|
|
CHUNK_FLAG.clean();
|
2019-10-30 11:26:52 +00:00
|
|
|
BYTE_BUFFER_8192.clean();
|
2019-04-28 15:44:59 +00:00
|
|
|
BLOCK_TO_PALETTE.clean();
|
|
|
|
PALETTE_TO_BLOCK.clean();
|
|
|
|
BLOCK_STATES.clean();
|
|
|
|
SECTION_BLOCKS.clean();
|
|
|
|
PALETTE_CACHE.clean();
|
2019-05-28 20:31:22 +00:00
|
|
|
PALETTE_TO_BLOCK_CHAR.clean();
|
2019-08-18 01:09:09 +00:00
|
|
|
INDEX_STORE.clean();
|
2019-05-28 20:31:22 +00:00
|
|
|
|
|
|
|
MUTABLE_VECTOR3.clean();
|
|
|
|
MUTABLE_BLOCKVECTOR3.clean();
|
2019-08-07 00:13:07 +00:00
|
|
|
SECTION_BITS_TO_CHAR.clean();
|
2019-10-23 04:23:52 +00:00
|
|
|
for (Map.Entry<Class, CleanableThreadLocal> entry : REGISTERED_SINGLETONS.entrySet()) {
|
2019-08-18 01:09:09 +00:00
|
|
|
entry.getValue().clean();
|
|
|
|
}
|
|
|
|
for (Map.Entry<Class, Pool> entry : REGISTERED_POOLS.entrySet()) {
|
|
|
|
Pool pool = entry.getValue();
|
|
|
|
pool.clear();
|
|
|
|
}
|
|
|
|
|
2019-04-28 15:44:59 +00:00
|
|
|
return false;
|
|
|
|
}
|
2018-08-12 14:03:07 +00:00
|
|
|
|
2019-08-18 01:09:09 +00:00
|
|
|
public final <T> Pool<T> getPool(Class<T> clazz) {
|
|
|
|
Pool<T> pool = REGISTERED_POOLS.get(clazz);
|
|
|
|
if (pool == null) {
|
|
|
|
synchronized (this) {
|
|
|
|
pool = REGISTERED_POOLS.get(clazz);
|
|
|
|
if (pool == null) {
|
|
|
|
Fawe.debug("Not registered " + clazz);
|
|
|
|
Supplier<T> supplier = IOUtil.supplier(clazz::newInstance);
|
|
|
|
pool = supplier::get;
|
|
|
|
REGISTERED_POOLS.put(clazz, pool);
|
|
|
|
}
|
|
|
|
}
|
2019-04-13 06:44:23 +00:00
|
|
|
}
|
2019-08-18 01:09:09 +00:00
|
|
|
return pool;
|
|
|
|
}
|
2018-08-12 14:03:07 +00:00
|
|
|
|
2019-08-18 01:09:09 +00:00
|
|
|
public final <T> T getFromPool(Class<T> clazz) {
|
|
|
|
Pool<T> pool = getPool(clazz);
|
|
|
|
return pool.poll();
|
|
|
|
}
|
2019-08-07 00:13:07 +00:00
|
|
|
|
2019-08-18 01:09:09 +00:00
|
|
|
public final <T> T getSingleton(Class<T> clazz) {
|
2019-10-23 04:23:52 +00:00
|
|
|
CleanableThreadLocal<T> cache = REGISTERED_SINGLETONS.get(clazz);
|
2019-08-18 01:09:09 +00:00
|
|
|
if (cache == null) {
|
|
|
|
synchronized (this) {
|
|
|
|
cache = REGISTERED_SINGLETONS.get(clazz);
|
|
|
|
if (cache == null) {
|
|
|
|
Fawe.debug("Not registered " + clazz);
|
2019-10-23 04:23:52 +00:00
|
|
|
cache = new CleanableThreadLocal<>(IOUtil.supplier(clazz::newInstance));
|
2019-08-18 01:09:09 +00:00
|
|
|
REGISTERED_SINGLETONS.put(clazz, cache);
|
|
|
|
}
|
|
|
|
}
|
2019-04-30 16:19:10 +00:00
|
|
|
}
|
2019-08-18 01:09:09 +00:00
|
|
|
return cache.get();
|
|
|
|
}
|
2019-04-30 16:19:10 +00:00
|
|
|
|
2019-10-23 04:23:52 +00:00
|
|
|
public synchronized <T> CleanableThreadLocal<T> registerSingleton(Class<T> clazz, Supplier<T> cache) {
|
2019-08-18 01:09:09 +00:00
|
|
|
checkNotNull(cache);
|
2019-10-23 04:23:52 +00:00
|
|
|
CleanableThreadLocal<T> local = new CleanableThreadLocal<>(cache);
|
|
|
|
CleanableThreadLocal previous = REGISTERED_SINGLETONS.putIfAbsent(clazz, local);
|
2019-08-18 01:09:09 +00:00
|
|
|
if (previous != null) {
|
|
|
|
throw new IllegalStateException("Previous key");
|
2018-08-12 14:03:07 +00:00
|
|
|
}
|
2019-08-18 01:09:09 +00:00
|
|
|
return local;
|
|
|
|
}
|
2019-04-13 06:44:23 +00:00
|
|
|
|
2019-08-18 01:09:09 +00:00
|
|
|
public synchronized <T> Pool<T> registerPool(Class<T> clazz, Supplier<T> cache, boolean buffer) {
|
|
|
|
checkNotNull(cache);
|
|
|
|
Pool<T> pool;
|
|
|
|
if (buffer) {
|
|
|
|
pool = new QueuePool<>(cache);
|
|
|
|
} else {
|
|
|
|
pool = cache::get;
|
2018-08-12 14:03:07 +00:00
|
|
|
}
|
2019-08-18 01:09:09 +00:00
|
|
|
Pool previous = REGISTERED_POOLS.putIfAbsent(clazz, pool);
|
|
|
|
if (previous != null) {
|
|
|
|
throw new IllegalStateException("Previous key");
|
|
|
|
}
|
|
|
|
return pool;
|
|
|
|
}
|
2019-04-13 06:44:23 +00:00
|
|
|
|
2019-10-23 14:35:04 +00:00
|
|
|
/*
|
|
|
|
Exceptions
|
|
|
|
*/
|
|
|
|
public static final FaweChunkLoadException CHUNK = new FaweChunkLoadException();
|
|
|
|
public static final FaweBlockBagException BLOCK_BAG = new FaweBlockBagException();
|
|
|
|
public static final FaweException MANUAL = new FaweException(BBC.WORLDEDIT_CANCEL_REASON_MANUAL);
|
|
|
|
public static final FaweException NO_REGION = new FaweException(BBC.WORLDEDIT_CANCEL_REASON_NO_REGION);
|
|
|
|
public static final FaweException OUTSIDE_REGION = new FaweException(BBC.WORLDEDIT_CANCEL_REASON_OUTSIDE_REGION);
|
|
|
|
public static final FaweException MAX_CHECKS = new FaweException(BBC.WORLDEDIT_CANCEL_REASON_MAX_CHECKS);
|
|
|
|
public static final FaweException MAX_CHANGES = new FaweException(BBC.WORLDEDIT_CANCEL_REASON_MAX_CHANGES);
|
|
|
|
public static final FaweException LOW_MEMORY = new FaweException(BBC.WORLDEDIT_CANCEL_REASON_LOW_MEMORY);
|
|
|
|
public static final FaweException MAX_ENTITIES = new FaweException(BBC.WORLDEDIT_CANCEL_REASON_MAX_ENTITIES);
|
|
|
|
public static final FaweException MAX_TILES = new FaweException(BBC.WORLDEDIT_CANCEL_REASON_MAX_TILES);
|
|
|
|
|
|
|
|
/*
|
|
|
|
thread cache
|
|
|
|
*/
|
2019-10-30 11:58:21 +00:00
|
|
|
public final CleanableThreadLocal<AtomicBoolean> CHUNK_FLAG = new CleanableThreadLocal<>(AtomicBoolean::new); // resets to false
|
2019-10-23 14:35:04 +00:00
|
|
|
|
2019-11-01 18:11:05 +00:00
|
|
|
public final CleanableThreadLocal<long[]> LONG_BUFFER_1024 = new CleanableThreadLocal<>(() -> new long[1024]);
|
|
|
|
|
2019-10-30 11:26:52 +00:00
|
|
|
public final CleanableThreadLocal<byte[]> BYTE_BUFFER_8192 = new CleanableThreadLocal<>(() -> new byte[8192]);
|
|
|
|
|
2019-10-23 04:23:52 +00:00
|
|
|
public final CleanableThreadLocal<int[]> BLOCK_TO_PALETTE = new CleanableThreadLocal<>(() -> {
|
2019-08-18 01:09:09 +00:00
|
|
|
int[] result = new int[BlockTypes.states.length];
|
|
|
|
Arrays.fill(result, Integer.MAX_VALUE);
|
|
|
|
return result;
|
|
|
|
});
|
|
|
|
|
2019-10-23 04:23:52 +00:00
|
|
|
public final CleanableThreadLocal<char[]> SECTION_BITS_TO_CHAR = new CleanableThreadLocal<>(() -> new char[4096]);
|
2019-08-18 01:09:09 +00:00
|
|
|
|
2019-10-23 04:23:52 +00:00
|
|
|
public final CleanableThreadLocal<int[]> PALETTE_TO_BLOCK = new CleanableThreadLocal<>(() -> new int[Character.MAX_VALUE + 1]);
|
2019-08-18 01:09:09 +00:00
|
|
|
|
2019-10-23 04:23:52 +00:00
|
|
|
public final CleanableThreadLocal<char[]> PALETTE_TO_BLOCK_CHAR = new CleanableThreadLocal<>(
|
2019-08-18 01:09:09 +00:00
|
|
|
() -> new char[Character.MAX_VALUE + 1], a -> {
|
|
|
|
Arrays.fill(a, Character.MAX_VALUE);
|
2018-08-12 14:03:07 +00:00
|
|
|
}
|
2019-08-18 01:09:09 +00:00
|
|
|
);
|
|
|
|
|
2019-10-23 04:23:52 +00:00
|
|
|
public final CleanableThreadLocal<long[]> BLOCK_STATES = new CleanableThreadLocal<>(() -> new long[2048]);
|
2019-08-18 01:09:09 +00:00
|
|
|
|
2019-10-23 04:23:52 +00:00
|
|
|
public final CleanableThreadLocal<int[]> SECTION_BLOCKS = new CleanableThreadLocal<>(() -> new int[4096]);
|
2019-08-18 01:09:09 +00:00
|
|
|
|
2019-10-23 04:23:52 +00:00
|
|
|
public final CleanableThreadLocal<int[]> INDEX_STORE = new CleanableThreadLocal<>(() -> new int[256]);
|
2018-08-12 14:03:07 +00:00
|
|
|
|
2019-04-30 16:19:10 +00:00
|
|
|
/**
|
|
|
|
* Holds data for a palette used in a chunk section
|
|
|
|
*/
|
2019-08-18 01:09:09 +00:00
|
|
|
public final class Palette {
|
2019-10-26 13:21:49 +00:00
|
|
|
public int bitsPerEntry;
|
|
|
|
|
2019-04-28 15:44:59 +00:00
|
|
|
public int paletteToBlockLength;
|
|
|
|
/**
|
|
|
|
* Reusable buffer array, MUST check paletteToBlockLength for actual length
|
|
|
|
*/
|
|
|
|
public int[] paletteToBlock;
|
|
|
|
|
2019-07-22 02:49:08 +00:00
|
|
|
public int blockStatesLength;
|
2019-04-28 15:44:59 +00:00
|
|
|
/**
|
2019-07-22 02:49:08 +00:00
|
|
|
* Reusable buffer array, MUST check blockStatesLength for actual length
|
2019-04-28 15:44:59 +00:00
|
|
|
*/
|
2019-07-22 02:49:08 +00:00
|
|
|
public long[] blockStates;
|
2019-04-28 15:44:59 +00:00
|
|
|
}
|
|
|
|
|
2019-10-23 04:23:52 +00:00
|
|
|
private final CleanableThreadLocal<Palette> PALETTE_CACHE = new CleanableThreadLocal<>(Palette::new);
|
2019-04-28 15:44:59 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert raw char array to palette
|
2019-04-30 16:19:10 +00:00
|
|
|
* @param layerOffset
|
2019-04-28 15:44:59 +00:00
|
|
|
* @param blocks
|
|
|
|
* @return palette
|
|
|
|
*/
|
2019-08-18 01:09:09 +00:00
|
|
|
public Palette toPalette(int layerOffset, char[] blocks) {
|
2019-04-30 16:19:10 +00:00
|
|
|
return toPalette(layerOffset, null, blocks);
|
2019-04-28 15:44:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert raw int array to palette
|
2019-04-30 16:19:10 +00:00
|
|
|
* @param layerOffset
|
2019-04-28 15:44:59 +00:00
|
|
|
* @param blocks
|
|
|
|
* @return palette
|
|
|
|
*/
|
2019-08-18 01:09:09 +00:00
|
|
|
public Palette toPalette(int layerOffset, int[] blocks) {
|
2019-04-30 16:19:10 +00:00
|
|
|
return toPalette(layerOffset, blocks, null);
|
2019-04-28 15:44:59 +00:00
|
|
|
}
|
|
|
|
|
2019-08-18 01:09:09 +00:00
|
|
|
private Palette toPalette(int layerOffset, int[] blocksInts, char[] blocksChars) {
|
2019-04-28 15:44:59 +00:00
|
|
|
int[] blockToPalette = BLOCK_TO_PALETTE.get();
|
|
|
|
int[] paletteToBlock = PALETTE_TO_BLOCK.get();
|
2019-07-22 02:49:08 +00:00
|
|
|
long[] blockStates = BLOCK_STATES.get();
|
2019-04-28 15:44:59 +00:00
|
|
|
int[] blocksCopy = SECTION_BLOCKS.get();
|
|
|
|
|
2019-04-30 16:19:10 +00:00
|
|
|
int blockIndexStart = layerOffset << 12;
|
2019-04-28 15:44:59 +00:00
|
|
|
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) {
|
2019-04-30 16:19:10 +00:00
|
|
|
// BlockState state = BlockTypes.states[ordinal];
|
2019-04-28 15:44:59 +00:00
|
|
|
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
|
2019-07-22 02:49:08 +00:00
|
|
|
blockStates[0] = 0;
|
2019-04-28 15:44:59 +00:00
|
|
|
blockBitArrayEnd = 1;
|
|
|
|
} else {
|
2019-07-22 02:49:08 +00:00
|
|
|
BitArray4096 bitArray = new BitArray4096(blockStates, bitsPerEntry);
|
2019-04-28 15:44:59 +00:00
|
|
|
bitArray.fromRaw(blocksCopy);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Construct palette
|
|
|
|
Palette palette = PALETTE_CACHE.get();
|
2019-10-26 13:21:49 +00:00
|
|
|
palette.bitsPerEntry = bitsPerEntry;
|
2019-04-28 15:44:59 +00:00
|
|
|
palette.paletteToBlockLength = num_palette;
|
|
|
|
palette.paletteToBlock = paletteToBlock;
|
|
|
|
|
2019-07-22 02:49:08 +00:00
|
|
|
palette.blockStatesLength = blockBitArrayEnd;
|
|
|
|
palette.blockStates = blockStates;
|
2019-04-28 15:44:59 +00:00
|
|
|
|
|
|
|
return palette;
|
|
|
|
} catch (Throwable e) {
|
|
|
|
Arrays.fill(blockToPalette, Integer.MAX_VALUE);
|
|
|
|
e.printStackTrace();
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-28 20:31:22 +00:00
|
|
|
/*
|
|
|
|
* Vector cache
|
|
|
|
*/
|
|
|
|
|
2019-10-23 04:23:52 +00:00
|
|
|
public CleanableThreadLocal<MutableBlockVector3> MUTABLE_BLOCKVECTOR3 = new CleanableThreadLocal<>(MutableBlockVector3::new);
|
2019-05-28 20:31:22 +00:00
|
|
|
|
2019-10-23 04:23:52 +00:00
|
|
|
public CleanableThreadLocal<MutableVector3> MUTABLE_VECTOR3 = new CleanableThreadLocal<MutableVector3>(MutableVector3::new) {
|
2019-05-28 20:31:22 +00:00
|
|
|
@Override
|
|
|
|
public MutableVector3 init() {
|
|
|
|
return new MutableVector3();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-04-28 15:44:59 +00:00
|
|
|
/*
|
|
|
|
Conversion methods between JNBT tags and raw values
|
|
|
|
*/
|
2019-08-18 01:09:09 +00:00
|
|
|
public Map<String, Object> asMap(Object... pairs) {
|
2019-02-16 02:46:10 +00:00
|
|
|
HashMap<String, Object> map = new HashMap<>(pairs.length >> 1);
|
2018-08-12 14:03:07 +00:00
|
|
|
for (int i = 0; i < pairs.length; i += 2) {
|
|
|
|
String key = (String) pairs[i];
|
|
|
|
Object value = pairs[i + 1];
|
|
|
|
map.put(key, value);
|
|
|
|
}
|
|
|
|
return map;
|
|
|
|
}
|
|
|
|
|
2019-08-18 01:09:09 +00:00
|
|
|
public ShortTag asTag(short value) {
|
2018-08-12 14:03:07 +00:00
|
|
|
return new ShortTag(value);
|
|
|
|
}
|
|
|
|
|
2019-08-18 01:09:09 +00:00
|
|
|
public IntTag asTag(int value) {
|
2018-08-12 14:03:07 +00:00
|
|
|
return new IntTag(value);
|
|
|
|
}
|
|
|
|
|
2019-08-18 01:09:09 +00:00
|
|
|
public DoubleTag asTag(double value) {
|
2018-08-12 14:03:07 +00:00
|
|
|
return new DoubleTag(value);
|
|
|
|
}
|
|
|
|
|
2019-08-18 01:09:09 +00:00
|
|
|
public ByteTag asTag(byte value) {
|
2018-08-12 14:03:07 +00:00
|
|
|
return new ByteTag(value);
|
|
|
|
}
|
|
|
|
|
2019-08-18 01:09:09 +00:00
|
|
|
public FloatTag asTag(float value) {
|
2018-08-12 14:03:07 +00:00
|
|
|
return new FloatTag(value);
|
|
|
|
}
|
|
|
|
|
2019-08-18 01:09:09 +00:00
|
|
|
public LongTag asTag(long value) {
|
2018-08-12 14:03:07 +00:00
|
|
|
return new LongTag(value);
|
|
|
|
}
|
|
|
|
|
2019-08-18 01:09:09 +00:00
|
|
|
public ByteArrayTag asTag(byte[] value) {
|
2018-08-12 14:03:07 +00:00
|
|
|
return new ByteArrayTag(value);
|
|
|
|
}
|
|
|
|
|
2019-08-18 01:09:09 +00:00
|
|
|
public IntArrayTag asTag(int[] value) {
|
2018-08-12 14:03:07 +00:00
|
|
|
return new IntArrayTag(value);
|
|
|
|
}
|
|
|
|
|
2019-08-18 01:09:09 +00:00
|
|
|
public LongArrayTag asTag(long[] value) {
|
2018-08-12 14:03:07 +00:00
|
|
|
return new LongArrayTag(value);
|
|
|
|
}
|
|
|
|
|
2019-08-18 01:09:09 +00:00
|
|
|
public StringTag asTag(String value) {
|
2018-08-12 14:03:07 +00:00
|
|
|
return new StringTag(value);
|
|
|
|
}
|
|
|
|
|
2019-08-18 01:09:09 +00:00
|
|
|
public CompoundTag asTag(Map<String, Object> value) {
|
2018-08-12 14:03:07 +00:00
|
|
|
HashMap<String, Tag> map = new HashMap<>();
|
|
|
|
for (Map.Entry<String, Object> entry : value.entrySet()) {
|
|
|
|
Object child = entry.getValue();
|
|
|
|
Tag tag = asTag(child);
|
|
|
|
map.put(entry.getKey(), tag);
|
|
|
|
}
|
|
|
|
return new CompoundTag(map);
|
|
|
|
}
|
|
|
|
|
2019-08-18 01:09:09 +00:00
|
|
|
public Tag asTag(Object value) {
|
2018-08-12 14:03:07 +00:00
|
|
|
if (value instanceof Integer) {
|
|
|
|
return asTag((int) value);
|
|
|
|
} else if (value instanceof Short) {
|
|
|
|
return asTag((short) value);
|
|
|
|
} else if (value instanceof Double) {
|
|
|
|
return asTag((double) value);
|
|
|
|
} else if (value instanceof Byte) {
|
|
|
|
return asTag((byte) value);
|
|
|
|
} else if (value instanceof Float) {
|
|
|
|
return asTag((float) value);
|
|
|
|
} else if (value instanceof Long) {
|
|
|
|
return asTag((long) value);
|
|
|
|
} else if (value instanceof String) {
|
|
|
|
return asTag((String) value);
|
|
|
|
} else if (value instanceof Map) {
|
2019-07-09 19:50:13 +00:00
|
|
|
return asTag((Map<String, Object>) value);
|
2018-08-12 14:03:07 +00:00
|
|
|
} else if (value instanceof Collection) {
|
|
|
|
return asTag((Collection) value);
|
|
|
|
} else if (value instanceof Object[]) {
|
|
|
|
return asTag((Object[]) value);
|
|
|
|
} else if (value instanceof byte[]) {
|
|
|
|
return asTag((byte[]) value);
|
|
|
|
} else if (value instanceof int[]) {
|
|
|
|
return asTag((int[]) value);
|
|
|
|
} else if (value instanceof long[]) {
|
|
|
|
return asTag((long[]) value);
|
|
|
|
} else if (value instanceof Tag) {
|
|
|
|
return (Tag) value;
|
|
|
|
} else if (value instanceof Boolean) {
|
|
|
|
return asTag((byte) ((boolean) value ? 1 : 0));
|
|
|
|
} else if (value == null) {
|
|
|
|
System.out.println("Invalid nbt: " + value);
|
|
|
|
return null;
|
|
|
|
} else {
|
|
|
|
Class<? extends Object> clazz = value.getClass();
|
|
|
|
if (clazz.getName().startsWith("com.intellectualcrafters.jnbt")) {
|
|
|
|
try {
|
|
|
|
if (clazz.getName().equals("com.intellectualcrafters.jnbt.EndTag")) {
|
2019-03-22 16:51:44 +00:00
|
|
|
return new EndTag();
|
2018-08-12 14:03:07 +00:00
|
|
|
}
|
|
|
|
Field field = clazz.getDeclaredField("value");
|
|
|
|
field.setAccessible(true);
|
|
|
|
return asTag(field.get(value));
|
|
|
|
} catch (Throwable e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
System.out.println("Invalid nbt: " + value);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-18 01:09:09 +00:00
|
|
|
public ListTag asTag(Object... values) {
|
2019-07-09 19:50:13 +00:00
|
|
|
Class<? extends Tag> clazz = null;
|
2018-08-12 14:03:07 +00:00
|
|
|
List<Tag> list = new ArrayList<>(values.length);
|
|
|
|
for (Object value : values) {
|
|
|
|
Tag tag = asTag(value);
|
|
|
|
if (clazz == null) {
|
|
|
|
clazz = tag.getClass();
|
|
|
|
}
|
|
|
|
list.add(tag);
|
|
|
|
}
|
|
|
|
if (clazz == null) clazz = EndTag.class;
|
|
|
|
return new ListTag(clazz, list);
|
|
|
|
}
|
|
|
|
|
2019-08-18 01:09:09 +00:00
|
|
|
public ListTag asTag(Collection values) {
|
2019-07-09 19:50:13 +00:00
|
|
|
Class<? extends Tag> clazz = null;
|
2018-08-12 14:03:07 +00:00
|
|
|
List<Tag> list = new ArrayList<>(values.size());
|
|
|
|
for (Object value : values) {
|
|
|
|
Tag tag = asTag(value);
|
|
|
|
if (clazz == null) {
|
|
|
|
clazz = tag.getClass();
|
|
|
|
}
|
|
|
|
list.add(tag);
|
|
|
|
}
|
|
|
|
if (clazz == null) clazz = EndTag.class;
|
|
|
|
return new ListTag(clazz, list);
|
|
|
|
}
|
2019-05-01 15:45:18 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
Thread stuff
|
|
|
|
*/
|
2019-08-18 01:09:09 +00:00
|
|
|
public ThreadPoolExecutor newBlockingExecutor() {
|
2019-05-01 15:45:18 +00:00
|
|
|
int nThreads = Settings.IMP.QUEUE.PARALLEL_THREADS;
|
|
|
|
ArrayBlockingQueue<Runnable> queue = new ArrayBlockingQueue<>(nThreads);
|
|
|
|
return new ThreadPoolExecutor(nThreads, nThreads,
|
|
|
|
0L, TimeUnit.MILLISECONDS, queue
|
|
|
|
, Executors.defaultThreadFactory(),
|
2019-10-30 11:26:52 +00:00
|
|
|
new ThreadPoolExecutor.CallerRunsPolicy()) {
|
|
|
|
protected void afterExecute(Runnable r, Throwable t) {
|
|
|
|
try {
|
|
|
|
super.afterExecute(r, t);
|
|
|
|
if (t == null && r instanceof Future<?>) {
|
|
|
|
try {
|
|
|
|
Future<?> future = (Future<?>) r;
|
|
|
|
if (future.isDone()) {
|
|
|
|
future.get();
|
|
|
|
}
|
|
|
|
} catch (CancellationException ce) {
|
|
|
|
t = ce;
|
|
|
|
} catch (ExecutionException ee) {
|
|
|
|
t = ee.getCause();
|
|
|
|
} catch (InterruptedException ie) {
|
|
|
|
Thread.currentThread().interrupt();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (t != null) {
|
|
|
|
t.printStackTrace();
|
|
|
|
}
|
|
|
|
} catch (Throwable e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2019-05-01 15:45:18 +00:00
|
|
|
}
|
2018-08-12 14:03:07 +00:00
|
|
|
}
|