Skip to content

Commit

Permalink
Add Inventory command. Will expand soon.
Browse files Browse the repository at this point in the history
  • Loading branch information
davidcernat committed Jul 1, 2013
1 parent 455756e commit 27f9506
Show file tree
Hide file tree
Showing 7 changed files with 236 additions and 38 deletions.
Expand Up @@ -108,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 = new dInventory(player.getPlayerEntity().getInventory()).countItems(item);
final int initialQty = new dInventory(player.getPlayerEntity().getInventory()).count(item, false);

// 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 @@ -119,7 +119,7 @@ public void listenItem(InventoryClickEvent event)
@Override
public void run()
{
int newQty = new dInventory(player.getPlayerEntity().getInventory()).countItems(item);
int newQty = new dInventory(player.getPlayerEntity().getInventory()).count(item, false);
int difference = newQty - initialQty;

// If any items were obtained (i.e. if shift click was
Expand Down
132 changes: 100 additions & 32 deletions src/main/java/net/aufdemrand/denizen/objects/dInventory.java
@@ -1,11 +1,15 @@
package net.aufdemrand.denizen.objects;

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

import net.aufdemrand.denizen.utilities.Utilities;

import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.event.inventory.InventoryType;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;

Expand Down Expand Up @@ -53,6 +57,11 @@ public static boolean matches(String arg) {
public dInventory(Inventory inventory) {
this.inventory = inventory;
}

public dInventory(InventoryType type) {

inventory = Bukkit.getServer().createInventory(null, type);
}


/////////////////////
Expand All @@ -67,30 +76,71 @@ public Inventory getInventory() {
return inventory;
}

public int countItems(ItemStack item)
{
int qty = 0;
public dInventory add(ItemStack[] items) {

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();
}
}
for (ItemStack item : items) {

inventory.addItem(item);
}

return qty;
return this;
}

/**
* Copy this inventory's contents to another inventory,
* cropping it if necessary so that it fits.
*
* @param destination The destination inventory
*
*/

public void copy(dInventory destination) {

// If the destination is smaller than our current inventory,
// remove empty stacks from our current inventory in the hope
// that there will then be enough room

if (destination.getSize() < this.getSize()) {

List<ItemStack> itemList = new ArrayList<ItemStack>();

for (ItemStack item : this.getContents()) {

if (item != null) itemList.add(item);
}

// If there is still not enough room, crop our list of items
// so it fits

if (destination.getSize() < itemList.size()) {

itemList = itemList.subList(0, destination.getSize());
}

// Set the contents of the destination to our modified
// item list

ItemStack[] results = itemList.toArray(new ItemStack[itemList.size()]);
destination.setContents(results);
}
else {

destination.setContents(this.getContents());
}
}

public int countStacks(ItemStack item)
/**
* Count the number or quantities of stacks that
* match an item in an inventory.
*
* @param item The item (can be null)
* @param stacks Whether stacks should be counted
* instead of item quantities
* @return The number of stacks or quantity of items
*
*/

public int count(ItemStack item, boolean stacks)
{
int qty = 0;

Expand All @@ -99,19 +149,37 @@ public int countStacks(ItemStack item)
// If ItemStacks are empty here, they are null
if (invStack != null)
{
// If item is null, add up every stack in the
// If item is null, include all items in the
// inventory
//
// If not, add up the stacks that match the item

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

// If stacks is true, only count the number
// of stacks
//
// Otherwise, count the quantities of stacks

if (stacks == true) qty++;
else qty = qty + invStack.getAmount();
}
}
}

return qty;
}

public ItemStack[] getContents() {
return this.inventory.getContents();
}

public int getSize() {
return this.inventory.getSize();
}

public void setContents(ItemStack[] contents) {
this.inventory.setContents(contents);
}



//////////////////////////////
Expand Down Expand Up @@ -182,29 +250,29 @@ public String getAttribute(Attribute attribute) {

if (attribute.startsWith("qty"))
if (attribute.hasContext(1) && dItem.matches(attribute.getContext(1)))
return new Element(String.valueOf(countItems
(dItem.valueOf(attribute.getContext(1)).getItemStack())))
return new Element(String.valueOf(count
(dItem.valueOf(attribute.getContext(1)).getItemStack(), false)))
.getAttribute(attribute.fulfill(1));
else
return new Element(String.valueOf(countItems(null)))
return new Element(String.valueOf(count(null, false)))
.getAttribute(attribute.fulfill(1));

// Return the number of slots in the inventory

if (attribute.startsWith("size"))
return new Element(String.valueOf(getInventory().getSize()))
return new Element(String.valueOf(getSize()))
.getAttribute(attribute.fulfill(1));

// Get the number of itemstacks that match an item if one is
// specified, or the number of all itemstacks if one is not

if (attribute.startsWith("stacks"))
if (attribute.hasContext(1) && dItem.matches(attribute.getContext(1)))
return new Element(String.valueOf(countStacks
(dItem.valueOf(attribute.getContext(1)).getItemStack())))
return new Element(String.valueOf(count
(dItem.valueOf(attribute.getContext(1)).getItemStack(), true)))
.getAttribute(attribute.fulfill(1));
else
return new Element(String.valueOf(countStacks(null)))
return new Element(String.valueOf(count(null, true)))
.getAttribute(attribute.fulfill(1));

// Return the type of the inventory (e.g. "PLAYER", "CRAFTING")
Expand Down
Expand Up @@ -382,7 +382,7 @@ else return new Element(String.valueOf(this.distance(toLocation)))
return new Element(String.valueOf(getBlock().getBlockPower()))
.getAttribute(attribute.fulfill(1));

if (attribute.startsWith("region")) {
if (attribute.startsWith("in_region")) {
if (Depends.worldGuard == null) {
dB.echoError("Cannot check region! WorldGuard is not loaded!");
return null;
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/net/aufdemrand/denizen/objects/dPlayer.java
Expand Up @@ -13,6 +13,7 @@
import org.bukkit.OfflinePlayer;
import org.bukkit.World;
import org.bukkit.entity.Player;
import org.bukkit.event.inventory.InventoryType;

import java.util.HashMap;
import java.util.Map;
Expand Down Expand Up @@ -263,6 +264,13 @@ public String getAttribute(Attribute attribute) {
return new Element(String.valueOf(getPlayerEntity().getExp() * 100))
.getAttribute(attribute.fulfill(1));

if (attribute.startsWith("equipment"))
// The only way to return correct size for dInventory
// created from equipment is to use a CRAFTING type
// that has the expected 4 slots
return new dInventory(InventoryType.CRAFTING).add(getPlayerEntity().getInventory().getArmorContents())
.getAttribute(attribute.fulfill(1));

if (attribute.startsWith("inventory"))
return new dInventory(getPlayerEntity().getInventory())
.getAttribute(attribute.fulfill(1));
Expand Down
Expand Up @@ -165,6 +165,9 @@ public void registerCoreMembers() {
registerCoreMember(IfCommand.class,
"IF", "if [comparable] (!)(operator) (compared_to) (bridge) (...) [command] (else) (command) +--> see documentation.", 2);

registerCoreMember(InventoryCommand.class,
"INVENTORY", "inventory [origin:<entity>/<x,y,z,world>] [destination:<entity>/<x,y,z,world>]", 2);

registerCoreMember(InvisibleCommand.class,
"INVISIBLE", "invisible [npc] [toggle:true|false|toggle]", 2);

Expand Down
@@ -0,0 +1,122 @@
package net.aufdemrand.denizen.scripts.commands.item;

import net.aufdemrand.denizen.exceptions.CommandExecutionException;
import net.aufdemrand.denizen.exceptions.InvalidArgumentsException;
import net.aufdemrand.denizen.objects.aH;
import net.aufdemrand.denizen.objects.dEntity;
import net.aufdemrand.denizen.objects.dInventory;
import net.aufdemrand.denizen.objects.dLocation;
import net.aufdemrand.denizen.scripts.ScriptEntry;
import net.aufdemrand.denizen.scripts.commands.AbstractCommand;
import net.aufdemrand.denizen.utilities.debugging.dB.Messages;

import org.bukkit.block.BlockState;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.InventoryHolder;

/**
* Lets you store and edit inventories.
*
* @author David Cernat
*/

public class InventoryCommand extends AbstractCommand {

@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {

for (aH.Argument arg : aH.interpret(scriptEntry.getArguments())) {

if (!scriptEntry.hasObject("originEntity") &&
!scriptEntry.hasObject("originLocation") &&
arg.matchesPrefix("origin, o, source, s")) {

// Is entity
if (arg.matchesArgumentType(dEntity.class))
scriptEntry.addObject("originEntity", arg.asType(dEntity.class).setPrefix("entity"));
// Is location
else if (arg.matchesArgumentType(dLocation.class))
scriptEntry.addObject("originLocation", arg.asType(dLocation.class).setPrefix("location"));
}

else if (!scriptEntry.hasObject("destinationEntity") &&
!scriptEntry.hasObject("destinationLocation") &&
arg.matchesPrefix("destination, d")) {

// Is entity
if (arg.matchesArgumentType(dEntity.class))
scriptEntry.addObject("destinationEntity", arg.asType(dEntity.class).setPrefix("entity"));
// Is location
else if (arg.matchesArgumentType(dLocation.class))
scriptEntry.addObject("destinationLocation", arg.asType(dLocation.class).setPrefix("location"));
}
}

// Check to make sure required arguments have been filled

if (!scriptEntry.hasObject("originEntity") &&
!scriptEntry.hasObject("originLocation"))
throw new InvalidArgumentsException(Messages.ERROR_MISSING_OTHER, "ORIGIN");

if (!scriptEntry.hasObject("destinationEntity") &&
!scriptEntry.hasObject("destinationLocation"))
throw new InvalidArgumentsException(Messages.ERROR_MISSING_OTHER, "DESTINATION");
}

@SuppressWarnings("unchecked")
@Override
public void execute(final ScriptEntry scriptEntry) throws CommandExecutionException {
// Get objects
dEntity originEntity = (dEntity) scriptEntry.getObject("originEntity");
dLocation originLocation = (dLocation) scriptEntry.getObject("originLocation");

dEntity destinationEntity = (dEntity) scriptEntry.getObject("destinationEntity");
dLocation destinationLocation = (dLocation) scriptEntry.getObject("destinationLocation");

dInventory origin = null;
dInventory destination = null;

if (originLocation != null) {

BlockState blockState = originLocation.getBlock().getState();

if (blockState instanceof InventoryHolder) {

origin = new dInventory(((InventoryHolder) blockState).getInventory());
}
}
else if (originEntity != null) {

LivingEntity entity = originEntity.getLivingEntity();

if (entity instanceof Player) {

origin = new dInventory(((Player) entity).getInventory());
}
}

if (destinationLocation != null) {

BlockState blockState = destinationLocation.getBlock().getState();

if (blockState instanceof InventoryHolder) {

destination = new dInventory(((InventoryHolder) blockState).getInventory());
}
}
else if (destinationEntity != null) {

LivingEntity entity = destinationEntity.getLivingEntity();

if (entity instanceof Player) {

destination = new dInventory(((Player) entity).getInventory());
}
}

origin.copy(destination);

}
}
3 changes: 0 additions & 3 deletions src/main/java/net/aufdemrand/denizen/utilities/Utilities.java
Expand Up @@ -6,16 +6,13 @@

import net.aufdemrand.denizen.Settings;
import net.aufdemrand.denizen.objects.dNPC;
import net.aufdemrand.denizen.objects.aH;
import net.aufdemrand.denizen.objects.dPlayer;
import net.aufdemrand.denizen.tags.TagManager;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;

/**
* This class has utility methods for various tasks.
Expand Down

0 comments on commit 27f9506

Please sign in to comment.