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;
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Map;
|
|
|
|
|
2012-03-30 04:11:49 +00:00
|
|
|
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;
|
|
|
|
|
2011-01-23 08:36:26 +00:00
|
|
|
/**
|
|
|
|
* Represents a block that stores items.
|
2012-03-30 04:11:49 +00:00
|
|
|
*
|
2011-01-23 08:36:26 +00:00
|
|
|
* @author sk89q
|
|
|
|
*/
|
2012-03-30 04:11:49 +00:00
|
|
|
public abstract class ContainerBlock extends BaseBlock implements TileEntityBlock {
|
2012-08-23 23:52:37 +00:00
|
|
|
|
2012-03-30 04:11:49 +00:00
|
|
|
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];
|
|
|
|
}
|
|
|
|
|
2011-01-23 08:36:26 +00:00
|
|
|
/**
|
|
|
|
* Get the list of items.
|
|
|
|
*
|
|
|
|
* @return
|
|
|
|
*/
|
2012-03-30 04:11:49 +00:00
|
|
|
public BaseItemStack[] getItems() {
|
|
|
|
return this.items;
|
|
|
|
}
|
2011-01-23 08:36:26 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the list of items.
|
2012-03-30 04:11:49 +00:00
|
|
|
*
|
|
|
|
* @param items
|
2011-01-23 08:36:26 +00:00
|
|
|
*/
|
2012-03-30 04:11:49 +00:00
|
|
|
public void setItems(BaseItemStack[] items) {
|
|
|
|
this.items = items;
|
|
|
|
}
|
2012-08-23 23:52:37 +00:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean hasNbtData() {
|
|
|
|
return true;
|
|
|
|
}
|
2012-03-30 04:11:49 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2011-01-23 08:36:26 +00:00
|
|
|
}
|