From 14d2c9a5c3393fa9d27bb68004f7c4168283874b Mon Sep 17 00:00:00 2001 From: MattBDev <4009945+MattBDev@users.noreply.github.com> Date: Tue, 10 Mar 2020 17:22:53 -0400 Subject: [PATCH] Update BlockStateIdAccess and added a debug message --- .../mc1_15_2/BukkitGetBlocks_1_15_2.java | 14 +++- .../internal/block/BlockStateIdAccess.java | 71 ++++++++++++------- 2 files changed, 59 insertions(+), 26 deletions(-) diff --git a/worldedit-bukkit/src/main/java/com/boydti/fawe/bukkit/adapter/mc1_15_2/BukkitGetBlocks_1_15_2.java b/worldedit-bukkit/src/main/java/com/boydti/fawe/bukkit/adapter/mc1_15_2/BukkitGetBlocks_1_15_2.java index 334941957..cc6c4bae1 100644 --- a/worldedit-bukkit/src/main/java/com/boydti/fawe/bukkit/adapter/mc1_15_2/BukkitGetBlocks_1_15_2.java +++ b/worldedit-bukkit/src/main/java/com/boydti/fawe/bukkit/adapter/mc1_15_2/BukkitGetBlocks_1_15_2.java @@ -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 posNms2We = v -> BlockVector3.at(v.getX(), v.getY(), v.getZ()); private final static Function 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(); diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/internal/block/BlockStateIdAccess.java b/worldedit-core/src/main/java/com/sk89q/worldedit/internal/block/BlockStateIdAccess.java index 8a3d988f9..0066aeb4f 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/internal/block/BlockStateIdAccess.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/internal/block/BlockStateIdAccess.java @@ -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 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 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); }