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> <dependency>
<groupId>org.bukkit</groupId> <groupId>org.bukkit</groupId>
<artifactId>bukkit</artifactId> <artifactId>bukkit</artifactId>
<version>1.2.3-R0.3-SNAPSHOT</version> <version>1.2.4-R1.0</version>
</dependency> </dependency>
<!-- Archive reading library for snapshots --> <!-- Archive reading library for snapshots -->

View File

@ -2,24 +2,24 @@ package com.sk89q.jnbt;
/* /*
* JNBT License * JNBT License
* *
* Copyright (c) 2010 Graham Edgecombe * Copyright (c) 2010 Graham Edgecombe
* All rights reserved. * All rights reserved.
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met: * modification, are permitted provided that the following conditions are met:
* *
* * Redistributions of source code must retain the above copyright notice, * * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer. * this list of conditions and the following disclaimer.
* *
* * Redistributions in binary form must reproduce the above copyright * * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the * notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution. * documentation and/or other materials provided with the distribution.
* *
* * Neither the name of the JNBT team nor the names of its * * Neither the name of the JNBT team nor the names of its
* contributors may be used to endorse or promote products derived from * contributors may be used to endorse or promote products derived from
* this software without specific prior written permission. * this software without specific prior written permission.
* *
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
@ -30,7 +30,7 @@ package com.sk89q.jnbt;
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE. * POSSIBILITY OF SUCH DAMAGE.
*/ */
import java.util.Collections; import java.util.Collections;
@ -40,9 +40,9 @@ import com.sk89q.jnbt.Tag;
/** /**
* The <code>TAG_List</code> tag. * The <code>TAG_List</code> tag.
* *
* @author Graham Edgecombe * @author Graham Edgecombe
* *
*/ */
public final class ListTag extends Tag { public final class ListTag extends Tag {
@ -58,7 +58,7 @@ public final class ListTag extends Tag {
/** /**
* Creates the tag. * Creates the tag.
* *
* @param name * @param name
* The name. * The name.
* @param type * @param type
@ -66,7 +66,7 @@ public final class ListTag extends Tag {
* @param value * @param value
* The 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); super(name);
this.type = type; this.type = type;
this.value = Collections.unmodifiableList(value); this.value = Collections.unmodifiableList(value);
@ -74,7 +74,7 @@ public final class ListTag extends Tag {
/** /**
* Gets the type of item in this list. * Gets the type of item in this list.
* *
* @return The type of item in this list. * @return The type of item in this list.
*/ */
public Class<? extends Tag> getType() { public Class<? extends Tag> getType() {

View File

@ -19,24 +19,24 @@ import com.sk89q.worldedit.data.InvalidFormatException;
/* /*
* JNBT License * JNBT License
* *
* Copyright (c) 2010 Graham Edgecombe * Copyright (c) 2010 Graham Edgecombe
* All rights reserved. * All rights reserved.
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met: * modification, are permitted provided that the following conditions are met:
* *
* * Redistributions of source code must retain the above copyright notice, * * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer. * this list of conditions and the following disclaimer.
* *
* * Redistributions in binary form must reproduce the above copyright * * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the * notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution. * documentation and/or other materials provided with the distribution.
* *
* * Neither the name of the JNBT team nor the names of its * * Neither the name of the JNBT team nor the names of its
* contributors may be used to endorse or promote products derived from * contributors may be used to endorse or promote products derived from
* this software without specific prior written permission. * this software without specific prior written permission.
* *
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
@ -47,20 +47,20 @@ import com.sk89q.worldedit.data.InvalidFormatException;
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE. * POSSIBILITY OF SUCH DAMAGE.
*/ */
/** /**
* A class which contains NBT-related utility methods. * A class which contains NBT-related utility methods.
* *
* @author Graham Edgecombe * @author Graham Edgecombe
* *
*/ */
public final class NBTUtils { public final class NBTUtils {
/** /**
* Gets the type name of a tag. * Gets the type name of a tag.
* *
* @param clazz * @param clazz
* The tag class. * The tag class.
* @return The type name. * @return The type name.
@ -98,7 +98,7 @@ public final class NBTUtils {
/** /**
* Gets the type code of a tag class. * Gets the type code of a tag class.
* *
* @param clazz * @param clazz
* The tag class. * The tag class.
* @return The type code. * @return The type code.
@ -129,7 +129,7 @@ public final class NBTUtils {
} else if (clazz.equals(StringTag.class)) { } else if (clazz.equals(StringTag.class)) {
return NBTConstants.TYPE_STRING; return NBTConstants.TYPE_STRING;
} else if (clazz.equals(IntArrayTag.class)) { } else if (clazz.equals(IntArrayTag.class)) {
return NBTConstants.TYPE_INT_ARRAY; return NBTConstants.TYPE_INT_ARRAY;
} else { } else {
throw new IllegalArgumentException("Invalid tag classs (" throw new IllegalArgumentException("Invalid tag classs ("
+ clazz.getName() + ")."); + clazz.getName() + ").");
@ -138,7 +138,7 @@ public final class NBTUtils {
/** /**
* Gets the class of a type of tag. * Gets the class of a type of tag.
* *
* @param type * @param type
* The type. * The type.
* @return The class. * @return The class.
@ -183,7 +183,7 @@ public final class NBTUtils {
private NBTUtils() { private NBTUtils() {
} }
/** /**
* Get child tag of a NBT structure. * Get child tag of a NBT structure.
* *

View File

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

View File

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

View File

@ -25,25 +25,19 @@ import java.util.Map;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Map.Entry;
/** /**
* Represents chests. * Represents chests.
* *
* @author sk89q * @author sk89q
*/ */
public class ChestBlock extends BaseBlock implements TileEntityBlock, ContainerBlock { public class ChestBlock extends ContainerBlock {
/**
* Store the list of items.
*/
private BaseItemStack[] items;
/** /**
* Construct the chest block. * Construct the chest block.
*/ */
public ChestBlock() { public ChestBlock() {
super(BlockID.CHEST); super(BlockID.CHEST, 27);
items = new BaseItemStack[27];
} }
/** /**
@ -52,8 +46,7 @@ public class ChestBlock extends BaseBlock implements TileEntityBlock, ContainerB
* @param data * @param data
*/ */
public ChestBlock(int data) { public ChestBlock(int data) {
super(BlockID.CHEST, data); super(BlockID.CHEST, data, 27);
items = new BaseItemStack[27];
} }
/** /**
@ -63,24 +56,8 @@ public class ChestBlock extends BaseBlock implements TileEntityBlock, ContainerB
* @param items * @param items
*/ */
public ChestBlock(int data, BaseItemStack[] items) { public ChestBlock(int data, BaseItemStack[] items) {
super(BlockID.CHEST, data); super(BlockID.CHEST, data, 27);
this.items = items; setItems(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;
} }
/** /**
@ -100,36 +77,8 @@ public class ChestBlock extends BaseBlock implements TileEntityBlock, ContainerB
*/ */
public Map<String, Tag> toTileEntityNBT() public Map<String, Tag> toTileEntityNBT()
throws DataException { 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>(); 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; return values;
} }
@ -150,39 +99,15 @@ public class ChestBlock extends BaseBlock implements TileEntityBlock, ContainerB
throw new DataException("'Chest' tile entity expected"); throw new DataException("'Chest' tile entity expected");
} }
ListTag items = (ListTag) NBTUtils.getChildTag(values, "Items", ListTag.class); List<CompoundTag> items = new ArrayList<CompoundTag>();
BaseItemStack[] newItems = new BaseItemStack[27]; for (Tag tag : NBTUtils.getChildTag(values, "Items", ListTag.class).getValue()) {
for (Tag tag : items.getValue()) {
if (!(tag instanceof CompoundTag)) { if (!(tag instanceof CompoundTag)) {
throw new DataException("CompoundTag expected as child tag of Chest's Items"); throw new DataException("CompoundTag expected as child tag of Chest's Items");
} }
CompoundTag item = (CompoundTag) tag; items.add((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;
}
} }
this.items = newItems; setItems(deserializeInventory(items));
} }
} }

View File

@ -19,23 +19,119 @@
package com.sk89q.worldedit.blocks; 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. * Represents a block that stores items.
* *
* @author sk89q * @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. * Get the list of items.
* *
* @return * @return
*/ */
public BaseItemStack[] getItems(); public BaseItemStack[] getItems() {
return this.items;
}
/** /**
* Set the list of items. * Set the list of items.
* *
* @param 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 * @author sk89q
*/ */
public class DispenserBlock extends BaseBlock implements TileEntityBlock, ContainerBlock { public class DispenserBlock extends ContainerBlock {
/**
* Store the list of items.
*/
private BaseItemStack[] items;
/** /**
* Construct the dispenser block. * Construct the dispenser block.
*/ */
public DispenserBlock() { public DispenserBlock() {
super(BlockID.DISPENSER); super(BlockID.DISPENSER, 9);
items = new BaseItemStack[9];
} }
/** /**
@ -52,8 +47,7 @@ public class DispenserBlock extends BaseBlock implements TileEntityBlock, Contai
* @param data * @param data
*/ */
public DispenserBlock(int data) { public DispenserBlock(int data) {
super(BlockID.DISPENSER, data); super(BlockID.DISPENSER, data, 9);
items = new BaseItemStack[9];
} }
/** /**
@ -63,24 +57,8 @@ public class DispenserBlock extends BaseBlock implements TileEntityBlock, Contai
* @param items * @param items
*/ */
public DispenserBlock(int data, BaseItemStack[] items) { public DispenserBlock(int data, BaseItemStack[] items) {
super(BlockID.DISPENSER, data); super(BlockID.DISPENSER, data, 9);
this.items = items; this.setItems(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;
} }
/** /**
@ -100,36 +78,8 @@ public class DispenserBlock extends BaseBlock implements TileEntityBlock, Contai
*/ */
public Map<String, Tag> toTileEntityNBT() public Map<String, Tag> toTileEntityNBT()
throws DataException { 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>(); 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; return values;
} }
@ -150,39 +100,15 @@ public class DispenserBlock extends BaseBlock implements TileEntityBlock, Contai
throw new DataException("'Trap' tile entity expected"); throw new DataException("'Trap' tile entity expected");
} }
ListTag items = (ListTag) NBTUtils.getChildTag(values, "Items", ListTag.class); List<CompoundTag> items = new ArrayList<CompoundTag>();
BaseItemStack[] newItems = new BaseItemStack[9]; for (Tag tag : NBTUtils.getChildTag(values, "Items", ListTag.class).getValue()) {
for (Tag tag : items.getValue()) {
if (!(tag instanceof CompoundTag)) { if (!(tag instanceof CompoundTag)) {
throw new DataException("CompoundTag expected as child tag of Trap Items"); throw new DataException("CompoundTag expected as child tag of Trap Items");
} }
CompoundTag item = (CompoundTag) tag; items.add((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;
}
} }
this.items = newItems; setItems(deserializeInventory(items));
} }
} }

View File

@ -21,22 +21,18 @@ package com.sk89q.worldedit.blocks;
import com.sk89q.jnbt.*; import com.sk89q.jnbt.*;
import com.sk89q.worldedit.data.*; import com.sk89q.worldedit.data.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.HashMap; import java.util.HashMap;
import java.util.List;
import java.util.ArrayList;
import java.util.Map.Entry;
/** /**
* Represents furnaces. * Represents furnaces.
* *
* @author sk89q * @author sk89q
*/ */
public class FurnaceBlock extends BaseBlock implements TileEntityBlock, ContainerBlock { public class FurnaceBlock extends ContainerBlock {
/**
* Store the list of items.
*/
private BaseItemStack[] items;
/** /**
* Fuel time. * Fuel time.
@ -54,8 +50,7 @@ public class FurnaceBlock extends BaseBlock implements TileEntityBlock, Containe
* @param type * @param type
*/ */
public FurnaceBlock(int type) { public FurnaceBlock(int type) {
super(type); super(type, 2);
items = new BaseItemStack[2];
} }
/** /**
@ -65,8 +60,7 @@ public class FurnaceBlock extends BaseBlock implements TileEntityBlock, Containe
* @param data * @param data
*/ */
public FurnaceBlock(int type, int data) { public FurnaceBlock(int type, int data) {
super(type, data); super(type, data, 2);
items = new BaseItemStack[2];
} }
/** /**
@ -77,24 +71,8 @@ public class FurnaceBlock extends BaseBlock implements TileEntityBlock, Containe
* @param items * @param items
*/ */
public FurnaceBlock(int type, int data, BaseItemStack[] items) { public FurnaceBlock(int type, int data, BaseItemStack[] items) {
super(type, data); super(type, data, 2);
this.items = items; setItems(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;
} }
/** /**
@ -142,36 +120,8 @@ public class FurnaceBlock extends BaseBlock implements TileEntityBlock, Containe
*/ */
public Map<String, Tag> toTileEntityNBT() public Map<String, Tag> toTileEntityNBT()
throws DataException { 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>(); 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("BurnTime", new ShortTag("BurnTime", burnTime));
values.put("CookTime", new ShortTag("CookTime", cookTime)); values.put("CookTime", new ShortTag("CookTime", cookTime));
return values; return values;
@ -194,40 +144,17 @@ public class FurnaceBlock extends BaseBlock implements TileEntityBlock, Containe
throw new DataException("'Furnace' tile entity expected"); throw new DataException("'Furnace' tile entity expected");
} }
ListTag items = (ListTag) NBTUtils.getChildTag(values, "Items", ListTag.class); ListTag items = NBTUtils.getChildTag(values, "Items", ListTag.class);
BaseItemStack[] newItems = new BaseItemStack[3];
List<CompoundTag> compound = new ArrayList<CompoundTag>();
for (Tag tag : items.getValue()) { for (Tag tag : items.getValue()) {
if (!(tag instanceof CompoundTag)) { if (!(tag instanceof CompoundTag)) {
throw new DataException("CompoundTag expected as child tag of Trap Items"); throw new DataException("CompoundTag expected as child tag of Furnace 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;
} }
compound.add((CompoundTag) tag);
} }
setItems(deserializeInventory(compound));
this.items = newItems;
t = values.get("BurnTime"); t = values.get("BurnTime");
if (t instanceof ShortTag) { if (t instanceof ShortTag) {

View File

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

View File

@ -55,7 +55,7 @@ public class NoteBlock extends BaseBlock implements TileEntityBlock {
/** /**
* Construct the note block. * Construct the note block.
* *
* @param data * @param data
* @param note * @param note
*/ */
public NoteBlock(int data, byte note) { public NoteBlock(int data, byte note) {

View File

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

View File

@ -31,6 +31,11 @@ import com.sk89q.worldedit.*;
* @author sk89q * @author sk89q
*/ */
public abstract class ChunkStore { public abstract class ChunkStore {
/**
* >> to chunk
* << from chunk
*/
public static final int CHUNK_SHIFTS = 4;
/** /**
* Convert a position to a chunk. * Convert a position to a chunk.
* *
@ -66,7 +71,7 @@ public abstract class ChunkStore {
*/ */
public Chunk getChunk(Vector2D pos, LocalWorld world) public Chunk getChunk(Vector2D pos, LocalWorld world)
throws DataException, IOException { throws DataException, IOException {
CompoundTag tag = getChunkTag(pos, world); CompoundTag tag = getChunkTag(pos, world);
Map<String, Tag> tags = tag.getValue(); Map<String, Tag> tags = tag.getValue();
if(tags.containsKey("Sections")) { if(tags.containsKey("Sections")) {