Skip to content

Commit

Permalink
Item no longer extends ItemStack (Potential to fix a few issues)
Browse files Browse the repository at this point in the history
  • Loading branch information
aufdemrand committed Apr 1, 2013
1 parent 3112dc5 commit 3436713
Show file tree
Hide file tree
Showing 20 changed files with 232 additions and 238 deletions.
Expand Up @@ -62,7 +62,7 @@ public void onBuild(List<String> args) {
} catch (Exception e) { dB.echoDebug("...type " + type.name() + " is not valid."); }

} else if (aH.matchesItem(arg)) {
item = aH.getItemFrom(arg);
item = aH.getItemFrom(arg).getItemStack();
dB.echoDebug("...item set to: " + item);
continue;

Expand Down
Expand Up @@ -92,10 +92,9 @@ public void execute(ScriptEntry scriptEntry) throws CommandExecutionException {
}

// Remember the item entity
displayed.put(location.dScriptArgValue(), location.getWorld().dropItem(location, item));
displayed.put(location.dScriptArgValue(), location.getBlock().getLocation().getWorld().dropItem(location, item.getItemStack()));
displayed.get(location.dScriptArgValue()).setPickupDelay(Integer.MAX_VALUE);
displayed.get(location.dScriptArgValue()).setTicksLived(ticks);

}

// Remove the item
Expand Down
Expand Up @@ -71,7 +71,7 @@ public void execute(ScriptEntry scriptEntry) throws CommandExecutionException {

// Set quantity if not specified
if (qty != null && item != null)
item.setAmount(qty);
item.getItemStack().setAmount(qty);
else qty = 1;

// Report to dB
Expand All @@ -84,7 +84,7 @@ public void execute(ScriptEntry scriptEntry) throws CommandExecutionException {
((ExperienceOrb) location.getWorld().spawnEntity(location, EntityType.EXPERIENCE_ORB))
.setExperience(qty);
else
location.getWorld().dropItemNaturally(location, item);
location.getWorld().dropItemNaturally(location, item.getItemStack());

}

Expand Down
Expand Up @@ -49,7 +49,7 @@ public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException
if (aH.matchesArg("ADD, REMOVE, REMOVEALL", arg)) {
action = Action.valueOf(aH.getStringFrom(arg).toUpperCase());
} else if (aH.matchesItem(arg)) {
item = aH.getItemFrom(arg);
item = aH.getItemFrom(arg).getItemStack();
} else throw new InvalidArgumentsException(Messages.ERROR_UNKNOWN_ARGUMENT, arg);
}

Expand Down
Expand Up @@ -54,24 +54,19 @@ public void execute(ScriptEntry scriptEntry)

switch (equipType) {
case BOOTS:
trait.set(4, item);
dB.echoDebug("..." + item.getType() + " equipped to BOOTS");
trait.set(4, item.getItemStack());
break;
case CHEST:
trait.set(2, item);
dB.echoDebug("..." + item.getType() + " equipped to CHEST");
trait.set(2, item.getItemStack());
break;
case HAND:
trait.set(0, item);
dB.echoDebug("..." + item.getType() + " equipped to HAND");
trait.set(0, item.getItemStack());
break;
case HEAD:
trait.set(1, item);
dB.echoDebug("..." + item.getType() + " equipped to HEAD");
trait.set(1, item.getItemStack());
break;
case LEGS:
trait.set(3, item);
dB.echoDebug("..." + item.getType() + " equipped to LEGS");
trait.set(3, item.getItemStack());
break;
}

Expand Down
Expand Up @@ -119,13 +119,13 @@ public void execute(ScriptEntry scriptEntry) throws CommandExecutionException {
break;

case ITEM:
theItem.setAmount(theAmount);
theItem.getItemStack().setAmount(theAmount);
if(engrave) {
dB.echoDebug("...engraving " + theItem + " to the player");
NBTItem.addCustomNBT(theItem, "owner", player.getName());
NBTItem.addCustomNBT(theItem.getItemStack(), "owner", player.getName());
}
dB.echoDebug("..giving player " + theAmount + " of " + theItem);
HashMap<Integer, ItemStack> leftovers = player.getInventory().addItem(theItem);
HashMap<Integer, ItemStack> leftovers = player.getInventory().addItem(theItem.getItemStack());


if (!leftovers.isEmpty()) {
Expand Down
Expand Up @@ -47,7 +47,7 @@ public void onEnable() {
*
*/
public ItemStack getItem(String id) {
if (itemStacks.containsKey(id.toUpperCase())) return itemStacks.get(id.toUpperCase());
if (itemStacks.containsKey(id.toUpperCase())) return itemStacks.get(id.toUpperCase()).getItemStack();
else return null;
}

Expand Down Expand Up @@ -147,7 +147,7 @@ public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException
if (item == null)
throw new InvalidArgumentsException("Must specify a valid ITEM.");
// Set quantity on the ItemStack
item.setAmount(qty);
item.getItemStack().setAmount(qty);
// Save objects to the scriptEntry that are required for ItemStack creation
scriptEntry.addObject("itemstack", item);

Expand Down
Expand Up @@ -126,7 +126,7 @@ else if (aH.matchesLocation(arg)) {

else if (aH.matchesItem(arg)) {
book = aH.getItemFrom(arg);
if (book.getType() == Material.BOOK || book.getType() == Material.WRITTEN_BOOK) {
if (book.getItemStack().getType() == Material.BOOK || book.getItemStack().getType() == Material.WRITTEN_BOOK) {
savedItem = true;
} else {
book = null;
Expand Down Expand Up @@ -167,15 +167,15 @@ public void execute(ScriptEntry scriptEntry) throws CommandExecutionException {
// Post-write action? Can be NONE.
switch (action) {
case DROP:
dropBook(location, book);
dropBook(location, book.getItemStack());
break;

case GIVE:
giveBook(player, book);
giveBook(player, book.getItemStack());
break;

case EQUIP:
equipBook(player, book);
equipBook(player, book.getItemStack());
break;

case NONE:
Expand Down
Expand Up @@ -32,44 +32,34 @@ public class TakeCommand extends AbstractCommand{
private enum TakeType { MONEY, ITEMINHAND, ITEM }

@Override
public void parseArgs(ScriptEntry scriptEntry)
throws InvalidArgumentsException {
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)) {
if (aH.matchesArg("MONEY, COINS", arg))
takeType = TakeType.MONEY;
dB.echoDebug("...taking MONEY");
}

else if (aH.matchesArg("ITEMINHAND", arg)) {

else if (aH.matchesArg("ITEMINHAND", arg))
takeType = TakeType.ITEMINHAND;
dB.echoDebug("...taking ITEMINHAND");
}

else if (aH.matchesQuantity(arg)) {

else if (aH.matchesQuantity(arg))
quantity = aH.getDoubleFrom(arg);
dB.echoDebug ("...set quantity to " + quantity);
}

else if (aH.matchesItem(arg) || aH.matchesItem("item:" + arg)) {

else if (aH.matchesItem(arg) || aH.matchesItem("item:" + arg))
takeType = TakeType.ITEM;
item = aH.getItemFrom(arg);
dB.echoDebug("...taking " + item.getType());
}

else throw new InvalidArgumentsException(Messages.ERROR_UNKNOWN_ARGUMENT, arg);


}
scriptEntry.addObject("item", item);
scriptEntry.addObject("takeType", takeType);
scriptEntry.addObject("quantity", quantity);
}
}

@SuppressWarnings("deprecation")
}

@Override
public void execute(ScriptEntry scriptEntry)
throws CommandExecutionException {
Expand Down
Expand Up @@ -25,7 +25,7 @@ public Item getBookFrom(Player player, dNPC npc) {

public Item writeBookTo(Item book, Player player, dNPC npc) {
// Get current ItemMeta from the book
BookMeta bookInfo = (BookMeta) book.getItemMeta();
BookMeta bookInfo = (BookMeta) book.getItemStack().getItemMeta();



Expand Down Expand Up @@ -53,7 +53,7 @@ public Item writeBookTo(Item book, Player player, dNPC npc) {
}
}

book.setItemMeta(bookInfo);
book.getItemStack().setItemMeta(bookInfo);
return book;
}

Expand Down
Expand Up @@ -10,6 +10,7 @@
import net.aufdemrand.denizen.utilities.arguments.Item;
import net.aufdemrand.denizen.utilities.debugging.dB;
import net.aufdemrand.denizen.utilities.nbt.LeatherColorer;
import net.aufdemrand.denizen.utilities.nbt.NBTItem;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.Player;
Expand All @@ -24,9 +25,9 @@ public ItemScriptContainer(ConfigurationSection configurationSection, String scr
super(configurationSection, scriptContainerName);
}

// public Item getItemFrom() {
// return getItemFrom(null, null);
// }
public Item getItemFrom() {
return getItemFrom(null, null);
}

public Item getItemFrom(Player player, dNPC npc) {
// Try to use this script to make an item.
Expand All @@ -41,7 +42,7 @@ public Item getItemFrom(Player player, dNPC npc) {
// Make sure we're working with a valid base ItemStack
if (stack == null) return null;

ItemMeta meta = stack.getItemMeta();
ItemMeta meta = stack.getItemStack().getItemMeta();

// Set Display Name
if (contains("DISPLAY NAME")){
Expand All @@ -59,7 +60,7 @@ public Item getItemFrom(Player player, dNPC npc) {
meta.setLore(taggedLore);
}

stack.setItemMeta(meta);
stack.getItemStack().setItemMeta(meta);

// Set Enchantments
if (contains("ENCHANTMENTS")) {
Expand All @@ -75,9 +76,9 @@ public Item getItemFrom(Player player, dNPC npc) {
}
// Add enchantment
Enchantment ench = Enchantment.getByName(enchantment.toUpperCase());
stack.addUnsafeEnchantment(ench, level);
stack.getItemStack().addEnchantment(ench, level);
} catch (Exception e) {
// Invalid enchantment information, let's try the next entry
dB.echoError("While constructing '" + getName() + "', there has been a problem. '" + enchantment + "' is an invalid Enchantment!");
continue;
}
}
Expand All @@ -99,7 +100,7 @@ public Item getItemFrom(Player player, dNPC npc) {
}

// Set Id of the stack
stack.setId(getName());
stack.setItemStack(NBTItem.addCustomNBT(stack.getItemStack(), "denizen-script-id", getName()));

} catch (Exception e) {
dB.echoError("Woah! An exception has been called with this item script!");
Expand Down
Expand Up @@ -3,6 +3,7 @@
import net.aufdemrand.denizen.exceptions.RequirementCheckException;
import net.aufdemrand.denizen.scripts.requirements.AbstractRequirement;
import net.aufdemrand.denizen.scripts.requirements.RequirementsContext;
import net.aufdemrand.denizen.utilities.arguments.Item;
import net.aufdemrand.denizen.utilities.arguments.aH;
import net.aufdemrand.denizen.utilities.debugging.dB;
import org.bukkit.inventory.ItemStack;
Expand All @@ -17,30 +18,29 @@ public boolean check(RequirementsContext context, List<String> args) throws Requ

boolean exact = false;
int quantity = 1;
ItemStack itemToCheck = null;
Item itemToCheck = null;

for (String thisArg : args) {
if (aH.matchesQuantity(thisArg)){
if (aH.matchesQuantity(thisArg))
quantity = aH.getIntegerFrom(thisArg);
dB.echoDebug("...quantity set to: " + quantity);
} else if(aH.matchesArg("EXACT, EXACTLY, EQUALS", thisArg)) {

else if(aH.matchesArg("EXACT, EXACTLY, EQUALS", thisArg)) {
exact = true;
dB.echoDebug("...exact item match set to TRUE");
} else {
itemToCheck = new ItemStack(aH.getItemFrom(thisArg));
dB.echoDebug("...item set to: " + itemToCheck);
}
}

if (itemToCheck != null) {
itemToCheck.setAmount(quantity);

else itemToCheck = aH.getItemFrom(thisArg);
}

if (exact) outcome = context.getPlayer().getItemInHand().equals(itemToCheck);
else outcome = context.getPlayer().getItemInHand().isSimilar(itemToCheck);

if(outcome) dB.echoDebug("...player is holding item");
if (itemToCheck != null)
itemToCheck.getItemStack().setAmount(quantity);

if (exact)
outcome = context.getPlayer().getItemInHand().equals(itemToCheck);
else
outcome = context.getPlayer().getItemInHand().isSimilar(itemToCheck.getItemStack());

dB.report("Outcome", (outcome) ? (exact) ? "Player is holding exact item" : "Player is holding item" : "");

return outcome;
}
}
Expand Up @@ -29,16 +29,16 @@ public boolean check(RequirementsContext context, List<String> args)

} else if (aH.matchesItem(arg)) {
if (ScriptRegistry.getScriptContainerAs(aH.getStringFrom(arg), ItemScriptContainer.class) != null) {
item = ScriptRegistry.getScriptContainerAs(aH.getStringFrom(arg), ItemScriptContainer.class).getItemFrom(context.getPlayer(), context.getNPC());
item = ScriptRegistry.getScriptContainerAs(aH.getStringFrom(arg), ItemScriptContainer.class).getItemFrom(context.getPlayer(), context.getNPC()).getItemStack();
dB.echoDebug("...ITEM set from script");
continue;
} else {
item = aH.getItemFrom(arg);
item = aH.getItemFrom(arg).getItemStack();
dB.echoDebug("...ITEM set");
continue;
}
} else if (aH.matchesItem("item:" + arg)) {
item = aH.getItemFrom("item:" + arg);
item = aH.getItemFrom("item:" + arg).getItemStack();
dB.echoDebug("...ITEM set");
continue;

Expand Down
Expand Up @@ -76,7 +76,7 @@ public void clickTrigger(NPCRightClickEvent event) {
String entry_value = TagManager.tag(player, npc, entry.getValue());
// Check if the item specified in the specified id's 'trigger:' key
// matches the item that the player is holding.
if (Item.valueOf(entry_value).matches(player.getItemInHand())
if (Item.valueOf(entry_value).comparesTo(player.getItemInHand()) >= 0
&& script.checkSpecificTriggerScriptRequirementsFor(this.getClass(),
player, npc, entry.getKey()))
id = entry.getKey();
Expand Down
Expand Up @@ -48,7 +48,7 @@ else if (event.getDamager() instanceof Projectile
String entry_value = TagManager.tag(player, npc, entry.getValue());
// Check if the item specified in the specified id's 'trigger:' key
// matches the item that the player is holding.
if (Item.valueOf(entry_value).matches(player.getItemInHand())
if (Item.valueOf(entry_value).comparesTo(player.getItemInHand()) >= 0
&& script.checkSpecificTriggerScriptRequirementsFor(this.getClass(),
player, npc, entry.getKey()))
id = entry.getKey();
Expand Down
Expand Up @@ -517,7 +517,7 @@ else if (subType.equalsIgnoreCase("FAILED"))
}
else if (aH.matchesItem("item:" + subTypeContext))
{
ItemStack item = aH.getItemFrom("item:" + subTypeContext);
ItemStack item = aH.getItemFrom("item:" + subTypeContext).getItemStack();

if (specifier.equalsIgnoreCase("QTY") && (aH.matchesQuantity("qty:" + specifierContext)))
{
Expand All @@ -535,7 +535,7 @@ else if (subType.equalsIgnoreCase("QTY"))

if (aH.matchesItem("item:" + subTypeContext))
{
ItemStack item = new ItemStack(aH.getItemFrom("item:" + subTypeContext));
ItemStack item = new ItemStack(aH.getItemFrom("item:" + subTypeContext).getItemStack());

qty = Utilities.countItems(item, event.getPlayer().getInventory());

Expand Down

0 comments on commit 3436713

Please sign in to comment.