Search in sources :

Example 1 with Sidebar

use of net.aufdemrand.denizen.nms.abstracts.Sidebar in project Denizen-For-Bukkit by DenizenScript.

the class SidebarCommand method execute.

@Override
public void execute(ScriptEntry scriptEntry) throws CommandExecutionException {
    Element action = scriptEntry.getElement("action");
    Element elTitle = scriptEntry.getElement("title");
    Element elLines = scriptEntry.getElement("lines");
    Element elValue = scriptEntry.getElement("value");
    Element elIncrement = scriptEntry.getElement("increment");
    Element elStart = scriptEntry.getElement("start");
    Element elPlayers = scriptEntry.getElement("players");
    Element elPerPlayer = scriptEntry.getElement("per_player");
    dList players = dList.valueOf(TagManager.tag(elPlayers.asString(), new BukkitTagContext(scriptEntry, false)));
    boolean per_player = elPerPlayer.asBoolean();
    String perTitle = null;
    String perLines = null;
    String perValue = null;
    String perIncrement = null;
    String perStart = null;
    Element title = null;
    dList lines = null;
    dList value = null;
    Element increment = null;
    Element start = null;
    String debug;
    if (per_player) {
        if (elTitle != null) {
            perTitle = elTitle.asString();
        }
        if (elLines != null) {
            perLines = elLines.asString();
        }
        if (elValue != null) {
            perValue = elValue.asString();
        }
        if (elIncrement != null) {
            perIncrement = elIncrement.asString();
        }
        if (elStart != null) {
            perStart = elStart.asString();
        }
        debug = action.debug() + (elTitle != null ? elTitle.debug() : "") + (elLines != null ? elLines.debug() : "") + (elValue != null ? elValue.debug() : "") + (elIncrement != null ? elIncrement.debug() : "") + (elStart != null ? elStart.debug() : "") + players.debug();
    } else {
        BukkitTagContext context = (BukkitTagContext) DenizenAPI.getCurrentInstance().getTagContextFor(scriptEntry, false);
        if (elTitle != null) {
            title = new Element(TagManager.tag(elTitle.asString(), context));
        }
        if (elLines != null) {
            lines = dList.valueOf(TagManager.tag(elLines.asString(), context));
        }
        if (elValue != null) {
            value = dList.valueOf(TagManager.tag(elValue.asString(), context));
        }
        if (elIncrement != null) {
            increment = new Element(TagManager.tag(elIncrement.asString(), context));
        }
        if (elStart != null) {
            start = new Element(TagManager.tag(elStart.asString(), context));
        }
        debug = action.debug() + (title != null ? title.debug() : "") + (lines != null ? lines.debug() : "") + (value != null ? value.debug() : "") + (increment != null ? increment.debug() : "") + (start != null ? start.debug() : "") + players.debug();
    }
    dB.report(scriptEntry, getName(), debug);
    switch(Action.valueOf(action.asString())) {
        case ADD:
            for (dPlayer player : players.filter(dPlayer.class)) {
                if (player == null || !player.isValid()) {
                    dB.echoError("Invalid player!");
                    continue;
                }
                Sidebar sidebar = createSidebar(player);
                if (sidebar == null) {
                    continue;
                }
                List<String> current = sidebar.getLines();
                if (per_player) {
                    TagContext context = new BukkitTagContext(player, ((BukkitScriptEntryData) scriptEntry.entryData).getNPC(), false, scriptEntry, scriptEntry.shouldDebug(), scriptEntry.getScript());
                    value = dList.valueOf(TagManager.tag(perValue, context));
                    if (perLines != null) {
                        lines = dList.valueOf(TagManager.tag(perLines, context));
                    }
                }
                if (lines != null) {
                    try {
                        for (int i = 0; i < lines.size(); i++) {
                            int index = Integer.valueOf(lines.get(i)) - 1;
                            String line = value.get(i);
                            current.add(index, line);
                        }
                    } catch (Exception e) {
                        dB.echoError(e);
                        continue;
                    }
                } else {
                    current.addAll(value);
                }
                sidebar.setLines(current);
                sidebar.sendUpdate();
            }
            break;
        case REMOVE:
            for (dPlayer player : players.filter(dPlayer.class)) {
                if (player == null || !player.isValid()) {
                    dB.echoError("Invalid player!");
                    continue;
                }
                Sidebar sidebar = createSidebar(player);
                if (sidebar == null) {
                    continue;
                }
                List<String> current = sidebar.getLines();
                if (per_player) {
                    TagContext context = new BukkitTagContext(player, ((BukkitScriptEntryData) scriptEntry.entryData).getNPC(), false, scriptEntry, scriptEntry.shouldDebug(), scriptEntry.getScript());
                    if (perValue != null) {
                        value = dList.valueOf(TagManager.tag(perValue, context));
                    }
                    if (perLines != null) {
                        lines = dList.valueOf(TagManager.tag(perLines, context));
                    }
                }
                if (lines != null) {
                    try {
                        int offset = 0;
                        for (String line : lines) {
                            int index = Integer.valueOf(line) - 1 - offset;
                            current.remove(index);
                            offset++;
                        }
                    } catch (Exception e) {
                        dB.echoError(e);
                        continue;
                    }
                    sidebar.setLines(current);
                    sidebar.sendUpdate();
                } else if (value != null) {
                    try {
                        Iterator<String> it = current.iterator();
                        while (it.hasNext()) {
                            String next = it.next();
                            for (String line : value) {
                                if (next.equalsIgnoreCase(line)) {
                                    it.remove();
                                }
                            }
                        }
                        for (String line : value) {
                            for (int i = 0; i < current.size(); i++) {
                                if (current.get(i).equalsIgnoreCase(line)) {
                                    current.remove(i);
                                }
                            }
                        }
                    } catch (Exception e) {
                        dB.echoError(e);
                        continue;
                    }
                    sidebar.setLines(current);
                    sidebar.sendUpdate();
                } else {
                    sidebar.remove();
                    sidebars.remove(player.getPlayerEntity().getUniqueId());
                }
            }
            break;
        case SET:
            for (dPlayer player : players.filter(dPlayer.class)) {
                if (player == null || !player.isValid()) {
                    dB.echoError("Invalid player!");
                    continue;
                }
                Sidebar sidebar = createSidebar(player);
                if (sidebar == null) {
                    continue;
                }
                List<String> current = sidebar.getLines();
                boolean currEdited = false;
                if (per_player) {
                    TagContext context = new BukkitTagContext(player, ((BukkitScriptEntryData) scriptEntry.entryData).getNPC(), false, scriptEntry, scriptEntry.shouldDebug(), scriptEntry.getScript());
                    if (perValue != null) {
                        value = dList.valueOf(TagManager.tag(perValue, context));
                    }
                    if (perLines != null) {
                        lines = dList.valueOf(TagManager.tag(perLines, context));
                    }
                    if (perStart != null) {
                        start = new Element(TagManager.tag(perStart, context));
                    }
                    if (perIncrement != null) {
                        increment = new Element(TagManager.tag(perIncrement, context));
                    }
                    if (perTitle != null) {
                        title = new Element(TagManager.tag(perTitle, context));
                    }
                }
                if (lines != null) {
                    try {
                        for (int i = 0; i < lines.size(); i++) {
                            int index = Integer.valueOf(lines.get(i)) - 1;
                            String line = value.get(i);
                            if (index > current.size()) {
                                current.add(line);
                            } else {
                                current.set(index, line);
                            }
                        }
                    } catch (Exception e) {
                        dB.echoError(e);
                        continue;
                    }
                    currEdited = true;
                } else if (value != null) {
                    current = value;
                    currEdited = true;
                }
                if (start != null) {
                    sidebar.setStart(start.asInt());
                    currEdited = true;
                }
                if (increment != null) {
                    sidebar.setIncrement(increment.asInt());
                    currEdited = true;
                }
                if (title != null) {
                    sidebar.setTitle(title.asString());
                }
                if (currEdited) {
                    sidebar.setLines(current);
                }
                sidebar.sendUpdate();
            }
            break;
    }
}
Also used : BukkitTagContext(net.aufdemrand.denizen.tags.BukkitTagContext) BukkitTagContext(net.aufdemrand.denizen.tags.BukkitTagContext) TagContext(net.aufdemrand.denizencore.tags.TagContext) Element(net.aufdemrand.denizencore.objects.Element) net.aufdemrand.denizencore.objects.dList(net.aufdemrand.denizencore.objects.dList) net.aufdemrand.denizen.objects.dPlayer(net.aufdemrand.denizen.objects.dPlayer) Iterator(java.util.Iterator) Sidebar(net.aufdemrand.denizen.nms.abstracts.Sidebar) InvalidArgumentsException(net.aufdemrand.denizencore.exceptions.InvalidArgumentsException) CommandExecutionException(net.aufdemrand.denizencore.exceptions.CommandExecutionException)

Example 2 with Sidebar

use of net.aufdemrand.denizen.nms.abstracts.Sidebar in project Denizen-For-Bukkit by DenizenScript.

the class dPlayer method getAttribute.

@Override
public String getAttribute(Attribute attribute) {
    if (attribute == null) {
        return null;
    }
    if (offlinePlayer == null) {
        return null;
    }
    // Defined in dEntity
    if (attribute.startsWith("is_player")) {
        return Element.TRUE.getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("debug.log")) {
        dB.log(debug());
        return Element.TRUE.getAttribute(attribute.fulfill(2));
    }
    // -->
    if (attribute.startsWith("debug.no_color")) {
        return new Element(ChatColor.stripColor(debug())).getAttribute(attribute.fulfill(2));
    }
    // -->
    if (attribute.startsWith("debug")) {
        return new Element(debug()).getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("prefix")) {
        return new Element(prefix).getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("chat_history_list")) {
        return // TODO: UUID?
        new dList(PlayerTags.playerChatHistory.get(getName())).getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("chat_history")) {
        int x = 1;
        if (attribute.hasContext(1) && aH.matchesInteger(attribute.getContext(1))) {
            x = attribute.getIntContext(1);
        }
        // No playerchathistory? Return null.
        if (!PlayerTags.playerChatHistory.containsKey(getName())) {
            // TODO: UUID?
            return null;
        }
        // TODO: UUID?
        List<String> messages = PlayerTags.playerChatHistory.get(getName());
        if (messages.size() < x || x < 1) {
            return null;
        }
        return new Element(messages.get(x - 1)).getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("flag") && attribute.hasContext(1)) {
        String flag_name = attribute.getContext(1);
        if (attribute.getAttribute(2).equalsIgnoreCase("is_expired") || attribute.startsWith("isexpired")) {
            return new Element(!FlagManager.playerHasFlag(this, flag_name)).getAttribute(attribute.fulfill(2));
        }
        if (attribute.getAttribute(2).equalsIgnoreCase("size") && !FlagManager.playerHasFlag(this, flag_name)) {
            return new Element(0).getAttribute(attribute.fulfill(2));
        }
        if (FlagManager.playerHasFlag(this, flag_name)) {
            FlagManager.Flag flag = DenizenAPI.getCurrentInstance().flagManager().getPlayerFlag(this, flag_name);
            return new dList(flag.toString(), true, flag.values()).getAttribute(attribute.fulfill(1));
        }
        return new Element(identify()).getAttribute(attribute);
    }
    // -->
    if (attribute.startsWith("has_flag") && attribute.hasContext(1)) {
        String flag_name = attribute.getContext(1);
        return new Element(FlagManager.playerHasFlag(this, flag_name)).getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("list_flags")) {
        dList allFlags = new dList(DenizenAPI.getCurrentInstance().flagManager().listPlayerFlags(this));
        dList searchFlags = null;
        if (!allFlags.isEmpty() && attribute.hasContext(1)) {
            searchFlags = new dList();
            String search = attribute.getContext(1);
            if (search.startsWith("regex:")) {
                try {
                    Pattern pattern = Pattern.compile(search.substring(6), Pattern.CASE_INSENSITIVE);
                    for (String flag : allFlags) {
                        if (pattern.matcher(flag).matches()) {
                            searchFlags.add(flag);
                        }
                    }
                } catch (Exception e) {
                    dB.echoError(e);
                }
            } else {
                search = CoreUtilities.toLowerCase(search);
                for (String flag : allFlags) {
                    if (CoreUtilities.toLowerCase(flag).contains(search)) {
                        searchFlags.add(flag);
                    }
                }
            }
        }
        return searchFlags == null ? allFlags.getAttribute(attribute.fulfill(1)) : searchFlags.getAttribute(attribute.fulfill(1));
    }
    if (attribute.startsWith("current_step")) {
        String outcome = "null";
        if (attribute.hasContext(1)) {
            try {
                outcome = DenizenAPI.getCurrentInstance().getSaves().getString("Players." + getName() + ".Scripts." + dScript.valueOf(attribute.getContext(1)).getName() + ".Current Step");
            } catch (Exception e) {
                outcome = "null";
            }
        }
        return new Element(outcome).getAttribute(attribute.fulfill(1));
    }
    if (attribute.startsWith("money")) {
        if (Depends.economy != null) {
            // -->
            if (attribute.startsWith("money.currency_singular")) {
                return new Element(Depends.economy.currencyNameSingular()).getAttribute(attribute.fulfill(2));
            }
            // -->
            if (attribute.startsWith("money.currency")) {
                return new Element(Depends.economy.currencyNamePlural()).getAttribute(attribute.fulfill(2));
            }
            return new Element(Depends.economy.getBalance(getOfflinePlayer())).getAttribute(attribute.fulfill(1));
        } else {
            if (!attribute.hasAlternative()) {
                dB.echoError("No economy loaded! Have you installed Vault and a compatible economy plugin?");
            }
            return null;
        }
    }
    if (attribute.startsWith("target")) {
        int range = 50;
        int attribs = 1;
        // -->
        if (attribute.getAttribute(2).startsWith("within") && attribute.hasContext(2) && aH.matchesInteger(attribute.getContext(2))) {
            attribs = 2;
            range = attribute.getIntContext(2);
        }
        List<Entity> entities = getPlayerEntity().getNearbyEntities(range, range, range);
        ArrayList<LivingEntity> possibleTargets = new ArrayList<LivingEntity>();
        for (Entity entity : entities) {
            if (entity instanceof LivingEntity) {
                // if we have a context for entity types, check the entity
                if (attribute.hasContext(1)) {
                    String context = attribute.getContext(1);
                    if (CoreUtilities.toLowerCase(context).startsWith("li@")) {
                        context = context.substring(3);
                    }
                    for (String ent : context.split("\\|")) {
                        boolean valid = false;
                        if (ent.equalsIgnoreCase("npc") && dEntity.isCitizensNPC(entity)) {
                            valid = true;
                        } else if (dEntity.matches(ent)) {
                            // only accept generic entities that are not NPCs
                            if (dEntity.valueOf(ent).isGeneric()) {
                                if (dEntity.isCitizensNPC(entity)) {
                                    valid = true;
                                }
                            } else {
                                valid = true;
                            }
                        }
                        if (valid) {
                            possibleTargets.add((LivingEntity) entity);
                        }
                    }
                } else {
                    // no entity type specified
                    possibleTargets.add((LivingEntity) entity);
                    entity.getType();
                }
            }
        }
        // Find the valid target
        BlockIterator bi;
        try {
            bi = new BlockIterator(getPlayerEntity(), range);
        } catch (IllegalStateException e) {
            return null;
        }
        Block b;
        Location l;
        int bx, by, bz;
        double ex, ey, ez;
        // Loop through player's line of sight
        while (bi.hasNext()) {
            b = bi.next();
            bx = b.getX();
            by = b.getY();
            bz = b.getZ();
            if (b.getType() != Material.AIR) {
                // Line of sight is broken
                break;
            } else {
                // Check for entities near this block in the line of sight
                for (LivingEntity possibleTarget : possibleTargets) {
                    l = possibleTarget.getLocation();
                    ex = l.getX();
                    ey = l.getY();
                    ez = l.getZ();
                    if ((bx - .50 <= ex && ex <= bx + 1.50) && (bz - .50 <= ez && ez <= bz + 1.50) && (by - 1 <= ey && ey <= by + 2.5)) {
                        // Entity is close enough, so return it
                        return new dEntity(possibleTarget).getDenizenObject().getAttribute(attribute.fulfill(attribs));
                    }
                }
            }
        }
        return null;
    }
    // workaround for <e@entity.list_effects>
    if (attribute.startsWith("list_effects")) {
        dList effects = new dList();
        for (PotionEffect effect : getPlayerEntity().getActivePotionEffects()) {
            effects.add(effect.getType().getName() + "," + effect.getAmplifier() + "," + effect.getDuration() + "t");
        }
        return effects.getAttribute(attribute.fulfill(1));
    }
    if (attribute.startsWith("list")) {
        dB.echoError("DO NOT USE PLAYER.LIST AS A TAG, please use <server.list_online_players> and related tags!");
        List<String> players = new ArrayList<String>();
        if (attribute.startsWith("list.online")) {
            for (Player player : Bukkit.getOnlinePlayers()) {
                players.add(player.getName());
            }
            return new dList(players).getAttribute(attribute.fulfill(2));
        } else if (attribute.startsWith("list.offline")) {
            for (OfflinePlayer player : Bukkit.getOfflinePlayers()) {
                if (!player.isOnline()) {
                    players.add("p@" + player.getUniqueId().toString());
                }
            }
            return new dList(players).getAttribute(attribute.fulfill(2));
        } else {
            for (OfflinePlayer player : Bukkit.getOfflinePlayers()) {
                players.add("p@" + player.getUniqueId().toString());
            }
            return new dList(players).getAttribute(attribute.fulfill(1));
        }
    }
    if (attribute.startsWith("name") && !isOnline()) // This can be parsed later with more detail if the player is online, so only check for offline.
    {
        return new Element(getName()).getAttribute(attribute.fulfill(1));
    } else if (attribute.startsWith("uuid") && !isOnline()) // This can be parsed later with more detail if the player is online, so only check for offline.
    {
        return new Element(offlinePlayer.getUniqueId().toString()).getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("type")) {
        return new Element("Player").getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("save_name")) {
        return new Element(getSaveName()).getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("bed_spawn")) {
        if (getOfflinePlayer().getBedSpawnLocation() == null) {
            return null;
        }
        return new dLocation(getOfflinePlayer().getBedSpawnLocation()).getAttribute(attribute.fulfill(1));
    }
    if (attribute.startsWith("location") && !isOnline()) {
        return getLocation().getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("first_played")) {
        attribute = attribute.fulfill(1);
        if (attribute.startsWith("milliseconds") || attribute.startsWith("in_milliseconds")) {
            return new Element(getOfflinePlayer().getFirstPlayed()).getAttribute(attribute.fulfill(1));
        }
        return new Duration(getOfflinePlayer().getFirstPlayed() / 50).getAttribute(attribute);
    }
    // -->
    if (attribute.startsWith("has_played_before")) {
        return new Element(true).getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("health.is_scaled")) {
        return new Element(getPlayerEntity().isHealthScaled()).getAttribute(attribute.fulfill(2));
    }
    // -->
    if (attribute.startsWith("health.scale")) {
        return new Element(getPlayerEntity().getHealthScale()).getAttribute(attribute.fulfill(2));
    }
    // -->
    if (attribute.startsWith("exhaustion")) {
        return new Element(getPlayerEntity().getExhaustion()).getAttribute(attribute.fulfill(1));
    }
    // Handle dEntity oxygen tags here to allow getting them when the player is offline
    if (attribute.startsWith("oxygen.max")) {
        return new Duration((long) getMaximumAir()).getAttribute(attribute.fulfill(2));
    }
    if (attribute.startsWith("oxygen")) {
        return new Duration((long) getRemainingAir()).getAttribute(attribute.fulfill(1));
    }
    // Same with health tags
    if (attribute.startsWith("health.formatted")) {
        return EntityHealth.getHealthFormatted(new dEntity(getPlayerEntity()), attribute);
    }
    if (attribute.startsWith("health.percentage")) {
        double maxHealth = getPlayerEntity().getMaxHealth();
        if (attribute.hasContext(2)) {
            maxHealth = attribute.getIntContext(2);
        }
        return new Element((getPlayerEntity().getHealth() / maxHealth) * 100).getAttribute(attribute.fulfill(2));
    }
    if (attribute.startsWith("health.max")) {
        return new Element(getMaxHealth()).getAttribute(attribute.fulfill(2));
    }
    if (attribute.matches("health")) {
        return new Element(getHealth()).getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("is_banned")) {
        BanEntry ban = Bukkit.getBanList(BanList.Type.NAME).getBanEntry(getName());
        if (ban == null) {
            return Element.FALSE.getAttribute(attribute.fulfill(1));
        } else if (ban.getExpiration() == null) {
            return Element.TRUE.getAttribute(attribute.fulfill(1));
        }
        return new Element(ban.getExpiration().after(new Date())).getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("is_online")) {
        return new Element(isOnline()).getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("is_op")) {
        return new Element(getOfflinePlayer().isOp()).getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("is_whitelisted")) {
        return new Element(getOfflinePlayer().isWhitelisted()).getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("last_played")) {
        attribute = attribute.fulfill(1);
        if (attribute.startsWith("milliseconds") || attribute.startsWith("in_milliseconds")) {
            if (isOnline()) {
                return new Element(System.currentTimeMillis()).getAttribute(attribute.fulfill(1));
            }
            return new Element(getOfflinePlayer().getLastPlayed()).getAttribute(attribute.fulfill(1));
        }
        if (isOnline()) {
            return new Duration(System.currentTimeMillis() / 50).getAttribute(attribute);
        }
        return new Duration(getOfflinePlayer().getLastPlayed() / 50).getAttribute(attribute);
    }
    // -->
    if (attribute.startsWith("groups")) {
        if (Depends.permissions == null) {
            if (!attribute.hasAlternative()) {
                dB.echoError("No permission system loaded! Have you installed Vault and a compatible permissions plugin?");
            }
            return null;
        }
        dList list = new dList();
        // TODO: optionally specify world
        for (String group : Depends.permissions.getGroups()) {
            if (Depends.permissions.playerInGroup(null, offlinePlayer, group)) {
                list.add(group);
            }
        }
        return list.getAttribute(attribute.fulfill(1));
    }
    if (attribute.startsWith("ban_info")) {
        attribute.fulfill(1);
        BanEntry ban = Bukkit.getBanList(BanList.Type.NAME).getBanEntry(getName());
        if (ban == null) {
            return null;
        } else if (ban.getExpiration() != null && ban.getExpiration().before(new Date())) {
            return null;
        }
        // -->
        if (attribute.startsWith("expiration") && ban.getExpiration() != null) {
            return new Duration(ban.getExpiration().getTime() / 50).getAttribute(attribute.fulfill(1));
        } else // -->
        if (attribute.startsWith("reason")) {
            return new Element(ban.getReason()).getAttribute(attribute.fulfill(1));
        } else // -->
        if (attribute.startsWith("created")) {
            return new Duration(ban.getCreated().getTime() / 50).getAttribute(attribute.fulfill(1));
        }
        return null;
    }
    // -->
    if (attribute.startsWith("in_group")) {
        if (Depends.permissions == null) {
            if (!attribute.hasAlternative()) {
                dB.echoError("No permission system loaded! Have you installed Vault and a compatible permissions plugin?");
            }
            return null;
        }
        String group = attribute.getContext(1);
        // Non-world specific permission
        if (attribute.getAttribute(2).startsWith("global")) {
            return // TODO: Vault UUID support?
            new Element(Depends.permissions.playerInGroup((World) null, getName(), group)).getAttribute(attribute.fulfill(2));
        } else // Permission in certain world
        if (attribute.getAttribute(2).startsWith("world")) {
            return // TODO: Vault UUID support?
            new Element(Depends.permissions.playerInGroup(attribute.getContext(2), getName(), group)).getAttribute(attribute.fulfill(2));
        } else // Permission in current world
        if (isOnline()) {
            return new Element(Depends.permissions.playerInGroup(getPlayerEntity(), group)).getAttribute(attribute.fulfill(1));
        }
    }
    // -->
    if (attribute.startsWith("permission") || attribute.startsWith("has_permission")) {
        String permission = attribute.getContext(1);
        // Non-world specific permission
        if (attribute.getAttribute(2).startsWith("global")) {
            if (Depends.permissions == null) {
                if (!attribute.hasAlternative()) {
                    dB.echoError("No permission system loaded! Have you installed Vault and a compatible permissions plugin?");
                }
                return null;
            }
            return // TODO: Vault UUID support?
            new Element(Depends.permissions.has((World) null, getName(), permission)).getAttribute(attribute.fulfill(2));
        } else // Permission in certain world
        if (attribute.getAttribute(2).startsWith("world")) {
            if (Depends.permissions == null) {
                if (!attribute.hasAlternative()) {
                    dB.echoError("No permission system loaded! Have you installed Vault and a compatible permissions plugin?");
                }
                return null;
            }
            return // TODO: Vault UUID support?
            new Element(Depends.permissions.has(attribute.getContext(2), getName(), permission)).getAttribute(attribute.fulfill(2));
        } else // Permission in current world
        if (isOnline()) {
            return new Element(getPlayerEntity().hasPermission(permission)).getAttribute(attribute.fulfill(1));
        }
    }
    // -->
    if (attribute.startsWith("inventory")) {
        return getInventory().getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("enderchest")) {
        return getEnderChest().getAttribute(attribute.fulfill(1));
    }
    // Player is required to be online after this point...
    if (!isOnline()) {
        return new Element(identify()).getAttribute(attribute);
    }
    // -->
    if (attribute.startsWith("open_inventory")) {
        return new dInventory(getPlayerEntity().getOpenInventory().getTopInventory()).getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("item_on_cursor")) {
        return new dItem(getPlayerEntity().getItemOnCursor()).getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("item_in_hand.slot")) {
        return new Element(getPlayerEntity().getInventory().getHeldItemSlot() + 1).getAttribute(attribute.fulfill(2));
    }
    // -->
    if (attribute.startsWith("sidebar.lines")) {
        Sidebar sidebar = SidebarCommand.getSidebar(this);
        if (sidebar == null) {
            return null;
        }
        return new dList(sidebar.getLines()).getAttribute(attribute.fulfill(2));
    }
    // -->
    if (attribute.startsWith("sidebar.title")) {
        Sidebar sidebar = SidebarCommand.getSidebar(this);
        if (sidebar == null) {
            return null;
        }
        return new Element(sidebar.getTitle()).getAttribute(attribute.fulfill(2));
    }
    // -->
    if (attribute.startsWith("sidebar.scores")) {
        Sidebar sidebar = SidebarCommand.getSidebar(this);
        if (sidebar == null) {
            return null;
        }
        dList scores = new dList();
        for (int score : sidebar.getScores()) {
            scores.add(String.valueOf(score));
        }
        return scores.getAttribute(attribute.fulfill(2));
    }
    // -->
    if (attribute.startsWith("sidebar.start")) {
        Sidebar sidebar = SidebarCommand.getSidebar(this);
        if (sidebar == null) {
            return null;
        }
        return new Element(sidebar.getStart()).getAttribute(attribute.fulfill(2));
    }
    // -->
    if (attribute.startsWith("sidebar.increment")) {
        Sidebar sidebar = SidebarCommand.getSidebar(this);
        if (sidebar == null) {
            return null;
        }
        return new Element(sidebar.getIncrement()).getAttribute(attribute.fulfill(2));
    }
    // -->
    if (attribute.startsWith("skin_blob")) {
        return new Element(NMSHandler.getInstance().getProfileEditor().getPlayerSkinBlob(getPlayerEntity())).getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("selected_npc")) {
        if (getPlayerEntity().hasMetadata("selected")) {
            return getSelectedNPC().getAttribute(attribute.fulfill(1));
        }
    }
    // -->
    if (attribute.startsWith("entity") && !attribute.startsWith("entity_")) {
        return new dEntity(getPlayerEntity()).getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("ip") || attribute.startsWith("host_name")) {
        attribute = attribute.fulfill(1);
        // -->
        if (attribute.startsWith("address_only")) {
            return new Element(getPlayerEntity().getAddress().toString()).getAttribute(attribute.fulfill(1));
        }
        String host = getPlayerEntity().getAddress().getHostName();
        // -->
        if (attribute.startsWith("address")) {
            return new Element(getPlayerEntity().getAddress().toString()).getAttribute(attribute.fulfill(1));
        }
        return new Element(host).getAttribute(attribute);
    }
    // -->
    if (attribute.startsWith("name.display")) {
        return new Element(getPlayerEntity().getDisplayName()).getAttribute(attribute.fulfill(2));
    }
    // -->
    if (attribute.startsWith("name.list")) {
        return new Element(getPlayerEntity().getPlayerListName()).getAttribute(attribute.fulfill(2));
    }
    // -->
    if (attribute.startsWith("nameplate")) {
        return new Element(NMSHandler.getInstance().getProfileEditor().getPlayerName(getPlayerEntity())).getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("name")) {
        return new Element(getName()).getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("has_finished")) {
        dScript script = dScript.valueOf(attribute.getContext(1));
        if (script == null) {
            return Element.FALSE.getAttribute(attribute.fulfill(1));
        }
        return new Element(FinishCommand.getScriptCompletes(getName(), script.getName()) > 0).getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("has_failed")) {
        dScript script = dScript.valueOf(attribute.getContext(1));
        if (script == null) {
            return Element.FALSE.getAttribute(attribute.fulfill(1));
        }
        return new Element(FailCommand.getScriptFails(getName(), script.getName()) > 0).getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("compass_target")) {
        Location target = getPlayerEntity().getCompassTarget();
        if (target != null) {
            return new dLocation(target).getAttribute(attribute.fulfill(1));
        }
    }
    // -->
    if (attribute.startsWith("chunk_loaded") && attribute.hasContext(1)) {
        dChunk chunk = dChunk.valueOf(attribute.getContext(1));
        if (chunk == null) {
            return null;
        }
        return new Element(hasChunkLoaded(chunk.chunk)).getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("can_fly") || attribute.startsWith("allowed_flight")) {
        return new Element(getPlayerEntity().getAllowFlight()).getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("fly_speed")) {
        return new Element(getPlayerEntity().getFlySpeed()).getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("food_level.formatted")) {
        double maxHunger = getPlayerEntity().getMaxHealth();
        if (attribute.hasContext(2)) {
            maxHunger = attribute.getIntContext(2);
        }
        int foodLevel = getFoodLevel();
        if (foodLevel / maxHunger < .10) {
            return new Element("starving").getAttribute(attribute.fulfill(2));
        } else if (foodLevel / maxHunger < .40) {
            return new Element("famished").getAttribute(attribute.fulfill(2));
        } else if (foodLevel / maxHunger < .75) {
            return new Element("parched").getAttribute(attribute.fulfill(2));
        } else if (foodLevel / maxHunger < 1) {
            return new Element("hungry").getAttribute(attribute.fulfill(2));
        } else {
            return new Element("healthy").getAttribute(attribute.fulfill(2));
        }
    }
    // -->
    if (attribute.startsWith("saturation")) {
        return new Element(getPlayerEntity().getSaturation()).getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("food_level")) {
        return new Element(getFoodLevel()).getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("gamemode")) {
        attribute = attribute.fulfill(1);
        // -->
        if (attribute.startsWith("id")) {
            return new Element(getPlayerEntity().getGameMode().getValue()).getAttribute(attribute.fulfill(1));
        }
        return new Element(getPlayerEntity().getGameMode().name()).getAttribute(attribute);
    }
    // -->
    if (attribute.startsWith("is_blocking")) {
        return new Element(getPlayerEntity().isBlocking()).getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("ping")) {
        return new Element(NMSHandler.getInstance().getPlayerHelper().getPing(getPlayerEntity())).getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("is_flying")) {
        return new Element(getPlayerEntity().isFlying()).getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("is_sleeping")) {
        return new Element(getPlayerEntity().isSleeping()).getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("is_sneaking")) {
        return new Element(getPlayerEntity().isSneaking()).getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("is_sprinting")) {
        return new Element(getPlayerEntity().isSprinting()).getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("has_achievement")) {
        Achievement ach = Achievement.valueOf(attribute.getContext(1).toUpperCase());
        return new Element(getPlayerEntity().hasAchievement(ach)).getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("statistic")) {
        Statistic statistic = Statistic.valueOf(attribute.getContext(1).toUpperCase());
        if (statistic == null) {
            return null;
        }
        // -->
        if (attribute.getAttribute(2).startsWith("qualifier")) {
            dObject obj = ObjectFetcher.pickObjectFor(attribute.getContext(2));
            try {
                if (obj instanceof dMaterial) {
                    return new Element(getPlayerEntity().getStatistic(statistic, ((dMaterial) obj).getMaterial())).getAttribute(attribute.fulfill(2));
                } else if (obj instanceof dEntity) {
                    return new Element(getPlayerEntity().getStatistic(statistic, ((dEntity) obj).getBukkitEntityType())).getAttribute(attribute.fulfill(2));
                } else {
                    return null;
                }
            } catch (Exception e) {
                dB.echoError("Invalid statistic: " + statistic + " for this player!");
                return null;
            }
        }
        try {
            return new Element(getPlayerEntity().getStatistic(statistic)).getAttribute(attribute.fulfill(1));
        } catch (Exception e) {
            dB.echoError("Invalid statistic: " + statistic + " for this player!");
            return null;
        }
    }
    // -->
    if (attribute.startsWith("time_asleep")) {
        return new Duration(getPlayerEntity().getSleepTicks() / 20).getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("time")) {
        return new Element(getPlayerEntity().getPlayerTime()).getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("walk_speed")) {
        return new Element(getPlayerEntity().getWalkSpeed()).getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("weather")) {
        if (getPlayerEntity().getPlayerWeather() != null) {
            return new Element(getPlayerEntity().getPlayerWeather().name()).getAttribute(attribute.fulfill(1));
        } else {
            return null;
        }
    }
    // -->
    if (attribute.startsWith("xp.level")) {
        return new Element(getPlayerEntity().getLevel()).getAttribute(attribute.fulfill(2));
    }
    // -->
    if (attribute.startsWith("xp.to_next_level")) {
        return new Element(getPlayerEntity().getExpToLevel()).getAttribute(attribute.fulfill(2));
    }
    // -->
    if (attribute.startsWith("xp.total")) {
        return new Element(getPlayerEntity().getTotalExperience()).getAttribute(attribute.fulfill(2));
    }
    // -->
    if (attribute.startsWith("xp")) {
        return new Element(getPlayerEntity().getExp() * 100).getAttribute(attribute.fulfill(1));
    }
    if (Depends.chat != null) {
        // -->
        if (attribute.startsWith("chat_prefix")) {
            String prefix = Depends.chat.getPlayerPrefix(getWorld().getName(), getOfflinePlayer());
            if (prefix == null) {
                return null;
            }
            return new Element(prefix).getAttribute(attribute.fulfill(1));
        } else // -->
        if (attribute.startsWith("chat_suffix")) {
            String suffix = Depends.chat.getPlayerSuffix(getWorld().getName(), getOfflinePlayer());
            if (suffix == null) {
                return null;
            }
            return new Element(suffix).getAttribute(attribute.fulfill(1));
        }
    }
    // Iterate through this object's properties' attributes
    for (Property property : PropertyParser.getProperties(this)) {
        String returned = property.getAttribute(attribute);
        if (returned != null) {
            return returned;
        }
    }
    return new dEntity(getPlayerEntity()).getAttribute(attribute);
}
Also used : Entity(org.bukkit.entity.Entity) LivingEntity(org.bukkit.entity.LivingEntity) BlockIterator(org.bukkit.util.BlockIterator) PotionEffect(org.bukkit.potion.PotionEffect) ArrayList(java.util.ArrayList) FlagManager(net.aufdemrand.denizen.flags.FlagManager) LivingEntity(org.bukkit.entity.LivingEntity) ImprovedOfflinePlayer(net.aufdemrand.denizen.nms.abstracts.ImprovedOfflinePlayer) Property(net.aufdemrand.denizencore.objects.properties.Property) Pattern(java.util.regex.Pattern) Player(org.bukkit.entity.Player) ImprovedOfflinePlayer(net.aufdemrand.denizen.nms.abstracts.ImprovedOfflinePlayer) Date(java.util.Date) Block(org.bukkit.block.Block) Sidebar(net.aufdemrand.denizen.nms.abstracts.Sidebar)

Aggregations

Sidebar (net.aufdemrand.denizen.nms.abstracts.Sidebar)2 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 Iterator (java.util.Iterator)1 Pattern (java.util.regex.Pattern)1 FlagManager (net.aufdemrand.denizen.flags.FlagManager)1 ImprovedOfflinePlayer (net.aufdemrand.denizen.nms.abstracts.ImprovedOfflinePlayer)1 net.aufdemrand.denizen.objects.dPlayer (net.aufdemrand.denizen.objects.dPlayer)1 BukkitTagContext (net.aufdemrand.denizen.tags.BukkitTagContext)1 CommandExecutionException (net.aufdemrand.denizencore.exceptions.CommandExecutionException)1 InvalidArgumentsException (net.aufdemrand.denizencore.exceptions.InvalidArgumentsException)1 Element (net.aufdemrand.denizencore.objects.Element)1 net.aufdemrand.denizencore.objects.dList (net.aufdemrand.denizencore.objects.dList)1 Property (net.aufdemrand.denizencore.objects.properties.Property)1 TagContext (net.aufdemrand.denizencore.tags.TagContext)1 Block (org.bukkit.block.Block)1 Entity (org.bukkit.entity.Entity)1 LivingEntity (org.bukkit.entity.LivingEntity)1 Player (org.bukkit.entity.Player)1 PotionEffect (org.bukkit.potion.PotionEffect)1