2011-01-23 08:36:26 +00:00
|
|
|
// $Id$
|
|
|
|
/*
|
|
|
|
* WorldEdit
|
2012-01-05 21:38:23 +00:00
|
|
|
* Copyright (C) 2010 sk89q <http://www.sk89q.com> and contributors
|
2011-01-23 08:36:26 +00:00
|
|
|
*
|
|
|
|
* 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;
|
|
|
|
|
2012-08-23 23:52:37 +00:00
|
|
|
import java.util.ArrayList;
|
2011-01-23 08:36:26 +00:00
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.List;
|
2012-08-23 23:52:37 +00:00
|
|
|
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;
|
2011-01-23 08:36:26 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Represents dispensers.
|
|
|
|
*
|
|
|
|
* @author sk89q
|
|
|
|
*/
|
2012-03-30 04:11:49 +00:00
|
|
|
public class DispenserBlock extends ContainerBlock {
|
2011-01-23 08:36:26 +00:00
|
|
|
|
|
|
|
/**
|
2012-08-23 23:52:37 +00:00
|
|
|
* Construct an empty dispenser block.
|
2011-01-23 08:36:26 +00:00
|
|
|
*/
|
|
|
|
public DispenserBlock() {
|
2012-03-30 04:11:49 +00:00
|
|
|
super(BlockID.DISPENSER, 9);
|
2011-01-23 08:36:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-08-23 23:52:37 +00:00
|
|
|
* Construct an empty dispenser block.
|
2011-01-23 08:36:26 +00:00
|
|
|
*
|
2012-08-23 23:52:37 +00:00
|
|
|
* @param data data value (orientation)
|
2011-01-23 08:36:26 +00:00
|
|
|
*/
|
|
|
|
public DispenserBlock(int data) {
|
2012-03-30 04:11:49 +00:00
|
|
|
super(BlockID.DISPENSER, data, 9);
|
2011-01-23 08:36:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-08-23 23:52:37 +00:00
|
|
|
* Construct a dispenser block with the given orientation and inventory.
|
2011-01-23 08:36:26 +00:00
|
|
|
*
|
2012-08-23 23:52:37 +00:00
|
|
|
* @param data data value (orientation)
|
|
|
|
* @param items array of items in the inventory
|
2011-01-23 08:36:26 +00:00
|
|
|
*/
|
|
|
|
public DispenserBlock(int data, BaseItemStack[] items) {
|
2012-03-30 04:11:49 +00:00
|
|
|
super(BlockID.DISPENSER, data, 9);
|
|
|
|
this.setItems(items);
|
2011-01-23 08:36:26 +00:00
|
|
|
}
|
|
|
|
|
2012-08-23 23:52:37 +00:00
|
|
|
@Override
|
|
|
|
public String getNbtId() {
|
2011-01-23 08:36:26 +00:00
|
|
|
return "Trap";
|
|
|
|
}
|
|
|
|
|
2012-08-23 23:52:37 +00:00
|
|
|
@Override
|
|
|
|
public CompoundTag getNbtData() {
|
2011-11-23 01:29:48 +00:00
|
|
|
Map<String, Tag> values = new HashMap<String, Tag>();
|
2012-08-23 23:52:37 +00:00
|
|
|
values.put("Items", new ListTag("Items", CompoundTag.class,
|
|
|
|
serializeInventory(getItems())));
|
|
|
|
return new CompoundTag(getNbtId(), values);
|
2011-01-23 08:36:26 +00:00
|
|
|
}
|
|
|
|
|
2012-08-23 23:52:37 +00:00
|
|
|
@Override
|
|
|
|
public void setNbtData(CompoundTag rootTag) throws DataException {
|
|
|
|
if (rootTag == null) {
|
2011-01-23 08:36:26 +00:00
|
|
|
return;
|
|
|
|
}
|
2012-08-23 23:52:37 +00:00
|
|
|
|
|
|
|
Map<String, Tag> values = rootTag.getValue();
|
2011-01-23 08:36:26 +00:00
|
|
|
|
|
|
|
Tag t = values.get("id");
|
2011-11-23 01:29:48 +00:00
|
|
|
if (!(t instanceof StringTag) || !((StringTag) t).getValue().equals("Trap")) {
|
2011-01-23 08:36:26 +00:00
|
|
|
throw new DataException("'Trap' tile entity expected");
|
|
|
|
}
|
|
|
|
|
2012-03-30 04:11:49 +00:00
|
|
|
List<CompoundTag> items = new ArrayList<CompoundTag>();
|
|
|
|
for (Tag tag : NBTUtils.getChildTag(values, "Items", ListTag.class).getValue()) {
|
2011-01-23 08:36:26 +00:00
|
|
|
if (!(tag instanceof CompoundTag)) {
|
|
|
|
throw new DataException("CompoundTag expected as child tag of Trap Items");
|
|
|
|
}
|
|
|
|
|
2012-03-30 04:11:49 +00:00
|
|
|
items.add((CompoundTag) tag);
|
2011-01-23 08:36:26 +00:00
|
|
|
}
|
|
|
|
|
2012-03-30 04:11:49 +00:00
|
|
|
setItems(deserializeInventory(items));
|
2011-01-23 08:36:26 +00:00
|
|
|
}
|
|
|
|
}
|