Update BlockStateIdAccess and added a debug message

This commit is contained in:
MattBDev 2020-03-10 17:22:53 -04:00
parent ef6a5fd442
commit 14d2c9a5c3
2 changed files with 59 additions and 26 deletions

View File

@ -36,10 +36,15 @@ import java.util.*;
import java.util.concurrent.Callable;
import java.util.concurrent.Future;
import java.util.function.Function;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import static org.slf4j.LoggerFactory.getLogger;
public class BukkitGetBlocks_1_15_2 extends CharGetBlocks {
private static final Logger log = LoggerFactory.getLogger(BukkitGetBlocks_1_15_2.class);
private static final Function<BlockPosition, BlockVector3> posNms2We = v -> BlockVector3.at(v.getX(), v.getY(), v.getZ());
private final static Function<TileEntity, CompoundTag> nmsTile2We = tileEntity -> new LazyCompoundTag_1_15_2(Suppliers.memoize(() -> tileEntity.save(new NBTTagCompound())));
public ChunkSection[] sections;
@ -225,7 +230,14 @@ public class BukkitGetBlocks_1_15_2 extends CharGetBlocks {
if (!set.hasSection(layer)) {
continue;
}
if (set.getBlock(lx, ly, lz).getOrdinal() != 0) {
int ordinal = set.getBlock(lx, ly, lz).getOrdinal();
if (log.isInfoEnabled()) {
log.info("ordinal: " + ordinal + "NBT: " + set.getBlock(lx, ly, lz).hasNbtData());
} else {
log.error("ordinal: " + ordinal + "NBT: " + set.getBlock(lx, ly, lz).hasNbtData());
}
if (ordinal != 0) {
TileEntity tile = entry.getValue();
tile.hasWorld();
tile.invalidateBlockCache();

View File

@ -19,28 +19,50 @@
package com.sk89q.worldedit.internal.block;
import com.google.common.collect.BiMap;
import com.google.common.collect.HashBiMap;
import com.sk89q.worldedit.world.block.BlockState;
import com.sk89q.worldedit.world.registry.BlockRegistry;
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
import javax.annotation.Nullable;
import java.util.BitSet;
import java.util.OptionalInt;
import static com.google.common.base.Preconditions.checkState;
public final class BlockStateIdAccess {
/*
private static final BiMap<BlockState, Integer> ASSIGNED_IDS = HashBiMap.create(2 << 13);
public static OptionalInt getBlockStateId(BlockState holder) {
Integer value = ASSIGNED_IDS.get(holder);
return value == null ? OptionalInt.empty() : OptionalInt.of(value);
private static final int INVALID_ID = -1;
private static final int EXPECTED_BLOCK_COUNT = 2 << 13;
private static final Int2ObjectOpenHashMap<BlockState> TO_STATE =
new Int2ObjectOpenHashMap<>(EXPECTED_BLOCK_COUNT);
static {
TO_STATE.defaultReturnValue(null);
}
public interface BlockStateInternalId {
int getInternalId(BlockState blockState);
void setInternalId(BlockState blockState, int internalId);
}
private static BlockStateInternalId blockStateInternalId;
public static void setBlockStateInternalId(BlockStateInternalId blockStateInternalId) {
BlockStateIdAccess.blockStateInternalId = blockStateInternalId;
}
/**
* @return an internal ID which is never valid
*/
public static int invalidId() {
return INVALID_ID;
}
public static boolean isValidInternalId(int internalId) {
return internalId != INVALID_ID;
}
public static int getBlockStateId(BlockState holder) {
return holder.getOrdinal();
//return blockStateInternalId.getInternalId(holder);
}
/*
public static @Nullable BlockState getBlockStateById(int id) {
return ASSIGNED_IDS.inverse().get(id);
return TO_STATE.get(id);
}
/**
@ -56,28 +78,27 @@ public final class BlockStateIdAccess {
private static final BitSet usedIds = new BitSet();
public static void register(BlockState blockState, OptionalInt id) {
int i = id.orElseGet(BlockStateIdAccess::provideUnusedWorldEditId);
BlockState existing = ASSIGNED_IDS.inverse().get(i);
public static void register(BlockState blockState, int id) {
int i = isValidInternalId(id) ? id : provideUnusedWorldEditId();
BlockState existing = getBlockStateById(id);
checkState(existing == null || existing == blockState,
"BlockState %s is using the same block ID (%s) as BlockState %s",
blockState, i, existing);
ASSIGNED_IDS.put(blockState, i);
blockStateInternalId.setInternalId(blockState, i);
TO_STATE.put(i, blockState);
usedIds.set(i);
}
public static void clear() {
ASSIGNED_IDS.clear();
for (BlockState value : TO_STATE.values()) {
blockStateInternalId.setInternalId(value, invalidId());
}
TO_STATE.clear();
usedIds.clear();
}
private BlockStateIdAccess() {
}
*/
public static OptionalInt getBlockStateId(BlockState holder) {
return OptionalInt.of(holder.getOrdinal());
}
public static @Nullable BlockState getBlockStateById(int id) {
return BlockState.getFromOrdinal(id);
}