removed Inventory from Citizens, added to API

This commit is contained in:
aPunch 2012-02-11 08:33:19 -06:00
parent da7787720d
commit 42ae0e35ad
5 changed files with 3 additions and 99 deletions

Binary file not shown.

View File

@ -13,6 +13,7 @@ import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.api.npc.trait.Character;
import net.citizensnpcs.api.npc.trait.DefaultInstanceFactory;
import net.citizensnpcs.api.npc.trait.Trait;
import net.citizensnpcs.api.npc.trait.trait.Inventory;
import net.citizensnpcs.api.npc.trait.trait.Owner;
import net.citizensnpcs.api.npc.trait.trait.SpawnLocation;
import net.citizensnpcs.api.npc.trait.trait.Spawned;
@ -31,7 +32,6 @@ import net.citizensnpcs.npc.CitizensNPCManager;
import net.citizensnpcs.storage.DatabaseStorage;
import net.citizensnpcs.storage.Storage;
import net.citizensnpcs.storage.YamlStorage;
import net.citizensnpcs.trait.Inventory;
import net.citizensnpcs.trait.LookClose;
import net.citizensnpcs.trait.Sneak;
import net.citizensnpcs.util.Messaging;

View File

@ -3,10 +3,10 @@ package net.citizensnpcs.npc;
import net.citizensnpcs.api.event.NPCDespawnEvent;
import net.citizensnpcs.api.event.NPCSpawnEvent;
import net.citizensnpcs.api.npc.AbstractNPC;
import net.citizensnpcs.api.npc.trait.trait.Inventory;
import net.citizensnpcs.api.npc.trait.trait.SpawnLocation;
import net.citizensnpcs.api.npc.trait.trait.Spawned;
import net.citizensnpcs.npc.ai.CitizensAI;
import net.citizensnpcs.trait.Inventory;
import net.citizensnpcs.util.Messaging;
import net.minecraft.server.EntityLiving;

View File

@ -88,7 +88,7 @@ public class NPCInventory implements IInventory {
index++;
}
npc.getTrait(net.citizensnpcs.trait.Inventory.class).setContents(bukkitItems);
npc.getTrait(net.citizensnpcs.api.npc.trait.trait.Inventory.class).setContents(bukkitItems);
}
@Override

View File

@ -1,96 +0,0 @@
package net.citizensnpcs.trait;
import java.util.HashMap;
import java.util.Map;
import net.citizensnpcs.api.DataKey;
import net.citizensnpcs.api.exception.NPCLoadException;
import net.citizensnpcs.api.npc.trait.SaveId;
import net.citizensnpcs.api.npc.trait.Trait;
import org.bukkit.Material;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.inventory.ItemStack;
@SaveId("inventory")
public class Inventory extends Trait {
private ItemStack[] contents;
public Inventory() {
contents = new ItemStack[36];
}
public Inventory(org.bukkit.inventory.Inventory inventory) {
contents = inventory.getContents();
}
public ItemStack[] getContents() {
return contents;
}
public void setContents(ItemStack[] contents) {
this.contents = contents;
}
@Override
public void load(DataKey key) throws NPCLoadException {
contents = parseContents(key);
}
@Override
public void save(DataKey key) {
int slot = 0;
for (ItemStack item : contents) {
// Clear previous items to avoid conflicts
key.removeKey(String.valueOf(slot));
if (item != null)
saveItem(item, key.getRelative(String.valueOf(slot)));
slot++;
}
}
private ItemStack[] parseContents(DataKey key) throws NPCLoadException {
ItemStack[] contents = new ItemStack[36];
for (DataKey slotKey : key.getIntegerSubKeys())
contents[Integer.parseInt(slotKey.name())] = getItemStack(slotKey);
return contents;
}
private ItemStack getItemStack(DataKey key) throws NPCLoadException {
try {
ItemStack item = new ItemStack(Material.getMaterial(key.getString("name").toUpperCase().replace('-', '_')),
key.getInt("amount"), (short) key.getLong("data"));
if (key.keyExists("enchantments")) {
Map<Enchantment, Integer> enchantments = new HashMap<Enchantment, Integer>();
for (DataKey subKey : key.getRelative("enchantments").getSubKeys()) {
Enchantment enchantment = Enchantment.getByName(subKey.name().toUpperCase().replace('-', '_'));
if (enchantment != null && enchantment.canEnchantItem(item))
enchantments.put(
enchantment,
subKey.getInt("") <= enchantment.getMaxLevel() ? subKey.getInt("") : enchantment
.getMaxLevel());
}
item.addEnchantments(enchantments);
}
return item;
} catch (Exception ex) {
throw new NPCLoadException("Invalid item. " + ex.getMessage());
}
}
private void saveItem(ItemStack item, DataKey key) {
key.setString("name", item.getType().toString());
key.setInt("amount", item.getAmount());
key.setLong("data", item.getDurability());
for (Enchantment enchantment : item.getEnchantments().keySet()) {
key.getRelative("enchantments").setInt(enchantment.getName().toLowerCase().replace('_', '-'),
item.getEnchantmentLevel(enchantment));
}
}
@Override
public String toString() {
return "Inventory{" + contents + "}";
}
}