Search in sources :

Example 11 with MapTag

use of com.denizenscript.denizencore.objects.core.MapTag 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;
    });
}
Also used : ObjectTag(com.denizenscript.denizencore.objects.ObjectTag) ItemStack(org.bukkit.inventory.ItemStack) ItemTag(com.denizenscript.denizen.objects.ItemTag) MapTag(com.denizenscript.denizencore.objects.core.MapTag)

Example 12 with MapTag

use of com.denizenscript.denizencore.objects.core.MapTag 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);
        }
    }
}
Also used : ObjectTag(com.denizenscript.denizencore.objects.ObjectTag) ItemStack(org.bukkit.inventory.ItemStack) ItemTag(com.denizenscript.denizen.objects.ItemTag) ListTag(com.denizenscript.denizencore.objects.core.ListTag) MapTag(com.denizenscript.denizencore.objects.core.MapTag)

Example 13 with MapTag

use of com.denizenscript.denizencore.objects.core.MapTag in project Denizen-For-Bukkit by DenizenScript.

the class ItemPotion method effectToMap.

public static MapTag effectToMap(PotionEffect effect) {
    MapTag map = new MapTag();
    map.putObject("type", new ElementTag(effect.getType().getName()));
    map.putObject("amplifier", new ElementTag(effect.getAmplifier()));
    map.putObject("duration", new DurationTag((long) effect.getDuration()));
    map.putObject("ambient", new ElementTag(effect.isAmbient()));
    map.putObject("particles", new ElementTag(effect.hasParticles()));
    map.putObject("icon", new ElementTag(effect.hasIcon()));
    return map;
}
Also used : ElementTag(com.denizenscript.denizencore.objects.core.ElementTag) DurationTag(com.denizenscript.denizencore.objects.core.DurationTag) MapTag(com.denizenscript.denizencore.objects.core.MapTag)

Example 14 with MapTag

use of com.denizenscript.denizencore.objects.core.MapTag in project Denizen-For-Bukkit by DenizenScript.

the class ItemPotion method getMapTagData.

public ListTag getMapTagData() {
    ListTag result = new ListTag();
    MapTag base = new MapTag();
    PotionMeta meta = getMeta();
    base.putObject("type", new ElementTag(meta.getBasePotionData().getType().name()));
    base.putObject("upgraded", new ElementTag(meta.getBasePotionData().isUpgraded()));
    base.putObject("extended", new ElementTag(meta.getBasePotionData().isExtended()));
    if (meta.hasColor()) {
        base.putObject("color", new ColorTag(meta.getColor()));
    }
    result.addObject(base);
    for (PotionEffect effect : meta.getCustomEffects()) {
        result.addObject(effectToMap(effect));
    }
    return result;
}
Also used : PotionEffect(org.bukkit.potion.PotionEffect) ColorTag(com.denizenscript.denizen.objects.ColorTag) PotionMeta(org.bukkit.inventory.meta.PotionMeta) ElementTag(com.denizenscript.denizencore.objects.core.ElementTag) ListTag(com.denizenscript.denizencore.objects.core.ListTag) MapTag(com.denizenscript.denizencore.objects.core.MapTag)

Example 15 with MapTag

use of com.denizenscript.denizencore.objects.core.MapTag 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;
    }
}
Also used : MapTag(com.denizenscript.denizencore.objects.core.MapTag) ObjectTag(com.denizenscript.denizencore.objects.ObjectTag) ListTag(com.denizenscript.denizencore.objects.core.ListTag) StringHolder(com.denizenscript.denizencore.utilities.text.StringHolder) ListTag(com.denizenscript.denizencore.objects.core.ListTag) ObjectTag(com.denizenscript.denizencore.objects.ObjectTag) ItemTag(com.denizenscript.denizen.objects.ItemTag) MapTag(com.denizenscript.denizencore.objects.core.MapTag) ElementTag(com.denizenscript.denizencore.objects.core.ElementTag)

Aggregations

MapTag (com.denizenscript.denizencore.objects.core.MapTag)46 ElementTag (com.denizenscript.denizencore.objects.core.ElementTag)30 ListTag (com.denizenscript.denizencore.objects.core.ListTag)23 ObjectTag (com.denizenscript.denizencore.objects.ObjectTag)22 ItemTag (com.denizenscript.denizen.objects.ItemTag)9 StringHolder (com.denizenscript.denizencore.utilities.text.StringHolder)8 ArrayList (java.util.ArrayList)5 Map (java.util.Map)5 ItemStack (org.bukkit.inventory.ItemStack)5 ColorTag (com.denizenscript.denizen.objects.ColorTag)4 DurationTag (com.denizenscript.denizencore.objects.core.DurationTag)3 Attributable (org.bukkit.attribute.Attributable)3 Attribute (org.bukkit.attribute.Attribute)3 AttributeInstance (org.bukkit.attribute.AttributeInstance)3 AttributeModifier (org.bukkit.attribute.AttributeModifier)3 ItemMeta (org.bukkit.inventory.meta.ItemMeta)3 FlaggableObject (com.denizenscript.denizencore.flags.FlaggableObject)2 Attribute (com.denizenscript.denizencore.tags.Attribute)2 ArmorStandMeta (com.destroystokyo.paper.inventory.meta.ArmorStandMeta)2 UUID (java.util.UUID)2