Update BlockState to remove legacy ID usage.

This commit is contained in:
Matthew Miller 2018-07-01 23:38:56 +10:00
parent 93b225ca3c
commit 11f5d05e7b
7 changed files with 83 additions and 125 deletions

View File

@ -19,10 +19,8 @@
package com.sk89q.worldedit.bukkit; package com.sk89q.worldedit.bukkit;
import com.sk89q.worldedit.blocks.BaseItem; import com.sk89q.worldedit.blocks.type.BlockState;
import com.sk89q.worldedit.blocks.BaseItemStack; import com.sk89q.worldedit.blocks.type.BlockTypes;
import com.sk89q.worldedit.blocks.type.ItemType;
import com.sk89q.worldedit.blocks.type.ItemTypes;
import com.sk89q.worldedit.extent.inventory.BlockBag; import com.sk89q.worldedit.extent.inventory.BlockBag;
import com.sk89q.worldedit.extent.inventory.BlockBagException; import com.sk89q.worldedit.extent.inventory.BlockBagException;
import com.sk89q.worldedit.extent.inventory.OutOfBlocksException; import com.sk89q.worldedit.extent.inventory.OutOfBlocksException;
@ -64,12 +62,8 @@ public class BukkitPlayerBlockBag extends BlockBag {
} }
@Override @Override
public void fetchItem(BaseItem item) throws BlockBagException { public void fetchBlock(BlockState blockState) throws BlockBagException {
final ItemType id = item.getType(); if (blockState.getBlockType() == BlockTypes.AIR) {
int amount = (item instanceof BaseItemStack) ? ((BaseItemStack) item).getAmount() : 1;
assert(amount == 1);
if (id == ItemTypes.AIR) {
throw new IllegalArgumentException("Can't fetch air block"); throw new IllegalArgumentException("Can't fetch air block");
} }
@ -84,7 +78,7 @@ public class BukkitPlayerBlockBag extends BlockBag {
continue; continue;
} }
if (bukkitItem.getTypeId() != id.getLegacyId()) { if (bukkitItem.getTypeId() != blockState.getBlockType().getLegacyId()) {
// TODO Fix when bukkit gets not awful // TODO Fix when bukkit gets not awful
// Type id doesn't fit // Type id doesn't fit
continue; continue;
@ -113,12 +107,8 @@ public class BukkitPlayerBlockBag extends BlockBag {
} }
@Override @Override
public void storeItem(BaseItem item) throws BlockBagException { public void storeBlock(BlockState blockState, int amount) throws BlockBagException {
final ItemType id = item.getType(); if (blockState.getBlockType() == BlockTypes.AIR) {
int amount = (item instanceof BaseItemStack) ? ((BaseItemStack) item).getAmount() : 1;
assert(amount <= 64);
if (id == ItemTypes.AIR) {
throw new IllegalArgumentException("Can't store air block"); throw new IllegalArgumentException("Can't store air block");
} }
@ -139,7 +129,7 @@ public class BukkitPlayerBlockBag extends BlockBag {
continue; continue;
} }
if (bukkitItem.getTypeId() != id.getLegacyId()) { if (bukkitItem.getTypeId() != blockState.getBlockType().getLegacyId()) {
// TODO Fix when bukkit gets not terrible // TODO Fix when bukkit gets not terrible
// Type id doesn't fit // Type id doesn't fit
continue; continue;
@ -166,11 +156,11 @@ public class BukkitPlayerBlockBag extends BlockBag {
} }
if (freeSlot > -1) { if (freeSlot > -1) {
items[freeSlot] = new ItemStack(id.getLegacyId(), amount); // TODO Ditto items[freeSlot] = new ItemStack(blockState.getBlockType().getLegacyId(), amount); // TODO Ditto
return; return;
} }
throw new OutOfSpaceException(id); throw new OutOfSpaceException(blockState.getBlockType());
} }
@Override @Override

View File

@ -375,7 +375,7 @@ public class EditSession implements Extent {
* *
* @return a map of missing blocks * @return a map of missing blocks
*/ */
public Map<Integer, Integer> popMissingBlocks() { public Map<com.sk89q.worldedit.blocks.type.BlockType, Integer> popMissingBlocks() {
return blockBagExtent.popMissing(); return blockBagExtent.popMissing();
} }

View File

@ -479,7 +479,7 @@ public class WorldEdit {
blockBag.flushChanges(); blockBag.flushChanges();
} }
Map<Integer, Integer> missingBlocks = editSession.popMissingBlocks(); Map<BlockType, Integer> missingBlocks = editSession.popMissingBlocks();
if (!missingBlocks.isEmpty()) { if (!missingBlocks.isEmpty()) {
StringBuilder str = new StringBuilder(); StringBuilder str = new StringBuilder();
@ -487,12 +487,8 @@ public class WorldEdit {
int size = missingBlocks.size(); int size = missingBlocks.size();
int i = 0; int i = 0;
for (Integer id : missingBlocks.keySet()) { for (BlockType id : missingBlocks.keySet()) {
BlockType type = LegacyMapper.getInstance().getBlockFromLegacy(id).getBlockType(); str.append(id.getName());
str.append(type != null
? type.getName() + " (" + id + ")"
: id.toString());
str.append(" [Amt: ").append(missingBlocks.get(id)).append("]"); str.append(" [Amt: ").append(missingBlocks.get(id)).append("]");

View File

@ -21,7 +21,6 @@ package com.sk89q.worldedit.extent;
import com.sk89q.worldedit.Vector; import com.sk89q.worldedit.Vector;
import com.sk89q.worldedit.WorldEditException; import com.sk89q.worldedit.WorldEditException;
import com.sk89q.worldedit.blocks.BaseBlock;
import com.sk89q.worldedit.blocks.type.BlockStateHolder; import com.sk89q.worldedit.blocks.type.BlockStateHolder;
import com.sk89q.worldedit.entity.BaseEntity; import com.sk89q.worldedit.entity.BaseEntity;
import com.sk89q.worldedit.entity.Entity; import com.sk89q.worldedit.entity.Entity;

View File

@ -19,8 +19,8 @@
package com.sk89q.worldedit.extent.inventory; package com.sk89q.worldedit.extent.inventory;
import com.sk89q.worldedit.blocks.*; import com.sk89q.worldedit.blocks.type.BlockState;
import com.sk89q.worldedit.blocks.type.ItemTypes; import com.sk89q.worldedit.blocks.type.BlockTypes;
import com.sk89q.worldedit.util.Location; import com.sk89q.worldedit.util.Location;
/** /**
@ -31,123 +31,95 @@ public abstract class BlockBag {
/** /**
* Stores a block as if it was mined. * Stores a block as if it was mined.
* *
* @param id the type ID * @param blockState the block state
* @param data the data value
* @throws BlockBagException on error * @throws BlockBagException on error
*/ */
public void storeDroppedBlock(int id, int data) throws BlockBagException { public void storeDroppedBlock(BlockState blockState) throws BlockBagException {
BaseItem dropped = BlockType.getBlockBagItem(id, data); BlockState dropped = blockState; // TODO BlockType.getBlockBagItem(id, data);
if (dropped == null) return; 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. * Sets a block as if it was placed by hand.
* *
* @param id the type ID * @param blockState The block state
* @param data the data value
* @throws BlockBagException on error * @throws BlockBagException on error
*/ */
public void fetchPlacedBlock(int id, int data) throws BlockBagException { public void fetchPlacedBlock(BlockState blockState) throws BlockBagException {
try { try {
// Blocks that can't be fetched... // Blocks that can't be fetched...
switch (id) { // TODO switch (id) {
case BlockID.BEDROCK: // case BlockID.BEDROCK:
case BlockID.GOLD_ORE: // case BlockID.GOLD_ORE:
case BlockID.IRON_ORE: // case BlockID.IRON_ORE:
case BlockID.COAL_ORE: // case BlockID.COAL_ORE:
case BlockID.DIAMOND_ORE: // case BlockID.DIAMOND_ORE:
case BlockID.TNT: // case BlockID.TNT:
case BlockID.MOB_SPAWNER: // case BlockID.MOB_SPAWNER:
case BlockID.CROPS: // case BlockID.CROPS:
case BlockID.REDSTONE_ORE: // case BlockID.REDSTONE_ORE:
case BlockID.GLOWING_REDSTONE_ORE: // case BlockID.GLOWING_REDSTONE_ORE:
case BlockID.SNOW: // case BlockID.SNOW:
case BlockID.LIGHTSTONE: // case BlockID.LIGHTSTONE:
case BlockID.PORTAL: // case BlockID.PORTAL:
throw new UnplaceableBlockException(); // throw new UnplaceableBlockException();
//
case BlockID.WATER: // case BlockID.WATER:
case BlockID.STATIONARY_WATER: // case BlockID.STATIONARY_WATER:
case BlockID.LAVA: // case BlockID.LAVA:
case BlockID.STATIONARY_LAVA: // case BlockID.STATIONARY_LAVA:
// Override liquids // // Override liquids
return; // return;
// }
default: fetchBlock(blockState);
fetchBlock(id);
break;
}
} catch (OutOfBlocksException e) { } catch (OutOfBlocksException e) {
BaseItem placed = BlockType.getBlockBagItem(id, data); BlockState placed = blockState;// TODO BlockType.getBlockBagItem(id, data);
if (placed == null) throw e; // TODO: check if (placed == null || placed.getBlockType() == BlockTypes.AIR) throw e; // TODO: check
if (placed.getType() == ItemTypes.AIR) throw e; // TODO: check
fetchItem(placed); fetchBlock(placed);
} }
} }
/** /**
* Get a block. * Get a block.
* *
* <p>Either this method or fetchItem needs to be overridden.</p> * @param blockState the block state
*
* @param id the type ID
* @throws BlockBagException on error * @throws BlockBagException on error
*/ */
public void fetchBlock(int id) throws BlockBagException { public abstract void fetchBlock(BlockState blockState) throws BlockBagException;
fetchItem(new BaseItem(id));
}
/** /**
* Get a block. * Store a block.
* *
* <p>Either this method or fetchItem needs to be overridden.</p> * @param blockState The block state
*
* @param item the item
* @throws BlockBagException on error * @throws BlockBagException on error
*/ */
public void fetchItem(BaseItem item) throws BlockBagException { public void storeBlock(BlockState blockState) throws BlockBagException {
fetchBlock(item.getLegacyId()); this.storeBlock(blockState, 1);
} }
/** /**
* Store a block. * Store a block.
* *
* <p>Either this method or fetchItem needs to be overridden.</p> * @param blockState The block state
* * @param amount The amount
* @param id the type ID
* @throws BlockBagException on error * @throws BlockBagException on error
*/ */
public void storeBlock(int id) throws BlockBagException { public abstract void storeBlock(BlockState blockState, int amount) throws BlockBagException;
storeItem(new BaseItem(id));
}
/**
* Store a block.
*
* <p>Either this method or fetchItem needs to be overridden.</p>
*
* @param item the item
* @throws BlockBagException on error
*/
public void storeItem(BaseItem item) throws BlockBagException {
storeBlock(item.getLegacyId());
}
/** /**
* Checks to see if a block exists without removing it. * 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 * @return whether the block exists
*/ */
public boolean peekBlock(int id) { public boolean peekBlock(BlockState blockState) {
try { try {
fetchBlock(id); fetchBlock(blockState);
storeBlock(id); storeBlock(blockState);
return true; return true;
} catch (BlockBagException e) { } catch (BlockBagException e) {
return false; return false;

View File

@ -21,21 +21,24 @@ package com.sk89q.worldedit.extent.inventory;
import com.sk89q.worldedit.Vector; import com.sk89q.worldedit.Vector;
import com.sk89q.worldedit.WorldEditException; 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.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.AbstractDelegateExtent;
import com.sk89q.worldedit.extent.Extent; import com.sk89q.worldedit.extent.Extent;
import javax.annotation.Nullable;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import javax.annotation.Nullable;
/** /**
* Applies a {@link BlockBag} to operations. * Applies a {@link BlockBag} to operations.
*/ */
public class BlockBagExtent extends AbstractDelegateExtent { public class BlockBagExtent extends AbstractDelegateExtent {
private Map<Integer, Integer> missingBlocks = new HashMap<>(); private Map<BlockType, Integer> missingBlocks = new HashMap<>();
private BlockBag blockBag; private BlockBag blockBag;
/** /**
@ -73,8 +76,8 @@ public class BlockBagExtent extends AbstractDelegateExtent {
* *
* @return a map of missing blocks * @return a map of missing blocks
*/ */
public Map<Integer, Integer> popMissing() { public Map<BlockType, Integer> popMissing() {
Map<Integer, Integer> missingBlocks = this.missingBlocks; Map<BlockType, Integer> missingBlocks = this.missingBlocks;
this.missingBlocks = new HashMap<>(); this.missingBlocks = new HashMap<>();
return missingBlocks; return missingBlocks;
} }
@ -82,28 +85,26 @@ public class BlockBagExtent extends AbstractDelegateExtent {
@Override @Override
public boolean setBlock(Vector position, BlockStateHolder block) throws WorldEditException { public boolean setBlock(Vector position, BlockStateHolder block) throws WorldEditException {
if (blockBag != null) { if (blockBag != null) {
BaseBlock lazyBlock = getExtent().getLazyBlock(position); BlockState existing = getExtent().getBlock(position);
int existing = lazyBlock.getBlockType().getLegacyId();
final int type = block.getBlockType().getLegacyId();
if (type > 0) { if (block.getBlockType() != BlockTypes.AIR) {
try { try {
blockBag.fetchPlacedBlock(type, 0); blockBag.fetchPlacedBlock(block.toImmutableState());
} catch (UnplaceableBlockException e) { } catch (UnplaceableBlockException e) {
return false; return false;
} catch (BlockBagException e) { } catch (BlockBagException e) {
if (!missingBlocks.containsKey(type)) { if (!missingBlocks.containsKey(block.getBlockType())) {
missingBlocks.put(type, 1); missingBlocks.put(block.getBlockType(), 1);
} else { } else {
missingBlocks.put(type, missingBlocks.get(type) + 1); missingBlocks.put(block.getBlockType(), missingBlocks.get(block.getBlockType()) + 1);
} }
return false; return false;
} }
} }
if (existing > 0) { if (existing.getBlockType() != BlockTypes.AIR) {
try { try {
blockBag.storeDroppedBlock(existing, lazyBlock.getData()); blockBag.storeDroppedBlock(existing);
} catch (BlockBagException ignored) { } catch (BlockBagException ignored) {
} }
} }

View File

@ -19,21 +19,21 @@
package com.sk89q.worldedit.extent.inventory; 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. * Thrown when the target inventory of a block bag is full.
*/ */
public class OutOfSpaceException extends BlockBagException { public class OutOfSpaceException extends BlockBagException {
private ItemType type; private BlockType type;
/** /**
* Construct the object. * Construct the object.
* *
* @param type the type of the block * @param type the type of the block
*/ */
public OutOfSpaceException(ItemType type) { public OutOfSpaceException(BlockType type) {
this.type = type; this.type = type;
} }
@ -42,7 +42,7 @@ public class OutOfSpaceException extends BlockBagException {
* *
* @return the type * @return the type
*/ */
public ItemType getType() { public BlockType getType() {
return this.type; return this.type;
} }
} }