2021-07-01 20:16:25 +00:00
|
|
|
package com.fastasyncworldedit.core;
|
2020-01-29 20:01:38 +00:00
|
|
|
|
2021-07-01 20:16:25 +00:00
|
|
|
import com.fastasyncworldedit.core.configuration.Caption;
|
|
|
|
import com.fastasyncworldedit.core.configuration.Settings;
|
2021-07-23 15:48:51 +00:00
|
|
|
import com.fastasyncworldedit.core.internal.exception.FaweBlockBagException;
|
|
|
|
import com.fastasyncworldedit.core.internal.exception.FaweException;
|
2021-09-01 14:36:03 +00:00
|
|
|
import com.fastasyncworldedit.core.internal.exception.FaweException.Type;
|
2021-07-24 15:34:05 +00:00
|
|
|
import com.fastasyncworldedit.core.math.BitArray;
|
|
|
|
import com.fastasyncworldedit.core.math.BitArrayUnstretched;
|
|
|
|
import com.fastasyncworldedit.core.math.MutableBlockVector3;
|
|
|
|
import com.fastasyncworldedit.core.math.MutableVector3;
|
|
|
|
import com.fastasyncworldedit.core.queue.IChunkSet;
|
|
|
|
import com.fastasyncworldedit.core.queue.Pool;
|
|
|
|
import com.fastasyncworldedit.core.queue.Trimable;
|
|
|
|
import com.fastasyncworldedit.core.queue.implementation.QueuePool;
|
2021-07-01 20:16:25 +00:00
|
|
|
import com.fastasyncworldedit.core.util.MathMan;
|
2021-07-24 15:34:05 +00:00
|
|
|
import com.fastasyncworldedit.core.util.collection.CleanableThreadLocal;
|
2020-01-29 20:01:38 +00:00
|
|
|
import com.google.common.cache.CacheBuilder;
|
|
|
|
import com.google.common.cache.CacheLoader;
|
|
|
|
import com.google.common.cache.LoadingCache;
|
|
|
|
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;
|
2021-03-29 13:29:16 +00:00
|
|
|
import com.sk89q.worldedit.internal.util.LogManagerCompat;
|
2020-01-29 20:01:38 +00:00
|
|
|
import com.sk89q.worldedit.world.block.BlockTypesCache;
|
2021-03-29 13:29:16 +00:00
|
|
|
import org.apache.logging.log4j.Logger;
|
2020-07-14 02:50:59 +00:00
|
|
|
|
2021-07-24 15:34:05 +00:00
|
|
|
import javax.annotation.Nonnull;
|
2020-01-29 20:01:38 +00:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Arrays;
|
|
|
|
import java.util.Collection;
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.IdentityHashMap;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Map;
|
2020-03-05 21:43:26 +00:00
|
|
|
import java.util.Map.Entry;
|
2020-01-29 20:01:38 +00:00
|
|
|
import java.util.concurrent.ArrayBlockingQueue;
|
|
|
|
import java.util.concurrent.CancellationException;
|
|
|
|
import java.util.concurrent.ExecutionException;
|
|
|
|
import java.util.concurrent.Executors;
|
|
|
|
import java.util.concurrent.Future;
|
|
|
|
import java.util.concurrent.ThreadPoolExecutor;
|
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
import java.util.concurrent.atomic.AtomicBoolean;
|
|
|
|
import java.util.function.Function;
|
|
|
|
import java.util.function.Supplier;
|
2020-07-14 02:50:59 +00:00
|
|
|
|
|
|
|
import static com.google.common.base.Preconditions.checkNotNull;
|
2020-01-29 20:01:38 +00:00
|
|
|
|
|
|
|
public enum FaweCache implements Trimable {
|
2021-07-24 15:34:05 +00:00
|
|
|
IMP; // singleton
|
2020-01-29 20:01:38 +00:00
|
|
|
|
2021-03-29 13:29:16 +00:00
|
|
|
private static final Logger LOGGER = LogManagerCompat.getLogger();
|
|
|
|
|
2020-01-29 20:01:38 +00:00
|
|
|
public final int BLOCKS_PER_LAYER = 4096;
|
|
|
|
|
|
|
|
public final char[] EMPTY_CHAR_4096 = new char[4096];
|
|
|
|
|
2020-03-05 21:43:26 +00:00
|
|
|
private final IdentityHashMap<Class<? extends IChunkSet>, Pool<? extends IChunkSet>> REGISTERED_POOLS = new IdentityHashMap<>();
|
2020-01-29 20:01:38 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
Palette buffers / cache
|
|
|
|
*/
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public synchronized boolean trim(boolean aggressive) {
|
2021-05-28 22:47:46 +00:00
|
|
|
CHUNK_FLAG.clean();
|
|
|
|
BYTE_BUFFER_8192.clean();
|
|
|
|
BLOCK_TO_PALETTE.clean();
|
|
|
|
PALETTE_TO_BLOCK.clean();
|
|
|
|
BLOCK_STATES.clean();
|
|
|
|
SECTION_BLOCKS.clean();
|
|
|
|
PALETTE_CACHE.clean();
|
|
|
|
PALETTE_TO_BLOCK_CHAR.clean();
|
|
|
|
INDEX_STORE.clean();
|
|
|
|
|
|
|
|
MUTABLE_VECTOR3.clean();
|
|
|
|
MUTABLE_BLOCKVECTOR3.clean();
|
|
|
|
SECTION_BITS_TO_CHAR.clean();
|
2020-03-05 21:43:26 +00:00
|
|
|
for (Entry<Class<? extends IChunkSet>, Pool<? extends IChunkSet>> entry : REGISTERED_POOLS.entrySet()) {
|
|
|
|
Pool<? extends IChunkSet> pool = entry.getValue();
|
2020-01-29 20:01:38 +00:00
|
|
|
pool.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-03-05 21:43:26 +00:00
|
|
|
public synchronized <T extends IChunkSet> Pool<T> registerPool(Class<T> clazz, Supplier<T> cache, boolean buffer) {
|
2020-01-29 20:01:38 +00:00
|
|
|
checkNotNull(cache);
|
|
|
|
Pool<T> pool;
|
|
|
|
if (buffer) {
|
|
|
|
pool = new QueuePool<>(cache);
|
|
|
|
} else {
|
|
|
|
pool = cache::get;
|
|
|
|
}
|
2020-03-05 21:43:26 +00:00
|
|
|
Pool<? extends IChunkSet> previous = REGISTERED_POOLS.putIfAbsent(clazz, pool);
|
2020-01-29 20:01:38 +00:00
|
|
|
if (previous != null) {
|
|
|
|
throw new IllegalStateException("Previous key");
|
|
|
|
}
|
|
|
|
return pool;
|
|
|
|
}
|
|
|
|
|
|
|
|
public <T, V> LoadingCache<T, V> createCache(Supplier<V> withInitial) {
|
|
|
|
return CacheBuilder.newBuilder().build(new CacheLoader<T, V>() {
|
|
|
|
@Override
|
2021-07-24 13:52:08 +00:00
|
|
|
public V load(@Nonnull T key) {
|
2020-01-29 20:01:38 +00:00
|
|
|
return withInitial.get();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public <T, V> LoadingCache<T, V> createCache(Function<T, V> withInitial) {
|
|
|
|
return CacheBuilder.newBuilder().build(new CacheLoader<T, V>() {
|
|
|
|
@Override
|
2021-07-24 13:52:08 +00:00
|
|
|
public V load(@Nonnull T key) {
|
2020-01-29 20:01:38 +00:00
|
|
|
return withInitial.apply(key);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
Exceptions
|
|
|
|
*/
|
|
|
|
public static final FaweBlockBagException BLOCK_BAG = new FaweBlockBagException();
|
2021-09-01 14:36:03 +00:00
|
|
|
public static final FaweException MANUAL = new FaweException(
|
2021-10-04 12:34:40 +00:00
|
|
|
Caption.of("fawe.cancel.reason.manual"),
|
2021-09-01 14:36:03 +00:00
|
|
|
Type.MANUAL
|
|
|
|
);
|
|
|
|
public static final FaweException NO_REGION = new FaweException(
|
2021-10-04 12:34:40 +00:00
|
|
|
Caption.of("fawe.cancel.reason.no.region"),
|
2021-09-01 14:36:03 +00:00
|
|
|
Type.NO_REGION
|
|
|
|
);
|
2021-09-08 15:39:43 +00:00
|
|
|
public static final FaweException OUTSIDE_REGION = new FaweException(
|
|
|
|
Caption.of(
|
2021-10-04 12:34:40 +00:00
|
|
|
"fawe.cancel.reason.outside.region"),
|
2021-09-08 15:39:43 +00:00
|
|
|
Type.OUTSIDE_REGION
|
|
|
|
);
|
2021-09-01 14:36:03 +00:00
|
|
|
public static final FaweException MAX_CHECKS = new FaweException(
|
2021-10-04 12:34:40 +00:00
|
|
|
Caption.of("fawe.cancel.reason.max" + ".checks"),
|
2021-09-01 14:36:03 +00:00
|
|
|
Type.MAX_CHECKS
|
|
|
|
);
|
|
|
|
public static final FaweException MAX_CHANGES = new FaweException(
|
2021-10-04 12:34:40 +00:00
|
|
|
Caption.of("fawe.cancel.reason.max" + ".changes"),
|
2021-09-01 14:36:03 +00:00
|
|
|
Type.MAX_CHANGES
|
|
|
|
);
|
|
|
|
public static final FaweException LOW_MEMORY = new FaweException(
|
2021-10-04 12:34:40 +00:00
|
|
|
Caption.of("fawe.cancel.reason.low" + ".memory"),
|
2021-09-01 14:36:03 +00:00
|
|
|
Type.LOW_MEMORY
|
|
|
|
);
|
2021-09-08 15:39:43 +00:00
|
|
|
public static final FaweException MAX_ENTITIES = new FaweException(
|
|
|
|
Caption.of(
|
2021-10-04 12:34:40 +00:00
|
|
|
"fawe.cancel.reason.max.entities"),
|
2021-09-08 15:39:43 +00:00
|
|
|
Type.MAX_ENTITIES
|
|
|
|
);
|
2021-09-01 14:36:03 +00:00
|
|
|
public static final FaweException MAX_TILES = new FaweException(Caption.of(
|
2021-10-04 12:34:40 +00:00
|
|
|
"fawe.cancel.reason.max.tiles",
|
2021-09-01 14:36:03 +00:00
|
|
|
Type.MAX_TILES
|
|
|
|
));
|
2021-09-08 15:39:43 +00:00
|
|
|
public static final FaweException MAX_ITERATIONS = new FaweException(
|
|
|
|
Caption.of(
|
2021-10-04 12:34:40 +00:00
|
|
|
"fawe.cancel.reason.max.iterations"),
|
2021-09-08 15:39:43 +00:00
|
|
|
Type.MAX_ITERATIONS
|
|
|
|
);
|
2021-09-19 20:02:41 +00:00
|
|
|
public static final FaweException PLAYER_ONLY = new FaweException(
|
|
|
|
Caption.of(
|
2021-10-04 12:34:40 +00:00
|
|
|
"fawe.cancel.reason.player-only"),
|
2021-09-19 20:02:41 +00:00
|
|
|
Type.PLAYER_ONLY
|
|
|
|
);
|
|
|
|
public static final FaweException ACTOR_REQUIRED = new FaweException(
|
|
|
|
Caption.of(
|
2021-10-04 12:34:40 +00:00
|
|
|
"fawe.cancel.reason.actor-required"),
|
2021-09-19 20:02:41 +00:00
|
|
|
Type.ACTOR_REQUIRED
|
|
|
|
);
|
2020-01-29 20:01:38 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
thread cache
|
|
|
|
*/
|
|
|
|
public final CleanableThreadLocal<AtomicBoolean> CHUNK_FLAG = new CleanableThreadLocal<>(AtomicBoolean::new); // resets to false
|
|
|
|
|
|
|
|
|
|
|
|
public final CleanableThreadLocal<byte[]> BYTE_BUFFER_8192 = new CleanableThreadLocal<>(() -> new byte[8192]);
|
|
|
|
|
|
|
|
public final CleanableThreadLocal<int[]> BLOCK_TO_PALETTE = new CleanableThreadLocal<>(() -> {
|
|
|
|
int[] result = new int[BlockTypesCache.states.length];
|
|
|
|
Arrays.fill(result, Integer.MAX_VALUE);
|
|
|
|
return result;
|
|
|
|
});
|
|
|
|
|
|
|
|
public final CleanableThreadLocal<char[]> SECTION_BITS_TO_CHAR = new CleanableThreadLocal<>(() -> new char[4096]);
|
|
|
|
|
|
|
|
public final CleanableThreadLocal<int[]> PALETTE_TO_BLOCK = new CleanableThreadLocal<>(() -> new int[Character.MAX_VALUE + 1]);
|
|
|
|
|
|
|
|
public final CleanableThreadLocal<char[]> PALETTE_TO_BLOCK_CHAR = new CleanableThreadLocal<>(
|
2021-07-24 15:34:05 +00:00
|
|
|
() -> new char[Character.MAX_VALUE + 1], a -> {
|
|
|
|
Arrays.fill(a, Character.MAX_VALUE);
|
|
|
|
}
|
2020-01-29 20:01:38 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
public final CleanableThreadLocal<long[]> BLOCK_STATES = new CleanableThreadLocal<>(() -> new long[2048]);
|
|
|
|
|
|
|
|
public final CleanableThreadLocal<int[]> SECTION_BLOCKS = new CleanableThreadLocal<>(() -> new int[4096]);
|
|
|
|
|
|
|
|
public final CleanableThreadLocal<int[]> INDEX_STORE = new CleanableThreadLocal<>(() -> new int[256]);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Holds data for a palette used in a chunk section
|
|
|
|
*/
|
|
|
|
public static final class Palette {
|
2021-07-24 15:34:05 +00:00
|
|
|
|
2020-01-29 20:01:38 +00:00
|
|
|
public int bitsPerEntry;
|
|
|
|
|
|
|
|
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;
|
2021-07-24 15:34:05 +00:00
|
|
|
|
2020-01-29 20:01:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private final CleanableThreadLocal<Palette> PALETTE_CACHE = new CleanableThreadLocal<>(Palette::new);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert raw char array to palette
|
2021-07-24 15:34:05 +00:00
|
|
|
*
|
2020-01-29 20:01:38 +00:00
|
|
|
* @param layerOffset
|
|
|
|
* @param blocks
|
|
|
|
* @return palette
|
|
|
|
*/
|
|
|
|
public Palette toPalette(int layerOffset, char[] blocks) {
|
|
|
|
return toPalette(layerOffset, null, blocks);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert raw int array to palette
|
2021-07-24 15:34:05 +00:00
|
|
|
*
|
2020-01-29 20:01:38 +00:00
|
|
|
* @param layerOffset
|
|
|
|
* @param blocks
|
|
|
|
* @return palette
|
|
|
|
*/
|
|
|
|
public Palette toPalette(int layerOffset, int[] blocks) {
|
|
|
|
return toPalette(layerOffset, blocks, null);
|
|
|
|
}
|
|
|
|
|
|
|
|
private Palette toPalette(int layerOffset, 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();
|
|
|
|
|
|
|
|
try {
|
2020-03-05 21:07:20 +00:00
|
|
|
int num_palette = 0;
|
|
|
|
int blockIndexStart = layerOffset << 12;
|
|
|
|
int blockIndexEnd = blockIndexStart + 4096;
|
2020-01-29 20:01:38 +00:00
|
|
|
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) {
|
|
|
|
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 = BlockTypesCache.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);
|
|
|
|
if (Settings.IMP.PROTOCOL_SUPPORT_FIX || num_palette != 1) {
|
|
|
|
bitsPerEntry = Math.max(bitsPerEntry, 4); // Protocol support breaks <4 bits per entry
|
|
|
|
} else {
|
|
|
|
bitsPerEntry = Math.max(bitsPerEntry, 1); // For some reason minecraft needs 4096 bits to store 0 entries
|
|
|
|
}
|
|
|
|
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 {
|
2020-04-30 20:26:52 +00:00
|
|
|
BitArray bitArray = new BitArray(bitsPerEntry, 4096, blockStates);
|
2020-01-29 20:01:38 +00:00
|
|
|
bitArray.fromRaw(blocksCopy);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Construct palette
|
|
|
|
Palette palette = PALETTE_CACHE.get();
|
|
|
|
palette.bitsPerEntry = bitsPerEntry;
|
|
|
|
palette.paletteToBlockLength = num_palette;
|
|
|
|
palette.paletteToBlock = paletteToBlock;
|
2020-07-01 11:41:20 +00:00
|
|
|
|
|
|
|
palette.blockStatesLength = blockBitArrayEnd;
|
|
|
|
palette.blockStates = blockStates;
|
|
|
|
|
|
|
|
return palette;
|
|
|
|
} catch (Throwable e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
Arrays.fill(blockToPalette, Integer.MAX_VALUE);
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert raw int array to unstretched palette (1.16)
|
2021-07-24 15:34:05 +00:00
|
|
|
*
|
2020-07-01 11:41:20 +00:00
|
|
|
* @param layerOffset
|
|
|
|
* @param blocks
|
|
|
|
* @return palette
|
|
|
|
*/
|
|
|
|
public Palette toPaletteUnstretched(int layerOffset, char[] blocks) {
|
|
|
|
return toPaletteUnstretched(layerOffset, null, blocks);
|
|
|
|
}
|
|
|
|
|
|
|
|
private Palette toPaletteUnstretched(int layerOffset, 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();
|
|
|
|
|
|
|
|
try {
|
|
|
|
int num_palette = 0;
|
|
|
|
int blockIndexStart = layerOffset << 12;
|
|
|
|
int blockIndexEnd = blockIndexStart + 4096;
|
|
|
|
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) {
|
|
|
|
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 = BlockTypesCache.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);
|
|
|
|
if (Settings.IMP.PROTOCOL_SUPPORT_FIX || num_palette != 1) {
|
|
|
|
bitsPerEntry = Math.max(bitsPerEntry, 4); // Protocol support breaks <4 bits per entry
|
|
|
|
} else {
|
|
|
|
bitsPerEntry = Math.max(bitsPerEntry, 1); // For some reason minecraft needs 4096 bits to store 0 entries
|
|
|
|
}
|
|
|
|
int blocksPerLong = MathMan.floorZero((double) 64 / bitsPerEntry);
|
|
|
|
int blockBitArrayEnd = MathMan.ceilZero((float) 4096 / blocksPerLong);
|
|
|
|
if (num_palette == 1) {
|
|
|
|
// Set a value, because minecraft needs it for some reason
|
|
|
|
blockStates[0] = 0;
|
|
|
|
blockBitArrayEnd = 1;
|
|
|
|
} else {
|
2020-09-14 13:19:23 +00:00
|
|
|
BitArrayUnstretched bitArray = new BitArrayUnstretched(bitsPerEntry, 4096, blockStates);
|
2020-07-01 11:41:20 +00:00
|
|
|
bitArray.fromRaw(blocksCopy);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Construct palette
|
|
|
|
Palette palette = PALETTE_CACHE.get();
|
|
|
|
palette.bitsPerEntry = bitsPerEntry;
|
|
|
|
palette.paletteToBlockLength = num_palette;
|
|
|
|
palette.paletteToBlock = paletteToBlock;
|
2020-01-29 20:01:38 +00:00
|
|
|
|
|
|
|
palette.blockStatesLength = blockBitArrayEnd;
|
|
|
|
palette.blockStates = blockStates;
|
|
|
|
|
|
|
|
return palette;
|
|
|
|
} catch (Throwable e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
Arrays.fill(blockToPalette, Integer.MAX_VALUE);
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Vector cache
|
|
|
|
*/
|
|
|
|
|
|
|
|
public CleanableThreadLocal<MutableBlockVector3> MUTABLE_BLOCKVECTOR3 = new CleanableThreadLocal<>(MutableBlockVector3::new);
|
|
|
|
|
|
|
|
public CleanableThreadLocal<MutableVector3> MUTABLE_VECTOR3 = new CleanableThreadLocal<MutableVector3>(MutableVector3::new) {
|
|
|
|
@Override
|
|
|
|
public MutableVector3 init() {
|
|
|
|
return new MutableVector3();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
Conversion methods between JNBT tags and raw values
|
|
|
|
*/
|
|
|
|
public Map<String, Object> asMap(Object... pairs) {
|
|
|
|
HashMap<String, Object> map = new HashMap<>(pairs.length >> 1);
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
public ShortTag asTag(short value) {
|
|
|
|
return new ShortTag(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
public IntTag asTag(int value) {
|
|
|
|
return new IntTag(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
public DoubleTag asTag(double value) {
|
|
|
|
return new DoubleTag(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
public ByteTag asTag(byte value) {
|
|
|
|
return new ByteTag(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
public FloatTag asTag(float value) {
|
|
|
|
return new FloatTag(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
public LongTag asTag(long value) {
|
|
|
|
return new LongTag(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
public ByteArrayTag asTag(byte[] value) {
|
|
|
|
return new ByteArrayTag(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
public IntArrayTag asTag(int[] value) {
|
|
|
|
return new IntArrayTag(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
public LongArrayTag asTag(long[] value) {
|
|
|
|
return new LongArrayTag(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
public StringTag asTag(String value) {
|
|
|
|
return new StringTag(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
public CompoundTag asTag(Map<String, Object> value) {
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
public Tag asTag(Object value) {
|
|
|
|
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) {
|
|
|
|
return asTag((Map<String, Object>) value);
|
|
|
|
} 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));
|
|
|
|
}
|
2021-03-29 13:29:16 +00:00
|
|
|
LOGGER.error("Invalid nbt: {}", value);
|
2020-01-29 20:01:38 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
public ListTag asTag(Object... values) {
|
|
|
|
Class<? extends Tag> clazz = null;
|
|
|
|
List<Tag> list = new ArrayList<>(values.length);
|
|
|
|
for (Object value : values) {
|
|
|
|
Tag tag = asTag(value);
|
|
|
|
if (clazz == null) {
|
|
|
|
clazz = tag.getClass();
|
|
|
|
}
|
|
|
|
list.add(tag);
|
|
|
|
}
|
2020-10-05 17:41:41 +00:00
|
|
|
if (clazz == null) {
|
|
|
|
clazz = EndTag.class;
|
|
|
|
}
|
2020-01-29 20:01:38 +00:00
|
|
|
return new ListTag(clazz, list);
|
|
|
|
}
|
|
|
|
|
|
|
|
public ListTag asTag(Collection values) {
|
|
|
|
Class<? extends Tag> clazz = null;
|
|
|
|
List<Tag> list = new ArrayList<>(values.size());
|
|
|
|
for (Object value : values) {
|
|
|
|
Tag tag = asTag(value);
|
|
|
|
if (clazz == null) {
|
|
|
|
clazz = tag.getClass();
|
|
|
|
}
|
|
|
|
list.add(tag);
|
|
|
|
}
|
2020-10-05 17:41:41 +00:00
|
|
|
if (clazz == null) {
|
|
|
|
clazz = EndTag.class;
|
|
|
|
}
|
2020-01-29 20:01:38 +00:00
|
|
|
return new ListTag(clazz, list);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
Thread stuff
|
|
|
|
*/
|
|
|
|
public ThreadPoolExecutor newBlockingExecutor() {
|
|
|
|
int nThreads = Settings.IMP.QUEUE.PARALLEL_THREADS;
|
2021-09-12 10:36:36 +00:00
|
|
|
ArrayBlockingQueue<Runnable> queue = new ArrayBlockingQueue<>(nThreads, true);
|
2020-01-29 20:01:38 +00:00
|
|
|
return new ThreadPoolExecutor(nThreads, nThreads,
|
2021-07-24 15:34:05 +00:00
|
|
|
0L, TimeUnit.MILLISECONDS, queue,
|
|
|
|
Executors.defaultThreadFactory(),
|
|
|
|
new ThreadPoolExecutor.CallerRunsPolicy()
|
|
|
|
) {
|
2021-09-01 14:36:03 +00:00
|
|
|
|
|
|
|
// Array for lazy avoidance of concurrent modification exceptions and needless overcomplication of code (synchronisation is
|
|
|
|
// not very important)
|
|
|
|
private final boolean[] faweExceptionReasonsUsed = new boolean[FaweException.Type.values().length];
|
|
|
|
private int lastException = Integer.MIN_VALUE;
|
|
|
|
private int count = 0;
|
|
|
|
|
|
|
|
protected synchronized void afterExecute(Runnable runnable, Throwable throwable) {
|
|
|
|
super.afterExecute(runnable, throwable);
|
|
|
|
if (throwable == null && runnable instanceof Future<?>) {
|
|
|
|
try {
|
|
|
|
Future<?> future = (Future<?>) runnable;
|
|
|
|
if (future.isDone()) {
|
|
|
|
future.get();
|
2020-01-29 20:01:38 +00:00
|
|
|
}
|
2021-09-01 14:36:03 +00:00
|
|
|
} catch (CancellationException ce) {
|
|
|
|
throwable = ce;
|
|
|
|
} catch (ExecutionException ee) {
|
|
|
|
throwable = ee.getCause();
|
|
|
|
} catch (InterruptedException ie) {
|
|
|
|
Thread.currentThread().interrupt();
|
2020-01-29 20:01:38 +00:00
|
|
|
}
|
2021-09-01 14:36:03 +00:00
|
|
|
}
|
|
|
|
if (throwable != null) {
|
|
|
|
if (throwable instanceof FaweException) {
|
|
|
|
handleFaweException((FaweException) throwable);
|
|
|
|
} else if (throwable.getCause() instanceof FaweException) {
|
|
|
|
handleFaweException((FaweException) throwable.getCause());
|
|
|
|
} else {
|
|
|
|
int hash = throwable.getMessage().hashCode();
|
|
|
|
if (hash != lastException) {
|
|
|
|
lastException = hash;
|
|
|
|
LOGGER.catching(throwable);
|
|
|
|
count = 0;
|
|
|
|
} else if (count < Settings.IMP.QUEUE.PARALLEL_THREADS) {
|
|
|
|
LOGGER.warn(throwable.getMessage());
|
|
|
|
count++;
|
|
|
|
}
|
2020-01-29 20:01:38 +00:00
|
|
|
}
|
2021-09-01 14:36:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void handleFaweException(FaweException e) {
|
|
|
|
FaweException.Type type = e.getType();
|
|
|
|
if (e.getType() == FaweException.Type.OTHER) {
|
|
|
|
LOGGER.catching(e);
|
|
|
|
} else if (!faweExceptionReasonsUsed[type.ordinal()]) {
|
|
|
|
faweExceptionReasonsUsed[type.ordinal()] = true;
|
|
|
|
LOGGER.warn("FaweException: " + e.getMessage());
|
2020-01-29 20:01:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|