Cleaned up ContainerBlock inventory reading from NBT

Changed ListTag's List parameter to List<? extends Tag>
This commit is contained in:
zml2008 2012-03-29 21:11:49 -07:00
parent 33752eb058
commit 3bfb12c051
13 changed files with 185 additions and 302 deletions

View File

@ -49,7 +49,7 @@
<dependency>
<groupId>org.bukkit</groupId>
<artifactId>bukkit</artifactId>
<version>1.2.3-R0.3-SNAPSHOT</version>
<version>1.2.4-R1.0</version>
</dependency>
<!-- Archive reading library for snapshots -->

View File

@ -66,7 +66,7 @@ public final class ListTag extends Tag {
* @param value
* The value.
*/
public ListTag(String name, Class<? extends Tag> type, List<Tag> value) {
public ListTag(String name, Class<? extends Tag> type, List<? extends Tag> value) {
super(name);
this.type = type;
this.value = Collections.unmodifiableList(value);

View File

@ -93,9 +93,9 @@ public class BlockVector extends Vector {
*/
@Override
public int hashCode() {
return (Integer.valueOf((int) x).hashCode() << 19) ^
(Integer.valueOf((int) y).hashCode() << 12) ^
Integer.valueOf((int) z).hashCode();
return ((int) x << 19) ^
((int) y << 12) ^
(int) z;
}
@Override

View File

@ -42,7 +42,7 @@ public class BaseBlock {
* @param type
*/
public BaseBlock(int type) {
this.type = (short) type;
this(type, 0);
}
/**

View File

@ -25,25 +25,19 @@ import java.util.Map;
import java.util.HashMap;
import java.util.List;
import java.util.ArrayList;
import java.util.Map.Entry;
/**
* Represents chests.
*
* @author sk89q
*/
public class ChestBlock extends BaseBlock implements TileEntityBlock, ContainerBlock {
/**
* Store the list of items.
*/
private BaseItemStack[] items;
public class ChestBlock extends ContainerBlock {
/**
* Construct the chest block.
*/
public ChestBlock() {
super(BlockID.CHEST);
items = new BaseItemStack[27];
super(BlockID.CHEST, 27);
}
/**
@ -52,8 +46,7 @@ public class ChestBlock extends BaseBlock implements TileEntityBlock, ContainerB
* @param data
*/
public ChestBlock(int data) {
super(BlockID.CHEST, data);
items = new BaseItemStack[27];
super(BlockID.CHEST, data, 27);
}
/**
@ -63,24 +56,8 @@ public class ChestBlock extends BaseBlock implements TileEntityBlock, ContainerB
* @param items
*/
public ChestBlock(int data, BaseItemStack[] items) {
super(BlockID.CHEST, data);
this.items = items;
}
/**
* Get the list of items.
*
* @return
*/
public BaseItemStack[] getItems() {
return items;
}
/**
* Set the list of items.
*/
public void setItems(BaseItemStack[] items) {
this.items = items;
super(BlockID.CHEST, data, 27);
setItems(items);
}
/**
@ -100,36 +77,8 @@ public class ChestBlock extends BaseBlock implements TileEntityBlock, ContainerB
*/
public Map<String, Tag> toTileEntityNBT()
throws DataException {
List<Tag> itemsList = new ArrayList<Tag>();
for (int i = 0; i < items.length; ++i) {
BaseItemStack item = items[i];
if (item != null) {
Map<String, Tag> data = new HashMap<String, Tag>();
CompoundTag itemTag = new CompoundTag("Items", data);
data.put("id", new ShortTag("id", (short) item.getType()));
data.put("Damage", new ShortTag("Damage", item.getDamage()));
data.put("Count", new ByteTag("Count", (byte) item.getAmount()));
data.put("Slot", new ByteTag("Slot", (byte) i));
if(item.getEnchantments().size() > 0) {
Map<String, Tag> ench = new HashMap<String, Tag>();
CompoundTag compound = new CompoundTag("tag", ench);
List<Tag> list = new ArrayList<Tag>();
ListTag enchlist = new ListTag("ench", CompoundTag.class, list);
for(Entry<Integer, Integer> entry : item.getEnchantments().entrySet()) {
Map<String, Tag> enchantment = new HashMap<String, Tag>();
CompoundTag enchantcompound = new CompoundTag(null, enchantment);
enchantment.put("id", new ShortTag("id", entry.getKey().shortValue()));
enchantment.put("lvl", new ShortTag("lvl", entry.getValue().shortValue()));
list.add(enchantcompound);
}
ench.put("ench", enchlist);
data.put("tag", compound);
}
itemsList.add(itemTag);
}
}
Map<String, Tag> values = new HashMap<String, Tag>();
values.put("Items", new ListTag("Items", CompoundTag.class, itemsList));
values.put("Items", new ListTag("Items", CompoundTag.class, serializeInventory(getItems())));
return values;
}
@ -150,39 +99,15 @@ public class ChestBlock extends BaseBlock implements TileEntityBlock, ContainerB
throw new DataException("'Chest' tile entity expected");
}
ListTag items = (ListTag) NBTUtils.getChildTag(values, "Items", ListTag.class);
BaseItemStack[] newItems = new BaseItemStack[27];
for (Tag tag : items.getValue()) {
List<CompoundTag> items = new ArrayList<CompoundTag>();
for (Tag tag : NBTUtils.getChildTag(values, "Items", ListTag.class).getValue()) {
if (!(tag instanceof CompoundTag)) {
throw new DataException("CompoundTag expected as child tag of Chest's Items");
}
CompoundTag item = (CompoundTag) tag;
Map<String, Tag> itemValues = item.getValue();
short id = NBTUtils.getChildTag(itemValues, "id", ShortTag.class).getValue();
short damage = NBTUtils.getChildTag(itemValues, "Damage", ShortTag.class).getValue();
byte count = NBTUtils.getChildTag(itemValues, "Count", ByteTag.class).getValue();
byte slot = NBTUtils.getChildTag(itemValues, "Slot", ByteTag.class).getValue();
if (slot >= 0 && slot <= 26) {
BaseItemStack itemstack = new BaseItemStack(id, count, damage);
if(itemValues.containsKey("tag")) {
ListTag ench = (ListTag) NBTUtils.getChildTag(itemValues, "tag", CompoundTag.class).getValue().get("ench");
for(Tag e : ench.getValue()) {
Map<String, Tag> vars = ((CompoundTag) e).getValue();
short enchid = NBTUtils.getChildTag(vars, "id", ShortTag.class).getValue();
short enchlvl = NBTUtils.getChildTag(vars, "lvl", ShortTag.class).getValue();
itemstack.getEnchantments().put((int) enchid, (int)enchlvl);
}
}
newItems[slot] = itemstack;
}
items.add((CompoundTag) tag);
}
this.items = newItems;
setItems(deserializeInventory(items));
}
}

View File

@ -19,23 +19,119 @@
package com.sk89q.worldedit.blocks;
import com.sk89q.jnbt.ByteTag;
import com.sk89q.jnbt.CompoundTag;
import com.sk89q.jnbt.ListTag;
import com.sk89q.jnbt.NBTUtils;
import com.sk89q.jnbt.ShortTag;
import com.sk89q.jnbt.Tag;
import com.sk89q.worldedit.data.DataException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Represents a block that stores items.
*
* @author sk89q
*/
public interface ContainerBlock {
public abstract class ContainerBlock extends BaseBlock implements TileEntityBlock {
private BaseItemStack[] items;
public ContainerBlock(int type, int inventorySize) {
super(type);
this.items = new BaseItemStack[inventorySize];
}
public ContainerBlock(int type, int data, int inventorySize) {
super(type, data);
this.items = new BaseItemStack[inventorySize];
}
/**
* Get the list of items.
*
* @return
*/
public BaseItemStack[] getItems();
public BaseItemStack[] getItems() {
return this.items;
}
/**
* Set the list of items.
*
* @param items
*/
public void setItems(BaseItemStack[] items);
public void setItems(BaseItemStack[] items) {
this.items = items;
}
public Map<String, Tag> serializeItem(BaseItemStack item) {
Map<String, Tag> data = new HashMap<String, Tag>();
data.put("id", new ShortTag("id", (short) item.getType()));
data.put("Damage", new ShortTag("Damage", item.getDamage()));
data.put("Count", new ByteTag("Count", (byte) item.getAmount()));
if (item.getEnchantments().size() > 0) {
List<CompoundTag> enchantmentList = new ArrayList<CompoundTag>();
for(Map.Entry<Integer, Integer> entry : item.getEnchantments().entrySet()) {
Map<String, Tag> enchantment = new HashMap<String, Tag>();
enchantment.put("id", new ShortTag("id", entry.getKey().shortValue()));
enchantment.put("lvl", new ShortTag("lvl", entry.getValue().shortValue()));
enchantmentList.add(new CompoundTag(null, enchantment));
}
Map<String, Tag> auxData = new HashMap<String, Tag>();
auxData.put("ench", new ListTag("ench", CompoundTag.class, enchantmentList));
data.put("tag", new CompoundTag("tag", auxData));
}
return data;
}
public BaseItemStack deserializeItem(Map<String, Tag> data) throws DataException {
short id = NBTUtils.getChildTag(data, "id", ShortTag.class).getValue();
short damage = NBTUtils.getChildTag(data, "Damage", ShortTag.class).getValue();
byte count = NBTUtils.getChildTag(data, "Count", ByteTag.class).getValue();
BaseItemStack stack = new BaseItemStack(id, count, damage);
if (data.containsKey("tag")) {
Map<String, Tag> auxData = NBTUtils.getChildTag(data, "tag", CompoundTag.class).getValue();
ListTag ench = (ListTag)auxData.get("ench");
for(Tag e : ench.getValue()) {
Map<String, Tag> vars = ((CompoundTag) e).getValue();
short enchId = NBTUtils.getChildTag(vars, "id", ShortTag.class).getValue();
short enchLevel = NBTUtils.getChildTag(vars, "lvl", ShortTag.class).getValue();
stack.getEnchantments().put((int) enchId, (int) enchLevel);
}
}
return stack;
}
public BaseItemStack[] deserializeInventory(List<CompoundTag> items) throws DataException {
BaseItemStack[] stacks = new BaseItemStack[items.size()];
for (CompoundTag tag : items) {
Map<String, Tag> item = tag.getValue();
BaseItemStack stack = deserializeItem(item);
byte slot = NBTUtils.getChildTag(item, "Slot", ByteTag.class).getValue();
if (slot >= 0 && slot < stacks.length) {
stacks[slot] = stack;
}
}
return stacks;
}
public List<CompoundTag> serializeInventory(BaseItemStack[] items) {
List<CompoundTag> tags = new ArrayList<CompoundTag>();
for (int i = 0; i < items.length; ++i) {
if (items[i] != null) {
Map<String, Tag> tagData = serializeItem(items[i]);
tagData.put("Slot", new ByteTag("Slot", (byte) i));
tags.add(new CompoundTag("", tagData));
}
}
return tags;
}
}

View File

@ -32,18 +32,13 @@ import java.util.Map.Entry;
*
* @author sk89q
*/
public class DispenserBlock extends BaseBlock implements TileEntityBlock, ContainerBlock {
/**
* Store the list of items.
*/
private BaseItemStack[] items;
public class DispenserBlock extends ContainerBlock {
/**
* Construct the dispenser block.
*/
public DispenserBlock() {
super(BlockID.DISPENSER);
items = new BaseItemStack[9];
super(BlockID.DISPENSER, 9);
}
/**
@ -52,8 +47,7 @@ public class DispenserBlock extends BaseBlock implements TileEntityBlock, Contai
* @param data
*/
public DispenserBlock(int data) {
super(BlockID.DISPENSER, data);
items = new BaseItemStack[9];
super(BlockID.DISPENSER, data, 9);
}
/**
@ -63,24 +57,8 @@ public class DispenserBlock extends BaseBlock implements TileEntityBlock, Contai
* @param items
*/
public DispenserBlock(int data, BaseItemStack[] items) {
super(BlockID.DISPENSER, data);
this.items = items;
}
/**
* Get the list of items.
*
* @return
*/
public BaseItemStack[] getItems() {
return items;
}
/**
* Set the list of items.
*/
public void setItems(BaseItemStack[] items) {
this.items = items;
super(BlockID.DISPENSER, data, 9);
this.setItems(items);
}
/**
@ -100,36 +78,8 @@ public class DispenserBlock extends BaseBlock implements TileEntityBlock, Contai
*/
public Map<String, Tag> toTileEntityNBT()
throws DataException {
List<Tag> itemsList = new ArrayList<Tag>();
for (int i = 0; i < items.length; ++i) {
BaseItemStack item = items[i];
if (item != null) {
Map<String, Tag> data = new HashMap<String, Tag>();
CompoundTag itemTag = new CompoundTag("Items", data);
data.put("id", new ShortTag("id", (short) item.getType()));
data.put("Damage", new ShortTag("Damage", item.getDamage()));
data.put("Count", new ByteTag("Count", (byte) item.getAmount()));
data.put("Slot", new ByteTag("Slot", (byte) i));
if(item.getEnchantments().size() > 0) {
Map<String, Tag> ench = new HashMap<String, Tag>();
CompoundTag compound = new CompoundTag("tag", ench);
List<Tag> list = new ArrayList<Tag>();
ListTag enchlist = new ListTag("ench", CompoundTag.class, list);
for(Entry<Integer, Integer> entry : item.getEnchantments().entrySet()) {
Map<String, Tag> enchantment = new HashMap<String, Tag>();
CompoundTag enchantcompound = new CompoundTag(null, enchantment);
enchantment.put("id", new ShortTag("id", entry.getKey().shortValue()));
enchantment.put("lvl", new ShortTag("lvl", entry.getValue().shortValue()));
list.add(enchantcompound);
}
ench.put("ench", enchlist);
data.put("tag", compound);
}
itemsList.add(itemTag);
}
}
Map<String, Tag> values = new HashMap<String, Tag>();
values.put("Items", new ListTag("Items", CompoundTag.class, itemsList));
values.put("Items", new ListTag("Items", CompoundTag.class, serializeInventory(getItems())));
return values;
}
@ -150,39 +100,15 @@ public class DispenserBlock extends BaseBlock implements TileEntityBlock, Contai
throw new DataException("'Trap' tile entity expected");
}
ListTag items = (ListTag) NBTUtils.getChildTag(values, "Items", ListTag.class);
BaseItemStack[] newItems = new BaseItemStack[9];
for (Tag tag : items.getValue()) {
List<CompoundTag> items = new ArrayList<CompoundTag>();
for (Tag tag : NBTUtils.getChildTag(values, "Items", ListTag.class).getValue()) {
if (!(tag instanceof CompoundTag)) {
throw new DataException("CompoundTag expected as child tag of Trap Items");
}
CompoundTag item = (CompoundTag) tag;
Map<String, Tag> itemValues = item.getValue();
short id = NBTUtils.getChildTag(itemValues, "id", ShortTag.class).getValue();
short damage = NBTUtils.getChildTag(itemValues, "Damage", ShortTag.class).getValue();
byte count = NBTUtils.getChildTag(itemValues, "Count", ByteTag.class).getValue();
byte slot = NBTUtils.getChildTag(itemValues, "Slot", ByteTag.class).getValue();
if (slot >= 0 && slot <= 8) {
BaseItemStack itemstack = new BaseItemStack(id, count, damage);
if(itemValues.containsKey("tag")) {
ListTag ench = (ListTag) NBTUtils.getChildTag(itemValues, "tag", CompoundTag.class).getValue().get("ench");
for(Tag e : ench.getValue()) {
Map<String, Tag> vars = ((CompoundTag) e).getValue();
short enchid = NBTUtils.getChildTag(vars, "id", ShortTag.class).getValue();
short enchlvl = NBTUtils.getChildTag(vars, "lvl", ShortTag.class).getValue();
itemstack.getEnchantments().put((int) enchid, (int)enchlvl);
}
}
newItems[slot] = itemstack;
}
items.add((CompoundTag) tag);
}
this.items = newItems;
setItems(deserializeInventory(items));
}
}

View File

@ -21,22 +21,18 @@ package com.sk89q.worldedit.blocks;
import com.sk89q.jnbt.*;
import com.sk89q.worldedit.data.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
import java.util.List;
import java.util.ArrayList;
import java.util.Map.Entry;
/**
* Represents furnaces.
*
* @author sk89q
*/
public class FurnaceBlock extends BaseBlock implements TileEntityBlock, ContainerBlock {
/**
* Store the list of items.
*/
private BaseItemStack[] items;
public class FurnaceBlock extends ContainerBlock {
/**
* Fuel time.
@ -54,8 +50,7 @@ public class FurnaceBlock extends BaseBlock implements TileEntityBlock, Containe
* @param type
*/
public FurnaceBlock(int type) {
super(type);
items = new BaseItemStack[2];
super(type, 2);
}
/**
@ -65,8 +60,7 @@ public class FurnaceBlock extends BaseBlock implements TileEntityBlock, Containe
* @param data
*/
public FurnaceBlock(int type, int data) {
super(type, data);
items = new BaseItemStack[2];
super(type, data, 2);
}
/**
@ -77,24 +71,8 @@ public class FurnaceBlock extends BaseBlock implements TileEntityBlock, Containe
* @param items
*/
public FurnaceBlock(int type, int data, BaseItemStack[] items) {
super(type, data);
this.items = items;
}
/**
* Get the list of items.
*
* @return
*/
public BaseItemStack[] getItems() {
return items;
}
/**
* Set the list of items.
*/
public void setItems(BaseItemStack[] items) {
this.items = items;
super(type, data, 2);
setItems(items);
}
/**
@ -142,36 +120,8 @@ public class FurnaceBlock extends BaseBlock implements TileEntityBlock, Containe
*/
public Map<String, Tag> toTileEntityNBT()
throws DataException {
List<Tag> itemsList = new ArrayList<Tag>();
for (int i = 0; i < items.length; ++i) {
BaseItemStack item = items[i];
if (item != null) {
Map<String, Tag> data = new HashMap<String, Tag>();
CompoundTag itemTag = new CompoundTag("Items", data);
data.put("id", new ShortTag("id", (short) item.getType()));
data.put("Damage", new ShortTag("Damage", item.getDamage()));
data.put("Count", new ByteTag("Count", (byte) item.getAmount()));
data.put("Slot", new ByteTag("Slot", (byte) i));
if(item.getEnchantments().size() > 0) {
Map<String, Tag> ench = new HashMap<String, Tag>();
CompoundTag compound = new CompoundTag("tag", ench);
List<Tag> list = new ArrayList<Tag>();
ListTag enchlist = new ListTag("ench", CompoundTag.class, list);
for(Entry<Integer, Integer> entry : item.getEnchantments().entrySet()) {
Map<String, Tag> enchantment = new HashMap<String, Tag>();
CompoundTag enchantcompound = new CompoundTag(null, enchantment);
enchantment.put("id", new ShortTag("id", entry.getKey().shortValue()));
enchantment.put("lvl", new ShortTag("lvl", entry.getValue().shortValue()));
list.add(enchantcompound);
}
ench.put("ench", enchlist);
data.put("tag", compound);
}
itemsList.add(itemTag);
}
}
Map<String, Tag> values = new HashMap<String, Tag>();
values.put("Items", new ListTag("Items", CompoundTag.class, itemsList));
values.put("Items", new ListTag("Items", CompoundTag.class, serializeInventory(getItems())));
values.put("BurnTime", new ShortTag("BurnTime", burnTime));
values.put("CookTime", new ShortTag("CookTime", cookTime));
return values;
@ -194,40 +144,17 @@ public class FurnaceBlock extends BaseBlock implements TileEntityBlock, Containe
throw new DataException("'Furnace' tile entity expected");
}
ListTag items = (ListTag) NBTUtils.getChildTag(values, "Items", ListTag.class);
BaseItemStack[] newItems = new BaseItemStack[3];
ListTag items = NBTUtils.getChildTag(values, "Items", ListTag.class);
List<CompoundTag> compound = new ArrayList<CompoundTag>();
for (Tag tag : items.getValue()) {
if (!(tag instanceof CompoundTag)) {
throw new DataException("CompoundTag expected as child tag of Trap Items");
}
CompoundTag item = (CompoundTag) tag;
Map<String, Tag> itemValues = item.getValue();
short id = NBTUtils.getChildTag(itemValues, "id", ShortTag.class).getValue();
short damage = NBTUtils.getChildTag(itemValues, "Damage", ShortTag.class).getValue();
byte count = NBTUtils.getChildTag(itemValues, "Count", ByteTag.class).getValue();
byte slot = NBTUtils.getChildTag(itemValues, "Slot", ByteTag.class).getValue();
if (slot >= 0 && slot <= 2) {
BaseItemStack itemstack = new BaseItemStack(id, count, damage);
if(itemValues.containsKey("tag")) {
ListTag ench = (ListTag) NBTUtils.getChildTag(itemValues, "tag", CompoundTag.class).getValue().get("ench");
for(Tag e : ench.getValue()) {
Map<String, Tag> vars = ((CompoundTag) e).getValue();
short enchid = NBTUtils.getChildTag(vars, "id", ShortTag.class).getValue();
short enchlvl = NBTUtils.getChildTag(vars, "lvl", ShortTag.class).getValue();
itemstack.getEnchantments().put((int) enchid, (int)enchlvl);
}
}
newItems[slot] = itemstack;
throw new DataException("CompoundTag expected as child tag of Furnace Items");
}
compound.add((CompoundTag) tag);
}
this.items = newItems;
setItems(deserializeInventory(compound));
t = values.get("BurnTime");
if (t instanceof ShortTag) {

View File

@ -151,8 +151,8 @@ public class MobSpawnerBlock extends BaseBlock implements TileEntityBlock {
throw new DataException("'MobSpawner' tile entity expected");
}
StringTag mobTypeTag = (StringTag) NBTUtils.getChildTag(values, "EntityId", StringTag.class);
ShortTag delayTag = (ShortTag) NBTUtils.getChildTag(values, "Delay", ShortTag.class);
StringTag mobTypeTag = NBTUtils.getChildTag(values, "EntityId", StringTag.class);
ShortTag delayTag = NBTUtils.getChildTag(values, "Delay", ShortTag.class);
this.mobType = mobTypeTag.getValue();
this.delay = delayTag.getValue();

View File

@ -21,6 +21,7 @@ package com.sk89q.worldedit.blocks;
import com.sk89q.jnbt.Tag;
import com.sk89q.worldedit.data.*;
import java.util.Map;
/**
@ -29,12 +30,13 @@ import java.util.Map;
* @author sk89q
*/
public interface TileEntityBlock {
/**
* Return the name of the title entity ID.
*
* @return tile entity ID
*/
public String getTileEntityID();
public abstract String getTileEntityID();
/**
* Store additional tile entity data.
@ -42,7 +44,7 @@ public interface TileEntityBlock {
* @return map of values
* @throws DataException When invalid data is encountered
*/
public Map<String, Tag> toTileEntityNBT()
public abstract Map<String, Tag> toTileEntityNBT()
throws DataException;
/**
@ -51,6 +53,8 @@ public interface TileEntityBlock {
* @param values map of data
* @throws DataException When invalid data is encountered
*/
public void fromTileEntityNBT(Map<String, Tag> values)
public abstract void fromTileEntityNBT(Map<String, Tag> values)
throws DataException;
}

View File

@ -31,6 +31,11 @@ import com.sk89q.worldedit.*;
* @author sk89q
*/
public abstract class ChunkStore {
/**
* >> to chunk
* << from chunk
*/
public static final int CHUNK_SHIFTS = 4;
/**
* Convert a position to a chunk.
*