use of com.denizenscript.denizencore.objects.ObjectTag in project Denizen-For-Bukkit by DenizenScript.
the class ParseTagBase method parseTags.
public void parseTags(ReplaceableTagEvent event) {
Deprecations.oldParseTag.warn(event.getScriptEntry());
if (event.matches("parse")) {
if (!event.hasValue()) {
Debug.echoError("Escape tag '" + event.raw_tag + "' does not have a value!");
return;
}
ObjectTag read = TagManager.tagObject(event.getValue(), event.getContext());
event.setReplacedObject(CoreUtilities.autoAttrib(read, event.getAttributes().fulfill(1)));
}
}
use of com.denizenscript.denizencore.objects.ObjectTag in project Denizen-For-Bukkit by DenizenScript.
the class ItemFirework method adjust.
@Override
public void adjust(Mechanism mechanism) {
// -->
if (mechanism.matches("firework_power") && mechanism.requireInteger()) {
if (item.getItemMeta() instanceof FireworkMeta) {
ItemMeta meta = item.getItemMeta();
((FireworkMeta) meta).setPower(mechanism.getValue().asInt());
item.setItemMeta(meta);
} else {
mechanism.echoError("Cannot set the power of a firework effect!");
}
}
// -->
if (mechanism.matches("firework")) {
ItemMeta meta = item.getItemMeta();
if (!mechanism.hasValue()) {
if (meta instanceof FireworkMeta) {
((FireworkMeta) meta).clearEffects();
} else {
((FireworkEffectMeta) meta).setEffect(null);
}
} else {
Collection<ObjectTag> list = CoreUtilities.objectToList(mechanism.getValue(), mechanism.context);
for (ObjectTag object : list) {
if (object.canBeType(MapTag.class)) {
MapTag effectMap = object.asType(MapTag.class, mechanism.context);
FireworkEffect.Builder builder = FireworkEffect.builder();
ObjectTag type = effectMap.getObject("type");
ObjectTag color = effectMap.getObject("color");
ObjectTag fadeColor = effectMap.getObject("fade_color");
ObjectTag trail = effectMap.getObject("trail");
ObjectTag flicker = effectMap.getObject("flicker");
builder.trail(trail != null && trail.asElement().asBoolean());
builder.flicker(flicker != null && flicker.asElement().asBoolean());
if (type != null) {
ElementTag effectType = type.asElement();
if (effectType.matchesEnum(FireworkEffect.Type.class)) {
builder.with(FireworkEffect.Type.valueOf(effectType.asString().toUpperCase()));
} else {
mechanism.echoError("Invalid firework type '" + effectType.asString() + "'");
}
}
ColorTag co = new ColorTag(Color.BLACK);
if (color != null && ColorTag.matches(color.toString())) {
co = ColorTag.valueOf(color.toString(), mechanism.context);
} else if (color != null) {
mechanism.echoError("Invalid color '" + color + "'");
}
builder.withColor(co.getColor());
if (fadeColor != null) {
ColorTag fadeCo = ColorTag.valueOf(fadeColor.toString(), mechanism.context);
if (fadeCo != null) {
builder.withFade(fadeCo.getColor());
} else {
mechanism.echoError("Invalid fade color '" + fadeColor + "'");
}
}
FireworkEffect built = builder.build();
if (meta instanceof FireworkMeta) {
((FireworkMeta) meta).addEffect(built);
} else {
((FireworkEffectMeta) meta).setEffect(built);
}
} else {
String effect = object.toString();
String[] data = effect.split(",");
if (data.length == 9) {
FireworkEffect.Builder builder = FireworkEffect.builder();
builder.trail(new ElementTag(data[0]).asBoolean());
builder.flicker(new ElementTag(data[1]).asBoolean());
if (new ElementTag(data[2]).matchesEnum(FireworkEffect.Type.class)) {
builder.with(FireworkEffect.Type.valueOf(data[2].toUpperCase()));
} else {
mechanism.echoError("Invalid firework type '" + data[2] + "'");
}
builder.withColor(Color.fromRGB(new ElementTag(data[3]).asInt(), new ElementTag(data[4]).asInt(), new ElementTag(data[5]).asInt()));
builder.withFade(Color.fromRGB(new ElementTag(data[6]).asInt(), new ElementTag(data[7]).asInt(), new ElementTag(data[8]).asInt()));
FireworkEffect built = builder.build();
if (meta instanceof FireworkMeta) {
((FireworkMeta) meta).addEffect(built);
} else {
((FireworkEffectMeta) meta).setEffect(built);
}
} else if (data.length == 1) {
if (meta instanceof FireworkMeta) {
((FireworkMeta) meta).setPower(new ElementTag(data[0]).asInt());
} else {
mechanism.echoError("Cannot set the power of a firework effect!");
}
} else {
mechanism.echoError("Invalid firework data '" + effect + "'");
}
}
}
}
item.setItemMeta(meta);
}
}
use of com.denizenscript.denizencore.objects.ObjectTag in project Denizen-For-Bukkit by DenizenScript.
the class ItemArmorPose method procMechKey.
public static void procMechKey(Mechanism mech, CompoundTagBuilder pose, String nmsName, String denizenName, MapTag input) {
ObjectTag value = input.getObject(denizenName);
if (value == null) {
return;
}
List<String> raw = CoreUtilities.split(value.toString(), ',');
if (raw.size() != 3) {
mech.echoError("Invalid pose piece '" + value + "'");
return;
}
List<FloatTag> rawList = new ArrayList<>();
for (int i = 0; i < 3; i++) {
rawList.add(new FloatTag(Float.valueOf(raw.get(i))));
}
JNBTListTag list = new JNBTListTag(FloatTag.class, rawList);
pose.put(nmsName, list);
}
use of com.denizenscript.denizencore.objects.ObjectTag in project Denizen-For-Bukkit by DenizenScript.
the class ItemEnchantments method adjust.
@Override
public void adjust(Mechanism mechanism) {
// -->
if (mechanism.matches("remove_enchantments")) {
if (mechanism.hasValue()) {
List<EnchantmentTag> toRemove = mechanism.valueAsType(ListTag.class).filter(EnchantmentTag.class, mechanism.context);
if (item.getBukkitMaterial() == Material.ENCHANTED_BOOK) {
EnchantmentStorageMeta meta = (EnchantmentStorageMeta) item.getItemMeta();
for (EnchantmentTag ench : toRemove) {
meta.removeStoredEnchant(ench.enchantment);
}
item.setItemMeta(meta);
} else {
for (EnchantmentTag ench : toRemove) {
item.getItemStack().removeEnchantment(ench.enchantment);
}
item.resetCache();
}
} else {
if (item.getBukkitMaterial() == Material.ENCHANTED_BOOK) {
EnchantmentStorageMeta meta = (EnchantmentStorageMeta) item.getItemMeta();
for (Enchantment ench : meta.getStoredEnchants().keySet()) {
meta.removeStoredEnchant(ench);
}
item.setItemMeta(meta);
} else {
for (Enchantment ench : item.getItemStack().getEnchantments().keySet()) {
item.getItemStack().removeEnchantment(ench);
}
item.resetCache();
}
}
}
// -->
if (mechanism.matches("enchantments")) {
String val = mechanism.getValue().asString();
if (val.startsWith("map@") || val.startsWith("[") || (val.contains("=") && !val.contains(","))) {
MapTag map = mechanism.valueAsType(MapTag.class);
for (Map.Entry<StringHolder, ObjectTag> enchantments : map.map.entrySet()) {
Enchantment ench = EnchantmentTag.valueOf(enchantments.getKey().low, mechanism.context).enchantment;
int level = enchantments.getValue().asElement().asInt();
if (ench != null) {
if (item.getBukkitMaterial() == Material.ENCHANTED_BOOK) {
EnchantmentStorageMeta meta = (EnchantmentStorageMeta) item.getItemMeta();
meta.addStoredEnchant(ench, level, true);
item.setItemMeta(meta);
} else {
item.getItemStack().addUnsafeEnchantment(ench, level);
item.resetCache();
}
} else {
mechanism.echoError("Unknown enchantment '" + enchantments.getKey().str + "'");
}
}
} else {
for (String enchant : mechanism.valueAsType(ListTag.class)) {
if (!enchant.contains(",")) {
mechanism.echoError("Invalid enchantment format, use name,level|...");
} else {
String[] data = enchant.split(",", 2);
try {
Enchantment ench = EnchantmentTag.valueOf(data[0], mechanism.context).enchantment;
if (ench != null) {
if (item.getBukkitMaterial() == Material.ENCHANTED_BOOK) {
EnchantmentStorageMeta meta = (EnchantmentStorageMeta) item.getItemMeta();
meta.addStoredEnchant(ench, Integer.valueOf(data[1]), true);
item.setItemMeta(meta);
} else {
item.getItemStack().addUnsafeEnchantment(ench, Integer.valueOf(data[1]));
item.resetCache();
}
} else {
mechanism.echoError("Unknown enchantment '" + data[0] + "'");
}
} catch (NullPointerException e) {
mechanism.echoError("Unknown enchantment '" + data[0] + "'");
} catch (NumberFormatException ex) {
mechanism.echoError("Cannot apply enchantment '" + data[0] + "': '" + data[1] + "' is not a valid integer!");
if (Debug.verbose) {
Debug.echoError(ex);
}
}
}
}
}
}
}
use of com.denizenscript.denizencore.objects.ObjectTag in project Denizen-For-Bukkit by DenizenScript.
the class ItemBook method adjust.
@Override
public void adjust(Mechanism mechanism) {
if (mechanism.matches("book_raw_pages")) {
Deprecations.bookItemRawTags.warn(mechanism.context);
BookMeta meta = (BookMeta) item.getItemMeta();
ListTag data = mechanism.valueAsType(ListTag.class);
ArrayList<BaseComponent[]> newPages = new ArrayList<>();
for (String str : data) {
newPages.add(ComponentSerializer.parse(EscapeTagBase.unEscape(str)));
}
meta.spigot().setPages(newPages);
item.setItemMeta(meta);
}
// -->
if (mechanism.matches("book_pages")) {
BookMeta meta = (BookMeta) item.getItemMeta();
ListTag data = mechanism.valueAsType(ListTag.class);
ArrayList<BaseComponent[]> newPages = new ArrayList<>();
for (String str : data) {
newPages.add(FormattedTextHelper.parse(str, ChatColor.BLACK));
}
meta.spigot().setPages(newPages);
item.setItemMeta(meta);
}
// -->
if (mechanism.matches("book_author")) {
if (!item.getBukkitMaterial().equals(Material.WRITTEN_BOOK)) {
mechanism.echoError("Only WRITTEN_BOOK (not WRITABLE_BOOK) can have a title or author!");
} else {
BookMeta meta = (BookMeta) item.getItemMeta();
meta.setAuthor(mechanism.getValue().asString());
item.setItemMeta(meta);
}
}
// -->
if (mechanism.matches("book_title")) {
if (!item.getBukkitMaterial().equals(Material.WRITTEN_BOOK)) {
mechanism.echoError("Only WRITTEN_BOOK (not WRITABLE_BOOK) can have a title or author!");
} else {
BookMeta meta = (BookMeta) item.getItemMeta();
meta.setTitle(mechanism.getValue().asString());
item.setItemMeta(meta);
}
}
// -->
if (mechanism.matches("book")) {
BookMeta meta = (BookMeta) item.getItemMeta();
if (mechanism.getValue().asString().startsWith("map@")) {
MapTag mapData = mechanism.valueAsType(MapTag.class);
if (mapData == null) {
mechanism.echoError("Book input is an invalid map?");
return;
}
ObjectTag author = mapData.getObject("author");
ObjectTag title = mapData.getObject("title");
if (author != null && title != null) {
if (!item.getBukkitMaterial().equals(Material.WRITTEN_BOOK)) {
mechanism.echoError("Only WRITTEN_BOOK (not WRITABLE_BOOK) can have a title or author!");
} else {
meta.setAuthor(author.toString());
meta.setTitle(title.toString());
}
}
ObjectTag pages = mapData.getObject("pages");
if (pages != null) {
ListTag pageList = ListTag.getListFor(pages, mechanism.context);
ArrayList<BaseComponent[]> newPages = new ArrayList<>(pageList.size());
for (int i = 0; i < pageList.size(); i++) {
newPages.add(FormattedTextHelper.parse(pageList.get(i), ChatColor.BLACK));
}
meta.spigot().setPages(newPages);
}
item.setItemMeta(meta);
return;
}
ListTag data = mechanism.valueAsType(ListTag.class);
if (data.size() < 1) {
mechanism.echoError("Invalid book input!");
return;
}
if (data.size() < 2) {
// Nothing to do, but not necessarily invalid.
return;
}
if (data.size() > 4 && data.get(0).equalsIgnoreCase("author") && data.get(2).equalsIgnoreCase("title")) {
if (!item.getBukkitMaterial().equals(Material.WRITTEN_BOOK)) {
mechanism.echoError("Only WRITTEN_BOOK (not WRITABLE_BOOK) can have a title or author!");
} else {
meta.setAuthor(EscapeTagBase.unEscape(data.get(1)));
meta.setTitle(EscapeTagBase.unEscape(data.get(3)));
for (int i = 0; i < 4; i++) {
// No .removeRange?
data.removeObject(0);
}
}
}
if (data.get(0).equalsIgnoreCase("raw_pages")) {
ArrayList<BaseComponent[]> newPages = new ArrayList<>(data.size());
for (int i = 1; i < data.size(); i++) {
newPages.add(ComponentSerializer.parse(EscapeTagBase.unEscape(data.get(i))));
}
meta.spigot().setPages(newPages);
} else if (data.get(0).equalsIgnoreCase("pages")) {
ArrayList<BaseComponent[]> newPages = new ArrayList<>(data.size());
for (int i = 1; i < data.size(); i++) {
newPages.add(FormattedTextHelper.parse(EscapeTagBase.unEscape(data.get(i)), ChatColor.BLACK));
}
meta.spigot().setPages(newPages);
} else {
mechanism.echoError("Invalid book input!");
}
item.setItemMeta(meta);
}
}
Aggregations