use of com.denizenscript.denizencore.objects.ObjectTag in project Denizen-For-Bukkit by DenizenScript.
the class EntityEquipment method registerTags.
public static void registerTags() {
// <--[tag]
// @attribute <EntityTag.equipment>
// @returns ListTag(ItemTag)
// @mechanism EntityTag.equipment
// @group inventory
// @description
// Returns a ListTag containing the entity's equipment.
// Output list is boots|leggings|chestplate|helmet
// -->
PropertyParser.<EntityEquipment, ObjectTag>registerTag(ObjectTag.class, "equipment", (attribute, object) -> {
org.bukkit.inventory.EntityEquipment equipment = object.entity.getLivingEntity().getEquipment();
if (attribute.startsWith("equipment.boots")) {
Deprecations.entityEquipmentSubtags.warn(attribute.context);
attribute.fulfill(1);
ItemStack boots = equipment.getBoots();
return new ItemTag(boots != null ? boots : new ItemStack(Material.AIR));
} else if (attribute.startsWith("equipment.chestplate") || attribute.startsWith("equipment.chest")) {
Deprecations.entityEquipmentSubtags.warn(attribute.context);
attribute.fulfill(1);
ItemStack chestplate = equipment.getChestplate();
return new ItemTag(chestplate != null ? chestplate : new ItemStack(Material.AIR));
} else if (attribute.startsWith("equipment.helmet") || attribute.startsWith("equipment.head")) {
Deprecations.entityEquipmentSubtags.warn(attribute.context);
attribute.fulfill(1);
ItemStack helmet = equipment.getHelmet();
return new ItemTag(helmet != null ? helmet : new ItemStack(Material.AIR));
} else if (attribute.startsWith("equipment.leggings") || attribute.startsWith("equipment.legs")) {
Deprecations.entityEquipmentSubtags.warn(attribute.context);
attribute.fulfill(1);
ItemStack leggings = equipment.getLeggings();
return new ItemTag(leggings != null ? leggings : new ItemStack(Material.AIR));
}
return object.entity.getEquipment();
});
// <--[tag]
// @attribute <EntityTag.equipment_map>
// @returns MapTag
// @mechanism EntityTag.equipment
// @group inventory
// @description
// Returns a MapTag containing the entity's equipment.
// Output keys are boots, leggings, chestplate, helmet.
// Air items will be left out of the map.
// -->
PropertyParser.<EntityEquipment, MapTag>registerTag(MapTag.class, "equipment_map", (attribute, object) -> {
MapTag output = new MapTag();
org.bukkit.inventory.EntityEquipment equip = object.entity.getLivingEntity().getEquipment();
InventoryTag.addToMapIfNonAir(output, "boots", equip.getBoots());
InventoryTag.addToMapIfNonAir(output, "leggings", equip.getLeggings());
InventoryTag.addToMapIfNonAir(output, "chestplate", equip.getChestplate());
InventoryTag.addToMapIfNonAir(output, "helmet", equip.getHelmet());
return output;
});
}
use of com.denizenscript.denizencore.objects.ObjectTag in project Denizen-For-Bukkit by DenizenScript.
the class EntityEquipment method adjust.
@Override
public void adjust(Mechanism mechanism) {
// -->
if (mechanism.matches("equipment") && mechanism.hasValue()) {
org.bukkit.inventory.EntityEquipment equip = entity.getLivingEntity().getEquipment();
if (mechanism.value.canBeType(MapTag.class)) {
MapTag map = mechanism.valueAsType(MapTag.class);
ObjectTag boots = map.getObject("boots");
if (boots != null) {
ItemStack bootsItem = boots.asType(ItemTag.class, mechanism.context).getItemStack();
if (entity.isCitizensNPC()) {
entity.getDenizenNPC().getEquipmentTrait().set(Equipment.EquipmentSlot.BOOTS, bootsItem);
} else {
equip.setBoots(bootsItem);
}
}
ObjectTag leggings = map.getObject("leggings");
if (leggings != null) {
ItemStack leggingsItem = leggings.asType(ItemTag.class, mechanism.context).getItemStack();
if (entity.isCitizensNPC()) {
entity.getDenizenNPC().getEquipmentTrait().set(Equipment.EquipmentSlot.LEGGINGS, leggingsItem);
} else {
equip.setLeggings(leggingsItem);
}
}
ObjectTag chestplate = map.getObject("chestplate");
if (chestplate != null) {
ItemStack chestplateItem = chestplate.asType(ItemTag.class, mechanism.context).getItemStack();
if (entity.isCitizensNPC()) {
entity.getDenizenNPC().getEquipmentTrait().set(Equipment.EquipmentSlot.CHESTPLATE, chestplateItem);
} else {
equip.setChestplate(chestplateItem);
}
}
ObjectTag helmet = map.getObject("helmet");
if (helmet != null) {
ItemStack helmetItem = helmet.asType(ItemTag.class, mechanism.context).getItemStack();
if (entity.isCitizensNPC()) {
entity.getDenizenNPC().getEquipmentTrait().set(Equipment.EquipmentSlot.HELMET, helmetItem);
} else {
equip.setHelmet(helmetItem);
}
}
} else {
// Soft-deprecate: no warn, but long term back-support
ListTag list = mechanism.valueAsType(ListTag.class);
ItemStack[] stacks = new ItemStack[list.size()];
for (int i = 0; i < list.size(); i++) {
stacks[i] = ItemTag.valueOf(list.get(i), mechanism.context).getItemStack();
}
equip.setArmorContents(stacks);
}
}
}
use of com.denizenscript.denizencore.objects.ObjectTag in project Denizen-For-Bukkit by DenizenScript.
the class ItemRawNBT method convertObjectToNbt.
// <--[language]
// @name Raw NBT Encoding
// @group Useful Lists
// @description
// The item Raw_NBT property encodes and decodes raw NBT data.
// For the sake of inter-compatibility, a special standard format is used to preserve data types.
// This system exists in Denizen primarily for the sake of compatibility with external plugins.
// It should not be used in any scripts that don't rely on data from external plugins.
//
// NBT Tags are encoded as follows:
// CompoundTag: (a fully formed MapTag)
// ListTag: list:(NBT type-code):(a fully formed ListTag)
// ByteArrayTag: byte_array:(a pipe-separated list of numbers)
// IntArrayTag: int_array:(a pipe-separated list of numbers)
// ByteTag: byte:(#)
// ShortTag: short:(#)
// IntTag: int:(#)
// LongTag: long:(#)
// FloatTag: float:(#)
// DoubleTag: double:(#)
// StringTag: string:(text here)
// EndTag: end
//
// -->
public static Tag convertObjectToNbt(String object, TagContext context, String path) {
if (object.startsWith("map@")) {
MapTag map = MapTag.valueOf(object, context);
Map<String, Tag> result = new LinkedHashMap<>();
for (Map.Entry<StringHolder, ObjectTag> entry : map.map.entrySet()) {
try {
result.put(entry.getKey().str, convertObjectToNbt(entry.getValue().toString(), context, path + "." + entry.getKey().str));
} catch (Exception ex) {
Debug.echoError("Object NBT interpretation failed for key '" + path + "." + entry.getKey().str + "'.");
Debug.echoError(ex);
return null;
}
}
return NMSHandler.getInstance().createCompoundTag(result);
} else if (object.startsWith("list:")) {
int nextColonIndex = object.indexOf(':', "list:".length() + 1);
int typeCode = Integer.parseInt(object.substring("list:".length(), nextColonIndex));
String listValue = object.substring(nextColonIndex + 1);
List<Tag> result = new ArrayList<>();
ListTag listTag = ListTag.valueOf(listValue, context);
for (int i = 0; i < listTag.size(); i++) {
try {
result.add(convertObjectToNbt(listTag.get(i), context, path + "[" + i + "]"));
} catch (Exception ex) {
Debug.echoError("Object NBT interpretation failed for list key '" + path + "' at index " + i + ".");
Debug.echoError(ex);
return null;
}
}
return new JNBTListTag(NBTUtils.getTypeClass(typeCode), result);
} else if (object.startsWith("byte_array:")) {
ListTag numberStrings = ListTag.valueOf(object.substring("byte_array:".length()), context);
byte[] result = new byte[numberStrings.size()];
for (int i = 0; i < result.length; i++) {
result[i] = Byte.parseByte(numberStrings.get(i));
}
return new ByteArrayTag(result);
} else if (object.startsWith("int_array:")) {
ListTag numberStrings = ListTag.valueOf(object.substring("int_array:".length()), context);
int[] result = new int[numberStrings.size()];
for (int i = 0; i < result.length; i++) {
result[i] = Integer.parseInt(numberStrings.get(i));
}
return new IntArrayTag(result);
} else if (object.startsWith("byte:")) {
return new ByteTag(Byte.parseByte(object.substring("byte:".length())));
} else if (object.startsWith("short:")) {
return new ShortTag(Short.parseShort(object.substring("short:".length())));
} else if (object.startsWith("int:")) {
return new IntTag(Integer.parseInt(object.substring("int:".length())));
} else if (object.startsWith("long:")) {
return new LongTag(Long.parseLong(object.substring("long:".length())));
} else if (object.startsWith("float:")) {
return new FloatTag(Float.parseFloat(object.substring("float:".length())));
} else if (object.startsWith("double:")) {
return new DoubleTag(Double.parseDouble(object.substring("double:".length())));
} else if (object.startsWith("string:")) {
return new StringTag(object.substring("string:".length()));
} else if (object.equals("end")) {
return new EndTag();
} else {
if (context == null || context.showErrors()) {
Debug.echoError("Unknown raw NBT value: " + object);
}
return null;
}
}
use of com.denizenscript.denizencore.objects.ObjectTag in project Denizen-For-Bukkit by DenizenScript.
the class ItemAttributeModifiers method adjust.
@Override
public void adjust(Mechanism mechanism) {
// -->
if (mechanism.matches("attribute_modifiers") && mechanism.requireObject(MapTag.class)) {
Multimap<org.bukkit.attribute.Attribute, AttributeModifier> metaMap = LinkedHashMultimap.create();
MapTag map = mechanism.valueAsType(MapTag.class);
for (Map.Entry<StringHolder, ObjectTag> mapEntry : map.map.entrySet()) {
org.bukkit.attribute.Attribute attr = org.bukkit.attribute.Attribute.valueOf(mapEntry.getKey().str.toUpperCase());
for (ObjectTag listValue : CoreUtilities.objectToList(mapEntry.getValue(), mechanism.context)) {
metaMap.put(attr, EntityAttributeModifiers.modiferForMap(attr, (MapTag) listValue));
}
}
ItemMeta meta = item.getItemMeta();
meta.setAttributeModifiers(metaMap);
item.setItemMeta(meta);
}
// -->
if (mechanism.matches("add_attribute_modifiers") && mechanism.requireObject(MapTag.class)) {
ItemMeta meta = item.getItemMeta();
MapTag input = mechanism.valueAsType(MapTag.class);
for (Map.Entry<StringHolder, ObjectTag> subValue : input.map.entrySet()) {
org.bukkit.attribute.Attribute attr = org.bukkit.attribute.Attribute.valueOf(subValue.getKey().str.toUpperCase());
for (ObjectTag listValue : CoreUtilities.objectToList(subValue.getValue(), mechanism.context)) {
meta.addAttributeModifier(attr, EntityAttributeModifiers.modiferForMap(attr, (MapTag) listValue));
}
}
item.setItemMeta(meta);
}
// -->
if (mechanism.matches("remove_attribute_modifiers") && mechanism.requireObject(ListTag.class)) {
ItemMeta meta = item.getItemMeta();
ArrayList<String> inputList = new ArrayList<>(mechanism.valueAsType(ListTag.class));
for (String toRemove : new ArrayList<>(inputList)) {
if (new ElementTag(toRemove).matchesEnum(org.bukkit.attribute.Attribute.class)) {
inputList.remove(toRemove);
org.bukkit.attribute.Attribute attr = org.bukkit.attribute.Attribute.valueOf(toRemove.toUpperCase());
meta.removeAttributeModifier(attr);
}
}
for (String toRemove : inputList) {
UUID id = UUID.fromString(toRemove);
Multimap<org.bukkit.attribute.Attribute, AttributeModifier> metaMap = meta.getAttributeModifiers();
for (org.bukkit.attribute.Attribute attribute : metaMap.keys()) {
for (AttributeModifier modifer : metaMap.get(attribute)) {
if (modifer.getUniqueId().equals(id)) {
meta.removeAttributeModifier(attribute, modifer);
break;
}
}
}
}
item.setItemMeta(meta);
}
}
use of com.denizenscript.denizencore.objects.ObjectTag in project Denizen-For-Bukkit by DenizenScript.
the class EntityPotionEffects method adjust.
public void adjust(Mechanism mechanism) {
// -->
if (mechanism.matches("potion_effects")) {
for (ObjectTag effectObj : CoreUtilities.objectToList(mechanism.value, mechanism.context)) {
PotionEffect effect;
if (effectObj.canBeType(MapTag.class)) {
MapTag effectMap = effectObj.asType(MapTag.class, mechanism.context);
effect = ItemPotion.parseEffect(effectMap, mechanism.context);
} else {
String effectStr = effectObj.toString();
effect = ItemPotion.parseEffect(effectStr, mechanism.context);
}
if (effect == null) {
mechanism.echoError("Invalid potion effect '" + effectObj + "'");
continue;
}
if (entity.isLivingEntity()) {
entity.getLivingEntity().addPotionEffect(effect);
} else if (isArrow()) {
getArrow().addCustomEffect(effect, true);
}
}
}
}
Aggregations