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

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