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;
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

View File

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

View File

@ -479,7 +479,7 @@ public class WorldEdit {
blockBag.flushChanges();
}
Map<Integer, Integer> missingBlocks = editSession.popMissingBlocks();
Map<BlockType, Integer> 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("]");

View File

@ -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;

View File

@ -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.
*
* <p>Either this method or fetchItem needs to be overridden.</p>
*
* @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.
*
* <p>Either this method or fetchItem needs to be overridden.</p>
*
* @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.
*
* <p>Either this method or fetchItem needs to be overridden.</p>
*
* @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.
*
* <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());
}
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;

View File

@ -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<Integer, Integer> missingBlocks = new HashMap<>();
private Map<BlockType, Integer> missingBlocks = new HashMap<>();
private BlockBag blockBag;
/**
@ -73,8 +76,8 @@ public class BlockBagExtent extends AbstractDelegateExtent {
*
* @return a map of missing blocks
*/
public Map<Integer, Integer> popMissing() {
Map<Integer, Integer> missingBlocks = this.missingBlocks;
public Map<BlockType, Integer> popMissing() {
Map<BlockType, Integer> 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) {
}
}

View File

@ -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;
}
}