Search in sources :

Example 26 with BukkitTagContext

use of com.denizenscript.denizen.tags.BukkitTagContext in project Denizen-For-Bukkit by DenizenScript.

the class ItemScriptHelper method registerShapedRecipe.

public void registerShapedRecipe(ItemScriptContainer container, ItemStack item, List<String> recipeList, String internalId, String group) {
    for (int n = 0; n < recipeList.size(); n++) {
        recipeList.set(n, TagManager.tag(ScriptBuilder.stripLinePrefix(recipeList.get(n)), new BukkitTagContext(null, null, new ScriptTag(container))));
    }
    List<ItemStack[]> ingredients = new ArrayList<>();
    List<Boolean> exacts = new ArrayList<>();
    int width = 1;
    for (String recipeRow : recipeList) {
        String[] elements = recipeRow.split("\\|", 3);
        if (width < 3 && elements.length == 3) {
            width = 3;
        }
        if (width < 2 && elements.length >= 2) {
            width = 2;
        }
        for (String element : elements) {
            String itemText = element;
            boolean isExact = !itemText.startsWith("material:");
            if (!isExact) {
                itemText = itemText.substring("material:".length());
            }
            exacts.add(isExact);
            ItemStack[] items = textToItemArray(container, itemText, isExact);
            if (items == null) {
                return;
            }
            ingredients.add(items);
        }
    }
    NamespacedKey key = new NamespacedKey("denizen", internalId);
    ShapedRecipe recipe = new ShapedRecipe(key, item);
    recipe.setGroup(group);
    String shape1 = "ABC".substring(0, width);
    String shape2 = "DEF".substring(0, width);
    String shape3 = "GHI".substring(0, width);
    String itemChars = shape1 + shape2 + shape3;
    if (recipeList.size() == 3) {
        recipe = recipe.shape(shape1, shape2, shape3);
    } else if (recipeList.size() == 2) {
        recipe = recipe.shape(shape1, shape2);
    } else {
        recipe = recipe.shape(shape1);
    }
    for (int i = 0; i < ingredients.size(); i++) {
        if (ingredients.get(i).length != 0) {
            NMSHandler.getItemHelper().setShapedRecipeIngredient(recipe, itemChars.charAt(i), ingredients.get(i), exacts.get(i));
        }
    }
    Bukkit.addRecipe(recipe);
}
Also used : BukkitTagContext(com.denizenscript.denizen.tags.BukkitTagContext) ScriptTag(com.denizenscript.denizencore.objects.core.ScriptTag)

Example 27 with BukkitTagContext

use of com.denizenscript.denizen.tags.BukkitTagContext in project Denizen-For-Bukkit by DenizenScript.

the class ItemScriptHelper method rebuildRecipes.

public void rebuildRecipes() {
    for (ItemScriptContainer container : item_scripts.values()) {
        try {
            if (container.contains("recipes", Map.class)) {
                YamlConfiguration section = container.getConfigurationSection("recipes");
                int id = 0;
                for (StringHolder key : section.getKeys(false)) {
                    id++;
                    YamlConfiguration subSection = section.getConfigurationSection(key.str);
                    String type = CoreUtilities.toLowerCase(subSection.getString("type"));
                    String internalId;
                    if (subSection.contains("recipe_id")) {
                        internalId = subSection.getString("recipe_id");
                        recipeIdToItemScript.put("denizen:" + internalId, container);
                    } else {
                        internalId = getIdFor(container, type + "_recipe", id);
                    }
                    String group = subSection.contains("group") ? subSection.getString("group") : "";
                    ItemStack item = container.getCleanReference().getItemStack().clone();
                    if (subSection.contains("output_quantity")) {
                        item.setAmount(Integer.parseInt(subSection.getString("output_quantity")));
                    }
                    switch(type) {
                        case "shaped":
                            registerShapedRecipe(container, item, subSection.getStringList("input"), internalId, group);
                            break;
                        case "shapeless":
                            registerShapelessRecipe(container, item, subSection.getString("input"), internalId, group);
                            break;
                        case "stonecutting":
                            registerStonecuttingRecipe(container, item, subSection.getString("input"), internalId, group);
                            break;
                        case "furnace":
                        case "blast":
                        case "smoker":
                        case "campfire":
                            float exp = 0;
                            int cookTime = 40;
                            if (subSection.contains("experience")) {
                                exp = Float.parseFloat(subSection.getString("experience"));
                            }
                            if (subSection.contains("cook_time")) {
                                cookTime = DurationTag.valueOf(subSection.getString("cook_time"), new BukkitTagContext(container)).getTicksAsInt();
                            }
                            registerFurnaceRecipe(container, item, subSection.getString("input"), exp, cookTime, type, internalId, group);
                            break;
                        case "smithing":
                            String retain = null;
                            if (subSection.contains("retain")) {
                                retain = subSection.getString("retain");
                            }
                            registerSmithingRecipe(container, item, subSection.getString("base"), subSection.getString("upgrade"), internalId, retain);
                            break;
                    }
                }
            }
            // Old script style
            if (container.contains("RECIPE", List.class)) {
                Deprecations.oldRecipeScript.warn(container);
                registerShapedRecipe(container, container.getCleanReference().getItemStack().clone(), container.getStringList("RECIPE"), getIdFor(container, "old_recipe", 0), "custom");
            }
            if (container.contains("SHAPELESS_RECIPE", String.class)) {
                Deprecations.oldRecipeScript.warn(container);
                registerShapelessRecipe(container, container.getCleanReference().getItemStack().clone(), container.getString("SHAPELESS_RECIPE"), getIdFor(container, "old_shapeless", 0), "custom");
            }
            if (container.contains("FURNACE_RECIPE", String.class)) {
                Deprecations.oldRecipeScript.warn(container);
                registerFurnaceRecipe(container, container.getCleanReference().getItemStack().clone(), container.getString("FURNACE_RECIPE"), 0, 40, "furnace", getIdFor(container, "old_furnace", 0), "custom");
            }
        } catch (Exception ex) {
            Debug.echoError("Error while rebuilding item script recipes for '" + container.getName() + "'...");
            Debug.echoError(ex);
        }
    }
}
Also used : StringHolder(com.denizenscript.denizencore.utilities.text.StringHolder) BukkitTagContext(com.denizenscript.denizen.tags.BukkitTagContext) YamlConfiguration(com.denizenscript.denizencore.utilities.YamlConfiguration)

Example 28 with BukkitTagContext

use of com.denizenscript.denizen.tags.BukkitTagContext in project Denizen-For-Bukkit by DenizenScript.

the class SidebarCommand method execute.

@Override
public void execute(ScriptEntry scriptEntry) {
    ElementTag action = scriptEntry.getElement("action");
    ElementTag elTitle = scriptEntry.getElement("title");
    ElementTag elScores = scriptEntry.getElement("scores");
    ElementTag elValue = scriptEntry.getElement("value");
    ElementTag elIncrement = scriptEntry.getElement("increment");
    ElementTag elStart = scriptEntry.getElement("start");
    ElementTag elPlayers = scriptEntry.getElement("players");
    ElementTag elPerPlayer = scriptEntry.getElement("per_player");
    ListTag players = ListTag.valueOf(TagManager.tag(elPlayers.asString(), scriptEntry.getContext()), scriptEntry.getContext());
    boolean per_player = elPerPlayer.asBoolean();
    String perTitle = null;
    String perScores = null;
    String perValue = null;
    String perIncrement = null;
    String perStart = null;
    ElementTag title = null;
    ListTag scores = null;
    ListTag value = null;
    ElementTag increment = null;
    ElementTag start = null;
    if (per_player) {
        if (elTitle != null) {
            perTitle = elTitle.asString();
        }
        if (elScores != null) {
            perScores = elScores.asString();
        }
        if (elValue != null) {
            perValue = elValue.asString();
        }
        if (elIncrement != null) {
            perIncrement = elIncrement.asString();
        }
        if (elStart != null) {
            perStart = elStart.asString();
        }
        if (scriptEntry.dbCallShouldDebug()) {
            Debug.report(scriptEntry, getName(), action, elTitle, elScores, elValue, elIncrement, elStart, db("players", players));
        }
    } else {
        BukkitTagContext context = (BukkitTagContext) scriptEntry.getContext();
        if (elTitle != null) {
            title = new ElementTag(TagManager.tag(elTitle.asString(), context));
        }
        if (elScores != null) {
            scores = ListTag.getListFor(TagManager.tagObject(elScores.asString(), context), context);
        }
        if (elValue != null) {
            value = ListTag.getListFor(TagManager.tagObject(elValue.asString(), context), context);
        }
        if (elIncrement != null) {
            increment = new ElementTag(TagManager.tag(elIncrement.asString(), context));
        }
        if (elStart != null) {
            start = new ElementTag(TagManager.tag(elStart.asString(), context));
        }
        if (scriptEntry.dbCallShouldDebug()) {
            Debug.report(scriptEntry, getName(), action, title, scores, value, increment, start, db("players", players));
        }
    }
    switch(Action.valueOf(action.asString())) {
        case ADD:
            for (PlayerTag player : players.filter(PlayerTag.class, scriptEntry)) {
                if (player == null || !player.isValid()) {
                    Debug.echoError("Invalid player!");
                    continue;
                }
                Sidebar sidebar = createSidebar(player);
                if (sidebar == null) {
                    continue;
                }
                List<Sidebar.SidebarLine> current = sidebar.getLines();
                if (per_player) {
                    TagContext context = new BukkitTagContext(player, Utilities.getEntryNPC(scriptEntry), scriptEntry, scriptEntry.shouldDebug(), scriptEntry.getScript());
                    value = ListTag.getListFor(TagManager.tagObject(perValue, context), context);
                    if (perScores != null) {
                        scores = ListTag.getListFor(TagManager.tagObject(perScores, context), context);
                    }
                }
                try {
                    int index = start != null ? start.asInt() : (current.size() > 0 ? current.get(current.size() - 1).score : value.size());
                    int incr = increment != null ? increment.asInt() : -1;
                    for (int i = 0; i < value.size(); i++, index += incr) {
                        int score = (scores != null && i < scores.size()) ? Integer.valueOf(scores.get(i)) : index;
                        while (hasScoreAlready(current, score)) {
                            score += (incr == 0 ? 1 : incr);
                        }
                        current.add(new Sidebar.SidebarLine(value.get(i), score));
                    }
                } catch (Exception e) {
                    Debug.echoError(e);
                    continue;
                }
                sidebar.setLines(current);
                sidebar.sendUpdate();
            }
            break;
        case REMOVE:
            for (PlayerTag player : players.filter(PlayerTag.class, scriptEntry)) {
                if (player == null || !player.isValid()) {
                    Debug.echoError("Invalid player!");
                    continue;
                }
                Sidebar sidebar = createSidebar(player);
                if (sidebar == null) {
                    continue;
                }
                List<Sidebar.SidebarLine> current = sidebar.getLines();
                if (per_player) {
                    TagContext context = new BukkitTagContext(player, Utilities.getEntryNPC(scriptEntry), scriptEntry, scriptEntry.shouldDebug(), scriptEntry.getScript());
                    if (perValue != null) {
                        value = ListTag.getListFor(TagManager.tagObject(perValue, context), context);
                    }
                    if (perScores != null) {
                        scores = ListTag.getListFor(TagManager.tagObject(perScores, context), context);
                    }
                }
                boolean removedAny = false;
                if (scores != null) {
                    try {
                        for (String scoreString : scores) {
                            int score = Integer.valueOf(scoreString);
                            for (int i = 0; i < current.size(); i++) {
                                if (current.get(i).score == score) {
                                    current.remove(i--);
                                }
                            }
                        }
                    } catch (Exception e) {
                        Debug.echoError(e);
                        continue;
                    }
                    sidebar.setLines(current);
                    sidebar.sendUpdate();
                    removedAny = true;
                }
                if (value != null) {
                    for (String line : value) {
                        for (int i = 0; i < current.size(); i++) {
                            if (current.get(i).text.equalsIgnoreCase(line)) {
                                current.remove(i--);
                            }
                        }
                    }
                    sidebar.setLines(current);
                    sidebar.sendUpdate();
                    removedAny = true;
                }
                if (!removedAny) {
                    sidebar.remove();
                    sidebars.remove(player.getPlayerEntity().getUniqueId());
                }
            }
            break;
        case SET_LINE:
            for (PlayerTag player : players.filter(PlayerTag.class, scriptEntry)) {
                if (player == null || !player.isValid()) {
                    Debug.echoError("Invalid player!");
                    continue;
                }
                if ((scores == null || scores.isEmpty()) && perScores == null) {
                    Debug.echoError("Missing or invalid 'scores' parameter.");
                    return;
                }
                if ((value == null || value.size() != scores.size()) && perValue == null) {
                    Debug.echoError("Missing or invalid 'values' parameter.");
                    return;
                }
                Sidebar sidebar = createSidebar(player);
                if (sidebar == null) {
                    continue;
                }
                List<Sidebar.SidebarLine> current = sidebar.getLines();
                if (per_player) {
                    TagContext context = new BukkitTagContext(player, Utilities.getEntryNPC(scriptEntry), scriptEntry, scriptEntry.shouldDebug(), scriptEntry.getScript());
                    if (perValue != null) {
                        value = ListTag.getListFor(TagManager.tagObject(perValue, context), context);
                    }
                    if (perScores != null) {
                        scores = ListTag.getListFor(TagManager.tagObject(perScores, context), context);
                    }
                }
                try {
                    for (int i = 0; i < value.size(); i++) {
                        if (!ArgumentHelper.matchesInteger(scores.get(i))) {
                            Debug.echoError("Sidebar command scores input contains not-a-valid-number: " + scores.get(i));
                            return;
                        }
                        int score = Integer.parseInt(scores.get(i));
                        if (hasScoreAlready(current, score)) {
                            for (Sidebar.SidebarLine line : current) {
                                if (line.score == score) {
                                    line.text = value.get(i);
                                    break;
                                }
                            }
                        } else {
                            current.add(new Sidebar.SidebarLine(value.get(i), score));
                        }
                    }
                } catch (Exception e) {
                    Debug.echoError(e);
                    continue;
                }
                sidebar.setLines(current);
                sidebar.sendUpdate();
            }
            break;
        case SET:
            for (PlayerTag player : players.filter(PlayerTag.class, scriptEntry)) {
                if (player == null || !player.isValid()) {
                    Debug.echoError("Invalid player!");
                    continue;
                }
                Sidebar sidebar = createSidebar(player);
                if (sidebar == null) {
                    continue;
                }
                List<Sidebar.SidebarLine> current = new ArrayList<>();
                if (per_player) {
                    TagContext context = new BukkitTagContext(player, Utilities.getEntryNPC(scriptEntry), scriptEntry, scriptEntry.shouldDebug(), scriptEntry.getScript());
                    if (perValue != null) {
                        value = ListTag.getListFor(TagManager.tagObject(perValue, context), context);
                    }
                    if (perScores != null) {
                        scores = ListTag.getListFor(TagManager.tagObject(perScores, context), context);
                    }
                    if (perStart != null) {
                        start = new ElementTag(TagManager.tag(perStart, context));
                    }
                    if (perIncrement != null) {
                        increment = new ElementTag(TagManager.tag(perIncrement, context));
                    }
                    if (perTitle != null) {
                        title = new ElementTag(TagManager.tag(perTitle, context));
                    }
                }
                if (value != null) {
                    try {
                        int index = start != null ? start.asInt() : value.size();
                        int incr = increment != null ? increment.asInt() : -1;
                        for (int i = 0; i < value.size(); i++, index += incr) {
                            int score = (scores != null && i < scores.size()) ? Integer.valueOf(scores.get(i)) : index;
                            current.add(new Sidebar.SidebarLine(value.get(i), score));
                        }
                    } catch (Exception e) {
                        Debug.echoError(e);
                        continue;
                    }
                    sidebar.setLines(current);
                }
                if (title != null) {
                    sidebar.setTitle(title.asString());
                }
                sidebar.sendUpdate();
            }
            break;
    }
}
Also used : PlayerTag(com.denizenscript.denizen.objects.PlayerTag) BukkitTagContext(com.denizenscript.denizen.tags.BukkitTagContext) TagContext(com.denizenscript.denizencore.tags.TagContext) ListTag(com.denizenscript.denizencore.objects.core.ListTag) InvalidArgumentsException(com.denizenscript.denizencore.exceptions.InvalidArgumentsException) BukkitTagContext(com.denizenscript.denizen.tags.BukkitTagContext) ElementTag(com.denizenscript.denizencore.objects.core.ElementTag) Sidebar(com.denizenscript.denizen.nms.abstracts.Sidebar)

Aggregations

BukkitTagContext (com.denizenscript.denizen.tags.BukkitTagContext)28 ScriptTag (com.denizenscript.denizencore.objects.core.ScriptTag)16 PlayerTag (com.denizenscript.denizen.objects.PlayerTag)9 ElementTag (com.denizenscript.denizencore.objects.core.ElementTag)9 ObjectTag (com.denizenscript.denizencore.objects.ObjectTag)5 TagContext (com.denizenscript.denizencore.tags.TagContext)5 Player (org.bukkit.entity.Player)5 EntityTag (com.denizenscript.denizen.objects.EntityTag)4 ItemTag (com.denizenscript.denizen.objects.ItemTag)4 NPCTag (com.denizenscript.denizen.objects.NPCTag)4 ListTag (com.denizenscript.denizencore.objects.core.ListTag)4 ScriptEntry (com.denizenscript.denizencore.scripts.ScriptEntry)4 List (java.util.List)4 EventHandler (org.bukkit.event.EventHandler)4 FormatScriptContainer (com.denizenscript.denizen.scripts.containers.core.FormatScriptContainer)3 ScriptQueue (com.denizenscript.denizencore.scripts.queues.ScriptQueue)3 YamlConfiguration (com.denizenscript.denizencore.utilities.YamlConfiguration)3 StringHolder (com.denizenscript.denizencore.utilities.text.StringHolder)3 NMSHandler (com.denizenscript.denizen.nms.NMSHandler)2 TriggerTrait (com.denizenscript.denizen.npc.traits.TriggerTrait)2