mirror of
https://github.com/CitizensDev/Citizens2.git
synced 2024-12-25 18:47:40 +01:00
removed Inventory from Citizens, added to API
This commit is contained in:
parent
3a9496fc71
commit
39dd529e9c
Binary file not shown.
@ -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;
|
||||
|
@ -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;
|
||||
|
@ -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
|
||||
|
@ -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 + "}";
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user