Search in sources :

Example 86 with ListTag

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

the class CuboidTag method valueOf.

@Fetchable("cu")
public static CuboidTag valueOf(String string, TagContext context) {
    if (string == null) {
        return null;
    }
    if (CoreUtilities.toLowerCase(string).startsWith("cu@")) {
        string = string.substring("cu@".length());
    }
    Notable noted = NoteManager.getSavedObject(string);
    if (noted instanceof CuboidTag) {
        return (CuboidTag) noted;
    }
    if (CoreUtilities.contains(string, '@')) {
        if (CoreUtilities.contains(string, '|') && string.contains("l@")) {
            Debug.echoError("Warning: likely improperly constructed CuboidTag '" + string + "' - use to_cuboid");
        } else {
            return null;
        }
    }
    if (CoreUtilities.contains(string, '|')) {
        ListTag positions = ListTag.valueOf(string, context);
        if (positions.size() > 1 && LocationTag.matches(positions.get(0)) && LocationTag.matches(positions.get(1))) {
            if (positions.size() % 2 != 0) {
                if (context == null || context.showErrors()) {
                    Debug.echoError("valueOf CuboidTag returning null (Uneven number of locations): '" + string + "'.");
                }
                return null;
            }
            CuboidTag toReturn = new CuboidTag();
            for (int i = 0; i < positions.size(); i += 2) {
                LocationTag pos_1 = LocationTag.valueOf(positions.get(i), context);
                LocationTag pos_2 = LocationTag.valueOf(positions.get(i + 1), context);
                if (pos_1 == null || pos_2 == null) {
                    if (context == null || context.showErrors()) {
                        Debug.echoError("valueOf in CuboidTag returning null (null locations): '" + string + "'.");
                    }
                    return null;
                }
                if (pos_1.getWorldName() == null || pos_2.getWorldName() == null) {
                    if (context == null || context.showErrors()) {
                        Debug.echoError("valueOf in CuboidTag returning null (null worlds): '" + string + "'.");
                    }
                    return null;
                }
                toReturn.addPair(pos_1, pos_2);
            }
            if (toReturn.pairs.size() > 0) {
                return toReturn;
            }
        }
    } else if (CoreUtilities.contains(string, ',')) {
        List<String> subStrs = CoreUtilities.split(string, ',');
        if (subStrs.size() < 7 || (subStrs.size() - 1) % 6 != 0) {
            if (context == null || context.showErrors()) {
                Debug.echoError("valueOf CuboidTag returning null (Improper number of commas): '" + string + "'.");
            }
            return null;
        }
        CuboidTag toReturn = new CuboidTag();
        String worldName = subStrs.get(0);
        if (worldName.startsWith("w@")) {
            worldName = worldName.substring("w@".length());
        }
        try {
            for (int i = 0; i < subStrs.size() - 1; i += 6) {
                LocationTag locationOne = new LocationTag(parseRoundDouble(subStrs.get(i + 1)), parseRoundDouble(subStrs.get(i + 2)), parseRoundDouble(subStrs.get(i + 3)), worldName);
                LocationTag locationTwo = new LocationTag(parseRoundDouble(subStrs.get(i + 4)), parseRoundDouble(subStrs.get(i + 5)), parseRoundDouble(subStrs.get(i + 6)), worldName);
                toReturn.addPair(locationOne, locationTwo);
            }
        } catch (NumberFormatException ex) {
            if (context == null || context.showErrors()) {
                Debug.echoError("valueOf CuboidTag returning null (Improper number value inputs): '" + ex.getMessage() + "'.");
            }
            return null;
        }
        if (toReturn.pairs.size() > 0) {
            return toReturn;
        }
    }
    if (context == null || context.showErrors()) {
        Debug.echoError("Minor: valueOf CuboidTag returning null: " + string);
    }
    return null;
}
Also used : ArrayList(java.util.ArrayList) List(java.util.List) Notable(com.denizenscript.denizencore.objects.notable.Notable) ListTag(com.denizenscript.denizencore.objects.core.ListTag)

Example 87 with ListTag

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

the class CuboidTag method getBlocks.

@Override
public ListTag getBlocks(Predicate<Location> test) {
    List<LocationTag> locs = getBlocks_internal(test);
    ListTag list = new ListTag();
    for (LocationTag loc : locs) {
        list.addObject(loc);
    }
    return list;
}
Also used : ListTag(com.denizenscript.denizencore.objects.core.ListTag)

Example 88 with ListTag

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

the class InventoryScriptContainer method getInventoryFrom.

public InventoryTag getInventoryFrom(TagContext context) {
    InventoryTag inventory;
    context = (context == null ? CoreUtilities.basicContext : context).clone();
    ScriptTag thisScript = new ScriptTag(this);
    context.script = thisScript;
    context.debug = context.debug && shouldDebug();
    try {
        InventoryType type = InventoryType.CHEST;
        if (contains("inventory", String.class)) {
            try {
                type = InventoryType.valueOf(getString("inventory").toUpperCase());
            } catch (IllegalArgumentException ex) {
                Debug.echoError(this, "Invalid inventory type specified. Assuming \"CHEST\" (" + ex.getMessage() + ")");
            }
        } else {
            Debug.echoError(this, "Inventory script '" + getName() + "' does not specify an inventory type. Assuming \"CHEST\".");
        }
        if (type == InventoryType.PLAYER) {
            Debug.echoError(this, "Inventory type 'player' is not valid for inventory scripts - defaulting to 'CHEST'.");
            type = InventoryType.CHEST;
        }
        int size = 0;
        if (contains("size", String.class)) {
            if (type != InventoryType.CHEST) {
                Debug.echoError(this, "You can only set the size of chest inventories!");
            } else {
                String sizeText = TagManager.tag(getString("size"), context);
                if (!ArgumentHelper.matchesInteger(sizeText)) {
                    Debug.echoError(this, "Invalid (not-a-number) size value.");
                } else {
                    size = Integer.parseInt(sizeText);
                }
                if (size == 0) {
                    Debug.echoError(this, "Inventory size can't be 0. Assuming default of inventory type...");
                }
                if (size % 9 != 0) {
                    size = (int) Math.ceil(size / 9.0) * 9;
                    Debug.echoError(this, "Inventory size must be a multiple of 9! Rounding up to " + size + "...");
                }
                if (size < 0) {
                    size = size * -1;
                    Debug.echoError(this, "Inventory size must be a positive number! Inverting to " + size + "...");
                }
            }
        }
        if (size == 0) {
            if (contains("slots", List.class) && type == InventoryType.CHEST) {
                size = getStringList("slots").size() * 9;
            } else {
                size = type.getDefaultSize();
            }
        }
        String title = contains("title", String.class) ? TagManager.tag(getString("title"), context) : null;
        if (type == InventoryType.CHEST) {
            inventory = new InventoryTag(size, title != null ? title : "Chest");
        } else {
            if (title == null) {
                inventory = new InventoryTag(type);
            } else {
                inventory = new InventoryTag(type, title);
            }
        }
        inventory.idType = "script";
        inventory.idHolder = thisScript;
        boolean[] filledSlots = new boolean[size];
        if (contains("slots", List.class)) {
            ItemStack[] finalItems = new ItemStack[size];
            int itemsAdded = 0;
            for (String items : getStringList("slots")) {
                items = TagManager.tag(items, context).trim();
                if (items.isEmpty()) {
                    continue;
                }
                if (!items.startsWith("[") || !items.endsWith("]")) {
                    Debug.echoError(this, "Invalid slots line: [" + items + "]... Ignoring it");
                    continue;
                }
                String[] itemsInLine = items.substring(1, items.length() - 1).split("\\[?\\]?\\s+\\[", -1);
                for (String item : itemsInLine) {
                    if (item.isEmpty()) {
                        finalItems[itemsAdded++] = new ItemStack(Material.AIR);
                        continue;
                    }
                    filledSlots[itemsAdded] = true;
                    if (contains("definitions." + item, String.class)) {
                        ItemTag def = ItemTag.valueOf(TagManager.tag(getString("definitions." + item), context), context);
                        if (def == null) {
                            Debug.echoError(this, "Invalid definition '" + item + "'... Ignoring it and assuming 'AIR'");
                            finalItems[itemsAdded] = new ItemStack(Material.AIR);
                        } else {
                            finalItems[itemsAdded] = def.getItemStack();
                        }
                    } else {
                        try {
                            ItemTag itemTag = ItemTag.valueOf(item, context);
                            if (itemTag == null) {
                                finalItems[itemsAdded] = new ItemStack(Material.AIR);
                                Debug.echoError(this, "Invalid slot item: [" + item + "]... ignoring it and assuming 'AIR'");
                            } else {
                                finalItems[itemsAdded] = itemTag.getItemStack();
                            }
                        } catch (Exception ex) {
                            Debug.echoError(this, "Invalid slot item: [" + item + "]...");
                            Debug.echoError(ex);
                        }
                    }
                    itemsAdded++;
                }
            }
            inventory.setContents(finalItems);
        }
        if (containsScriptSection("procedural items")) {
            List<ScriptEntry> entries = getEntries(context.getScriptEntryData(), "procedural items");
            if (!entries.isEmpty()) {
                InstantQueue queue = new InstantQueue("INV_SCRIPT_ITEM_PROC");
                queue.addEntries(entries);
                if (contains("definitions", Map.class)) {
                    YamlConfiguration section = getConfigurationSection("definitions");
                    for (StringHolder string : section.getKeys(false)) {
                        String definition = string.str;
                        queue.addDefinition(definition, section.getString(definition));
                    }
                }
                queue.procedural = true;
                queue.start();
                if (queue.determinations != null) {
                    ListTag list = ListTag.getListFor(queue.determinations.getObject(0), context);
                    if (list != null) {
                        int x = 0;
                        for (ItemTag item : list.filter(ItemTag.class, context, true)) {
                            while (x < filledSlots.length && filledSlots[x]) {
                                x++;
                            }
                            if (x >= filledSlots.length || filledSlots[x]) {
                                break;
                            }
                            inventory.setSlots(x, item.getItemStack());
                            filledSlots[x] = true;
                        }
                    }
                }
            }
        }
    } catch (Exception e) {
        Debug.echoError(this, "Woah! An exception has been called while building this inventory script!");
        Debug.echoError(e);
        inventory = null;
    }
    if (inventory != null) {
        InventoryTag.trackTemporaryInventory(inventory);
    }
    return inventory;
}
Also used : InventoryType(org.bukkit.event.inventory.InventoryType) ScriptEntry(com.denizenscript.denizencore.scripts.ScriptEntry) YamlConfiguration(com.denizenscript.denizencore.utilities.YamlConfiguration) ListTag(com.denizenscript.denizencore.objects.core.ListTag) StringHolder(com.denizenscript.denizencore.utilities.text.StringHolder) ScriptTag(com.denizenscript.denizencore.objects.core.ScriptTag) List(java.util.List) InstantQueue(com.denizenscript.denizencore.scripts.queues.core.InstantQueue) ItemStack(org.bukkit.inventory.ItemStack) ItemTag(com.denizenscript.denizen.objects.ItemTag) InventoryTag(com.denizenscript.denizen.objects.InventoryTag)

Example 89 with ListTag

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

the class SignCommand method execute.

@Override
public void execute(final ScriptEntry scriptEntry) {
    String direction = scriptEntry.hasObject("direction") ? ((ElementTag) scriptEntry.getObject("direction")).asString() : null;
    ElementTag typeElement = scriptEntry.getElement("type");
    ListTag text = scriptEntry.getObjectTag("text");
    LocationTag location = scriptEntry.getObjectTag("location");
    MaterialTag material = scriptEntry.getObjectTag("material");
    if (scriptEntry.dbCallShouldDebug()) {
        Debug.report(scriptEntry, getName(), typeElement, location, db("direction", direction), material, text);
    }
    Type type = Type.valueOf(typeElement.asString().toUpperCase());
    Block sign = location.getBlock();
    if (type != Type.AUTOMATIC || !isAnySign(sign.getType())) {
        if (type == Type.WALL_SIGN) {
            BlockFace bf;
            if (direction != null) {
                bf = Utilities.chooseSignRotation(direction);
            } else {
                bf = Utilities.chooseSignRotation(sign);
            }
            setWallSign(sign, bf, material);
        } else {
            sign.setType(material == null ? Material.OAK_SIGN : material.getMaterial(), false);
            if (direction != null) {
                Utilities.setSignRotation(sign.getState(), direction);
            }
        }
    } else if (!isAnySign(sign.getType())) {
        if (sign.getRelative(BlockFace.DOWN).getType().isSolid()) {
            sign.setType(material == null ? Material.OAK_SIGN : material.getMaterial(), false);
        } else {
            BlockFace bf = Utilities.chooseSignRotation(sign);
            setWallSign(sign, bf, material);
        }
    }
    BlockState signState = sign.getState();
    Utilities.setSignLines((Sign) signState, text.toArray(new String[4]));
}
Also used : LocationTag(com.denizenscript.denizen.objects.LocationTag) MaterialTag(com.denizenscript.denizen.objects.MaterialTag) BlockState(org.bukkit.block.BlockState) BlockFace(org.bukkit.block.BlockFace) Block(org.bukkit.block.Block) ElementTag(com.denizenscript.denizencore.objects.core.ElementTag) ListTag(com.denizenscript.denizencore.objects.core.ListTag)

Example 90 with ListTag

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

the class PlayEffectCommand method execute.

@Override
public void execute(ScriptEntry scriptEntry) {
    List<LocationTag> locations = (List<LocationTag>) scriptEntry.getObject("location");
    List<PlayerTag> targets = (List<PlayerTag>) scriptEntry.getObject("targets");
    Effect effect = (Effect) scriptEntry.getObject("effect");
    Particle particleEffect = (Particle) scriptEntry.getObject("particleeffect");
    ItemTag iconcrack = scriptEntry.getObjectTag("iconcrack");
    ElementTag radius = scriptEntry.getElement("radius");
    ElementTag data = scriptEntry.getElement("data");
    ElementTag quantity = scriptEntry.getElement("quantity");
    ElementTag no_offset = scriptEntry.getElement("no_offset");
    boolean should_offset = no_offset == null || !no_offset.asBoolean();
    LocationTag offset = scriptEntry.getObjectTag("offset");
    ElementTag special_data = scriptEntry.getElement("special_data");
    LocationTag velocity = scriptEntry.getObjectTag("velocity");
    if (scriptEntry.dbCallShouldDebug()) {
        Debug.report(scriptEntry, getName(), (effect != null ? db("effect", effect.name()) : particleEffect != null ? db("special effect", particleEffect.getName()) : iconcrack), db("locations", locations), db("targets", targets), radius, data, quantity, offset, special_data, velocity, (should_offset ? db("note", "Location will be offset 1 block-height upward (see documentation)") : ""));
    }
    for (LocationTag location : locations) {
        if (should_offset) {
            // Slightly increase the location's Y so effects don't seem to come out of the ground
            location = new LocationTag(location.clone().add(0, 1, 0));
        }
        // Play the Bukkit effect the number of times specified
        if (effect != null) {
            for (int n = 0; n < quantity.asInt(); n++) {
                if (targets != null) {
                    for (PlayerTag player : targets) {
                        if (player.isValid() && player.isOnline()) {
                            // TODO: 1.13
                            player.getPlayerEntity().playEffect(location, effect, data.asInt());
                        }
                    }
                } else {
                    location.getWorld().playEffect(location, effect, data.asInt(), radius.asInt());
                }
            }
        } else // Play a ParticleEffect
        if (particleEffect != null) {
            List<Player> players = new ArrayList<>();
            if (targets == null) {
                float rad = radius.asFloat();
                for (Player player : location.getWorld().getPlayers()) {
                    if (player.getLocation().distanceSquared(location) < rad * rad) {
                        players.add(player);
                    }
                }
            } else {
                for (PlayerTag player : targets) {
                    if (player.isValid() && player.isOnline()) {
                        players.add(player.getPlayerEntity());
                    }
                }
            }
            Class clazz = particleEffect.neededData();
            Object dataObject = null;
            if (clazz != null) {
                if (special_data == null) {
                    Debug.echoError("Missing required special data for particle: " + particleEffect.getName());
                    return;
                } else if (clazz == org.bukkit.Particle.DustOptions.class) {
                    ListTag dataList = ListTag.valueOf(special_data.asString(), scriptEntry.getContext());
                    if (dataList.size() != 2) {
                        Debug.echoError("DustOptions special_data must have 2 list entries for particle: " + particleEffect.getName());
                        return;
                    } else {
                        float size = Float.parseFloat(dataList.get(0));
                        ColorTag color = ColorTag.valueOf(dataList.get(1), scriptEntry.context);
                        dataObject = new org.bukkit.Particle.DustOptions(color.getColor(), size);
                    }
                } else if (clazz == BlockData.class) {
                    MaterialTag blockMaterial = MaterialTag.valueOf(special_data.asString(), scriptEntry.getContext());
                    dataObject = blockMaterial.getModernData();
                } else if (clazz == ItemStack.class) {
                    ItemTag itemType = ItemTag.valueOf(special_data.asString(), scriptEntry.getContext());
                    dataObject = itemType.getItemStack();
                } else // Intentionally list last due to requiring 1.17+
                if (clazz == org.bukkit.Particle.DustTransition.class) {
                    ListTag dataList = ListTag.valueOf(special_data.asString(), scriptEntry.getContext());
                    if (dataList.size() != 3) {
                        Debug.echoError("DustTransition special_data must have 3 list entries for particle: " + particleEffect.getName());
                        return;
                    } else {
                        float size = Float.parseFloat(dataList.get(0));
                        ColorTag fromColor = ColorTag.valueOf(dataList.get(1), scriptEntry.context);
                        ColorTag toColor = ColorTag.valueOf(dataList.get(2), scriptEntry.context);
                        dataObject = new org.bukkit.Particle.DustTransition(fromColor.getColor(), toColor.getColor(), size);
                    }
                } else {
                    Debug.echoError("Unknown particle data type: " + clazz.getCanonicalName() + " for particle: " + particleEffect.getName());
                    return;
                }
            } else if (special_data != null) {
                Debug.echoError("Particles of type '" + particleEffect.getName() + "' cannot take special_data as input.");
                return;
            }
            Random random = CoreUtilities.getRandom();
            int quantityInt = quantity.asInt();
            for (Player player : players) {
                if (velocity == null) {
                    particleEffect.playFor(player, location, quantityInt, offset.toVector(), data.asDouble(), dataObject);
                } else {
                    Vector velocityVector = velocity.toVector();
                    for (int i = 0; i < quantityInt; i++) {
                        LocationTag singleLocation = location.clone().add((random.nextDouble() - 0.5) * offset.getX(), (random.nextDouble() - 0.5) * offset.getY(), (random.nextDouble() - 0.5) * offset.getZ());
                        particleEffect.playFor(player, singleLocation, 0, velocityVector, 1f, dataObject);
                    }
                }
            }
        } else // Play an iconcrack (item break) effect
        {
            List<Player> players = new ArrayList<>();
            if (targets == null) {
                float rad = radius.asFloat();
                for (Player player : location.getWorld().getPlayers()) {
                    if (player.getLocation().distanceSquared(location) < rad * rad) {
                        players.add(player);
                    }
                }
            } else {
                for (PlayerTag player : targets) {
                    if (player.isValid() && player.isOnline()) {
                        players.add(player.getPlayerEntity());
                    }
                }
            }
            if (iconcrack != null) {
                ItemStack itemStack = iconcrack.getItemStack();
                Particle particle = NMSHandler.getParticleHelper().getParticle("ITEM_CRACK");
                for (Player player : players) {
                    particle.playFor(player, location, quantity.asInt(), offset.toVector(), data.asFloat(), itemStack);
                }
            }
        }
    }
}
Also used : Particle(com.denizenscript.denizen.nms.interfaces.Particle) BlockData(org.bukkit.block.data.BlockData) Vector(org.bukkit.util.Vector) Player(org.bukkit.entity.Player) ListTag(com.denizenscript.denizencore.objects.core.ListTag) Effect(org.bukkit.Effect) ElementTag(com.denizenscript.denizencore.objects.core.ElementTag) ItemStack(org.bukkit.inventory.ItemStack)

Aggregations

ListTag (com.denizenscript.denizencore.objects.core.ListTag)122 ElementTag (com.denizenscript.denizencore.objects.core.ElementTag)71 MapTag (com.denizenscript.denizencore.objects.core.MapTag)21 ItemStack (org.bukkit.inventory.ItemStack)18 EntityTag (com.denizenscript.denizen.objects.EntityTag)16 ObjectTag (com.denizenscript.denizencore.objects.ObjectTag)14 List (java.util.List)14 ItemTag (com.denizenscript.denizen.objects.ItemTag)13 PlayerTag (com.denizenscript.denizen.objects.PlayerTag)13 Player (org.bukkit.entity.Player)13 DurationTag (com.denizenscript.denizencore.objects.core.DurationTag)12 LocationTag (com.denizenscript.denizen.objects.LocationTag)11 NPCTag (com.denizenscript.denizen.objects.NPCTag)9 InvalidArgumentsException (com.denizenscript.denizencore.exceptions.InvalidArgumentsException)9 ArrayList (java.util.ArrayList)9 ScriptTag (com.denizenscript.denizencore.objects.core.ScriptTag)8 MaterialTag (com.denizenscript.denizen.objects.MaterialTag)7 ScriptEntry (com.denizenscript.denizencore.scripts.ScriptEntry)7 NPC (net.citizensnpcs.api.npc.NPC)7 Entity (org.bukkit.entity.Entity)7