From dc21b4df58111d3d276ffb2851441757d2335627 Mon Sep 17 00:00:00 2001 From: wizjany Date: Sun, 12 May 2019 14:19:46 -0400 Subject: [PATCH] This does something idk. --- .../worldedit/bukkit/BukkitBlockRegistry.java | 31 ++++++++++++++++--- .../sk89q/worldedit/bukkit/BukkitWorld.java | 14 +++++++-- .../sk89q/worldedit/regions/CuboidRegion.java | 6 ++-- .../worldedit/world/block/BlockState.java | 5 +-- .../world/registry/BlockRegistry.java | 7 +++++ .../world/registry/BundledBlockRegistry.java | 5 +++ 6 files changed, 57 insertions(+), 11 deletions(-) diff --git a/worldedit-bukkit/src/main/java/com/sk89q/worldedit/bukkit/BukkitBlockRegistry.java b/worldedit-bukkit/src/main/java/com/sk89q/worldedit/bukkit/BukkitBlockRegistry.java index 43c3f330d..79690df26 100644 --- a/worldedit-bukkit/src/main/java/com/sk89q/worldedit/bukkit/BukkitBlockRegistry.java +++ b/worldedit-bukkit/src/main/java/com/sk89q/worldedit/bukkit/BukkitBlockRegistry.java @@ -19,6 +19,7 @@ package com.sk89q.worldedit.bukkit; +import com.sk89q.worldedit.world.block.BlockTypes; import com.sk89q.worldedit.world.registry.BlockMaterial; import com.sk89q.worldedit.registry.state.Property; import com.sk89q.worldedit.world.block.BlockState; @@ -34,8 +35,8 @@ import java.util.OptionalInt; import javax.annotation.Nullable; public class BukkitBlockRegistry extends BundledBlockRegistry { - private Map materialMap = new EnumMap<>(Material.class); + private BlockState[] statesById = new BlockState[2 << 14]; @Nullable @Override @@ -58,10 +59,32 @@ public class BukkitBlockRegistry extends BundledBlockRegistry { @Override public OptionalInt getInternalBlockStateId(BlockState state) { - if (WorldEditPlugin.getInstance().getBukkitImplAdapter() != null) { - return WorldEditPlugin.getInstance().getBukkitImplAdapter().getInternalBlockStateId(state); + if (state.getBlockType() == BlockTypes.AIR) { + statesById[0] = state; + return OptionalInt.of(0); } - return super.getInternalBlockStateId(state); + final OptionalInt id; + if (WorldEditPlugin.getInstance().getBukkitImplAdapter() != null) { + id = WorldEditPlugin.getInstance().getBukkitImplAdapter().getInternalBlockStateId(state); + } else { + id = super.getInternalBlockStateId(state); + } + if (id.isPresent()) { + final int idx = id.getAsInt(); + if (statesById.length <= idx) { + BlockState[] newArr = new BlockState[statesById.length * 2]; + System.arraycopy(statesById, 0, newArr, 0, statesById.length); + statesById = newArr; + } + statesById[idx] = state; + } + return id; + } + + @Nullable + @Override + public BlockState getBlockStateByInternalId(int id) { + return id >= statesById.length ? null : statesById[id]; } public static class BukkitBlockMaterial extends PassthroughBlockMaterial { diff --git a/worldedit-bukkit/src/main/java/com/sk89q/worldedit/bukkit/BukkitWorld.java b/worldedit-bukkit/src/main/java/com/sk89q/worldedit/bukkit/BukkitWorld.java index cd12e220c..c8d8f6259 100644 --- a/worldedit-bukkit/src/main/java/com/sk89q/worldedit/bukkit/BukkitWorld.java +++ b/worldedit-bukkit/src/main/java/com/sk89q/worldedit/bukkit/BukkitWorld.java @@ -415,8 +415,18 @@ public class BukkitWorld extends AbstractWorld { @Override public com.sk89q.worldedit.world.block.BlockState getBlock(BlockVector3 position) { - Block bukkitBlock = getWorld().getBlockAt(position.getBlockX(), position.getBlockY(), position.getBlockZ()); - return BukkitAdapter.adapt(bukkitBlock.getBlockData()); + BukkitImplAdapter adapter = WorldEditPlugin.getInstance().getBukkitImplAdapter(); + if (adapter != null) { + try { + return adapter.getBlock(BukkitAdapter.adapt(getWorld(), position)).toImmutableState(); + } catch (Exception e) { + Block bukkitBlock = getWorld().getBlockAt(position.getBlockX(), position.getBlockY(), position.getBlockZ()); + return BukkitAdapter.adapt(bukkitBlock.getBlockData()); + } + } else { + Block bukkitBlock = getWorld().getBlockAt(position.getBlockX(), position.getBlockY(), position.getBlockZ()); + return BukkitAdapter.adapt(bukkitBlock.getBlockData()); + } } @Override diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/regions/CuboidRegion.java b/worldedit-core/src/main/java/com/sk89q/worldedit/regions/CuboidRegion.java index e011228d0..3cfddc15d 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/regions/CuboidRegion.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/regions/CuboidRegion.java @@ -345,9 +345,9 @@ public class CuboidRegion extends AbstractRegion implements FlatRegion { BlockVector3 answer = BlockVector3.at(nextX, nextY, nextZ); if (++nextX > max.getBlockX()) { nextX = min.getBlockX(); - if (++nextY > max.getBlockY()) { - nextY = min.getBlockY(); - if (++nextZ > max.getBlockZ()) { + if (++nextZ > max.getBlockZ()) { + nextZ = min.getBlockZ(); + if (++nextY > max.getBlockY()) { nextX = Integer.MIN_VALUE; } } diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/world/block/BlockState.java b/worldedit-core/src/main/java/com/sk89q/worldedit/world/block/BlockState.java index 7463fefb1..98c5966c7 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/world/block/BlockState.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/world/block/BlockState.java @@ -46,7 +46,7 @@ import java.util.Set; */ @SuppressWarnings("unchecked") public class BlockState implements BlockStateHolder { - + static { BlockStateIdAccess.setBlockStateStateId(x -> x.internalId); } @@ -86,13 +86,14 @@ public class BlockState implements BlockStateHolder { List> valueLists = Lists.cartesianProduct(separatedValues); for (List valueList : valueLists) { Map, Object> valueMap = Maps.newTreeMap(Comparator.comparing(Property::getName)); - BlockState stateMaker = new BlockState(blockType).initializeId(registry); + BlockState stateMaker = new BlockState(blockType); for (int i = 0; i < valueList.size(); i++) { Property property = properties.get(i); Object value = valueList.get(i); valueMap.put(property, value); stateMaker.setState(property, value); } + stateMaker.initializeId(registry); stateMap.put(valueMap, stateMaker); } } diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/world/registry/BlockRegistry.java b/worldedit-core/src/main/java/com/sk89q/worldedit/world/registry/BlockRegistry.java index fb572f019..58a15ff30 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/world/registry/BlockRegistry.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/world/registry/BlockRegistry.java @@ -67,4 +67,11 @@ public interface BlockRegistry { */ OptionalInt getInternalBlockStateId(BlockState state); + /** + * Retrieve a block state by its internal ID, if possible. + * + * @param id The internal ID + * @return the block state, if available + */ + BlockState getBlockStateByInternalId(int id); } diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/world/registry/BundledBlockRegistry.java b/worldedit-core/src/main/java/com/sk89q/worldedit/world/registry/BundledBlockRegistry.java index 0e5615475..0b52ae01a 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/world/registry/BundledBlockRegistry.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/world/registry/BundledBlockRegistry.java @@ -59,4 +59,9 @@ public class BundledBlockRegistry implements BlockRegistry { return OptionalInt.empty(); } + @Override + public BlockState getBlockStateByInternalId(int id) { + return null; + } + }