use of com.denizenscript.denizen.objects.ItemTag in project Denizen-For-Bukkit by DenizenScript.
the class ToastCommand method parseArgs.
// <--[command]
// @Name Toast
// @Syntax toast [<text>] (targets:<player>|...) (icon:<item>) (frame:{task}/challenge/goal)
// @Required 1
// @Maximum 4
// @Short Shows the player a custom advancement toast.
// @Group player
//
// @Description
// Displays a client-side custom advancement "toast" notification popup to the player(s).
// If no target is specified it will default to the attached player.
// The icon argument changes the icon displayed in the toast pop-up notification.
// The frame argument changes the type of advancement.
//
// @Tags
// None
//
// @Usage
// Welcomes the player with an advancement toast.
// - toast "Welcome <player.name>!"
//
// @Usage
// Sends the player an advancement toast with a custom icon.
// - toast "Diggy Diggy Hole" icon:iron_spade
//
// @Usage
// Sends the player a "Challenge Complete!" type advancement toast.
// - toast "You finished a challenge!" frame:challenge icon:diamond
//
// -->
@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {
for (Argument arg : scriptEntry) {
if (!scriptEntry.hasObject("targets") && arg.matchesPrefix("target", "targets", "t") && arg.matchesArgumentList(PlayerTag.class)) {
scriptEntry.addObject("targets", arg.asType(ListTag.class).filter(PlayerTag.class, scriptEntry));
} else if (!scriptEntry.hasObject("icon") && arg.matchesPrefix("icon", "i") && arg.matchesArgumentType(ItemTag.class)) {
scriptEntry.addObject("icon", arg.asType(ItemTag.class));
} else if (!scriptEntry.hasObject("frame") && arg.matchesPrefix("frame", "f") && arg.matchesEnum(Advancement.Frame.class)) {
scriptEntry.addObject("frame", arg.asElement());
} else if (!scriptEntry.hasObject("text")) {
scriptEntry.addObject("text", arg.getRawElement());
} else {
arg.reportUnhandled();
}
}
if (!scriptEntry.hasObject("text")) {
throw new InvalidArgumentsException("Must specify a message!");
}
if (!scriptEntry.hasObject("targets")) {
if (!Utilities.entryHasPlayer(scriptEntry)) {
throw new InvalidArgumentsException("Must specify valid player targets!");
} else {
scriptEntry.addObject("targets", Collections.singletonList(Utilities.getEntryPlayer(scriptEntry)));
}
}
scriptEntry.defaultObject("icon", new ItemTag(Material.AIR));
scriptEntry.defaultObject("frame", new ElementTag("TASK"));
}
use of com.denizenscript.denizen.objects.ItemTag in project Denizen-For-Bukkit by DenizenScript.
the class TakeCommand method removeItem.
public boolean removeItem(Inventory inventory, ItemTag item, int amount) {
if (item == null) {
return false;
}
item = new ItemTag(item.getItemStack().clone());
item.setAmount(1);
String myItem = CoreUtilities.toLowerCase(item.identify());
for (int i = 0; i < inventory.getSize(); i++) {
ItemStack is = inventory.getItem(i);
if (is == null) {
continue;
}
is = is.clone();
int count = is.getAmount();
is.setAmount(1);
// Note: this double-parsing is intentional, as part of a hotfix for a larger issue
String newItem = CoreUtilities.toLowerCase(ItemTag.valueOf(new ItemTag(is).identify(), false).identify());
if (myItem.equals(newItem)) {
if (count <= amount) {
NMSHandler.getItemHelper().setInventoryItem(inventory, null, i);
amount -= count;
if (amount == 0) {
return true;
}
} else {
is.setAmount(count - amount);
NMSHandler.getItemHelper().setInventoryItem(inventory, is, i);
return true;
}
}
}
return false;
}
use of com.denizenscript.denizen.objects.ItemTag in project Denizen-For-Bukkit by DenizenScript.
the class EnchantmentScriptContainer method canEnchant.
public boolean canEnchant(ItemStack item) {
if (!Bukkit.isPrimaryThread()) {
return false;
}
ContextSource.SimpleMap src = new ContextSource.SimpleMap();
src.contexts = new HashMap<>();
src.contexts.put("item", new ItemTag(item));
String res = autoTag(canEnchantTaggable, src);
return CoreUtilities.toLowerCase(res).equals("true");
}
use of com.denizenscript.denizen.objects.ItemTag in project Denizen-For-Bukkit by DenizenScript.
the class ItemScriptHelper method textToItemArray.
public ItemStack[] textToItemArray(ItemScriptContainer container, String text, boolean exact) {
if (CoreUtilities.toLowerCase(text).equals("air")) {
return new ItemStack[0];
}
List<String> ingredientText = splitByNonBracketedSlashes(text);
List<ItemStack> outputItems = new ArrayList<>(ingredientText.size());
for (int i = 0; i < ingredientText.size(); i++) {
String entry = ingredientText.get(i);
if (ScriptEvent.isAdvancedMatchable(entry)) {
boolean any = false;
for (Material material : Material.values()) {
if (BukkitScriptEvent.tryMaterial(material, entry)) {
outputItems.add(new ItemStack(material, 1));
any = true;
}
}
if (exact) {
for (ItemScriptContainer possibleContainer : ItemScriptHelper.item_scripts.values()) {
if (BukkitScriptEvent.tryItem(possibleContainer.getCleanReference(), entry)) {
outputItems.add(possibleContainer.getCleanReference().getItemStack());
any = true;
}
}
}
if (!any) {
Debug.echoError("Invalid ItemTag ingredient (empty advanced matcher), recipe will not be registered for item script '" + container.getName() + "': " + entry);
return null;
}
} else {
ItemTag ingredient = ItemTag.valueOf(entry, container);
if (ingredient == null) {
Debug.echoError("Invalid ItemTag ingredient, recipe will not be registered for item script '" + container.getName() + "': " + entry);
return null;
}
outputItems.add(ingredient.getItemStack().clone());
}
}
return outputItems.toArray(new ItemStack[0]);
}
use of com.denizenscript.denizen.objects.ItemTag in project Denizen-For-Bukkit by DenizenScript.
the class ItemScriptHelper method onItemSmithing.
@EventHandler(priority = EventPriority.LOW)
public void onItemSmithing(PrepareSmithingEvent event) {
if (!isItemscript(event.getInventory().getItem(0))) {
return;
}
Recipe recipe = event.getInventory().getRecipe();
SmithingRecipe smithRecipe = (SmithingRecipe) recipe;
if (smithRecipe == null || !(smithRecipe.getKey().getNamespace().equals("denizen"))) {
if (!isAllowedToCraftWith(event.getInventory().getItem(0))) {
event.setResult(new ItemStack(Material.AIR));
}
return;
}
ItemScriptContainer realResult = recipeIdToItemScript.get(smithRecipe.getKey().toString());
if (realResult == null) {
if (!isAllowedToCraftWith(event.getInventory().getItem(0))) {
event.setResult(new ItemStack(Material.AIR));
}
return;
}
String[] retain = smithingRetain.get(smithRecipe.getKey().getKey());
if (retain == null) {
Debug.echoError("Smithing recipe mis-registered for script item: " + realResult.getName());
if (!isAllowedToCraftWith(event.getInventory().getItem(0))) {
event.setResult(new ItemStack(Material.AIR));
}
return;
}
PlayerTag player = null;
if (!event.getInventory().getViewers().isEmpty()) {
HumanEntity human = event.getInventory().getViewers().get(0);
if (!EntityTag.isNPC(human) && human instanceof Player) {
player = new PlayerTag((Player) human);
}
}
ItemTag got = realResult.getItemFrom(new BukkitTagContext(player, null, new ScriptTag(realResult)));
if (got == null) {
return;
}
if (retain.length > 0) {
ItemMeta originalMeta = event.getInventory().getItem(0).getItemMeta();
ItemMeta newMeta = got.getItemMeta();
for (String retainable : retain) {
switch(retainable) {
case "display":
newMeta.setDisplayName(originalMeta.hasDisplayName() ? originalMeta.getDisplayName() : null);
break;
case "enchantments":
if (newMeta.hasEnchants()) {
for (Enchantment enchant : new ArrayList<>(newMeta.getEnchants().keySet())) {
newMeta.removeEnchant(enchant);
}
}
if (originalMeta.hasEnchants()) {
for (Map.Entry<Enchantment, Integer> enchant : originalMeta.getEnchants().entrySet()) {
newMeta.addEnchant(enchant.getKey(), enchant.getValue(), true);
}
}
break;
}
}
got.setItemMeta(newMeta);
}
event.setResult(got.getItemStack());
}
Aggregations