Skip to content

Commit

Permalink
Add "bycover" argument for books in the Take command
Browse files Browse the repository at this point in the history
For taking books by title and author name
  • Loading branch information
Morphan1 committed Dec 25, 2014
1 parent 5c93393 commit fba4128
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 34 deletions.
37 changes: 17 additions & 20 deletions src/main/java/net/aufdemrand/denizen/objects/dInventory.java
Expand Up @@ -949,46 +949,43 @@ public dInventory remove(ItemStack[] items) {
* Denizen support for updatable quest journals
* and their like
*
* @param book The itemStack of the book
* @param title The title of the book
* @param author The author of the book
* @param quantity The number of books to remove
* @return The resulting dInventory
*
*/

public dInventory removeBook(ItemStack book) {
public dInventory removeBook(String title, String author, int quantity) {

if (inventory == null || book == null) return this;

// We have to manually keep track of the quantity
// we are removing, because we are not relying on
// Bukkit methods to find matching itemStacks
int qty = book.getAmount();

// Store the book's meta information in a variable
BookMeta bookMeta = (BookMeta) book.getItemMeta();
if (inventory == null || (title == null && author == null)) return this;

for (ItemStack invStack : inventory) {

if (qty == 0) break;
if (quantity == 0) break;

if (invStack != null && invStack.getItemMeta() instanceof BookMeta) {

BookMeta invMeta = (BookMeta) invStack.getItemMeta();

if (invMeta.getAuthor().equalsIgnoreCase(bookMeta.getAuthor())
&& invMeta.getTitle().equalsIgnoreCase(bookMeta.getTitle())) {
String invTitle = invMeta.getTitle();
String invAuthor = invMeta.getAuthor();

if ((invTitle == null && title != null) || (invAuthor == null && author != null))
continue;
else if (invTitle == null || invAuthor == null)
continue;

if (invAuthor.equalsIgnoreCase(author) && invTitle.equalsIgnoreCase(title)) {
// Make sure we don't remove more books than we
// need to
if (qty - invStack.getAmount() < 0) {

invStack.setAmount((qty - invStack.getAmount()) * -1);
if (quantity - invStack.getAmount() < 0) {
invStack.setAmount((quantity - invStack.getAmount()) * -1);
}
else {

inventory.removeItem(invStack);

// Update the quantity we still have to remove
qty = qty - invStack.getAmount();
quantity -= invStack.getAmount();
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/net/aufdemrand/denizen/objects/dItem.java
@@ -1,7 +1,6 @@
package net.aufdemrand.denizen.objects;

import net.aufdemrand.denizen.objects.notable.*;
import net.aufdemrand.denizen.objects.notable.Note;
import net.aufdemrand.denizen.objects.properties.item.*;
import net.aufdemrand.denizen.objects.properties.Property;
import net.aufdemrand.denizen.objects.properties.PropertyParser;
Expand All @@ -11,7 +10,8 @@
import net.aufdemrand.denizen.scripts.containers.core.ItemScriptHelper;
import net.aufdemrand.denizen.tags.Attribute;
import net.aufdemrand.denizen.utilities.debugging.dB;
import org.bukkit.*;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.craftbukkit.v1_8_R1.inventory.CraftItemStack;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.Item;
Expand Down
Expand Up @@ -2612,7 +2612,7 @@ public void registerCoreMembers() {

// <--[command]
// @Name Take
// @Syntax take [money/iteminhand/bydisplay:<name>/slot:<#>/<item>|...] (qty:<#>) (from:<inventory>)
// @Syntax take [money/iteminhand/bydisplay:<name>/bycover:<title>|<author>/slot:<#>/<item>|...] (qty:<#>) (from:<inventory>)
// @Required 1
// @Stable stable
// @Short Takes an item from the player.
Expand All @@ -2627,7 +2627,7 @@ public void registerCoreMembers() {
// TODO: Document Command Details
// -->
registerCoreMember(TakeCommand.class,
"TAKE", "take [money/iteminhand/bydisplay:<name>/slot:<#>/<item>|...] (qty:<#>) (from:<inventory>)", 1);
"TAKE", "take [money/iteminhand/bydisplay:<name>/bycover:<title>|<author>/slot:<#>/<item>|...] (qty:<#>) (from:<inventory>)", 1);

// <--[command]
// @Name Teleport
Expand Down
Expand Up @@ -33,7 +33,7 @@

public class TakeCommand extends AbstractCommand{

private enum Type { MONEY, ITEMINHAND, ITEM, INVENTORY, BYDISPLAY, SLOT }
private enum Type { MONEY, ITEMINHAND, ITEM, INVENTORY, BYDISPLAY, SLOT, BYCOVER }

@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {
Expand All @@ -60,6 +60,13 @@ else if (!scriptEntry.hasObject("items")
scriptEntry.addObject("displayname", arg.asElement());
}

else if (!scriptEntry.hasObject("type")
&& !scriptEntry.hasObject("items")
&& arg.matchesPrefix("bycover")) {
scriptEntry.addObject("type", Type.BYCOVER);
scriptEntry.addObject("cover", arg.asType(dList.class));
}

else if (!scriptEntry.hasObject("slot")
&& !scriptEntry.hasObject("type")
&& arg.matchesPrefix("slot")
Expand Down Expand Up @@ -111,6 +118,7 @@ public void execute(ScriptEntry scriptEntry) throws CommandExecutionException {
Element qty = scriptEntry.getElement("qty");
Element displayname = scriptEntry.getElement("displayname");
Element slot = scriptEntry.getElement("slot");
dList titleAuthor = scriptEntry.getdObject("cover");
Type type = (Type) scriptEntry.getObject("type");

Object items_object = scriptEntry.getObject("items");
Expand All @@ -125,7 +133,8 @@ public void execute(ScriptEntry scriptEntry) throws CommandExecutionException {
+ (inventory != null ? inventory.debug(): "")
+ (displayname != null ? displayname.debug(): "")
+ aH.debugObj("Items", items)
+ (slot != null ? slot.debug() : ""));
+ (slot != null ? slot.debug() : "")
+ (titleAuthor != null ? titleAuthor.debug() : ""));

switch (type) {

Expand Down Expand Up @@ -170,14 +179,7 @@ public void execute(ScriptEntry scriptEntry) throws CommandExecutionException {
ItemStack is = item.getItemStack();
is.setAmount(qty.asInt());

// Remove books with a certain title even if they
// are not identical to an item script, to allow
// books that update
if (is.getItemMeta() instanceof BookMeta
&& ((BookMeta) is.getItemMeta()).hasTitle()) {
inventory.removeBook(is);
}
else if (!inventory.removeItem(item, item.getAmount()))
if (!inventory.removeItem(item, item.getAmount()))
dB.echoDebug(scriptEntry, "Inventory does not contain at least "
+ qty.asInt() + " of " + item.getFullString() +
"... Taking as much as possible...");
Expand Down Expand Up @@ -210,6 +212,12 @@ else if (!inventory.removeItem(item, item.getAmount()))
inventory.setSlots(slot.asInt()-1, new ItemStack(Material.AIR));
break;

case BYCOVER:
inventory.removeBook(titleAuthor.get(0),
titleAuthor.size() > 1 ? titleAuthor.get(1) : null,
qty.asInt());
break;

}
}
}

0 comments on commit fba4128

Please sign in to comment.