Updated foundation classes a bit and added raw access for NBT. This release marks experimental support for custom blocks, and nearly all future Minecraft blocks, and is also the beginning of the gradual transition to the new foundation classes as a replacement of the current BaseBlock, etc. classes.

This commit is contained in:
sk89q
2012-08-23 16:52:37 -07:00
parent a2aae2c4da
commit 48af65cac3
21 changed files with 1037 additions and 515 deletions

View File

@ -20,26 +20,20 @@
package com.sk89q.worldedit.blocks;
import com.sk89q.worldedit.CuboidClipboard.FlipDirection;
import com.sk89q.worldedit.foundation.Block;
/**
* Represents a block.
*
* @see Block new class to replace this one
* @author sk89q
*/
public class BaseBlock {
public class BaseBlock extends Block {
/**
* BaseBlock type.
*/
private short type = 0;
/**
* BaseBlock data.
*/
private short data = 0;
/**
* Construct the block with its type.
* Construct the block with its type, with default data value 0.
*
* @param type
* @param type type ID of block
*/
public BaseBlock(int type) {
this(type, 0);
@ -48,40 +42,29 @@ public class BaseBlock {
/**
* Construct the block with its type and data.
*
* @param type
* @param data
* @param type type ID of block
* @param data data value
*/
public BaseBlock(int type, int data) {
this.type = (short) type;
this.data = (short) data;
super(type, data);
}
/**
* Get the type of block.
*
* @return the type
*/
public int getType() {
return (int) type;
return getId();
}
/**
* Set the type of block.
*
* @param type the type to set
*/
public void setType(int type) {
this.type = (short) type;
}
/**
* @return the data
*/
public int getData() {
return (int) data;
}
/**
* @param data the data to set
*/
public void setData(int data) {
this.data = (short) data;
setId(type);
}
/**
@ -90,24 +73,28 @@ public class BaseBlock {
* @return if air
*/
public boolean isAir() {
return type == BlockID.AIR;
return getType() == BlockID.AIR;
}
/**
* Rotate this block 90 degrees.
*
* @return new data value
*/
public int rotate90() {
int newData = BlockData.rotate90(type, data);
this.data = (short) newData;
return data;
int newData = BlockData.rotate90(getType(), getData());
setData(newData);
return newData;
}
/**
* Rotate this block -90 degrees.
*
* @return new data value
*/
public int rotate90Reverse() {
int newData = BlockData.rotate90Reverse(type, data);
this.data = (short) newData;
int newData = BlockData.rotate90Reverse(getType(), getData());
setData((short) newData);
return newData;
}
@ -118,56 +105,55 @@ public class BaseBlock {
* @return new data value
*/
public int cycleData(int increment) {
int newData = BlockData.cycle(this.type, this.data, increment);
this.data = (short) newData;
int newData = BlockData.cycle(getType(), getData(), increment);
setData((short) newData);
return newData;
}
/**
* Flip this block.
*
* @return this block
*/
public BaseBlock flip() {
data = (short) BlockData.flip(type, data);
setData((short) BlockData.flip(getType(), getData()));
return this;
}
/**
* Flip this block.
* @param direction
*
* @param direction direction to flip in
* @return this block
*/
public BaseBlock flip(FlipDirection direction) {
data = (short) BlockData.flip(type, data, direction);
setData((short) BlockData.flip(getType(), getData(), direction));
return this;
}
/**
* Checks whether the type ID and data value are equal.
*/
@Override
public boolean equals(Object o) {
if (!(o instanceof BaseBlock)) {
return false;
}
return type == ((BaseBlock) o).type && data == ((BaseBlock) o).data;
}
public boolean equalsFuzzy(BaseBlock o) {
return (type == o.type && data == o.data) || data == -1 || o.data == -1;
}
@Override
public int hashCode() {
int ret = type << 3;
if (data != (byte) -1) ret |= data;
return ret;
}
@Override
public String toString() {
return "BaseBlock id: " + getType() + " with damage: " + getData();
return getType() == ((BaseBlock) o).getType() && getData() == ((BaseBlock) o).getData();
}
/**
* Checks if the type is the same, and if data is the same if only data != -1.
*
* @param o other block
* @return true if equal
*/
public boolean equalsFuzzy(BaseBlock o) {
return (getType() == o.getType() && getData() == o.getData()) || getData() == -1 || o.getData() == -1;
}
/**
*
*
* @param iter
* @return
* @deprecated This method is silly

View File

@ -23,44 +23,41 @@ import java.util.HashMap;
import java.util.Map;
/**
* Represents an item.
* Represents an item, without an amount value. See {@link BaseItemStack} for an instance
* with stack amount information.
*
* @author sk89q
*/
public class BaseItem {
/**
* Item ID.
*/
private int id;
/**
* Item damage.
*/
private short damage;
private Map<Integer, Integer> enchantments = new HashMap<Integer, Integer>();
private short data;
private final Map<Integer, Integer> enchantments = new HashMap<Integer, Integer>();
/**
* Construct the object.
*
* @param id
* @param id ID of the item
*/
public BaseItem(int id) {
this.id = id;
this.damage = 0;
this.data = 0;
}
/**
* Construct the object.
*
* @param id
* @param damage
* @param id ID of the item
* @param data data value of the item
*/
public BaseItem(int id, short damage) {
public BaseItem(int id, short data) {
this.id = id;
this.damage = damage;
this.data = data;
}
/**
* Get the type of item.
*
* @return the id
*/
public int getType() {
@ -68,6 +65,8 @@ public class BaseItem {
}
/**
* Get the type of item.
*
* @param id the id to set
*/
public void setType(int id) {
@ -75,19 +74,48 @@ public class BaseItem {
}
/**
* Get the damage value.
*
* @return the damage
*/
@Deprecated
public short getDamage() {
return damage;
return data;
}
/**
* @param damage the damage to set
* Get the data value.
*
* @return the data
*/
public void setDamage(short damage) {
this.damage = damage;
public short getData() {
return data;
}
/**
* Set the data value.
*
* @param data the damage to set
*/
@Deprecated
public void setDamage(short data) {
this.data = data;
}
/**
* Set the data value.
*
* @param data the damage to set
*/
public void setData(short data) {
this.data = data;
}
/**
* Get the map of enchantments.
*
* @return map of enchantments
*/
public Map<Integer, Integer> getEnchantments() {
return enchantments;
}

View File

@ -31,9 +31,9 @@ public class BaseItemStack extends BaseItem {
private int amount = 1;
/**
* Construct the object.
* Construct the object with default stack size of one, with data value of 0.
*
* @param id
* @param id with data value of 0.
*/
public BaseItemStack(int id) {
super(id);
@ -42,8 +42,8 @@ public class BaseItemStack extends BaseItem {
/**
* Construct the object.
*
* @param id
* @param amount
* @param id type ID
* @param amount amount in the stack
*/
public BaseItemStack(int id, int amount) {
super(id);
@ -53,16 +53,18 @@ public class BaseItemStack extends BaseItem {
/**
* Construct the object.
*
* @param id
* @param amount
* @param damage
* @param id type ID
* @param amount amount in the stack
* @param data data value
*/
public BaseItemStack(int id, int amount, short damage) {
super(id, damage);
public BaseItemStack(int id, int amount, short data) {
super(id, data);
this.amount = amount;
}
/**
* Get the number of items in the stack.
*
* @return the amount
*/
public int getAmount() {
@ -70,6 +72,8 @@ public class BaseItemStack extends BaseItem {
}
/**
* Set the amount of items in the stack.
*
* @param amount the amount to set
*/
public void setAmount(int amount) {

View File

@ -19,80 +19,71 @@
package com.sk89q.worldedit.blocks;
import com.sk89q.jnbt.*;
import com.sk89q.worldedit.data.*;
import java.util.Map;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.ArrayList;
import java.util.Map;
import com.sk89q.jnbt.CompoundTag;
import com.sk89q.jnbt.ListTag;
import com.sk89q.jnbt.NBTUtils;
import com.sk89q.jnbt.StringTag;
import com.sk89q.jnbt.Tag;
import com.sk89q.worldedit.data.DataException;
/**
* Represents chests.
* Represents a chest block.
*
* @author sk89q
*/
public class ChestBlock extends ContainerBlock {
/**
* Construct the chest block.
* Construct an empty chest block with the default orientation (data value).
*/
public ChestBlock() {
super(BlockID.CHEST, 27);
}
/**
* Construct the chest block.
* Construct an empty chest block with a custom data value.
*
* @param data
* @param data data indicating the position of the chest
*/
public ChestBlock(int data) {
super(BlockID.CHEST, data, 27);
}
/**
* Construct the chest block.
* Construct the chest block with a custom data value and a list of items.
*
* @param data
* @param items
* @param data data indicating the position of the chest
* @param items array of items
*/
public ChestBlock(int data, BaseItemStack[] items) {
super(BlockID.CHEST, data, 27);
setItems(items);
}
/**
* Get the tile entity ID.
*
* @return
*/
public String getTileEntityID() {
@Override
public String getNbtId() {
return "Chest";
}
/**
* Store additional tile entity data. Returns true if the data is used.
*
* @return map of values
* @throws DataException
*/
public Map<String, Tag> toTileEntityNBT()
throws DataException {
@Override
public CompoundTag getNbtData() {
Map<String, Tag> values = new HashMap<String, Tag>();
values.put("Items", new ListTag("Items", CompoundTag.class, serializeInventory(getItems())));
return values;
return new CompoundTag(getNbtId(), values);
}
/**
* Get additional information from the title entity data.
*
* @param values
* @throws DataException
*/
public void fromTileEntityNBT(Map<String, Tag> values)
throws DataException {
if (values == null) {
@Override
public void setNbtData(CompoundTag rootTag) throws DataException {
if (rootTag == null) {
return;
}
Map<String, Tag> values = rootTag.getValue();
Tag t = values.get("id");
if (!(t instanceof StringTag) || !((StringTag) t).getValue().equals("Chest")) {
@ -100,6 +91,7 @@ public class ChestBlock extends ContainerBlock {
}
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");

View File

@ -19,6 +19,11 @@
package com.sk89q.worldedit.blocks;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.sk89q.jnbt.ByteTag;
import com.sk89q.jnbt.CompoundTag;
import com.sk89q.jnbt.ListTag;
@ -27,17 +32,13 @@ 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 abstract class ContainerBlock extends BaseBlock implements TileEntityBlock {
private BaseItemStack[] items;
public ContainerBlock(int type, int inventorySize) {
@ -50,7 +51,6 @@ public abstract class ContainerBlock extends BaseBlock implements TileEntityBloc
this.items = new BaseItemStack[inventorySize];
}
/**
* Get the list of items.
*
@ -68,6 +68,11 @@ public abstract class ContainerBlock extends BaseBlock implements TileEntityBloc
public void setItems(BaseItemStack[] items) {
this.items = items;
}
@Override
public boolean hasNbtData() {
return true;
}
public Map<String, Tag> serializeItem(BaseItemStack item) {
Map<String, Tag> data = new HashMap<String, Tag>();

View File

@ -19,12 +19,17 @@
package com.sk89q.worldedit.blocks;
import com.sk89q.jnbt.*;
import com.sk89q.worldedit.data.*;
import java.util.Map;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.ArrayList;
import java.util.Map;
import com.sk89q.jnbt.CompoundTag;
import com.sk89q.jnbt.ListTag;
import com.sk89q.jnbt.NBTUtils;
import com.sk89q.jnbt.StringTag;
import com.sk89q.jnbt.Tag;
import com.sk89q.worldedit.data.DataException;
/**
* Represents dispensers.
@ -34,65 +39,52 @@ import java.util.ArrayList;
public class DispenserBlock extends ContainerBlock {
/**
* Construct the dispenser block.
* Construct an empty dispenser block.
*/
public DispenserBlock() {
super(BlockID.DISPENSER, 9);
}
/**
* Construct the dispenser block.
* Construct an empty dispenser block.
*
* @param data
* @param data data value (orientation)
*/
public DispenserBlock(int data) {
super(BlockID.DISPENSER, data, 9);
}
/**
* Construct the dispenser block.
* Construct a dispenser block with the given orientation and inventory.
*
* @param data
* @param items
* @param data data value (orientation)
* @param items array of items in the inventory
*/
public DispenserBlock(int data, BaseItemStack[] items) {
super(BlockID.DISPENSER, data, 9);
this.setItems(items);
}
/**
* Get the tile entity ID.
*
* @return
*/
public String getTileEntityID() {
@Override
public String getNbtId() {
return "Trap";
}
/**
* Store additional tile entity data. Returns true if the data is used.
*
* @return map of values
* @throws DataException
*/
public Map<String, Tag> toTileEntityNBT()
throws DataException {
@Override
public CompoundTag getNbtData() {
Map<String, Tag> values = new HashMap<String, Tag>();
values.put("Items", new ListTag("Items", CompoundTag.class, serializeInventory(getItems())));
return values;
values.put("Items", new ListTag("Items", CompoundTag.class,
serializeInventory(getItems())));
return new CompoundTag(getNbtId(), values);
}
/**
* Get additional information from the title entity data.
*
* @param values
* @throws DataException
*/
public void fromTileEntityNBT(Map<String, Tag> values)
throws DataException {
if (values == null) {
@Override
public void setNbtData(CompoundTag rootTag) throws DataException {
if (rootTag == null) {
return;
}
Map<String, Tag> values = rootTag.getValue();
Tag t = values.get("id");
if (!(t instanceof StringTag) || !((StringTag) t).getValue().equals("Trap")) {

View File

@ -15,60 +15,58 @@
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
*/
package com.sk89q.worldedit.blocks;
import com.sk89q.jnbt.*;
import com.sk89q.worldedit.data.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
import com.sk89q.jnbt.CompoundTag;
import com.sk89q.jnbt.ListTag;
import com.sk89q.jnbt.NBTUtils;
import com.sk89q.jnbt.ShortTag;
import com.sk89q.jnbt.StringTag;
import com.sk89q.jnbt.Tag;
import com.sk89q.worldedit.data.DataException;
/**
* Represents furnaces.
*
* Represents a furnace block.
*
* @author sk89q
*/
public class FurnaceBlock extends ContainerBlock {
/**
* Fuel time.
*/
private short burnTime;
/**
* Cook time.
*/
private short cookTime;
/**
* Construct the chest block.
*
* @param type
* Construct an empty furnace block with the default orientation.
*
* @param type type ID
*/
public FurnaceBlock(int type) {
super(type, 2);
}
/**
* Construct the chest block.
*
* @param type
* @param data
* Construct an empty furnace block with a given orientation.
*
* @param type type ID
* @param data orientation
*/
public FurnaceBlock(int type, int data) {
super(type, data, 2);
}
/**
* Construct the chest block.
*
* @param type
* @param data
* @param items
* Construct an furnace block with a given orientation and inventory.
*
* @param type type ID
* @param data orientation
* @param items inventory items
*/
public FurnaceBlock(int type, int data, BaseItemStack[] items) {
super(type, data, 2);
@ -76,71 +74,67 @@ public class FurnaceBlock extends ContainerBlock {
}
/**
* @return the burnTime
* Get the burn time.
*
* @return the burn time
*/
public short getBurnTime() {
return burnTime;
}
/**
* @param burnTime the burnTime to set
* Set the burn time.
*
* @param burnTime the burn time
*/
public void setBurnTime(short burnTime) {
this.burnTime = burnTime;
}
/**
* @return the cookTime
* Get the cook time.
*
* @return the cook time
*/
public short getCookTime() {
return cookTime;
}
/**
* @param cookTime the cookTime to set
* Set the cook time.
*
* @param cookTime the cook time to set
*/
public void setCookTime(short cookTime) {
this.cookTime = cookTime;
}
/**
* Get the tile entity ID.
*
* @return
*/
public String getTileEntityID() {
@Override
public String getNbtId() {
return "Furnace";
}
/**
* Store additional tile entity data. Returns true if the data is used.
*
* @return map of values
* @throws DataException
*/
public Map<String, Tag> toTileEntityNBT()
throws DataException {
@Override
public CompoundTag getNbtData() {
Map<String, Tag> values = new HashMap<String, Tag>();
values.put("Items", new ListTag("Items", CompoundTag.class, serializeInventory(getItems())));
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;
return new CompoundTag(getNbtId(), values);
}
/**
* Get additional information from the title entity data.
*
* @param values
* @throws DataException
*/
public void fromTileEntityNBT(Map<String, Tag> values)
throws DataException {
if (values == null) {
@Override
public void setNbtData(CompoundTag rootTag) throws DataException {
if (rootTag == null) {
return;
}
Map<String, Tag> values = rootTag.getValue();
Tag t = values.get("id");
if (!(t instanceof StringTag) || !((StringTag) t).getValue().equals("Furnace")) {
if (!(t instanceof StringTag)
|| !((StringTag) t).getValue().equals("Furnace")) {
throw new DataException("'Furnace' tile entity expected");
}
@ -150,7 +144,8 @@ public class FurnaceBlock extends ContainerBlock {
for (Tag tag : items.getValue()) {
if (!(tag instanceof CompoundTag)) {
throw new DataException("CompoundTag expected as child tag of Furnace Items");
throw new DataException(
"CompoundTag expected as child tag of Furnace Items");
}
compound.add((CompoundTag) tag);
}

View File

@ -19,30 +19,29 @@
package com.sk89q.worldedit.blocks;
import com.sk89q.jnbt.*;
import com.sk89q.worldedit.MobType;
import com.sk89q.worldedit.data.*;
import java.util.Map;
import java.util.HashMap;
import java.util.Map;
import com.sk89q.jnbt.CompoundTag;
import com.sk89q.jnbt.NBTUtils;
import com.sk89q.jnbt.ShortTag;
import com.sk89q.jnbt.StringTag;
import com.sk89q.jnbt.Tag;
import com.sk89q.worldedit.MobType;
import com.sk89q.worldedit.data.DataException;
/**
* Represents mob spawners.
* A mob spawner block.
*
* @author sk89q
*/
public class MobSpawnerBlock extends BaseBlock implements TileEntityBlock {
/**
* Store mob spawn type.
*/
private String mobType;
/**
* Delay until next spawn.
*/
private short delay;
/**
* Construct the mob spawner block.
*
* Construct the mob spawner block with a pig as the mob type.
*/
public MobSpawnerBlock() {
super(BlockID.MOB_SPAWNER);
@ -50,9 +49,9 @@ public class MobSpawnerBlock extends BaseBlock implements TileEntityBlock {
}
/**
* Construct the mob spawner block.
* Construct the mob spawner block with a given mob type.
*
* @param mobType
* @param mobType mob type
*/
public MobSpawnerBlock(String mobType) {
super(BlockID.MOB_SPAWNER);
@ -60,9 +59,9 @@ public class MobSpawnerBlock extends BaseBlock implements TileEntityBlock {
}
/**
* Construct the mob spawner block.
* Construct the mob spawner block with a specified data value.
*
* @param data
* @param data data value
*/
public MobSpawnerBlock(int data) {
super(BlockID.MOB_SPAWNER, data);
@ -71,8 +70,8 @@ public class MobSpawnerBlock extends BaseBlock implements TileEntityBlock {
/**
* Construct the mob spawner block.
*
* @param data
* @param mobType
* @param data data value
* @param mobType mob type
*/
public MobSpawnerBlock(int data, String mobType) {
super(BlockID.MOB_SPAWNER, data);
@ -82,7 +81,7 @@ public class MobSpawnerBlock extends BaseBlock implements TileEntityBlock {
/**
* Get the mob type.
*
* @return
* @return the mob type
*/
public String getMobType() {
return mobType;
@ -91,13 +90,15 @@ public class MobSpawnerBlock extends BaseBlock implements TileEntityBlock {
/**
* Set the mob type.
*
* @param mobType
* @param mobType the mob type
*/
public void setMobType(String mobType) {
this.mobType = mobType;
}
/**
* Get the spawn delay.
*
* @return the delay
*/
public short getDelay() {
@ -105,47 +106,40 @@ public class MobSpawnerBlock extends BaseBlock implements TileEntityBlock {
}
/**
* Set the spawn delay.
*
* @param delay the delay to set
*/
public void setDelay(short delay) {
this.delay = delay;
}
@Override
public boolean hasNbtData() {
return true;
}
/**
* Get the tile entity ID.
*
* @return
*/
public String getTileEntityID() {
@Override
public String getNbtId() {
return "MobSpawner";
}
/**
* Store additional tile entity data. Returns true if the data is used.
*
* @return map of values
* @throws DataException
*/
public Map<String, Tag> toTileEntityNBT()
throws DataException {
@Override
public CompoundTag getNbtData() {
Map<String, Tag> values = new HashMap<String, Tag>();
values.put("EntityId", new StringTag("EntityId", mobType));
values.put("Delay", new ShortTag("Delay", delay));
return values;
return new CompoundTag(getNbtId(), values);
}
/**
* Get additional information from the title entity data.
*
* @param values
* @throws DataException
*/
public void fromTileEntityNBT(Map<String, Tag> values)
throws DataException {
if (values == null) {
@Override
public void setNbtData(CompoundTag rootTag) throws DataException {
if (rootTag == null) {
return;
}
Map<String, Tag> values = rootTag.getValue();
Tag t = values.get("id");
if (!(t instanceof StringTag) || !((StringTag) t).getValue().equals("MobSpawner")) {
throw new DataException("'MobSpawner' tile entity expected");

View File

@ -15,27 +15,30 @@
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
*/
package com.sk89q.worldedit.blocks;
import com.sk89q.jnbt.*;
import com.sk89q.worldedit.data.*;
import java.util.Map;
import java.util.HashMap;
import java.util.Map;
import com.sk89q.jnbt.ByteTag;
import com.sk89q.jnbt.CompoundTag;
import com.sk89q.jnbt.StringTag;
import com.sk89q.jnbt.Tag;
import com.sk89q.worldedit.data.DataException;
/**
*
* A note block.
*
* @author sk89q
*/
public class NoteBlock extends BaseBlock implements TileEntityBlock {
/**
* Stores the pitch.
*/
private byte note;
/**
* Construct the note block.
* Construct the note block with a data value of 0.
*/
public NoteBlock() {
super(BlockID.NOTE_BLOCK);
@ -43,9 +46,9 @@ public class NoteBlock extends BaseBlock implements TileEntityBlock {
}
/**
* Construct the note block.
*
* @param data
* Construct the note block with a given data value.
*
* @param data data value
*/
public NoteBlock(int data) {
super(BlockID.NOTE_BLOCK, data);
@ -53,10 +56,10 @@ public class NoteBlock extends BaseBlock implements TileEntityBlock {
}
/**
* Construct the note block.
*
* @param data
* @param note
* Construct the note block with a given data value and note.
*
* @param data data value
* @param note note
*/
public NoteBlock(int data, byte note) {
super(BlockID.NOTE_BLOCK, data);
@ -64,6 +67,8 @@ public class NoteBlock extends BaseBlock implements TileEntityBlock {
}
/**
* Get the note.
*
* @return the note
*/
public byte getNote() {
@ -71,50 +76,44 @@ public class NoteBlock extends BaseBlock implements TileEntityBlock {
}
/**
* Set the note.
*
* @param note the note to set
*/
public void setNote(byte note) {
this.note = note;
}
@Override
public boolean hasNbtData() {
return true;
}
/**
* Return the name of the title entity ID.
*
* @return title entity ID
*/
public String getTileEntityID() {
@Override
public String getNbtId() {
return "Music";
}
/**
* Store additional tile entity data. Returns true if the data is used.
*
* @return map of values
* @throws DataException
*/
public Map<String, Tag> toTileEntityNBT()
throws DataException {
@Override
public CompoundTag getNbtData() {
Map<String, Tag> values = new HashMap<String, Tag>();
values.put("note", new ByteTag("note", note));
return values;
return new CompoundTag(getNbtId(), values);
}
/**
* Get additional information from the title entity data.
*
* @param values
* @throws DataException
*/
public void fromTileEntityNBT(Map<String, Tag> values)
throws DataException {
if (values == null) {
@Override
public void setNbtData(CompoundTag rootTag) throws DataException {
if (rootTag == null) {
return;
}
Map<String, Tag> values = rootTag.getValue();
Tag t;
t = values.get("id");
if (!(t instanceof StringTag) || !((StringTag) t).getValue().equals("Music")) {
if (!(t instanceof StringTag)
|| !((StringTag) t).getValue().equals("Music")) {
throw new DataException("'Music' tile entity expected");
}

View File

@ -15,30 +15,32 @@
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
*/
package com.sk89q.worldedit.blocks;
import com.sk89q.jnbt.*;
import com.sk89q.worldedit.data.*;
import java.util.Map;
import java.util.HashMap;
import java.util.Map;
import com.sk89q.jnbt.CompoundTag;
import com.sk89q.jnbt.StringTag;
import com.sk89q.jnbt.Tag;
import com.sk89q.worldedit.data.DataException;
/**
*
* Represents a sign block.
*
* @author sk89q
*/
public class SignBlock extends BaseBlock implements TileEntityBlock {
/**
* Stores the sign's text.
*/
private String[] text;
/**
* Construct the sign without text.
*
* @param type
* @param data
*
* @param type type ID
* @param data data value (orientation)
*/
public SignBlock(int type, int data) {
super(type, data);
@ -47,17 +49,22 @@ public class SignBlock extends BaseBlock implements TileEntityBlock {
/**
* Construct the sign with text.
*
* @param type
* @param data
* @param text
*
* @param type type ID
* @param data data value (orientation)
* @param text lines of text
*/
public SignBlock(int type, int data, String[] text) {
super(type, data);
if (text == null) {
this.text = new String[] { "", "", "", "" };
}
this.text = text;
}
/**
* Get the text.
*
* @return the text
*/
public String[] getText() {
@ -65,55 +72,52 @@ public class SignBlock extends BaseBlock implements TileEntityBlock {
}
/**
* Set the text.
*
* @param text the text to set
*/
public void setText(String[] text) {
if (text == null) {
throw new IllegalArgumentException("Can't set null text for a sign");
}
this.text = text;
}
@Override
public boolean hasNbtData() {
return true;
}
/**
* Return the name of the title entity ID.
*
* @return title entity ID
*/
public String getTileEntityID() {
@Override
public String getNbtId() {
return "Sign";
}
/**
* Store additional tile entity data. Returns true if the data is used.
*
* @return map of values
* @throws DataException
*/
public Map<String, Tag> toTileEntityNBT()
throws DataException {
@Override
public CompoundTag getNbtData() {
Map<String, Tag> values = new HashMap<String, Tag>();
values.put("Text1", new StringTag("Text1", text[0]));
values.put("Text2", new StringTag("Text2", text[1]));
values.put("Text3", new StringTag("Text3", text[2]));
values.put("Text4", new StringTag("Text4", text[3]));
return values;
return new CompoundTag(getNbtId(), values);
}
/**
* Get additional information from the title entity data.
*
* @param values
* @throws DataException
*/
public void fromTileEntityNBT(Map<String, Tag> values)
throws DataException {
if (values == null) {
@Override
public void setNbtData(CompoundTag rootTag) throws DataException {
if (rootTag == null) {
return;
}
Map<String, Tag> values = rootTag.getValue();
Tag t;
text = new String[] { "", "", "", "" };
t = values.get("id");
if (!(t instanceof StringTag) || !((StringTag) t).getValue().equals("Sign")) {
if (!(t instanceof StringTag)
|| !((StringTag) t).getValue().equals("Sign")) {
throw new DataException("'Sign' tile entity expected");
}

View File

@ -1,7 +1,7 @@
// $Id$
/*
* WorldEdit
* Copyright (C) 2010 sk89q <http://www.sk89q.com> and contributors
* Copyright (C) sk89q <http://www.sk89q.com> and contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@ -19,42 +19,22 @@
package com.sk89q.worldedit.blocks;
import com.sk89q.jnbt.Tag;
import com.sk89q.worldedit.data.*;
import java.util.Map;
import com.sk89q.worldedit.foundation.NbtValued;
/**
* A class implementing this interface has extra TileEntityBlock data to store.
* Indicates a block that contains extra data identified as an NBT structure. Compared
* to a {@link NbtValued}, tile entity blocks also contain an ID.
*
* @see NbtValued
* @author sk89q
*/
public interface TileEntityBlock {
public interface TileEntityBlock extends NbtValued {
/**
* Return the name of the title entity ID.
*
* @return tile entity ID
* @return tile entity ID, non-null string
*/
public abstract String getTileEntityID();
/**
* Store additional tile entity data.
*
* @return map of values
* @throws DataException When invalid data is encountered
*/
public abstract Map<String, Tag> toTileEntityNBT()
throws DataException;
/**
* Get additional information from the tile entity data.
*
* @param values map of data
* @throws DataException When invalid data is encountered
*/
public abstract void fromTileEntityNBT(Map<String, Tag> values)
throws DataException;
String getNbtId();
}