Skip to content

Commit

Permalink
Add "inventory:" to inventory scripts to specify one of http://jd.buk…
Browse files Browse the repository at this point in the history
  • Loading branch information
Morphan1 committed Sep 2, 2013
1 parent 865427d commit ab6e08c
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 18 deletions.
44 changes: 38 additions & 6 deletions src/main/java/net/aufdemrand/denizen/objects/dInventory.java
Expand Up @@ -21,13 +21,15 @@
import org.bukkit.inventory.PlayerInventory;
import org.bukkit.inventory.meta.BookMeta;

import net.aufdemrand.denizen.objects.notable.Notable;
import net.aufdemrand.denizen.objects.notable.NotableManager;
import net.aufdemrand.denizen.scripts.ScriptRegistry;
import net.aufdemrand.denizen.scripts.containers.core.InventoryScriptContainer;
import net.aufdemrand.denizen.tags.Attribute;
import net.aufdemrand.denizen.utilities.debugging.dB;
import net.citizensnpcs.api.CitizensAPI;

public class dInventory implements dObject {
public class dInventory implements dObject, Notable {

/////////////////////
// PATTERNS
Expand All @@ -36,6 +38,34 @@ public class dInventory implements dObject {
final static Pattern inventory_by_type = Pattern.compile("(in@)(npc|player|entity|location|equipment)(\\[)(.+?)(\\])", Pattern.CASE_INSENSITIVE);
final static Pattern inventory_by_script = Pattern.compile("(in@)(.+)");

/////////////////////
// NOTABLE METHODS
/////////////////

public boolean isUnique() {
return holderType.equals("notable");
}

public String getSaveString() {
return holderIdentifier;
}

public void makeUnique(String id) {
holderType_old = holderType;
holderIdentifier_old = holderIdentifier;
holderType = "notable";
holderIdentifier = id;
NotableManager.saveAs(this, id);
}

public void forget() {
NotableManager.remove(holderIdentifier);
holderType = holderType_old;
holderIdentifier = holderIdentifier_old;
holderType_old = null;
holderIdentifier_old = null;
}

//////////////////
// OBJECT FETCHER
////////////////
Expand Down Expand Up @@ -127,6 +157,9 @@ else if (dEntity.matches(h) && dEntity.valueOf(h).isLivingEntity()
if (ScriptRegistry.containsScript(m.group(2), InventoryScriptContainer.class))
return ScriptRegistry.getScriptContainerAs(m.group(2), InventoryScriptContainer.class).getInventoryFrom(player, npc);

if (NotableManager.isSaved(m.group(2)) && NotableManager.isType(m.group(2), dInventory.class))
return (dInventory) NotableManager.getSavedObject(m.group(2));

dB.echoError("Value of dInventory returning null. Invalid script specified: " + m.group(2));
return null;

Expand Down Expand Up @@ -159,7 +192,9 @@ public static boolean matches(String arg) {
/////////////

String holderType = null;
String holderType_old = null;
String holderIdentifier = null;
String holderIdentifier_old = null;

public dInventory(Inventory inventory) {
if (inventory.getHolder() != null) {
Expand Down Expand Up @@ -635,10 +670,6 @@ public void setContents(ItemStack[] contents) {
public String getType() {
return "Inventory";
}

public boolean isUnique() {
return true;
}

public String getPrefix() {
return prefix;
Expand All @@ -654,7 +685,8 @@ public String debug() {
}

public String identify() {
return "in@" + (holderType.equals("script") ? holderIdentifier : (holderType + "[" + holderIdentifier + "]"));
return "in@" + (holderType.equals("script") || holderType.equals("notable")
? holderIdentifier : (holderType + "[" + holderIdentifier + "]"));
}

public String getAttribute(Attribute attribute) {
Expand Down
Expand Up @@ -28,14 +28,9 @@ public InventoryScriptContainer(ConfigurationSection configurationSection, Strin

public int getSize() {
InventoryType invType = getInventoryType();
Integer size = Integer.valueOf(getString("size"));
int size = aH.getIntegerFrom(getString("SIZE", String.valueOf(invType.getDefaultSize())));

if (size > 0) {
if (invType == InventoryType.CHEST)
return Math.round(size/9)*9;
}

return invType.getDefaultSize();
return size;
}

public InventoryType getInventoryType() {
Expand All @@ -44,8 +39,8 @@ public InventoryType getInventoryType() {
try {
InventoryType type = InventoryType.valueOf(typeStr);
return type;
} catch(Exception e) {
}
catch(Exception e) {
return InventoryType.CHEST;
}
}
Expand All @@ -59,8 +54,38 @@ public dInventory getInventoryFrom(dPlayer player, dNPC npc) {
dInventory inventory = null;

try {
if (contains("INVENTORY")) {
if (InventoryType.valueOf(getString("INVENTORY")) != null) {
inventory = new dInventory(InventoryType.valueOf(getString("INVENTORY")), "script", getName());
}
else {
dB.echoError("Invalid inventory type specified. Assuming \"CHEST\"");
}
}
if (contains("SIZE")) {
inventory = new dInventory(aH.getIntegerFrom(getString("SIZE")), "script", getName());
if (inventory != null && !getInventoryType().name().equalsIgnoreCase("CHEST")) {
dB.echoError("You can only set the size of chest inventories!");
}
else {
int size = aH.getIntegerFrom(getString("SIZE"));

if (size == 0) {
dB.echoError("Inventory size can't be 0. Assuming default...");
size = 27;
}
if (size % 9 != 0) {
dB.echoError("Inventory size must be a multiple of 9! Rounding...");
size = Math.round(size/9)*9;
if (size == 0)
size = 9;
}
if (size < 0) {
dB.echoError("Inventory size must be a positive number! Inverting...");
size = size*-1;
}

inventory = new dInventory(aH.getIntegerFrom(getString("SIZE")), "script", getName());
}
}
if (contains("SLOTS")) {
ItemStack[] finalItems = new ItemStack[getSize()];
Expand Down
Expand Up @@ -22,7 +22,7 @@ public class ItemScriptContainer extends ScriptContainer {

dNPC npc = null;
dPlayer player = null;
public Boolean bound = false;
public boolean bound = false;

public ItemScriptContainer(ConfigurationSection configurationSection, String scriptContainerName) {
super(configurationSection, scriptContainerName);
Expand Down Expand Up @@ -82,7 +82,7 @@ public dItem getItemFrom(dPlayer player, dNPC npc) {

// Set if the object is bound to the player
if (contains("BOUND")) {
bound = Boolean.valueOf(TagManager.tag(player, npc, getString("BOUND")));
bound = Boolean.valueOf(TagManager.tag(player, npc, getString("BOUND")));
}

// Set Lore
Expand Down

0 comments on commit ab6e08c

Please sign in to comment.