Skip to content

Commit

Permalink
Turn countItems utility into a dInventory method.
Browse files Browse the repository at this point in the history
  • Loading branch information
davidcernat committed Jul 1, 2013
1 parent eea8f6e commit 48dc42f
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 86 deletions.
Expand Up @@ -10,6 +10,7 @@
import net.aufdemrand.denizen.utilities.Utilities;
import net.aufdemrand.denizen.objects.aH;
import net.aufdemrand.denizen.objects.aH.ArgumentType;
import net.aufdemrand.denizen.objects.dInventory;
import net.aufdemrand.denizen.utilities.debugging.dB;
import net.aufdemrand.denizen.utilities.debugging.dB.Messages;
import net.aufdemrand.denizen.utilities.depends.WorldGuardUtilities;
Expand Down Expand Up @@ -107,7 +108,7 @@ public void listenItem(InventoryClickEvent event)
{
// Save the quantity of items of this type that the player had
// before the event took place
final int initialQty = Utilities.countItems(item, player.getPlayerEntity().getInventory());
final int initialQty = new dInventory(player.getPlayerEntity().getInventory()).countItems(item);

// Run a task 1 tick later, after the event has occurred, and
// see how many items of this type the player has then in the
Expand All @@ -118,7 +119,7 @@ public void listenItem(InventoryClickEvent event)
@Override
public void run()
{
int newQty = Utilities.countItems(item, player.getPlayerEntity().getInventory());
int newQty = new dInventory(player.getPlayerEntity().getInventory()).countItems(item);
int difference = newQty - initialQty;

// If any items were obtained (i.e. if shift click was
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/net/aufdemrand/denizen/objects/dColor.java
Expand Up @@ -142,7 +142,7 @@ public boolean isUnique() {

@Override
public String getType() {
return "color";
return "dColor";
}

@Override
Expand Down
70 changes: 55 additions & 15 deletions src/main/java/net/aufdemrand/denizen/objects/dInventory.java
@@ -1,12 +1,20 @@
package net.aufdemrand.denizen.objects;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import net.aufdemrand.denizen.utilities.Utilities;

import org.bukkit.Material;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;

import net.aufdemrand.denizen.tags.Attribute;

public class dInventory implements dObject {


final static Pattern inventoryPattern = Pattern.compile("(\\w+):?(\\d+)?");

//////////////////
// OBJECT FETCHER
////////////////
Expand All @@ -15,11 +23,11 @@ public class dInventory implements dObject {
* Gets an Inventory Object from a string form.
*
* @param string the string
* @return a Material, or null if incorrectly formatted
* @return an Inventory, or null if incorrectly formatted
*
*/
public static dInventory valueOf(String string) {

public static dMaterial valueOf(String string) {
// No match
return null;
}
Expand All @@ -34,6 +42,7 @@ public static dInventory valueOf(String string) {
public static boolean matches(String arg) {

return false;

}


Expand All @@ -58,10 +67,51 @@ public Inventory getInventory() {
return inventory;
}

public int countItems(ItemStack item)
{
int qty = 0;

for (ItemStack invStack : inventory)
{
// If ItemStacks are empty here, they are null
if (invStack != null)
{
// If item is null, add up the quantity of every stack
// in the inventory
//
// If not, add up the quantities of the stacks that
// match the item

if (item == null || invStack.isSimilar(item))
qty = qty + invStack.getAmount();
}
}

return qty;
}



//////////////////////////////
// DSCRIPT ARGUMENT METHODS
/////////////////////////

private String prefix = "Inventory";

@Override
public String getType() {
return "dInventory";
}

@Override
public String getPrefix() {
return null;
return prefix;
}

@Override
public dInventory setPrefix(String prefix) {
this.prefix = prefix;
return this;
}

@Override
Expand All @@ -74,21 +124,11 @@ public boolean isUnique() {
return false;
}

@Override
public String getType() {
return "inventory";
}

@Override
public String identify() {
return null;
}

@Override
public dObject setPrefix(String prefix) {
return null;
}

@Override
public String getAttribute(Attribute attribute) {

Expand Down
Expand Up @@ -142,7 +142,7 @@ public boolean isUnique() {

@Override
public String getType() {
return "material";
return "dMaterial";
}

@Override
Expand Down
66 changes: 0 additions & 66 deletions src/main/java/net/aufdemrand/denizen/utilities/Utilities.java
Expand Up @@ -141,72 +141,6 @@ public String getVersionNumber() {
}


/**
* Counts the quantity of all items in an inventory.
*
* @param inventory the inventory to count it in
*/

public static int countItems(Inventory inventory)
{
int qty = 0;

for (ItemStack invStack : inventory)
{
// If ItemStacks are empty here, they are null
if (invStack != null)
{
qty = qty + invStack.getAmount();
}
}

return qty;
}


/**
* Counts the quantity of items of a specific type in an inventory.
*
* @param item the item as an itemstack
* @param inventory the inventory to count it in
*/

public static int countItems(ItemStack item, Inventory inventory)
{
int qty = 0;

for (ItemStack invStack : inventory)
{
// If ItemStacks are empty here, they are null
if (invStack != null)
{
if (invStack.isSimilar(item))
qty = qty + invStack.getAmount();
}
}

return qty;
}


/**
* Counts the quantity of items of a specific type in an inventory.
*
* @param item the item's material or ID as a string
* @param inventory the inventory to count it in
*/

public static int countItems(String item, Inventory inventory)
{
if (aH.matchesItem("item:" + item))
{
ItemStack itemstack = new ItemStack(aH.getItemFrom("item:" + item).getItemStack());
return countItems(itemstack, inventory);
}

return 0;
}


/**
* Finds the closest Player to a particular location.
Expand Down
Expand Up @@ -11,7 +11,7 @@
import org.bukkit.util.Vector;

/**
* Utilities related to entity yaw and pitches.
* Utilities related to entity yaws and pitches.
*
* @author David Cernat, fullwall
*/
Expand Down

0 comments on commit 48dc42f

Please sign in to comment.