Skip to content

Commit

Permalink
Fix dInventory errors with double chests and non-Player NPCs. Allow h…
Browse files Browse the repository at this point in the history
…orse, chest minecart and hopper minecart entity dInventories to be used
  • Loading branch information
Morphan1 committed Aug 19, 2013
1 parent a630665 commit 1c9c568
Showing 1 changed file with 87 additions and 23 deletions.
110 changes: 87 additions & 23 deletions src/main/java/net/aufdemrand/denizen/objects/dInventory.java
Expand Up @@ -5,8 +5,12 @@

import org.bukkit.Bukkit;
import org.bukkit.block.BlockState;
import org.bukkit.block.DoubleChest;
import org.bukkit.entity.Horse;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.entity.minecart.HopperMinecart;
import org.bukkit.entity.minecart.StorageMinecart;
import org.bukkit.event.inventory.InventoryType;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.InventoryHolder;
Expand Down Expand Up @@ -154,25 +158,58 @@ public static boolean matches(String arg) {
String holderIdentifier = null;

public dInventory(Inventory inventory) {
this.inventory = inventory;
if (inventory.getHolder() != null) {
if (!(inventory.getHolder() instanceof LivingEntity)) {

InventoryHolder holder = inventory.getHolder();

if (!(holder instanceof LivingEntity)) {
if (holder instanceof DoubleChest)
holderIdentifier = ((DoubleChest) holder).getLocation().toString();
else if (holder instanceof BlockState)
holderIdentifier = getLocation().toString();
holderType = "location";
holderIdentifier = getLocation().toString();
}
else if (inventory.getHolder() instanceof NPC) {
holderType = "npc";
holderIdentifier = String.valueOf(((NPC) inventory.getHolder()).getId());
else if (holder instanceof Player) {
// Check if it's an NPC... currently, only Player NPCs can have inventories
if (((Player) holder).hasMetadata("NPC")) {
holderType = "npc";
holderIdentifier = String.valueOf(((NPC) holder).getId());
}
else {
holderType = "player";
holderIdentifier = ((Player) holder).getName();
}
}
else if (inventory.getHolder() instanceof Player) {
holderType = "player";
holderIdentifier = ((Player) inventory.getHolder()).getName();
else if (holder instanceof Horse) {
holderType = "entity";
holderIdentifier = String.valueOf(((Horse) holder).getEntityId());
}
else {
else if (holder instanceof HopperMinecart) {
holderType = "entity";
holderIdentifier = String.valueOf(((HopperMinecart) holder).getEntityId());
}
else if (holder instanceof StorageMinecart) {
holderType = "entity";
holderIdentifier = String.valueOf(((LivingEntity) inventory.getHolder()).getEntityId());
holderIdentifier = String.valueOf(((StorageMinecart) holder).getEntityId());
}
else {
if (((LivingEntity) holder).hasMetadata("NPC")) {
// Return, because only Players and Player NPCs can have inventories right now.
// Uncomment this when Denizen implements inventories for all!
// holderType = "npc";
// holderIdentifier = String.valueOf(((NPC) holder).getId());

dB.echoError("Only Player-type NPCs can have inventories.");
return;
}
else {
holderType = "entity";
holderIdentifier = String.valueOf(((LivingEntity) holder).getEntityId());
}
}

}
this.inventory = inventory;
}

public dInventory(InventoryType type) {
Expand All @@ -187,22 +224,50 @@ public dInventory(InventoryType type, String id) {
}

public dInventory(InventoryHolder holder) {
this.inventory = holder.getInventory();
if (!(holder instanceof LivingEntity)) {
if (holder instanceof DoubleChest)
holderIdentifier = ((DoubleChest) holder).getLocation().toString();
else if (holder instanceof BlockState)
holderIdentifier = getLocation().toString();
holderType = "location";
holderIdentifier = getLocation().toString();
}
else if (holder instanceof NPC) {
holderType = "npc";
holderIdentifier = String.valueOf(((NPC) inventory.getHolder()).getId());
}
else if (holder instanceof Player) {
holderType = "player";
holderIdentifier = ((Player) inventory.getHolder()).getName();
// Check if it's an NPC... currently, only Player NPCs can have inventories
if (((Player) holder).hasMetadata("NPC")) {
holderType = "npc";
holderIdentifier = String.valueOf(((NPC) holder).getId());
}
else {
holderType = "player";
holderIdentifier = ((Player) holder).getName();
}
}
else {
else if (holder instanceof Horse) {
holderType = "entity";
holderIdentifier = String.valueOf(((Horse) holder).getEntityId());
}
else if (holder instanceof HopperMinecart) {
holderType = "entity";
holderIdentifier = String.valueOf(((LivingEntity) inventory.getHolder()).getEntityId());
holderIdentifier = String.valueOf(((HopperMinecart) holder).getEntityId());
}
else if (holder instanceof StorageMinecart) {
holderType = "entity";
holderIdentifier = String.valueOf(((StorageMinecart) holder).getEntityId());
}
else {
if (((LivingEntity) holder).hasMetadata("NPC")) {
// Return, because only Players and Player NPCs can have inventories right now.
// Uncomment this when Denizen implements inventories for all!
// holderType = "npc";
// holderIdentifier = String.valueOf(((NPC) holder).getId());

dB.echoError("Only Player-type NPCs can have inventories.");
return;
}
else {
holderType = "entity";
holderIdentifier = String.valueOf(((LivingEntity) holder).getEntityId());
}
}
}

Expand Down Expand Up @@ -413,7 +478,6 @@ public dLocation getLocation() {
InventoryHolder holder = inventory.getHolder();

if (holder instanceof BlockState) {

return new dLocation(((BlockState) holder).getLocation());
}
else if (holder instanceof Player) {
Expand Down Expand Up @@ -656,7 +720,7 @@ public String getAttribute(Attribute attribute) {

// <--
// <inventory.type> -> Element
// Returns the type of the inventory (e.g. "PLAYER", "CRAFTING")
// Returns the type of the inventory (e.g. "PLAYER", "CRAFTING", "HORSE")
// -->
if (attribute.startsWith("type"))
return new Element(getInventory().getType().name())
Expand Down

0 comments on commit 1c9c568

Please sign in to comment.