Skip to content

Commit

Permalink
Fix CITIZENS-480 (use IDs for item storage instead of Material names)
Browse files Browse the repository at this point in the history
  • Loading branch information
fullwall committed Mar 11, 2013
1 parent ba76413 commit 30b6660
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 8 deletions.
8 changes: 6 additions & 2 deletions src/main/java/net/citizensnpcs/api/trait/trait/Equipment.java
Expand Up @@ -71,12 +71,14 @@ public void load(DataKey key) throws NPCLoadException {
@Override
@SuppressWarnings("deprecation")
public void onSpawn() {
if (!(npc.getBukkitEntity() instanceof LivingEntity))
return;
if (npc.getBukkitEntity() instanceof Enderman) {
Enderman enderman = (Enderman) npc.getBukkitEntity();
if (equipment[0] != null)
enderman.setCarriedMaterial(equipment[0].getData());
} else {
LivingEntity entity = npc.getBukkitEntity();
LivingEntity entity = (LivingEntity) npc.getBukkitEntity();
EntityEquipment equip = getEquipmentFromEntity(entity);
if (equipment[0] != null)
equip.setItemInHand(equipment[0]);
Expand Down Expand Up @@ -117,12 +119,14 @@ private void saveOrRemove(DataKey key, ItemStack item) {
*/
@SuppressWarnings("deprecation")
public void set(int slot, ItemStack item) {
if (!(npc.getBukkitEntity() instanceof LivingEntity))
return;
if (npc.getBukkitEntity() instanceof Enderman) {
if (slot != 0)
throw new UnsupportedOperationException("Slot can only be 0 for enderman");
((Enderman) npc.getBukkitEntity()).setCarriedMaterial(item.getData());
} else {
EntityEquipment equip = getEquipmentFromEntity(npc.getBukkitEntity());
EntityEquipment equip = getEquipmentFromEntity((LivingEntity) npc.getBukkitEntity());
switch (slot) {
case 0:
equip.setItemInHand(item);
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/net/citizensnpcs/api/util/DataKey.java
Expand Up @@ -17,7 +17,7 @@ protected String createRelativeKey(String from) {
return path;
if (from.charAt(0) == '.')
return path.isEmpty() ? from.substring(1, from.length()) : path + from;
return path.isEmpty() ? from : path + "." + from;
return path.isEmpty() ? from : path + '.' + from;
}

@Override
Expand Down
19 changes: 14 additions & 5 deletions src/main/java/net/citizensnpcs/api/util/ItemStorage.java
Expand Up @@ -121,10 +121,20 @@ private static <T extends ItemMeta> T ensureMeta(ItemStack stack) {
}

public static ItemStack loadItemStack(DataKey root) {
Material matched = Material.matchMaterial(root.getString("type", root.getString("id")));
if (matched == null)
String raw = root.getString("type", root.getString("id"));
if (raw == null || raw.length() == 0)
return null;
ItemStack res = new ItemStack(matched, root.getInt("amount"), (short) (root.getInt("durability",
int id = -1;
try {
id = Integer.parseInt(raw);
} catch (NumberFormatException ex) {
Material match = Material.matchMaterial(root.getString("type", root.getString("id")));
if (match != null)
id = match.getId();
}
if (id <= 0)
return null;
ItemStack res = new ItemStack(id, root.getInt("amount"), (short) (root.getInt("durability",
root.getInt("data", 0))));
if (root.keyExists("mdata") && res.getData() != null) {
res.getData().setData((byte) root.getInt("mdata"));
Expand All @@ -147,8 +157,7 @@ public static void saveItem(DataKey key, ItemStack item) {
if (item == null)
return;
migrateForSave(key);
String type = item.getType() == null ? Material.AIR.name() : item.getType().name();
key.setString("type", type);
key.setInt("type", item.getTypeId());
key.setInt("amount", item.getAmount());
key.setInt("durability", item.getDurability());
if (item.getData() != null) {
Expand Down

0 comments on commit 30b6660

Please sign in to comment.