use of org.bukkit.inventory.ItemStack in project Denizen-For-Bukkit by DenizenScript.
the class TakeCommand method execute.
@Override
public void execute(ScriptEntry scriptEntry) throws CommandExecutionException {
dInventory inventory = (dInventory) scriptEntry.getObject("inventory");
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");
List<dItem> items = null;
if (items_object != null) {
items = (List<dItem>) items_object;
}
dB.report(scriptEntry, getName(), aH.debugObj("Type", type.name()) + qty.debug() + (inventory != null ? inventory.debug() : "") + (displayname != null ? displayname.debug() : "") + aH.debugObj("Items", items) + (slot != null ? slot.debug() : "") + (titleAuthor != null ? titleAuthor.debug() : ""));
switch(type) {
case INVENTORY:
inventory.clear();
break;
case ITEMINHAND:
int inHandAmt = ((BukkitScriptEntryData) scriptEntry.entryData).getPlayer().getPlayerEntity().getItemInHand().getAmount();
int theAmount = (int) qty.asDouble();
ItemStack newHandItem = new ItemStack(0);
if (theAmount > inHandAmt) {
dB.echoDebug(scriptEntry, "...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 <PLAYER.ITEM_IN_HAND.QTY>.");
((BukkitScriptEntryData) scriptEntry.entryData).getPlayer().getPlayerEntity().setItemInHand(newHandItem);
} else {
// amount is just right!
if (theAmount == inHandAmt) {
((BukkitScriptEntryData) scriptEntry.entryData).getPlayer().getPlayerEntity().setItemInHand(newHandItem);
} else {
// amount is less than what's in hand, need to make a new itemstack of what's left...
newHandItem = new ItemStack(((BukkitScriptEntryData) scriptEntry.entryData).getPlayer().getPlayerEntity().getItemInHand().getType(), inHandAmt - theAmount, ((BukkitScriptEntryData) scriptEntry.entryData).getPlayer().getPlayerEntity().getItemInHand().getData().getData());
newHandItem.setItemMeta(((BukkitScriptEntryData) scriptEntry.entryData).getPlayer().getPlayerEntity().getItemInHand().getItemMeta());
((BukkitScriptEntryData) scriptEntry.entryData).getPlayer().getPlayerEntity().setItemInHand(newHandItem);
((BukkitScriptEntryData) scriptEntry.entryData).getPlayer().getPlayerEntity().updateInventory();
}
}
break;
case MONEY:
if (Depends.economy != null) {
Depends.economy.withdrawPlayer(((BukkitScriptEntryData) scriptEntry.entryData).getPlayer().getOfflinePlayer(), qty.asDouble());
} else {
dB.echoError(scriptEntry.getResidingQueue(), "No economy loaded! Have you installed Vault and a compatible economy plugin?");
}
break;
case ITEM:
for (dItem item : items) {
ItemStack is = item.getItemStack();
is.setAmount(qty.asInt());
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...");
}
}
break;
case BYDISPLAY:
int found_items = 0;
if (displayname == null) {
dB.echoError(scriptEntry.getResidingQueue(), "Must specify a displayname!");
return;
}
for (ItemStack it : inventory.getContents()) {
if (found_items < qty.asInt() && it != null && it.hasItemMeta() && it.getItemMeta().hasDisplayName() && it.getItemMeta().getDisplayName().equalsIgnoreCase(displayname.identify())) {
int amt = it.getAmount();
if (found_items + it.getAmount() <= qty.asInt()) {
inventory.getInventory().removeItem(it);
} else {
it.setAmount(it.getAmount() - (qty.asInt() - found_items));
break;
}
found_items += amt;
}
}
break;
case SLOT:
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;
}
}
use of org.bukkit.inventory.ItemStack in project Denizen-For-Bukkit by DenizenScript.
the class ItemFlags method flags.
public dList flags() {
dList output = new dList();
ItemStack itemStack = item.getItemStack();
if (itemStack.hasItemMeta()) {
for (ItemFlag flag : itemStack.getItemMeta().getItemFlags()) {
output.add(flag.name());
}
}
return output;
}
use of org.bukkit.inventory.ItemStack in project Denizen-For-Bukkit by DenizenScript.
the class ItemPatterns method setPatterns.
private void setPatterns(List<Pattern> patterns) {
ItemStack itemStack = item.getItemStack();
ItemMeta itemMeta = itemStack.getItemMeta();
if (itemMeta instanceof BlockStateMeta) {
Banner banner = (Banner) ((BlockStateMeta) itemMeta).getBlockState();
banner.setPatterns(patterns);
banner.update();
((BlockStateMeta) itemMeta).setBlockState(banner);
} else {
((BannerMeta) itemMeta).setPatterns(patterns);
}
itemStack.setItemMeta(itemMeta);
}
use of org.bukkit.inventory.ItemStack in project Denizen-For-Bukkit by DenizenScript.
the class ItemChangeMessage method sendMessage.
public static void sendMessage(Player player, String message) {
ItemStack item = NMSHandler.getInstance().getEntityHelper().getItemInHand(player);
// which is probably the least intrusive
if (item == null || item.getType() == Material.AIR) {
item = new MaterialData(Material.STAINED_GLASS_PANE, DyeColor.GRAY.getDyeData()).toItemStack();
} else {
item = item.clone();
}
ItemMeta meta = item.getItemMeta();
meta.setDisplayName(message);
item.setItemMeta(meta);
int slot = player.getInventory().getHeldItemSlot() + 36;
NMSHandler.getInstance().getPacketHelper().setSlot(player, slot, item, true);
slotChanged.put(player.getUniqueId(), slot);
}
use of org.bukkit.inventory.ItemStack in project Bukkit by Bukkit.
the class PotionTest method illegalApplyToItemStack.
@Test(expected = IllegalArgumentException.class)
public void illegalApplyToItemStack() {
Potion potion = new Potion(PotionType.POISON);
potion.apply(new ItemStack(Material.AIR, 1));
}
Aggregations