mirror of
https://github.com/CitizensDev/Citizens2.git
synced 2024-11-26 04:35:50 +01:00
saving inventories kind of works now
This commit is contained in:
parent
589fd79b27
commit
368628094c
@ -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);
|
||||
|
@ -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
|
||||
|
@ -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 {
|
||||
|
Loading…
Reference in New Issue
Block a user