saving inventories kind of works now

This commit is contained in:
aPunch 2012-02-05 04:08:24 -06:00
parent 11fbd0c7b4
commit 8a63d0950e
3 changed files with 22 additions and 5 deletions

View File

@ -20,11 +20,12 @@ public abstract class CitizensNPC extends AbstractNPC {
protected final CitizensNPCManager manager;
protected final CitizensAI ai = new CitizensAI(this);
protected EntityLiving mcEntity;
protected final NPCInventory inventory = new NPCInventory();
protected final NPCInventory inventory;
protected CitizensNPC(CitizensNPCManager manager, int id, String name) {
super(id, name);
this.manager = manager;
inventory = new NPCInventory(this);
}
protected abstract EntityLiving createHandle(Location loc);

View File

@ -10,11 +10,14 @@ import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory;
public class NPCInventory implements IInventory {
private final int size = 36;
private final CitizensNPC npc;
private final ItemStack[] contents;
private final Inventory inventory = new CraftInventory(this);
public NPCInventory() {
contents = new ItemStack[36];
public NPCInventory(CitizensNPC npc) {
this.npc = npc;
contents = new ItemStack[size];
}
@Override
@ -63,7 +66,7 @@ public class NPCInventory implements IInventory {
@Override
public boolean a(EntityHuman entityhuman) {
return true; // always keep showing ?
return true;
}
@Override
@ -72,6 +75,14 @@ public class NPCInventory implements IInventory {
@Override
public void g() {
// close
org.bukkit.inventory.ItemStack[] bukkitItems = new org.bukkit.inventory.ItemStack[size];
int index = 0;
for (ItemStack item : contents)
if (item != null)
bukkitItems[index++] = new org.bukkit.inventory.ItemStack(item.id, item.count, (short) item.getData());
npc.getTrait(net.citizensnpcs.trait.Inventory.class).setContents(bukkitItems);
}
@Override

View File

@ -27,6 +27,10 @@ public class Inventory implements Trait {
return contents;
}
public void setContents(ItemStack[] contents) {
this.contents = contents;
}
@Override
public void load(DataKey key) throws NPCLoadException {
contents = parseContents(key);
@ -36,7 +40,8 @@ public class Inventory implements Trait {
public void save(DataKey key) {
int index = 0;
for (ItemStack item : contents)
saveItem(item, key.getRelative(String.valueOf(index++)));
if (item != null)
saveItem(item, key.getRelative(String.valueOf(index++)));
}
private ItemStack[] parseContents(DataKey key) throws NPCLoadException {