PlotSquared/src/main/java/com/plotsquared/object/schematic/StateWrapper.java

136 lines
4.7 KiB
Java
Raw Normal View History

2015-08-04 17:54:10 +02:00
package com.plotsquared.object.schematic;
2015-02-25 13:25:28 +01:00
2015-07-27 19:50:04 +02:00
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
2015-02-25 13:25:28 +01:00
2015-07-30 16:25:16 +02:00
import org.bukkit.block.BlockState;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.inventory.InventoryHolder;
import org.bukkit.inventory.ItemStack;
import com.intellectualcrafters.jnbt.ByteTag;
import com.intellectualcrafters.jnbt.CompoundTag;
import com.intellectualcrafters.jnbt.ListTag;
import com.intellectualcrafters.jnbt.ShortTag;
import com.intellectualcrafters.jnbt.Tag;
import com.intellectualcrafters.plot.object.schematic.ItemType;
import com.intellectualcrafters.plot.object.schematic.PlotItem;
import com.intellectualcrafters.plot.util.MathMan;
import com.intellectualcrafters.plot.util.SchematicHandler.Schematic;
2015-09-11 12:09:22 +02:00
public class StateWrapper
{
public BlockState state = null;
public CompoundTag tag = null;
2015-09-11 12:09:22 +02:00
public StateWrapper(final BlockState state)
{
2015-02-25 13:25:28 +01:00
this.state = state;
}
2015-09-11 12:09:22 +02:00
public StateWrapper(final CompoundTag tag)
{
this.tag = tag;
}
2015-09-11 12:09:22 +02:00
public boolean restoreTag(final short x, final short y, final short z, final Schematic schematic)
{
if (tag == null) { return false; }
final List<Tag> itemsTag = tag.getListTag("Items").getValue();
final int length = itemsTag.size();
final short[] ids = new short[length];
final byte[] datas = new byte[length];
final byte[] amounts = new byte[length];
for (int i = 0; i < length; i++)
{
final Tag itemTag = itemsTag.get(i);
final CompoundTag itemComp = (CompoundTag) itemTag;
short id = itemComp.getShort("id");
String idStr = itemComp.getString("id");
2015-09-11 12:09:22 +02:00
if ((idStr != null) && !MathMan.isInteger(idStr))
{
2015-02-27 08:50:16 +01:00
idStr = idStr.split(":")[1].toLowerCase();
id = (short) ItemType.getId(idStr);
}
ids[i] = id;
datas[i] = (byte) itemComp.getShort("Damage");
amounts[i] = itemComp.getByte("Count");
}
2015-09-11 12:09:22 +02:00
if (length != 0)
{
schematic.addItem(new PlotItem(x, y, z, ids, datas, amounts));
}
return true;
}
2015-09-11 12:09:22 +02:00
public CompoundTag getTag()
{
if (tag != null) { return tag; }
if (state instanceof InventoryHolder)
{
final InventoryHolder inv = (InventoryHolder) state;
final ItemStack[] contents = inv.getInventory().getContents();
final Map<String, Tag> values = new HashMap<String, Tag>();
2015-02-25 13:25:28 +01:00
values.put("Items", new ListTag("Items", CompoundTag.class, serializeInventory(contents)));
return new CompoundTag(values);
}
return null;
}
2015-09-11 12:09:22 +02:00
public String getId()
{
2015-02-25 13:25:28 +01:00
return "Chest";
}
2015-09-11 12:09:22 +02:00
public List<CompoundTag> serializeInventory(final ItemStack[] items)
{
final List<CompoundTag> tags = new ArrayList<CompoundTag>();
for (int i = 0; i < items.length; ++i)
{
if (items[i] != null)
{
final Map<String, Tag> tagData = serializeItem(items[i]);
2015-02-25 13:25:28 +01:00
tagData.put("Slot", new ByteTag("Slot", (byte) i));
tags.add(new CompoundTag(tagData));
}
}
return tags;
}
2015-09-11 12:09:22 +02:00
public Map<String, Tag> serializeItem(final org.spongepowered.api.item.inventory.ItemStack item)
{
final Map<String, Tag> data = new HashMap<String, Tag>();
2015-08-04 17:54:10 +02:00
// FIXME serialize sponge item
2015-09-11 12:09:22 +02:00
2015-08-04 17:54:10 +02:00
return data;
}
2015-09-11 12:09:22 +02:00
public Map<String, Tag> serializeItem(final ItemStack item)
{
final Map<String, Tag> data = new HashMap<String, Tag>();
2015-02-25 13:25:28 +01:00
data.put("id", new ShortTag("id", (short) item.getTypeId()));
data.put("Damage", new ShortTag("Damage", item.getDurability()));
data.put("Count", new ByteTag("Count", (byte) item.getAmount()));
2015-09-11 12:09:22 +02:00
if (!item.getEnchantments().isEmpty())
{
final List<CompoundTag> enchantmentList = new ArrayList<CompoundTag>();
for (final Entry<Enchantment, Integer> entry : item.getEnchantments().entrySet())
{
final Map<String, Tag> enchantment = new HashMap<String, Tag>();
2015-02-25 13:25:28 +01:00
enchantment.put("id", new ShortTag("id", (short) entry.getKey().getId()));
enchantment.put("lvl", new ShortTag("lvl", entry.getValue().shortValue()));
enchantmentList.add(new CompoundTag(enchantment));
}
2015-09-11 12:09:22 +02:00
final Map<String, Tag> auxData = new HashMap<String, Tag>();
2015-02-25 13:25:28 +01:00
auxData.put("ench", new ListTag("ench", CompoundTag.class, enchantmentList));
data.put("tag", new CompoundTag("tag", auxData));
}
return data;
}
}