Search in sources :

Example 41 with ElementTag

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

the class InventoryTag method loadIdentifiers.

private void loadIdentifiers(final InventoryHolder holder) {
    if (inventory == null) {
        return;
    }
    InventoryTag realInv = InventoryTrackerSystem.getTagFormFor(inventory);
    if (realInv != null) {
        Debug.echoError("Tried to load already-tracked inventory as new inventory?");
        return;
    }
    trackTemporaryInventory(this);
    if (holder != null) {
        if (holder instanceof NPCTag) {
            idType = "npc";
            idHolder = ((NPCTag) holder);
            return;
        } else if (holder instanceof Player) {
            if (Depends.citizens != null && CitizensAPI.getNPCRegistry().isNPC((Player) holder)) {
                idType = "npc";
                idHolder = (NPCTag.fromEntity((Player) holder));
                return;
            }
            if (inventory.getType() == InventoryType.CRAFTING) {
                idType = "crafting";
            }
            if (inventory.getType() == InventoryType.ENDER_CHEST) {
                idType = "enderchest";
            } else if (inventory.getType() == InventoryType.WORKBENCH) {
                idType = "workbench";
            } else {
                idType = "player";
            }
            idHolder = new PlayerTag((Player) holder);
            return;
        } else if (holder instanceof Entity) {
            idType = "entity";
            idHolder = new EntityTag((Entity) holder);
            return;
        } else {
            idType = "location";
            idHolder = getLocation(holder);
            if (idHolder != null) {
                return;
            }
        }
    } else if (inventory instanceof AnvilInventory && inventory.getLocation() != null) {
        idType = "location";
        idHolder = new LocationTag(inventory.getLocation());
    } else if (getIdType().equals("player")) {
        if (idHolder instanceof PlayerTag) {
            return;
        }
        // Iterate through offline player inventories
        for (Map.Entry<UUID, PlayerInventory> inv : ImprovedOfflinePlayer.offlineInventories.entrySet()) {
            // TODO: Less weird lookup?
            if (inv.getValue().equals(inventory)) {
                idHolder = new PlayerTag(inv.getKey());
                return;
            }
        }
    } else if (getIdType().equals("enderchest")) {
        if (idHolder instanceof PlayerTag) {
            return;
        }
        // Iterate through offline player enderchests
        for (Map.Entry<UUID, Inventory> inv : ImprovedOfflinePlayer.offlineEnderChests.entrySet()) {
            // TODO: Less weird lookup?
            if (inv.getValue().equals(inventory)) {
                idHolder = new PlayerTag(inv.getKey());
                return;
            }
        }
    } else if (getIdType().equals("script")) {
        if (idHolder instanceof ScriptTag) {
            return;
        }
        InventoryTag tracked = InventoryTrackerSystem.retainedInventoryLinks.get(inventory);
        if (tracked != null) {
            idHolder = tracked.idHolder;
            return;
        }
    }
    idType = "generic";
    idHolder = new ElementTag(CoreUtilities.toLowerCase(getInventory().getType().name()));
}
Also used : HumanEntity(org.bukkit.entity.HumanEntity) Entity(org.bukkit.entity.Entity) Player(org.bukkit.entity.Player) ImprovedOfflinePlayer(com.denizenscript.denizen.nms.abstracts.ImprovedOfflinePlayer) ScriptTag(com.denizenscript.denizencore.objects.core.ScriptTag) ElementTag(com.denizenscript.denizencore.objects.core.ElementTag)

Example 42 with ElementTag

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

the class InventoryTag method setContents.

public void setContents(ListTag list, TagContext context) {
    int size;
    if (inventory == null) {
        size = (int) Math.ceil(list.size() / 9.0) * 9;
        if (size == 0) {
            size = 9;
        }
        inventory = Bukkit.getServer().createInventory(null, size);
        idType = "generic";
        idHolder = new ElementTag("chest");
    } else {
        size = inventory.getSize();
    }
    ItemStack[] contents = new ItemStack[size];
    int filled = 0;
    for (ItemTag item : list.filter(ItemTag.class, context)) {
        if (filled >= contents.length) {
            Debug.echoError("Cannot set contents of inventory to " + list.size() + " items, as the inventory size is only " + size + "!");
            break;
        }
        contents[filled] = item.getItemStack();
        filled++;
    }
    while (filled < size) {
        contents[filled] = new ItemStack(Material.AIR);
        filled++;
    }
    inventory.setContents(contents);
    if (Depends.citizens != null && idHolder instanceof NPCTag) {
        ((NPCTag) idHolder).getInventoryTrait().setContents(contents);
    }
}
Also used : ElementTag(com.denizenscript.denizencore.objects.core.ElementTag)

Example 43 with ElementTag

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

the class InventoryTag method valueOf.

@Fetchable("in")
public static InventoryTag valueOf(String string, TagContext context) {
    if (string == null) {
        return null;
    }
    if (string.startsWith("in@")) {
        string = string.substring("in@".length());
    }
    List<String> properties = ObjectFetcher.separateProperties(string);
    if (properties != null && properties.size() > 1) {
        InventoryTag result = internalGetInventoryFor(context, properties);
        if (result == null) {
            if (context == null || context.showErrors()) {
                Debug.echoError("Value of InventoryTag returning null. Invalid InventoryTag-with-properties specified: " + string);
            }
            return null;
        }
        trackTemporaryInventory(result);
        return result;
    }
    Notable noted = NoteManager.getSavedObject(string);
    if (noted instanceof InventoryTag) {
        return (InventoryTag) noted;
    }
    if (ScriptRegistry.containsScript(string, InventoryScriptContainer.class)) {
        return ScriptRegistry.getScriptContainerAs(string, InventoryScriptContainer.class).getInventoryFrom(context);
    }
    if (new ElementTag(string).matchesEnum(InventoryType.class)) {
        InventoryType type = InventoryType.valueOf(string.toUpperCase());
        return new InventoryTag(type);
    }
    if (context == null || context.showErrors()) {
        Debug.echoError("Value of InventoryTag returning null. Invalid InventoryTag specified: " + string);
    }
    return null;
}
Also used : InventoryScriptContainer(com.denizenscript.denizen.scripts.containers.core.InventoryScriptContainer) InventoryType(org.bukkit.event.inventory.InventoryType) ElementTag(com.denizenscript.denizencore.objects.core.ElementTag) Notable(com.denizenscript.denizencore.objects.notable.Notable)

Example 44 with ElementTag

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

the class EntityColor method adjust.

@Override
public void adjust(Mechanism mechanism) {
    // -->
    if (mechanism.matches("color")) {
        EntityType type = colored.getBukkitEntityType();
        if (type == EntityType.HORSE && mechanism.requireObject(ListTag.class)) {
            ListTag list = mechanism.valueAsType(ListTag.class);
            Horse horse = (Horse) colored.getBukkitEntity();
            String color = list.get(0);
            if (new ElementTag(color).matchesEnum(Horse.Color.class)) {
                horse.setColor(Horse.Color.valueOf(color.toUpperCase()));
            } else {
                mechanism.echoError("Invalid horse color specified: " + color);
            }
            if (list.size() > 1) {
                String style = list.get(1);
                if (new ElementTag(style).matchesEnum(Horse.Style.class)) {
                    horse.setStyle(Horse.Style.valueOf(style.toUpperCase()));
                } else {
                    mechanism.echoError("Invalid horse style specified: " + style);
                }
            }
        } else if (type == EntityType.SHEEP && mechanism.requireEnum(DyeColor.class)) {
            ((Sheep) colored.getBukkitEntity()).setColor(DyeColor.valueOf(mechanism.getValue().asString().toUpperCase()));
        } else if (type == EntityType.WOLF && mechanism.requireEnum(DyeColor.class)) {
            ((Wolf) colored.getBukkitEntity()).setCollarColor(DyeColor.valueOf(mechanism.getValue().asString().toUpperCase()));
        } else if (type == EntityType.OCELOT && mechanism.requireEnum(Ocelot.Type.class)) {
            // TODO: Deprecate?
            ((Ocelot) colored.getBukkitEntity()).setCatType(Ocelot.Type.valueOf(mechanism.getValue().asString().toUpperCase()));
        } else if (type == EntityType.RABBIT && mechanism.requireEnum(Rabbit.Type.class)) {
            ((Rabbit) colored.getBukkitEntity()).setRabbitType(Rabbit.Type.valueOf(mechanism.getValue().asString().toUpperCase()));
        } else if ((type == EntityType.LLAMA || type == EntityType.TRADER_LLAMA) && mechanism.requireEnum(Llama.Color.class)) {
            ((Llama) colored.getBukkitEntity()).setColor(Llama.Color.valueOf(mechanism.getValue().asString().toUpperCase()));
        } else if (type == EntityType.PARROT && mechanism.requireEnum(Parrot.Variant.class)) {
            ((Parrot) colored.getBukkitEntity()).setVariant(Parrot.Variant.valueOf(mechanism.getValue().asString().toUpperCase()));
        } else if (type == EntityType.SHULKER && mechanism.requireEnum(DyeColor.class)) {
            ((Shulker) colored.getBukkitEntity()).setColor(DyeColor.valueOf(mechanism.getValue().asString().toUpperCase()));
        } else if (type == EntityType.MUSHROOM_COW && mechanism.requireEnum(MushroomCow.Variant.class)) {
            ((MushroomCow) colored.getBukkitEntity()).setVariant(MushroomCow.Variant.valueOf(mechanism.getValue().asString().toUpperCase()));
        } else if (type == EntityType.TROPICAL_FISH && mechanism.requireObject(ListTag.class)) {
            ListTag list = mechanism.valueAsType(ListTag.class);
            TropicalFish fish = ((TropicalFish) colored.getBukkitEntity());
            String pattern = list.get(0);
            if (new ElementTag(pattern).matchesEnum(TropicalFish.Pattern.class)) {
                fish.setPattern(TropicalFish.Pattern.valueOf(pattern.toUpperCase()));
            } else {
                mechanism.echoError("Invalid tropical fish pattern specified: " + pattern);
            }
            if (list.size() > 1) {
                String color = list.get(1);
                if (new ElementTag(color).matchesEnum(DyeColor.class)) {
                    fish.setBodyColor(DyeColor.valueOf(color.toUpperCase()));
                } else {
                    mechanism.echoError("Invalid color specified: " + color);
                }
            }
            if (list.size() > 2) {
                String patternColor = list.get(2);
                if (new ElementTag(patternColor).matchesEnum(DyeColor.class)) {
                    fish.setPatternColor(DyeColor.valueOf(patternColor.toUpperCase()));
                } else {
                    mechanism.echoError("Invalid pattern color specified: " + patternColor);
                }
            }
        } else if (type == EntityType.FOX && mechanism.requireEnum(Fox.Type.class)) {
            ((Fox) colored.getBukkitEntity()).setFoxType(Fox.Type.valueOf(mechanism.getValue().asString().toUpperCase()));
        } else if (type == EntityType.CAT && mechanism.requireObject(ListTag.class)) {
            Cat cat = (Cat) colored.getBukkitEntity();
            ListTag list = mechanism.valueAsType(ListTag.class);
            String catType = list.get(0);
            if (new ElementTag(catType).matchesEnum(Cat.Type.class)) {
                cat.setCatType(Cat.Type.valueOf(catType.toUpperCase()));
            } else {
                mechanism.echoError("Invalid cat type specified: " + catType);
            }
            if (list.size() > 1) {
                String color = list.get(1);
                if (new ElementTag(color).matchesEnum(DyeColor.class)) {
                    cat.setCollarColor(DyeColor.valueOf(list.get(1).toUpperCase()));
                } else {
                    mechanism.echoError("Invalid color specified: " + color);
                }
            }
        } else if (type == EntityType.PANDA && mechanism.requireObject(ListTag.class)) {
            Panda panda = (Panda) colored.getBukkitEntity();
            ListTag list = mechanism.valueAsType(ListTag.class);
            String mainGene = list.get(0);
            if (new ElementTag(mainGene).matchesEnum(Panda.Gene.class)) {
                panda.setMainGene(Panda.Gene.valueOf(mainGene.toUpperCase()));
            } else {
                mechanism.echoError("Invalid panda gene specified: " + mainGene);
            }
            if (list.size() > 1) {
                String hiddenGene = list.get(1);
                if (new ElementTag(hiddenGene).matchesEnum(Panda.Gene.class)) {
                    panda.setHiddenGene(Panda.Gene.valueOf(hiddenGene.toUpperCase()));
                } else {
                    mechanism.echoError("Invalid panda hidden gene specified: " + hiddenGene);
                }
            }
        } else if (type == EntityType.VILLAGER && mechanism.requireEnum(Villager.Type.class)) {
            ((Villager) colored.getBukkitEntity()).setVillagerType(Villager.Type.valueOf(mechanism.getValue().asString().toUpperCase()));
        } else if (type == EntityType.ARROW && mechanism.requireObject(ColorTag.class)) {
            ((Arrow) colored.getBukkitEntity()).setColor(mechanism.valueAsType(ColorTag.class).getColor());
        } else if (NMSHandler.getVersion().isAtLeast(NMSVersion.v1_17) && MultiVersionHelper1_17.colorIsApplicable(type)) {
            MultiVersionHelper1_17.setColor(colored.getBukkitEntity(), mechanism);
        }
    }
}
Also used : ColorTag(com.denizenscript.denizen.objects.ColorTag) DyeColor(org.bukkit.DyeColor) ListTag(com.denizenscript.denizencore.objects.core.ListTag) ElementTag(com.denizenscript.denizencore.objects.core.ElementTag)

Example 45 with ElementTag

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

the class ItemPotion method registerTags.

public static void registerTags() {
    // <--[tag]
    // @attribute <ItemTag.potion_base_type>
    // @returns ElementTag
    // @mechanism ItemTag.potion_effects
    // @group properties
    // @description
    // Returns the base potion type name for this potion item.
    // The type will be from <@link url https://hub.spigotmc.org/javadocs/spigot/org/bukkit/potion/PotionType.html>.
    // -->
    PropertyParser.<ItemPotion, ElementTag>registerTag(ElementTag.class, "potion_base_type", (attribute, object) -> {
        return new ElementTag(object.getMeta().getBasePotionData().getType().name());
    });
    // <--[tag]
    // @attribute <ItemTag.potion_base>
    // @returns ElementTag
    // @group attribute
    // @mechanism ItemTag.potion_effects
    // @deprecated use 'effects_data' instead
    // @description
    // Deprecated in favor of <@link tag ItemTag.effects_data>
    // -->
    PropertyParser.<ItemPotion, ElementTag>registerTag(ElementTag.class, "potion_base", (attribute, object) -> {
        Deprecations.oldPotionEffects.warn(attribute.context);
        PotionMeta meta = object.getMeta();
        return new ElementTag(meta.getBasePotionData().getType().name() + "," + (meta.getBasePotionData().isUpgraded() ? 2 : 1) + "," + meta.getBasePotionData().isExtended() + "," + (object.item.getBukkitMaterial() == Material.SPLASH_POTION) + (meta.hasColor() ? "," + new ColorTag(meta.getColor()).identify() : ""));
    });
    // <--[tag]
    // @attribute <ItemTag.potion_effects>
    // @returns ListTag
    // @group attribute
    // @mechanism ItemTag.potion_effects
    // @deprecated use 'effects_data' instead
    // @description
    // Deprecated in favor of <@link tag ItemTag.effects_data>
    // -->
    PropertyParser.<ItemPotion, ListTag>registerTag(ListTag.class, "potion_effects", (attribute, object) -> {
        Deprecations.oldPotionEffects.warn(attribute.context);
        ListTag result = new ListTag();
        for (PotionEffect pot : object.getMeta().getCustomEffects()) {
            result.add(stringifyEffect(pot));
        }
        return result;
    });
    // <--[tag]
    // @attribute <ItemTag.has_potion_effect>
    // @returns ElementTag(Boolean)
    // @mechanism ItemTag.potion_effects
    // @description
    // Returns whether the potion has a potion effect.
    // -->
    PropertyParser.<ItemPotion, ElementTag>registerTag(ElementTag.class, "has_potion_effect", (attribute, object) -> {
        return new ElementTag(object.getMeta().hasCustomEffects());
    });
    // <--[tag]
    // @attribute <ItemTag.potion_effect[<#>]>
    // @returns ElementTag
    // @group attribute
    // @mechanism ItemTag.potion_effects
    // @deprecated use 'effects_data' instead
    // @description
    // Deprecated in favor of <@link tag ItemTag.effects_data>
    // -->
    PropertyParser.<ItemPotion, ElementTag>registerTag(ElementTag.class, "potion_effect", (attribute, object) -> {
        Deprecations.oldPotionEffects.warn(attribute.context);
        PotionMeta meta = object.getMeta();
        int potN = attribute.hasParam() ? attribute.getIntParam() - 1 : 0;
        if (potN < 0 || potN > meta.getCustomEffects().size()) {
            return null;
        }
        if (attribute.startsWith("is_splash", 2)) {
            attribute.fulfill(1);
            return new ElementTag(object.item.getBukkitMaterial() == Material.SPLASH_POTION);
        }
        if (attribute.startsWith("is_extended", 2)) {
            attribute.fulfill(1);
            return new ElementTag(meta.getBasePotionData().isExtended());
        }
        if (attribute.startsWith("level", 2)) {
            attribute.fulfill(1);
            return new ElementTag(meta.getBasePotionData().isUpgraded() ? 2 : 1);
        }
        if (attribute.startsWith("is_ambient", 2)) {
            attribute.fulfill(1);
            return new ElementTag(meta.getCustomEffects().get(potN).isAmbient());
        }
        if (attribute.startsWith("icon", 2)) {
            attribute.fulfill(1);
            return new ElementTag(meta.getCustomEffects().get(potN).hasIcon());
        }
        if (attribute.startsWith("has_particles", 2)) {
            attribute.fulfill(1);
            return new ElementTag(meta.getCustomEffects().get(potN).hasParticles());
        }
        if (attribute.startsWith("duration", 2)) {
            attribute.fulfill(1);
            return new ElementTag(meta.getCustomEffects().get(potN).getDuration());
        }
        if (attribute.startsWith("amplifier", 2)) {
            attribute.fulfill(1);
            return new ElementTag(meta.getCustomEffects().get(potN).getAmplifier());
        }
        if (attribute.startsWith("type", 2)) {
            attribute.fulfill(1);
            return new ElementTag(meta.getCustomEffects().get(potN).getType().getName());
        }
        if (attribute.startsWith("data", 2)) {
            attribute.fulfill(1);
            return new ElementTag(0);
        }
        PotionData data = meta.getBasePotionData();
        return new ElementTag(data.getType().name() + "," + (data.isUpgraded() ? 2 : 1) + "," + data.isExtended() + "," + (object.item.getBukkitMaterial() == Material.SPLASH_POTION));
    });
    // <--[tag]
    // @attribute <ItemTag.effects_data>
    // @returns ListTag(MapTag)
    // @mechanism ItemTag.potion_effects
    // @group properties
    // @description
    // Returns a list of all potion effects on this item, in the same format as the MapTag input to the mechanism.
    // Note that the first value in the list is the potion's base type, and all subsequent entries are effect data.
    // -->
    PropertyParser.<ItemPotion, ListTag>registerTag(ListTag.class, "effects_data", (attribute, object) -> {
        return object.getMapTagData();
    });
}
Also used : PotionData(org.bukkit.potion.PotionData) 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)

Aggregations

ElementTag (com.denizenscript.denizencore.objects.core.ElementTag)237 ListTag (com.denizenscript.denizencore.objects.core.ListTag)86 EntityTag (com.denizenscript.denizen.objects.EntityTag)66 LocationTag (com.denizenscript.denizen.objects.LocationTag)49 PlayerTag (com.denizenscript.denizen.objects.PlayerTag)48 InvalidArgumentsException (com.denizenscript.denizencore.exceptions.InvalidArgumentsException)43 DurationTag (com.denizenscript.denizencore.objects.core.DurationTag)40 List (java.util.List)40 Argument (com.denizenscript.denizencore.objects.Argument)28 Player (org.bukkit.entity.Player)28 MapTag (com.denizenscript.denizencore.objects.core.MapTag)27 EventHandler (org.bukkit.event.EventHandler)26 NPCTag (com.denizenscript.denizen.objects.NPCTag)24 ObjectTag (com.denizenscript.denizencore.objects.ObjectTag)22 ItemTag (com.denizenscript.denizen.objects.ItemTag)19 ScriptTag (com.denizenscript.denizencore.objects.core.ScriptTag)18 ArrayList (java.util.ArrayList)16 Entity (org.bukkit.entity.Entity)16 MaterialTag (com.denizenscript.denizen.objects.MaterialTag)12 ScriptEntry (com.denizenscript.denizencore.scripts.ScriptEntry)12