From 7b4cf5f5a7f94b305e9163f077972f04dfbc034f Mon Sep 17 00:00:00 2001 From: Jeremy Schroeder Date: Mon, 8 Apr 2013 14:45:05 -0400 Subject: [PATCH] Fix GIVE/TAKE when dealing with doubles for QTY arguments. --- .../scripts/commands/core/GiveCommand.java | 14 +- .../scripts/commands/core/TakeCommand.java | 147 +++++++++--------- 2 files changed, 80 insertions(+), 81 deletions(-) diff --git a/src/main/java/net/aufdemrand/denizen/scripts/commands/core/GiveCommand.java b/src/main/java/net/aufdemrand/denizen/scripts/commands/core/GiveCommand.java index 4aea923f74..c34d5b60dc 100644 --- a/src/main/java/net/aufdemrand/denizen/scripts/commands/core/GiveCommand.java +++ b/src/main/java/net/aufdemrand/denizen/scripts/commands/core/GiveCommand.java @@ -39,14 +39,14 @@ public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException { GiveType type = null; - int amt = 1; + double amt = 1; dItem item = null; boolean engrave = false; /* Match arguments to expected variables */ for (String thisArg : scriptEntry.getArguments()) { - if (aH.matchesQuantity(thisArg)) - amt = aH.getIntegerFrom(thisArg); + if (aH.matchesValueArg("QTY", thisArg, aH.ArgumentType.Double)) + amt = aH.getDoubleFrom(thisArg); else if (aH.matchesArg("MONEY", thisArg)) type = GiveType.MONEY; @@ -83,7 +83,7 @@ else if (aH.matchesItem(thisArg) || aH.matchesItem("item:" + thisArg)) { public void execute(ScriptEntry scriptEntry) throws CommandExecutionException { GiveType type = (GiveType) scriptEntry.getObject("type"); - Integer amt = (Integer) scriptEntry.getObject("amt"); + Double amt = (Double) scriptEntry.getObject("amt"); dItem item = (dItem) scriptEntry.getObject("item"); Boolean engrave = (Boolean) scriptEntry.getObject("engrave"); @@ -97,17 +97,17 @@ public void execute(ScriptEntry scriptEntry) throws CommandExecutionException { case MONEY: if(Depends.economy != null) - Depends.economy.depositPlayer(scriptEntry.getPlayer().getName(), (double) amt); + Depends.economy.depositPlayer(scriptEntry.getPlayer().getName(), amt); else dB.echoError("No economy loaded! Have you installed Vault and a compatible economy plugin?"); break; case EXP: - scriptEntry.getPlayer().giveExp(amt); + scriptEntry.getPlayer().giveExp(amt.intValue()); break; case ITEM: ItemStack is = item.getItemStack(); - is.setAmount(amt); + is.setAmount(amt.intValue()); if(engrave) is = NBTItem.addCustomNBT(item.getItemStack(), "owner", scriptEntry.getPlayer().getName()); HashMap leftovers = scriptEntry.getPlayer().getInventory().addItem(is); diff --git a/src/main/java/net/aufdemrand/denizen/scripts/commands/core/TakeCommand.java b/src/main/java/net/aufdemrand/denizen/scripts/commands/core/TakeCommand.java index 1046cdada1..9989e34573 100644 --- a/src/main/java/net/aufdemrand/denizen/scripts/commands/core/TakeCommand.java +++ b/src/main/java/net/aufdemrand/denizen/scripts/commands/core/TakeCommand.java @@ -28,86 +28,85 @@ */ public class TakeCommand extends AbstractCommand{ - - private enum TakeType { MONEY, ITEMINHAND, ITEM } - - @Override - public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException { - - TakeType takeType = null; - double quantity = 1; - ItemStack item = null; - - for (String arg : scriptEntry.getArguments()) { - if (aH.matchesArg("MONEY, COINS", arg)) - takeType = TakeType.MONEY; - - else if (aH.matchesArg("ITEMINHAND", arg)) - takeType = TakeType.ITEMINHAND; - - else if (aH.matchesQuantity(arg)) - quantity = aH.getDoubleFrom(arg); - - else if (aH.matchesItem(arg) || aH.matchesItem("item:" + arg)) { - takeType = TakeType.ITEM; + + private enum TakeType { MONEY, ITEMINHAND, ITEM } + + @Override + public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException { + + TakeType takeType = null; + double quantity = 1; + ItemStack item = null; + + for (String arg : scriptEntry.getArguments()) { + if (aH.matchesArg("MONEY, COINS", arg)) + takeType = TakeType.MONEY; + + else if (aH.matchesArg("ITEM_IN_HAND", arg)) + takeType = TakeType.ITEMINHAND; + + else if (aH.matchesValueArg("QTY", arg, aH.ArgumentType.Double)) + quantity = aH.getDoubleFrom(arg); + + else if (aH.matchesItem(arg) || aH.matchesItem("item:" + arg)) { + takeType = TakeType.ITEM; item = aH.getItemFrom(arg).getItemStack(); } - - else throw new InvalidArgumentsException(Messages.ERROR_UNKNOWN_ARGUMENT, arg); - + else throw new InvalidArgumentsException(Messages.ERROR_UNKNOWN_ARGUMENT, arg); } - scriptEntry.addObject("item", item); - scriptEntry.addObject("takeType", takeType); - scriptEntry.addObject("quantity", quantity); + + scriptEntry.addObject("item", item); + scriptEntry.addObject("takeType", takeType); + scriptEntry.addObject("quantity", quantity); } - @Override - public void execute(ScriptEntry scriptEntry) - throws CommandExecutionException { - switch ((TakeType)scriptEntry.getObject("takeType")) { - - case ITEMINHAND: - int inHandAmt = scriptEntry.getPlayer().getItemInHand().getAmount(); - int theAmount = ((Double) scriptEntry.getObject("quantity")).intValue(); - ItemStack newHandItem = new ItemStack(0); - if (theAmount > inHandAmt) { - dB.echoDebug("...player did not have enough of the item in hand, so Denizen just took as many as it could. To avoid this situation, use an IF ."); - scriptEntry.getPlayer().setItemInHand(newHandItem); - } - else { - - // amount is just right! - if (theAmount == inHandAmt) { - scriptEntry.getPlayer().setItemInHand(newHandItem); - } else { - // amount is less than what's in hand, need to make a new itemstack of what's left... - newHandItem = new ItemStack(scriptEntry.getPlayer().getItemInHand().getType(), - inHandAmt - theAmount, scriptEntry.getPlayer().getItemInHand().getData().getData()); - newHandItem.setItemMeta(scriptEntry.getPlayer().getItemInHand().getItemMeta()); - scriptEntry.getPlayer().setItemInHand(newHandItem); - scriptEntry.getPlayer().updateInventory(); - } - } - break; - - case MONEY: - if(Depends.economy != null) { - double amount = (Double) scriptEntry.getObject("quantity"); - dB.echoDebug ("...taking " + amount + " money."); - Depends.economy.withdrawPlayer(scriptEntry.getPlayer().getName(), amount); - } else { - dB.echoError("No economy loaded! Have you installed Vault and a compatible economy plugin?"); - } - break; - - case ITEM: - ((ItemStack) scriptEntry.getObject("item")).setAmount(((Double) scriptEntry.getObject("quantity")).intValue()); - if (!scriptEntry.getPlayer().getInventory().removeItem((ItemStack)scriptEntry.getObject("item")).isEmpty()) - dB.echoDebug("The Player did not have enough " + ((ItemStack) scriptEntry.getObject("item")).getType().toString() - + " on hand, so Denizen took as much as possible. To avoid this situation, use an IF or REQUIREMENT to check."); + @Override + public void execute(ScriptEntry scriptEntry) throws CommandExecutionException { + + switch ((TakeType)scriptEntry.getObject("takeType")) { + + case ITEMINHAND: + int inHandAmt = scriptEntry.getPlayer().getItemInHand().getAmount(); + int theAmount = ((Double) scriptEntry.getObject("quantity")).intValue(); + ItemStack newHandItem = new ItemStack(0); + if (theAmount > inHandAmt) { + dB.echoDebug("...player did not have enough of the item in hand, so Denizen just took as many as it could. To avoid this situation, use an IF ."); + scriptEntry.getPlayer().setItemInHand(newHandItem); + } + else { + + // amount is just right! + if (theAmount == inHandAmt) { + scriptEntry.getPlayer().setItemInHand(newHandItem); + } else { + // amount is less than what's in hand, need to make a new itemstack of what's left... + newHandItem = new ItemStack(scriptEntry.getPlayer().getItemInHand().getType(), + inHandAmt - theAmount, scriptEntry.getPlayer().getItemInHand().getData().getData()); + newHandItem.setItemMeta(scriptEntry.getPlayer().getItemInHand().getItemMeta()); + scriptEntry.getPlayer().setItemInHand(newHandItem); + scriptEntry.getPlayer().updateInventory(); + } + } + break; + + case MONEY: + if(Depends.economy != null) { + double amount = (Double) scriptEntry.getObject("quantity"); + dB.echoDebug ("...taking " + amount + " money."); + Depends.economy.withdrawPlayer(scriptEntry.getPlayer().getName(), amount); + } else { + dB.echoError("No economy loaded! Have you installed Vault and a compatible economy plugin?"); + } break; - } - } + + case ITEM: + ((ItemStack) scriptEntry.getObject("item")).setAmount(((Double) scriptEntry.getObject("quantity")).intValue()); + if (!scriptEntry.getPlayer().getInventory().removeItem((ItemStack)scriptEntry.getObject("item")).isEmpty()) + dB.echoDebug("The Player did not have enough " + ((ItemStack) scriptEntry.getObject("item")).getType().toString() + + " on hand, so Denizen took as much as possible. To avoid this situation, use an IF or REQUIREMENT to check."); + break; + } + } }