use of com.denizenscript.denizen.objects.ItemTag in project Denizen-For-Bukkit by DenizenScript.
the class ItemScriptContainer method getItemFrom.
public ItemTag getItemFrom(TagContext context) {
if (isProcessing) {
Debug.echoError("Item script contains (or chains to) a reference to itself. Cannot process.");
return null;
}
if (context == null) {
context = new BukkitTagContext(null, null, new ScriptTag(this));
} else {
context = new BukkitTagContext((BukkitTagContext) context);
context.script = new ScriptTag(this);
}
// Try to use this script to make an item.
ItemTag stack;
isProcessing = true;
try {
if (!contains("material", String.class)) {
Debug.echoError("Item script '" + getName() + "' does not contain a material. Script cannot function.");
return null;
}
// Check validity of material
String material = TagManager.tag(getString("material"), context);
if (material.startsWith("m@")) {
material = material.substring(2);
}
stack = ItemTag.valueOf(material, this);
// Make sure we're working with a valid base ItemStack
if (stack == null) {
Debug.echoError("Item script '" + getName() + "' contains an invalid or incorrect material '" + material + "' (did you spell the material name wrong?). Script cannot function.");
return null;
}
// Handle listed mechanisms
if (contains("mechanisms", Map.class)) {
YamlConfiguration mechs = getConfigurationSection("mechanisms");
for (StringHolder key : mechs.getKeys(false)) {
ObjectTag obj = CoreUtilities.objectToTagForm(mechs.get(key.low), context, true, true);
stack.safeAdjust(new Mechanism(key.low, obj, context));
}
}
// Set Display Name
if (contains("display name", String.class)) {
String displayName = TagManager.tag(getString("display name"), context);
NMSHandler.getItemHelper().setDisplayName(stack, displayName);
}
// Set if the object is bound to the player
if (contains("bound", String.class)) {
Deprecations.boundWarning.warn(context);
}
// Set Lore
if (contains("lore", List.class)) {
List<String> lore = NMSHandler.getItemHelper().getLore(stack);
if (lore == null) {
lore = new ArrayList<>();
}
for (String line : getStringList("lore")) {
line = TagManager.tag(line, context);
lore.add(line);
}
CoreUtilities.fixNewLinesToListSeparation(lore);
NMSHandler.getItemHelper().setLore(stack, lore);
}
// Set Durability
if (contains("durability", String.class)) {
short durability = Short.valueOf(getString("durability"));
stack.setDurability(durability);
}
// Set Enchantments
if (contains("enchantments", List.class)) {
for (String enchantment : getStringList("enchantments")) {
enchantment = TagManager.tag(enchantment, context);
try {
// Build enchantment context
int level = 1;
int colon = enchantment.lastIndexOf(':');
if (colon == -1) {
Debug.echoError("Item script '" + getName() + "' has enchantment '" + enchantment + "' without a level.");
} else {
level = Integer.valueOf(enchantment.substring(colon + 1).replace(" ", ""));
enchantment = enchantment.substring(0, colon).replace(" ", "");
}
// Add enchantment
EnchantmentTag ench = EnchantmentTag.valueOf(enchantment, context);
if (ench == null) {
Debug.echoError("Item script '" + getName() + "' specifies enchantment '" + enchantment + "' which is invalid.");
continue;
}
if (stack.getBukkitMaterial() == Material.ENCHANTED_BOOK) {
EnchantmentStorageMeta meta = (EnchantmentStorageMeta) stack.getItemMeta();
meta.addStoredEnchant(ench.enchantment, level, true);
stack.setItemMeta(meta);
} else {
stack.getItemStack().addUnsafeEnchantment(ench.enchantment, level);
stack.resetCache();
}
} catch (Exception ex) {
Debug.echoError("While constructing item script '" + getName() + "', encountered error while applying enchantment '" + enchantment + "':");
Debug.echoError(ex);
}
}
}
// Set Color
if (contains("color", String.class)) {
Deprecations.itemScriptColor.warn(context);
String color = TagManager.tag(getString("color"), context);
LeatherColorer.colorArmor(stack, color);
}
// Set Book
if (contains("book", String.class)) {
BookScriptContainer book = ScriptRegistry.getScriptContainer(TagManager.tag(getString("book"), context).replace("s@", ""));
stack = book.writeBookTo(stack, context);
}
if (contains("flags", Map.class)) {
YamlConfiguration flagSection = getConfigurationSection("flags");
AbstractFlagTracker tracker = stack.getFlagTracker();
for (StringHolder key : flagSection.getKeys(false)) {
tracker.setFlag(key.str, CoreUtilities.objectToTagForm(flagSection.get(key.str), context, true, true), null);
}
stack.reapplyTracker(tracker);
}
stack.setItemScript(this);
} catch (Exception e) {
Debug.echoError("Woah! An exception has been called with this item script!");
Debug.echoError(e);
stack = null;
} finally {
isProcessing = false;
}
return stack;
}
use of com.denizenscript.denizen.objects.ItemTag in project Denizen-For-Bukkit by DenizenScript.
the class HoverFormatHelper method processHoverInput.
public static boolean processHoverInput(HoverEvent.Action action, TextComponent hoverableText, String input) {
Content content;
if (action == HoverEvent.Action.SHOW_ITEM) {
ItemTag item = ItemTag.valueOf(FormattedTextHelper.unescape(input), CoreUtilities.noDebugContext);
if (item == null) {
return true;
}
// TODO: Why is there not a direct conversion method for Spigot ItemStack -> BungeeChat Item?
String itemNbt = NMSHandler.getItemHelper().getRawHoverText(item.getItemStack());
content = new Item(item.getBukkitMaterial().getKey().toString(), item.getAmount(), net.md_5.bungee.api.chat.ItemTag.ofNbt(itemNbt));
} else if (action == HoverEvent.Action.SHOW_ENTITY) {
EntityTag entity = EntityTag.valueOf(FormattedTextHelper.unescape(input), CoreUtilities.basicContext);
if (entity == null) {
return true;
}
BaseComponent name = null;
if (entity.getBukkitEntity() != null && entity.getBukkitEntity().isCustomNameVisible()) {
name = new TextComponent();
for (BaseComponent component : FormattedTextHelper.parse(entity.getBukkitEntity().getCustomName(), ChatColor.WHITE)) {
name.addExtra(component);
}
}
content = new Entity(entity.getBukkitEntityType().getKey().toString(), entity.getUUID().toString(), name);
} else {
content = new Text(FormattedTextHelper.parse(FormattedTextHelper.unescape(input), ChatColor.WHITE));
}
hoverableText.setHoverEvent(new HoverEvent(action, content));
return false;
}
Aggregations