Skip to content

Commit

Permalink
Persistent inventory
Browse files Browse the repository at this point in the history
  • Loading branch information
fullwall committed Jul 21, 2014
1 parent cdad679 commit 2b01181
Showing 1 changed file with 21 additions and 2 deletions.
23 changes: 21 additions & 2 deletions src/main/java/net/citizensnpcs/api/trait/trait/Inventory.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import net.citizensnpcs.api.util.DataKey;
import net.citizensnpcs.api.util.ItemStorage;

import org.bukkit.entity.Player;
import org.bukkit.entity.minecart.StorageMinecart;
import org.bukkit.inventory.ItemStack;

/**
Expand All @@ -22,7 +24,7 @@ public Inventory() {

/**
* Gets the contents of an NPC's inventory.
*
*
* @return ItemStack array of an NPC's inventory contents
*/
public ItemStack[] getContents() {
Expand All @@ -34,6 +36,23 @@ public void load(DataKey key) throws NPCLoadException {
contents = parseContents(key);
}

@Override
public void onSpawn() {
switch (npc.getEntity().getType()) {
case PLAYER:
((Player) npc.getEntity()).getInventory().setContents(contents);
break;
case MINECART:
if (npc.getEntity() instanceof StorageMinecart) {
((StorageMinecart) npc.getEntity()).getInventory().setContents(
Arrays.copyOf(contents, contents.length * 2));
}
break;
default:
break;
}
}

private ItemStack[] parseContents(DataKey key) throws NPCLoadException {
ItemStack[] contents = new ItemStack[36];
for (DataKey slotKey : key.getIntegerSubKeys())
Expand All @@ -56,7 +75,7 @@ public void save(DataKey key) {

/**
* Sets the contents of an NPC's inventory.
*
*
* @param contents
* ItemStack array to set as the contents of an NPC's inventory
*/
Expand Down

0 comments on commit 2b01181

Please sign in to comment.