Permalink
Browse files

Update BlockState to remove legacy ID usage.

  • Loading branch information...
me4502 committed Jul 1, 2018
1 parent 4fece81 commit b0fd1426a4af13897e58f79e275180d0df5286ac
@@ -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 Player getPlayer() {
}
@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 void fetchItem(BaseItem item) throws BlockBagException {
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 void fetchItem(BaseItem item) throws BlockBagException {
}
@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 void storeItem(BaseItem item) throws BlockBagException {
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 void storeItem(BaseItem item) throws BlockBagException {
}
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
@@ -375,7 +375,7 @@ public void setBlockBag(BlockBag blockBag) {
*
* @return a map of missing blocks
*/
public Map<Integer, Integer> popMissingBlocks() {
public Map<com.sk89q.worldedit.blocks.type.BlockType, Integer> popMissingBlocks() {
return blockBagExtent.popMissing();
}
@@ -479,20 +479,16 @@ public void flushBlockBag(Actor actor, EditSession editSession) {
blockBag.flushChanges();
}
Map<Integer, Integer> missingBlocks = editSession.popMissingBlocks();
Map<BlockType, Integer> missingBlocks = editSession.popMissingBlocks();
if (!missingBlocks.isEmpty()) {
StringBuilder str = new StringBuilder();
str.append("Missing these blocks: ");
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("]");
@@ -21,7 +21,6 @@
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;
@@ -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 @@
/**
* 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
* @throws BlockBagException on error
*/
public void fetchBlock(int id) throws BlockBagException {
fetchItem(new BaseItem(id));
}
/**
* Get 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 abstract void fetchBlock(BlockState blockState) throws BlockBagException;
/**
* Store 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 storeBlock(int id) throws BlockBagException {
storeItem(new BaseItem(id));
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 item the item
* @param blockState The block state
* @param amount The amount
* @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;
@@ -21,21 +21,24 @@
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,37 +76,35 @@ public void setBlockBag(@Nullable BlockBag blockBag) {
*
* @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;
}
@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) {
}
}
Oops, something went wrong.

0 comments on commit b0fd142

Please sign in to comment.