items now save in the correct slot, enchantments are saved on inventory close

This commit is contained in:
aPunch 2012-02-05 05:26:55 -06:00
parent 97b6a74c68
commit 1f5dd50d3f
2 changed files with 12 additions and 6 deletions

View File

@ -6,6 +6,7 @@ import net.minecraft.server.ItemStack;
import org.bukkit.craftbukkit.entity.CraftPlayer;
import org.bukkit.craftbukkit.inventory.CraftInventory;
import org.bukkit.craftbukkit.inventory.CraftItemStack;
import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory;
@ -40,9 +41,8 @@ public class NPCInventory implements IInventory {
return itemstack;
} else {
ItemStack itemstack = contents[i].a(j);
if (contents[i].count == 0) {
if (contents[i].count == 0)
contents[i] = null;
}
return itemstack;
}
}
@ -80,9 +80,11 @@ public class NPCInventory implements IInventory {
// close
org.bukkit.inventory.ItemStack[] bukkitItems = new org.bukkit.inventory.ItemStack[size];
int index = 0;
for (ItemStack item : contents)
for (ItemStack item : contents) {
if (item != null)
bukkitItems[index++] = new org.bukkit.inventory.ItemStack(item.id, item.count, (short) item.getData());
bukkitItems[index] = new CraftItemStack(item);
index++;
}
npc.getTrait(net.citizensnpcs.trait.Inventory.class).setContents(bukkitItems);
}

View File

@ -39,9 +39,13 @@ public class Inventory implements Trait {
@Override
public void save(DataKey key) {
int index = 0;
for (ItemStack item : contents)
for (ItemStack item : contents) {
// Clear previous items to avoid conflicts
key.removeKey(String.valueOf(index));
if (item != null)
saveItem(item, key.getRelative(String.valueOf(index++)));
saveItem(item, key.getRelative(String.valueOf(index)));
index++;
}
}
private ItemStack[] parseContents(DataKey key) throws NPCLoadException {