Plex-FAWE/src/main/java/com/sk89q/worldedit/blocks/DispenserBlock.java

165 lines
5.0 KiB
Java
Raw Normal View History

// $Id$
/*
* WorldEdit
* Copyright (C) 2010 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
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* 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.List;
import java.util.ArrayList;
/**
* Represents dispensers.
*
* @author sk89q
*/
public class DispenserBlock extends BaseBlock implements TileEntityBlock, ContainerBlock {
/**
* Store the list of items.
*/
private BaseItemStack[] items;
/**
* Construct the dispenser block.
*/
public DispenserBlock() {
super(BlockID.DISPENSER);
items = new BaseItemStack[9];
}
/**
* Construct the dispenser block.
*
* @param data
*/
public DispenserBlock(int data) {
super(BlockID.DISPENSER, data);
items = new BaseItemStack[9];
}
/**
* Construct the dispenser block.
*
* @param data
* @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;
}
/**
* Get the tile entity ID.
*
* @return
*/
public String getTileEntityID() {
return "Trap";
}
/**
* Store additional tile entity data. Returns true if the data is used.
*
* @return map of values
* @throws DataException
*/
2011-11-23 01:29:48 +00:00
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) {
2011-11-23 01:29:48 +00:00
Map<String, Tag> data = new HashMap<String, Tag>();
CompoundTag itemTag = new CompoundTag("Items", data);
2011-11-23 01:29:48 +00:00
data.put("id", new ShortTag("id", (short) item.getType()));
data.put("Damage", new ShortTag("Damage", item.getDamage()));
2011-11-23 01:29:48 +00:00
data.put("Count", new ByteTag("Count", (byte) item.getAmount()));
data.put("Slot", new ByteTag("Slot", (byte) i));
itemsList.add(itemTag);
}
}
2011-11-23 01:29:48 +00:00
Map<String, Tag> values = new HashMap<String, Tag>();
values.put("Items", new ListTag("Items", CompoundTag.class, itemsList));
return values;
}
/**
* Get additional information from the title entity data.
*
* @param values
* @throws DataException
*/
2011-11-23 01:29:48 +00:00
public void fromTileEntityNBT(Map<String, Tag> values)
throws DataException {
if (values == null) {
return;
}
Tag t = values.get("id");
2011-11-23 01:29:48 +00:00
if (!(t instanceof StringTag) || !((StringTag) t).getValue().equals("Trap")) {
throw new DataException("'Trap' tile entity expected");
}
2011-11-23 01:29:48 +00:00
ListTag items = (ListTag) Chunk.getChildTag(values, "Items", ListTag.class);
BaseItemStack[] newItems = new BaseItemStack[27];
for (Tag tag : items.getValue()) {
if (!(tag instanceof CompoundTag)) {
throw new DataException("CompoundTag expected as child tag of Trap Items");
}
2011-11-23 01:29:48 +00:00
CompoundTag item = (CompoundTag) tag;
Map<String, Tag> itemValues = item.getValue();
2011-11-23 01:29:48 +00:00
short id = (Short) ((ShortTag) Chunk.getChildTag(itemValues, "id", ShortTag.class))
.getValue();
2011-11-23 01:29:48 +00:00
short damage = (Short) ((ShortTag) Chunk.getChildTag(itemValues, "Damage", ShortTag.class))
.getValue();
2011-11-23 01:29:48 +00:00
byte count = (Byte) ((ByteTag) Chunk.getChildTag(itemValues, "Count", ByteTag.class))
.getValue();
2011-11-23 01:29:48 +00:00
byte slot = (Byte) ((ByteTag) Chunk.getChildTag(itemValues, "Slot", ByteTag.class))
.getValue();
if (slot >= 0 && slot <= 8) {
newItems[slot] = new BaseItemStack(id, count, damage);
}
}
this.items = newItems;
}
}