Search in sources :

Example 21 with ObjectTag

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

the class EntityAttributeBaseValues method adjust.

@Override
public void adjust(Mechanism mechanism) {
    // -->
    if (mechanism.matches("attribute_base_values") && mechanism.requireObject(MapTag.class)) {
        MapTag input = mechanism.valueAsType(MapTag.class);
        Attributable ent = getAttributable();
        for (Map.Entry<StringHolder, ObjectTag> subValue : input.map.entrySet()) {
            Attribute attr = Attribute.valueOf(subValue.getKey().str.toUpperCase());
            AttributeInstance instance = ent.getAttribute(attr);
            if (instance == null) {
                mechanism.echoError("Attribute " + attr.name() + " is not applicable to entity of type " + entity.getBukkitEntityType().name());
                continue;
            }
            ElementTag value = subValue.getValue().asElement();
            if (!value.isDouble()) {
                mechanism.echoError("Invalid input '" + value + "': must be a decimal number.");
                continue;
            }
            instance.setBaseValue(value.asDouble());
        }
    }
}
Also used : Attributable(org.bukkit.attribute.Attributable) StringHolder(com.denizenscript.denizencore.utilities.text.StringHolder) ObjectTag(com.denizenscript.denizencore.objects.ObjectTag) Attribute(org.bukkit.attribute.Attribute) AttributeInstance(org.bukkit.attribute.AttributeInstance) ElementTag(com.denizenscript.denizencore.objects.core.ElementTag) Map(java.util.Map) MapTag(com.denizenscript.denizencore.objects.core.MapTag)

Example 22 with ObjectTag

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

the class ItemPotion method parseEffect.

public static PotionEffect parseEffect(MapTag effectMap, TagContext context) {
    PotionEffectType type;
    DurationTag duration = new DurationTag(0);
    int amplifier = 0;
    boolean ambient = true;
    boolean particles = true;
    boolean icon = false;
    if (effectMap.getObject("type") != null) {
        String typeString = effectMap.getObject("type").toString();
        type = PotionEffectType.getByName(typeString);
        if (type == null) {
            if (context.showErrors()) {
                Debug.echoError("Invalid potion effect type '" + typeString + "': effect type is required.");
            }
            return null;
        }
    } else {
        if (context.showErrors()) {
            Debug.echoError("Invalid potion effect type: effect type is required.");
        }
        return null;
    }
    if (effectMap.getObject("amplifier") != null) {
        ElementTag amplifierElement = effectMap.getObject("amplifier").asElement();
        if (amplifierElement.isInt()) {
            amplifier = amplifierElement.asInt();
        } else if (context.showErrors()) {
            Debug.echoError("Invalid amplifier '" + amplifierElement + "': must be an integer.");
        }
    }
    if (effectMap.getObject("duration") != null) {
        ObjectTag durationObj = effectMap.getObject("duration");
        if (durationObj.canBeType(DurationTag.class)) {
            duration = durationObj.asType(DurationTag.class, context);
        } else if (context.showErrors()) {
            Debug.echoError("Invalid duration '" + durationObj + "': must be a valid DurationTag");
        }
    }
    if (effectMap.getObject("ambient") != null) {
        ElementTag ambientElement = effectMap.getObject("ambient").asElement();
        if (ambientElement.isBoolean()) {
            ambient = ambientElement.asBoolean();
        } else if (context.showErrors()) {
            Debug.echoError("Invalid ambient state '" + ambientElement + "': must be a boolean.");
        }
    }
    if (effectMap.getObject("particles") != null) {
        ElementTag particlesElement = effectMap.getObject("particles").asElement();
        if (particlesElement.isBoolean()) {
            particles = particlesElement.asBoolean();
        } else if (context.showErrors()) {
            Debug.echoError("Invalid particles state '" + particlesElement + "': must be a boolean.");
        }
    }
    if (effectMap.getObject("icon") != null) {
        ElementTag iconElement = effectMap.getObject("icon").asElement();
        if (iconElement.isBoolean()) {
            icon = iconElement.asBoolean();
        } else if (context.showErrors()) {
            Debug.echoError("Invalid icon state '" + iconElement + "': must be a boolean.");
        }
    }
    return new PotionEffect(type, duration.getTicksAsInt(), amplifier, ambient, particles, icon);
}
Also used : ObjectTag(com.denizenscript.denizencore.objects.ObjectTag) PotionEffect(org.bukkit.potion.PotionEffect) PotionEffectType(org.bukkit.potion.PotionEffectType) ElementTag(com.denizenscript.denizencore.objects.core.ElementTag) DurationTag(com.denizenscript.denizencore.objects.core.DurationTag)

Example 23 with ObjectTag

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

the class ItemPotion method adjust.

@Override
public void adjust(Mechanism mechanism) {
    // -->
    if (mechanism.matches("potion_effects")) {
        List<ObjectTag> data = new ArrayList<>(CoreUtilities.objectToList(mechanism.value, mechanism.context));
        ObjectTag firstObj = data.remove(0);
        PotionMeta meta = getMeta();
        PotionType type;
        boolean upgraded = false;
        boolean extended = false;
        ColorTag color = null;
        if (firstObj.canBeType(MapTag.class)) {
            MapTag baseEffect = firstObj.asType(MapTag.class, mechanism.context);
            if (baseEffect.getObject("type") != null) {
                ElementTag typeElement = baseEffect.getObject("type").asElement();
                if (!typeElement.matchesEnum(PotionType.class)) {
                    mechanism.echoError("Invalid base potion type '" + typeElement + "': type is required");
                    return;
                }
                type = PotionType.valueOf(typeElement.asString().toUpperCase());
            } else {
                mechanism.echoError("No base potion type specified: type is required");
                return;
            }
            if (baseEffect.getObject("upgraded") != null) {
                ElementTag upgradedElement = baseEffect.getObject("upgraded").asElement();
                if (upgradedElement.isBoolean()) {
                    upgraded = upgradedElement.asBoolean();
                } else {
                    mechanism.echoError("Invalid upgraded state '" + upgradedElement + "': must be a boolean");
                }
            }
            if (baseEffect.getObject("extended") != null) {
                ElementTag extendedElement = baseEffect.getObject("extended").asElement();
                if (extendedElement.isBoolean()) {
                    extended = extendedElement.asBoolean();
                } else {
                    mechanism.echoError("Invalid extended state '" + extendedElement + "': must be a boolean");
                }
            }
            if (baseEffect.getObject("color") != null) {
                ObjectTag colorObj = baseEffect.getObject("color");
                if (colorObj.canBeType(ColorTag.class)) {
                    color = colorObj.asType(ColorTag.class, mechanism.context);
                } else {
                    mechanism.echoError("Invalid color '" + colorObj + "': must be a valid ColorTag");
                }
            }
        } else {
            String[] d1 = firstObj.toString().split(",");
            try {
                type = PotionType.valueOf(d1[0].toUpperCase());
            } catch (IllegalArgumentException ex) {
                mechanism.echoError("Invalid base potion type '" + d1[0] + "': type is required");
                return;
            }
            upgraded = CoreUtilities.equalsIgnoreCase(d1[1], "true");
            extended = CoreUtilities.equalsIgnoreCase(d1[2], "true");
            if (d1.length > 3) {
                ColorTag temp = ColorTag.valueOf(d1[3].replace("&comma", ","), mechanism.context);
                if (temp == null) {
                    mechanism.echoError("Invalid color '" + d1[3] + "': must be a valid ColorTag");
                } else {
                    color = temp;
                }
            }
        }
        if (upgraded && !type.isUpgradeable()) {
            mechanism.echoError("Cannot upgrade potion of type '" + type.name() + "'");
            upgraded = false;
        }
        if (extended && !type.isExtendable()) {
            mechanism.echoError("Cannot extend potion of type '" + type.name() + "'");
            extended = false;
        }
        if (upgraded && extended) {
            mechanism.echoError("Cannot both upgrade and extend a potion");
            extended = false;
        }
        if (color != null) {
            meta.setColor(color.getColor());
        }
        meta.setBasePotionData(new PotionData(type, extended, upgraded));
        meta.clearCustomEffects();
        for (ObjectTag effectObj : data) {
            PotionEffect effect;
            if (effectObj.canBeType(MapTag.class)) {
                effect = parseEffect(effectObj.asType(MapTag.class, mechanism.context), mechanism.context);
            } else {
                effect = parseEffect(effectObj.toString(), mechanism.context);
            }
            if (effect != null) {
                meta.addCustomEffect(effect, false);
            } else {
                mechanism.echoError("Invalid potion effect '" + effectObj + "'");
            }
        }
        item.setItemMeta(meta);
    }
}
Also used : PotionEffect(org.bukkit.potion.PotionEffect) ArrayList(java.util.ArrayList) ColorTag(com.denizenscript.denizen.objects.ColorTag) PotionMeta(org.bukkit.inventory.meta.PotionMeta) MapTag(com.denizenscript.denizencore.objects.core.MapTag) ObjectTag(com.denizenscript.denizencore.objects.ObjectTag) PotionData(org.bukkit.potion.PotionData) PotionType(org.bukkit.potion.PotionType) ElementTag(com.denizenscript.denizencore.objects.core.ElementTag)

Example 24 with ObjectTag

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

the class ItemRawNBT method setFullNBT.

public void setFullNBT(ItemTag item, MapTag input, TagContext context, boolean retainOld) {
    CompoundTag compoundTag = retainOld ? NMSHandler.getItemHelper().getNbtData(item.getItemStack()) : null;
    Map<String, Tag> result = compoundTag == null ? new LinkedHashMap<>() : new LinkedHashMap<>(compoundTag.getValue());
    for (Map.Entry<StringHolder, ObjectTag> entry : input.map.entrySet()) {
        try {
            Tag tag = convertObjectToNbt(entry.getValue().toString(), context, "(item).");
            if (tag != null) {
                result.put(entry.getKey().str, tag);
            }
        } catch (Exception ex) {
            Debug.echoError("Raw_Nbt input failed for root key '" + entry.getKey().str + "'.");
            Debug.echoError(ex);
            return;
        }
    }
    compoundTag = NMSHandler.getInstance().createCompoundTag(result);
    item.setItemStack(NMSHandler.getItemHelper().setNbtData(item.getItemStack(), compoundTag));
}
Also used : StringHolder(com.denizenscript.denizencore.utilities.text.StringHolder) ObjectTag(com.denizenscript.denizencore.objects.ObjectTag) 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)

Example 25 with ObjectTag

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

the class ItemRawNBT method jnbtTagToObject.

public static ObjectTag jnbtTagToObject(Tag tag) {
    if (tag instanceof CompoundTag) {
        MapTag result = new MapTag();
        for (Map.Entry<String, Tag> entry : ((CompoundTag) tag).getValue().entrySet()) {
            result.putObject(entry.getKey(), jnbtTagToObject(entry.getValue()));
        }
        return result;
    } else if (tag instanceof JNBTListTag) {
        ListTag result = new ListTag();
        for (Tag entry : ((JNBTListTag) tag).getValue()) {
            result.addObject(jnbtTagToObject(entry));
        }
        return new ElementTag("list:" + NBTUtils.getTypeCode(((JNBTListTag) tag).getType()) + ":" + result.identify());
    } else if (tag instanceof ByteArrayTag) {
        byte[] data = ((ByteArrayTag) tag).getValue();
        StringBuilder output = new StringBuilder(data.length * 4);
        for (int i = 0; i < data.length; i++) {
            output.append(data[i]).append("|");
        }
        return new ElementTag("byte_array:" + output.toString());
    } else if (tag instanceof IntArrayTag) {
        int[] data = ((IntArrayTag) tag).getValue();
        StringBuilder output = new StringBuilder(data.length * 4);
        for (int i = 0; i < data.length; i++) {
            output.append(data[i]).append("|");
        }
        return new ElementTag("int_array:" + output.toString());
    } else if (tag instanceof ByteTag) {
        return new ElementTag("byte:" + ((ByteTag) tag).getValue());
    } else if (tag instanceof ShortTag) {
        return new ElementTag("short:" + ((ShortTag) tag).getValue());
    } else if (tag instanceof IntTag) {
        return new ElementTag("int:" + ((IntTag) tag).getValue());
    } else if (tag instanceof LongTag) {
        return new ElementTag("long:" + ((LongTag) tag).getValue());
    } else if (tag instanceof FloatTag) {
        return new ElementTag("float:" + ((FloatTag) tag).getValue());
    } else if (tag instanceof DoubleTag) {
        return new ElementTag("double:" + ((DoubleTag) tag).getValue());
    } else if (tag instanceof StringTag) {
        return new ElementTag("string:" + ((StringTag) tag).getValue());
    } else if (tag instanceof EndTag) {
        return new ElementTag("end");
    } else {
        return new ElementTag("unknown:" + tag.getValue());
    }
}
Also used : ListTag(com.denizenscript.denizencore.objects.core.ListTag) MapTag(com.denizenscript.denizencore.objects.core.MapTag) 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) ElementTag(com.denizenscript.denizencore.objects.core.ElementTag)

Aggregations

ObjectTag (com.denizenscript.denizencore.objects.ObjectTag)38 ElementTag (com.denizenscript.denizencore.objects.core.ElementTag)21 MapTag (com.denizenscript.denizencore.objects.core.MapTag)19 ListTag (com.denizenscript.denizencore.objects.core.ListTag)16 StringHolder (com.denizenscript.denizencore.utilities.text.StringHolder)11 NPCTag (com.denizenscript.denizen.objects.NPCTag)8 PlayerTag (com.denizenscript.denizen.objects.PlayerTag)7 ItemTag (com.denizenscript.denizen.objects.ItemTag)6 ArrayList (java.util.ArrayList)6 BukkitTagContext (com.denizenscript.denizen.tags.BukkitTagContext)5 ScriptTag (com.denizenscript.denizencore.objects.core.ScriptTag)5 HashMap (java.util.HashMap)5 Map (java.util.Map)5 EntityTag (com.denizenscript.denizen.objects.EntityTag)4 UUID (java.util.UUID)4 ScriptEntry (com.denizenscript.denizencore.scripts.ScriptEntry)3 AbstractCommand (com.denizenscript.denizencore.scripts.commands.AbstractCommand)3 AttributeModifier (org.bukkit.attribute.AttributeModifier)3 Player (org.bukkit.entity.Player)3 TriggerTrait (com.denizenscript.denizen.npc.traits.TriggerTrait)2