Search in sources :

Example 1 with TagContext

use of com.denizenscript.denizencore.tags.TagContext in project Denizen-For-Bukkit by DenizenScript.

the class EnchantmentScriptContainer method autoTag.

public String autoTag(String value, ContextSource src) {
    if (value == null) {
        return null;
    }
    validateThread();
    TagContext context = new BukkitTagContext(null, new ScriptTag(this));
    context.contextSource = src;
    return TagManager.tag(value, context);
}
Also used : BukkitTagContext(com.denizenscript.denizen.tags.BukkitTagContext) TagContext(com.denizenscript.denizencore.tags.TagContext) BukkitTagContext(com.denizenscript.denizen.tags.BukkitTagContext) ScriptTag(com.denizenscript.denizencore.objects.core.ScriptTag)

Example 2 with TagContext

use of com.denizenscript.denizencore.tags.TagContext in project Denizen-For-Bukkit by DenizenScript.

the class ItemScriptHelper method registerShapelessRecipe.

public void registerShapelessRecipe(ItemScriptContainer container, ItemStack item, String shapelessString, String internalId, String group) {
    TagContext context = new BukkitTagContext(null, null, new ScriptTag(container));
    String list = TagManager.tag(shapelessString, context);
    List<ItemStack[]> ingredients = new ArrayList<>();
    List<Boolean> exacts = new ArrayList<>();
    for (String element : ListTag.valueOf(list, context)) {
        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);
    }
    boolean[] bools = new boolean[exacts.size()];
    for (int i = 0; i < exacts.size(); i++) {
        bools[i] = exacts.get(i);
    }
    NMSHandler.getItemHelper().registerShapelessRecipe(internalId, group, item, ingredients, bools);
}
Also used : TagContext(com.denizenscript.denizencore.tags.TagContext) BukkitTagContext(com.denizenscript.denizen.tags.BukkitTagContext) BukkitTagContext(com.denizenscript.denizen.tags.BukkitTagContext) ScriptTag(com.denizenscript.denizencore.objects.core.ScriptTag)

Example 3 with TagContext

use of com.denizenscript.denizencore.tags.TagContext in project Denizen-For-Bukkit by DenizenScript.

the class HealthTrait method onDamage.

@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onDamage(EntityDamageEvent event) {
    // Check if the event pertains to this NPC
    if (event.getEntity() != npc.getEntity() || dying) {
        return;
    }
    // Make sure this is a killing blow
    if (this.getHealth() - event.getFinalDamage() > 0) {
        return;
    }
    dying = true;
    // Save entityId for EntityDeath event
    entityId = npc.getEntity().getUniqueId();
    if (npc.getEntity() == null) {
        return;
    }
    TagContext context = new BukkitTagContext(null, new NPCTag(npc), null, true, null);
    // TODO: debug option?
    loc = getRespawnLocation();
    if (loc == null) {
        loc = npc.getStoredLocation();
    }
    if (animatedeath) {
        // Cancel navigation to keep the NPC from damaging players
        // while the death animation is being carried out.
        npc.getNavigator().cancelNavigation();
    // Reset health now to avoid the death from happening instantly
    // setHealth();
    // Play animation (TODO)
    // playDeathAnimation(npc.getEntity());
    }
    if (respawn && (getRespawnDelay().getTicks() > 0)) {
        Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(Denizen.getInstance(), () -> {
            if (CitizensAPI.getNPCRegistry().getById(npc.getId()) == null || npc.isSpawned()) {
                return;
            } else {
                npc.spawn(loc);
            }
        }, (getRespawnDelay().getTicks()));
    }
}
Also used : BukkitTagContext(com.denizenscript.denizen.tags.BukkitTagContext) BukkitTagContext(com.denizenscript.denizen.tags.BukkitTagContext) TagContext(com.denizenscript.denizencore.tags.TagContext) NPCTag(com.denizenscript.denizen.objects.NPCTag) EventHandler(org.bukkit.event.EventHandler)

Example 4 with TagContext

use of com.denizenscript.denizencore.tags.TagContext in project Denizen-For-Bukkit by DenizenScript.

the class EntityScriptContainer method getEntityFrom.

public EntityTag getEntityFrom(PlayerTag player, NPCTag npc) {
    EntityTag entity;
    try {
        TagContext context = new BukkitTagContext(player, npc, new ScriptTag(this));
        if (contains("entity_type", String.class)) {
            String entityType = TagManager.tag((getString("entity_type", "")), context);
            entity = EntityTag.valueOf(entityType, context);
        } else {
            throw new Exception("Missing entity_type argument!");
        }
        if (contains("flags", Map.class)) {
            YamlConfiguration flagSection = getConfigurationSection("flags");
            MapTagFlagTracker tracker = new MapTagFlagTracker();
            for (StringHolder key : flagSection.getKeys(false)) {
                tracker.setFlag(key.str, CoreUtilities.objectToTagForm(flagSection.get(key.str), context, true, true), null);
            }
            entity.safeAdjust(new Mechanism("flag_map", tracker.map, context));
        }
        if (contains("mechanisms", Map.class)) {
            YamlConfiguration mechSection = getConfigurationSection("mechanisms");
            Set<StringHolder> strings = mechSection.getKeys(false);
            for (StringHolder string : strings) {
                ObjectTag obj = CoreUtilities.objectToTagForm(mechSection.get(string.low), context, true, true);
                entity.safeAdjust(new Mechanism(string.low, obj, context));
            }
        }
        boolean any = false;
        Set<StringHolder> strings = getContents().getKeys(false);
        for (StringHolder string : strings) {
            if (!nonMechanismKeys.contains(string.low)) {
                any = true;
                ObjectTag obj = CoreUtilities.objectToTagForm(getContents().get(string.low), context, true, true);
                entity.safeAdjust(new Mechanism(string.low, obj, context));
            }
        }
        if (any) {
            Deprecations.entityMechanismsFormat.warn(this);
        }
        if (entity == null || entity.isUnique()) {
            return null;
        }
        entity.setEntityScript(getName());
    } catch (Exception e) {
        Debug.echoError("Woah! An exception has been called with this entity script!");
        Debug.echoError(e);
        entity = null;
    }
    return entity;
}
Also used : BukkitTagContext(com.denizenscript.denizen.tags.BukkitTagContext) TagContext(com.denizenscript.denizencore.tags.TagContext) MapTagFlagTracker(com.denizenscript.denizencore.flags.MapTagFlagTracker) YamlConfiguration(com.denizenscript.denizencore.utilities.YamlConfiguration) Mechanism(com.denizenscript.denizencore.objects.Mechanism) StringHolder(com.denizenscript.denizencore.utilities.text.StringHolder) ObjectTag(com.denizenscript.denizencore.objects.ObjectTag) BukkitTagContext(com.denizenscript.denizen.tags.BukkitTagContext) ScriptTag(com.denizenscript.denizencore.objects.core.ScriptTag) EntityTag(com.denizenscript.denizen.objects.EntityTag)

Example 5 with TagContext

use of com.denizenscript.denizencore.tags.TagContext 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)5 TagContext (com.denizenscript.denizencore.tags.TagContext)5 ScriptTag (com.denizenscript.denizencore.objects.core.ScriptTag)3 Sidebar (com.denizenscript.denizen.nms.abstracts.Sidebar)1 EntityTag (com.denizenscript.denizen.objects.EntityTag)1 NPCTag (com.denizenscript.denizen.objects.NPCTag)1 PlayerTag (com.denizenscript.denizen.objects.PlayerTag)1 InvalidArgumentsException (com.denizenscript.denizencore.exceptions.InvalidArgumentsException)1 MapTagFlagTracker (com.denizenscript.denizencore.flags.MapTagFlagTracker)1 Mechanism (com.denizenscript.denizencore.objects.Mechanism)1 ObjectTag (com.denizenscript.denizencore.objects.ObjectTag)1 ElementTag (com.denizenscript.denizencore.objects.core.ElementTag)1 ListTag (com.denizenscript.denizencore.objects.core.ListTag)1 YamlConfiguration (com.denizenscript.denizencore.utilities.YamlConfiguration)1 StringHolder (com.denizenscript.denizencore.utilities.text.StringHolder)1 EventHandler (org.bukkit.event.EventHandler)1