From 11f5d05e7bbd4e4e1b73b11ede048c8d0a0b6a63 Mon Sep 17 00:00:00 2001 From: Matthew Miller Date: Sun, 1 Jul 2018 23:38:56 +1000 Subject: [PATCH] Update BlockState to remove legacy ID usage. --- .../bukkit/BukkitPlayerBlockBag.java | 30 ++--- .../java/com/sk89q/worldedit/EditSession.java | 2 +- .../java/com/sk89q/worldedit/WorldEdit.java | 10 +- .../worldedit/extent/ChangeSetExtent.java | 1 - .../worldedit/extent/inventory/BlockBag.java | 126 +++++++----------- .../extent/inventory/BlockBagExtent.java | 31 ++--- .../extent/inventory/OutOfSpaceException.java | 8 +- 7 files changed, 83 insertions(+), 125 deletions(-) diff --git a/worldedit-bukkit/src/main/java/com/sk89q/worldedit/bukkit/BukkitPlayerBlockBag.java b/worldedit-bukkit/src/main/java/com/sk89q/worldedit/bukkit/BukkitPlayerBlockBag.java index a939c1ae0..bd544f620 100644 --- a/worldedit-bukkit/src/main/java/com/sk89q/worldedit/bukkit/BukkitPlayerBlockBag.java +++ b/worldedit-bukkit/src/main/java/com/sk89q/worldedit/bukkit/BukkitPlayerBlockBag.java @@ -19,10 +19,8 @@ package com.sk89q.worldedit.bukkit; -import com.sk89q.worldedit.blocks.BaseItem; -import com.sk89q.worldedit.blocks.BaseItemStack; -import com.sk89q.worldedit.blocks.type.ItemType; -import com.sk89q.worldedit.blocks.type.ItemTypes; +import com.sk89q.worldedit.blocks.type.BlockState; +import com.sk89q.worldedit.blocks.type.BlockTypes; import com.sk89q.worldedit.extent.inventory.BlockBag; import com.sk89q.worldedit.extent.inventory.BlockBagException; import com.sk89q.worldedit.extent.inventory.OutOfBlocksException; @@ -64,12 +62,8 @@ public class BukkitPlayerBlockBag extends BlockBag { } @Override - public void fetchItem(BaseItem item) throws BlockBagException { - final ItemType id = item.getType(); - int amount = (item instanceof BaseItemStack) ? ((BaseItemStack) item).getAmount() : 1; - assert(amount == 1); - - if (id == ItemTypes.AIR) { + public void fetchBlock(BlockState blockState) throws BlockBagException { + if (blockState.getBlockType() == BlockTypes.AIR) { throw new IllegalArgumentException("Can't fetch air block"); } @@ -84,7 +78,7 @@ public class BukkitPlayerBlockBag extends BlockBag { continue; } - if (bukkitItem.getTypeId() != id.getLegacyId()) { + if (bukkitItem.getTypeId() != blockState.getBlockType().getLegacyId()) { // TODO Fix when bukkit gets not awful // Type id doesn't fit continue; @@ -113,12 +107,8 @@ public class BukkitPlayerBlockBag extends BlockBag { } @Override - public void storeItem(BaseItem item) throws BlockBagException { - final ItemType id = item.getType(); - int amount = (item instanceof BaseItemStack) ? ((BaseItemStack) item).getAmount() : 1; - assert(amount <= 64); - - if (id == ItemTypes.AIR) { + public void storeBlock(BlockState blockState, int amount) throws BlockBagException { + if (blockState.getBlockType() == BlockTypes.AIR) { throw new IllegalArgumentException("Can't store air block"); } @@ -139,7 +129,7 @@ public class BukkitPlayerBlockBag extends BlockBag { continue; } - if (bukkitItem.getTypeId() != id.getLegacyId()) { + if (bukkitItem.getTypeId() != blockState.getBlockType().getLegacyId()) { // TODO Fix when bukkit gets not terrible // Type id doesn't fit continue; @@ -166,11 +156,11 @@ public class BukkitPlayerBlockBag extends BlockBag { } if (freeSlot > -1) { - items[freeSlot] = new ItemStack(id.getLegacyId(), amount); // TODO Ditto + items[freeSlot] = new ItemStack(blockState.getBlockType().getLegacyId(), amount); // TODO Ditto return; } - throw new OutOfSpaceException(id); + throw new OutOfSpaceException(blockState.getBlockType()); } @Override diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/EditSession.java b/worldedit-core/src/main/java/com/sk89q/worldedit/EditSession.java index 99d5a6033..5f733b086 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/EditSession.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/EditSession.java @@ -375,7 +375,7 @@ public class EditSession implements Extent { * * @return a map of missing blocks */ - public Map popMissingBlocks() { + public Map popMissingBlocks() { return blockBagExtent.popMissing(); } diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/WorldEdit.java b/worldedit-core/src/main/java/com/sk89q/worldedit/WorldEdit.java index 9245a1e45..9e83eeba4 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/WorldEdit.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/WorldEdit.java @@ -479,7 +479,7 @@ public class WorldEdit { blockBag.flushChanges(); } - Map missingBlocks = editSession.popMissingBlocks(); + Map missingBlocks = editSession.popMissingBlocks(); if (!missingBlocks.isEmpty()) { StringBuilder str = new StringBuilder(); @@ -487,12 +487,8 @@ public class WorldEdit { int size = missingBlocks.size(); int i = 0; - for (Integer id : missingBlocks.keySet()) { - BlockType type = LegacyMapper.getInstance().getBlockFromLegacy(id).getBlockType(); - - str.append(type != null - ? type.getName() + " (" + id + ")" - : id.toString()); + for (BlockType id : missingBlocks.keySet()) { + str.append(id.getName()); str.append(" [Amt: ").append(missingBlocks.get(id)).append("]"); diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/extent/ChangeSetExtent.java b/worldedit-core/src/main/java/com/sk89q/worldedit/extent/ChangeSetExtent.java index 021644153..639718531 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/extent/ChangeSetExtent.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/extent/ChangeSetExtent.java @@ -21,7 +21,6 @@ package com.sk89q.worldedit.extent; import com.sk89q.worldedit.Vector; import com.sk89q.worldedit.WorldEditException; -import com.sk89q.worldedit.blocks.BaseBlock; import com.sk89q.worldedit.blocks.type.BlockStateHolder; import com.sk89q.worldedit.entity.BaseEntity; import com.sk89q.worldedit.entity.Entity; diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/extent/inventory/BlockBag.java b/worldedit-core/src/main/java/com/sk89q/worldedit/extent/inventory/BlockBag.java index 2eb77c97c..eb2e6a9af 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/extent/inventory/BlockBag.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/extent/inventory/BlockBag.java @@ -19,8 +19,8 @@ package com.sk89q.worldedit.extent.inventory; -import com.sk89q.worldedit.blocks.*; -import com.sk89q.worldedit.blocks.type.ItemTypes; +import com.sk89q.worldedit.blocks.type.BlockState; +import com.sk89q.worldedit.blocks.type.BlockTypes; import com.sk89q.worldedit.util.Location; /** @@ -31,123 +31,95 @@ public abstract class BlockBag { /** * Stores a block as if it was mined. * - * @param id the type ID - * @param data the data value + * @param blockState the block state * @throws BlockBagException on error */ - public void storeDroppedBlock(int id, int data) throws BlockBagException { - BaseItem dropped = BlockType.getBlockBagItem(id, data); + public void storeDroppedBlock(BlockState blockState) throws BlockBagException { + BlockState dropped = blockState; // TODO BlockType.getBlockBagItem(id, data); if (dropped == null) return; - if (dropped.getType() == ItemTypes.AIR) return; + if (dropped.getBlockType() == BlockTypes.AIR) return; - storeItem(dropped); + storeBlock(dropped); } /** * Sets a block as if it was placed by hand. * - * @param id the type ID - * @param data the data value + * @param blockState The block state * @throws BlockBagException on error */ - public void fetchPlacedBlock(int id, int data) throws BlockBagException { + public void fetchPlacedBlock(BlockState blockState) throws BlockBagException { try { // Blocks that can't be fetched... - switch (id) { - case BlockID.BEDROCK: - case BlockID.GOLD_ORE: - case BlockID.IRON_ORE: - case BlockID.COAL_ORE: - case BlockID.DIAMOND_ORE: - case BlockID.TNT: - case BlockID.MOB_SPAWNER: - case BlockID.CROPS: - case BlockID.REDSTONE_ORE: - case BlockID.GLOWING_REDSTONE_ORE: - case BlockID.SNOW: - case BlockID.LIGHTSTONE: - case BlockID.PORTAL: - throw new UnplaceableBlockException(); - - case BlockID.WATER: - case BlockID.STATIONARY_WATER: - case BlockID.LAVA: - case BlockID.STATIONARY_LAVA: - // Override liquids - return; - - default: - fetchBlock(id); - break; - } - +// TODO switch (id) { +// case BlockID.BEDROCK: +// case BlockID.GOLD_ORE: +// case BlockID.IRON_ORE: +// case BlockID.COAL_ORE: +// case BlockID.DIAMOND_ORE: +// case BlockID.TNT: +// case BlockID.MOB_SPAWNER: +// case BlockID.CROPS: +// case BlockID.REDSTONE_ORE: +// case BlockID.GLOWING_REDSTONE_ORE: +// case BlockID.SNOW: +// case BlockID.LIGHTSTONE: +// case BlockID.PORTAL: +// throw new UnplaceableBlockException(); +// +// case BlockID.WATER: +// case BlockID.STATIONARY_WATER: +// case BlockID.LAVA: +// case BlockID.STATIONARY_LAVA: +// // Override liquids +// return; +// } + fetchBlock(blockState); } catch (OutOfBlocksException e) { - BaseItem placed = BlockType.getBlockBagItem(id, data); - if (placed == null) throw e; // TODO: check - if (placed.getType() == ItemTypes.AIR) throw e; // TODO: check + BlockState placed = blockState;// TODO BlockType.getBlockBagItem(id, data); + if (placed == null || placed.getBlockType() == BlockTypes.AIR) throw e; // TODO: check - fetchItem(placed); + fetchBlock(placed); } } /** * Get a block. * - *

Either this method or fetchItem needs to be overridden.

- * - * @param id the type ID + * @param blockState the block state * @throws BlockBagException on error */ - public void fetchBlock(int id) throws BlockBagException { - fetchItem(new BaseItem(id)); - } + public abstract void fetchBlock(BlockState blockState) throws BlockBagException; /** - * Get a block. + * Store a block. * - *

Either this method or fetchItem needs to be overridden.

- * - * @param item the item + * @param blockState The block state * @throws BlockBagException on error */ - public void fetchItem(BaseItem item) throws BlockBagException { - fetchBlock(item.getLegacyId()); + public void storeBlock(BlockState blockState) throws BlockBagException { + this.storeBlock(blockState, 1); } /** * Store a block. * - *

Either this method or fetchItem needs to be overridden.

- * - * @param id the type ID + * @param blockState The block state + * @param amount The amount * @throws BlockBagException on error */ - public void storeBlock(int id) throws BlockBagException { - storeItem(new BaseItem(id)); - } - - /** - * Store a block. - * - *

Either this method or fetchItem needs to be overridden.

- * - * @param item the item - * @throws BlockBagException on error - */ - public void storeItem(BaseItem item) throws BlockBagException { - storeBlock(item.getLegacyId()); - } + public abstract void storeBlock(BlockState blockState, int amount) throws BlockBagException; /** * Checks to see if a block exists without removing it. * - * @param id the type ID + * @param blockState the block state * @return whether the block exists */ - public boolean peekBlock(int id) { + public boolean peekBlock(BlockState blockState) { try { - fetchBlock(id); - storeBlock(id); + fetchBlock(blockState); + storeBlock(blockState); return true; } catch (BlockBagException e) { return false; diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/extent/inventory/BlockBagExtent.java b/worldedit-core/src/main/java/com/sk89q/worldedit/extent/inventory/BlockBagExtent.java index aaed19d05..6e78fc016 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/extent/inventory/BlockBagExtent.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/extent/inventory/BlockBagExtent.java @@ -21,21 +21,24 @@ package com.sk89q.worldedit.extent.inventory; import com.sk89q.worldedit.Vector; import com.sk89q.worldedit.WorldEditException; -import com.sk89q.worldedit.blocks.BaseBlock; +import com.sk89q.worldedit.blocks.type.BlockState; import com.sk89q.worldedit.blocks.type.BlockStateHolder; +import com.sk89q.worldedit.blocks.type.BlockType; +import com.sk89q.worldedit.blocks.type.BlockTypes; import com.sk89q.worldedit.extent.AbstractDelegateExtent; import com.sk89q.worldedit.extent.Extent; -import javax.annotation.Nullable; import java.util.HashMap; import java.util.Map; +import javax.annotation.Nullable; + /** * Applies a {@link BlockBag} to operations. */ public class BlockBagExtent extends AbstractDelegateExtent { - private Map missingBlocks = new HashMap<>(); + private Map missingBlocks = new HashMap<>(); private BlockBag blockBag; /** @@ -73,8 +76,8 @@ public class BlockBagExtent extends AbstractDelegateExtent { * * @return a map of missing blocks */ - public Map popMissing() { - Map missingBlocks = this.missingBlocks; + public Map popMissing() { + Map missingBlocks = this.missingBlocks; this.missingBlocks = new HashMap<>(); return missingBlocks; } @@ -82,28 +85,26 @@ public class BlockBagExtent extends AbstractDelegateExtent { @Override public boolean setBlock(Vector position, BlockStateHolder block) throws WorldEditException { if (blockBag != null) { - BaseBlock lazyBlock = getExtent().getLazyBlock(position); - int existing = lazyBlock.getBlockType().getLegacyId(); - final int type = block.getBlockType().getLegacyId(); + BlockState existing = getExtent().getBlock(position); - if (type > 0) { + if (block.getBlockType() != BlockTypes.AIR) { try { - blockBag.fetchPlacedBlock(type, 0); + blockBag.fetchPlacedBlock(block.toImmutableState()); } catch (UnplaceableBlockException e) { return false; } catch (BlockBagException e) { - if (!missingBlocks.containsKey(type)) { - missingBlocks.put(type, 1); + if (!missingBlocks.containsKey(block.getBlockType())) { + missingBlocks.put(block.getBlockType(), 1); } else { - missingBlocks.put(type, missingBlocks.get(type) + 1); + missingBlocks.put(block.getBlockType(), missingBlocks.get(block.getBlockType()) + 1); } return false; } } - if (existing > 0) { + if (existing.getBlockType() != BlockTypes.AIR) { try { - blockBag.storeDroppedBlock(existing, lazyBlock.getData()); + blockBag.storeDroppedBlock(existing); } catch (BlockBagException ignored) { } } diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/extent/inventory/OutOfSpaceException.java b/worldedit-core/src/main/java/com/sk89q/worldedit/extent/inventory/OutOfSpaceException.java index f9a0900f3..c7058ea8c 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/extent/inventory/OutOfSpaceException.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/extent/inventory/OutOfSpaceException.java @@ -19,21 +19,21 @@ package com.sk89q.worldedit.extent.inventory; -import com.sk89q.worldedit.blocks.type.ItemType; +import com.sk89q.worldedit.blocks.type.BlockType; /** * Thrown when the target inventory of a block bag is full. */ public class OutOfSpaceException extends BlockBagException { - private ItemType type; + private BlockType type; /** * Construct the object. * * @param type the type of the block */ - public OutOfSpaceException(ItemType type) { + public OutOfSpaceException(BlockType type) { this.type = type; } @@ -42,7 +42,7 @@ public class OutOfSpaceException extends BlockBagException { * * @return the type */ - public ItemType getType() { + public BlockType getType() { return this.type; } }