Search in sources :

Example 1 with FlagManager

use of net.aufdemrand.denizen.flags.FlagManager in project Denizen-For-Bukkit by DenizenScript.

the class FlagCommand method execute.

@Override
public void execute(ScriptEntry scriptEntry) throws CommandExecutionException {
    dObject flag_target = scriptEntry.getdObject("flag_target");
    Duration duration = (Duration) scriptEntry.getObject("duration");
    FlagManager.Action action = (FlagManager.Action) scriptEntry.getObject("action");
    Element value = scriptEntry.getElement("value");
    Element name = scriptEntry.getElement("flag_name");
    int index = -1;
    // Usage example: - FLAG FLAGNAME[3]:VALUE specifies an index of 3 should be set with VALUE.
    if (name.asString().contains("[")) {
        try {
            index = Integer.valueOf(name.asString().split("\\[")[1].replace("]", ""));
        } catch (Exception e) {
            index = -1;
        }
        name = Element.valueOf(name.asString().split("\\[")[0]);
    }
    // Send information to debugger
    dB.report(scriptEntry, getName(), name.debug() + (index > 0 ? aH.debugObj("Index", String.valueOf(index)) : "") + aH.debugUniqueObj("Action/Value", action.toString(), (value != null ? value.asString() : "null")) + (duration != null ? duration.debug() : "") + flag_target.debug());
    Flag flag;
    // Returns existing flag (if existing), or a new flag if not
    if (flag_target instanceof Element) {
        flag = DenizenAPI.getCurrentInstance().flagManager().getGlobalFlag(name.asString());
    } else if (flag_target instanceof dPlayer) {
        flag = DenizenAPI.getCurrentInstance().flagManager().getPlayerFlag((dPlayer) flag_target, name.asString());
    } else if (flag_target instanceof dNPC) {
        flag = DenizenAPI.getCurrentInstance().flagManager().getNPCFlag(((dNPC) flag_target).getId(), name.asString());
    } else if (flag_target instanceof dEntity) {
        flag = DenizenAPI.getCurrentInstance().flagManager().getEntityFlag((dEntity) flag_target, name.asString());
    } else {
        throw new CommandExecutionException("Could not fetch a flag for this entity: " + flag_target.debug());
    }
    // Do the action!
    flag.doAction(action, value, index);
    // Set flag duration
    if (flag.StillValid() && duration != null && duration.getSeconds() > 0) {
        flag.setExpiration(DenizenCore.currentTimeMillis + Double.valueOf(duration.getSeconds() * 1000.0).longValue());
    } else if (flag.StillValid() && flag.expiration().getMillis() != 0L) {
        flag.setExpiration(0L);
    }
}
Also used : Element(net.aufdemrand.denizencore.objects.Element) Duration(net.aufdemrand.denizencore.objects.Duration) FlagManager(net.aufdemrand.denizen.flags.FlagManager) Flag(net.aufdemrand.denizen.flags.FlagManager.Flag) InvalidArgumentsException(net.aufdemrand.denizencore.exceptions.InvalidArgumentsException) CommandExecutionException(net.aufdemrand.denizencore.exceptions.CommandExecutionException) net.aufdemrand.denizen.objects.dNPC(net.aufdemrand.denizen.objects.dNPC) net.aufdemrand.denizen.objects.dEntity(net.aufdemrand.denizen.objects.dEntity) net.aufdemrand.denizencore.objects.dObject(net.aufdemrand.denizencore.objects.dObject) net.aufdemrand.denizen.objects.dPlayer(net.aufdemrand.denizen.objects.dPlayer) CommandExecutionException(net.aufdemrand.denizencore.exceptions.CommandExecutionException)

Example 2 with FlagManager

use of net.aufdemrand.denizen.flags.FlagManager in project Denizen-For-Bukkit by DenizenScript.

the class ServerTags method serverTag.

@TagManager.TagEvents
public void serverTag(ReplaceableTagEvent event) {
    if (!event.matches("server", "svr", "global") || event.replaced()) {
        return;
    }
    Attribute attribute = event.getAttributes().fulfill(1);
    // -->
    if (attribute.startsWith("object_is_valid")) {
        dObject o = ObjectFetcher.pickObjectFor(attribute.getContext(1), new BukkitTagContext(null, null, false, null, false, null));
        event.setReplaced(new Element(!(o == null || o instanceof Element)).getAttribute(attribute.fulfill(1)));
        return;
    }
    // -->
    if (attribute.startsWith("has_whitelist")) {
        event.setReplaced(new Element(Bukkit.hasWhitelist()).getAttribute(attribute.fulfill(1)));
        return;
    }
    // -->
    if (attribute.startsWith("has_flag")) {
        String flag_name;
        if (attribute.hasContext(1)) {
            flag_name = attribute.getContext(1);
        } else {
            event.setReplaced("null");
            return;
        }
        event.setReplaced(new Element(FlagManager.serverHasFlag(flag_name)).getAttribute(attribute.fulfill(1)));
        return;
    }
    // -->
    if (attribute.startsWith("flag")) {
        String flag_name;
        if (attribute.hasContext(1)) {
            flag_name = attribute.getContext(1);
        } else {
            event.setReplaced("null");
            return;
        }
        attribute.fulfill(1);
        // NOTE: Meta is in dList.java
        if (attribute.startsWith("is_expired") || attribute.startsWith("isexpired")) {
            event.setReplaced(new Element(!FlagManager.serverHasFlag(flag_name)).getAttribute(attribute.fulfill(1)));
            return;
        }
        // NOTE: Meta is in dList.java
        if (attribute.startsWith("size") && !FlagManager.serverHasFlag(flag_name)) {
            event.setReplaced(new Element(0).getAttribute(attribute.fulfill(1)));
            return;
        }
        if (FlagManager.serverHasFlag(flag_name)) {
            FlagManager.Flag flag = DenizenAPI.getCurrentInstance().flagManager().getGlobalFlag(flag_name);
            event.setReplaced(new dList(flag.toString(), true, flag.values()).getAttribute(attribute));
        }
        return;
    }
    // -->
    if (attribute.startsWith("list_materials")) {
        dList allMats = new dList();
        for (Material mat : Material.values()) {
            allMats.add(mat.name());
        }
        event.setReplaced(allMats.getAttribute(attribute.fulfill(1)));
    }
    // -->
    if (attribute.startsWith("list_flags")) {
        dList allFlags = new dList(DenizenAPI.getCurrentInstance().flagManager().listGlobalFlags());
        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);
                    }
                }
            }
        }
        event.setReplaced(searchFlags == null ? allFlags.getAttribute(attribute.fulfill(1)) : searchFlags.getAttribute(attribute.fulfill(1)));
    }
    // -->
    if (attribute.startsWith("list_notables")) {
        dList allNotables = new dList();
        if (attribute.hasContext(1)) {
            String type = CoreUtilities.toLowerCase(attribute.getContext(1));
            types: for (Map.Entry<String, Class> typeClass : NotableManager.getReverseClassIdMap().entrySet()) {
                if (type.equals(CoreUtilities.toLowerCase(typeClass.getKey()))) {
                    for (Object notable : NotableManager.getAllType(typeClass.getValue())) {
                        allNotables.add(((dObject) notable).identify());
                    }
                    break types;
                }
            }
        } else {
            for (Notable notable : NotableManager.notableObjects.values()) {
                allNotables.add(((dObject) notable).identify());
            }
        }
        event.setReplaced(allNotables.getAttribute(attribute.fulfill(1)));
    }
    // -->
    if (attribute.startsWith("start_time")) {
        event.setReplaced(new Duration(Denizen.startTime / 50).getAttribute(attribute.fulfill(1)));
    }
    // -->
    if (attribute.startsWith("ram_allocated")) {
        event.setReplaced(new Element(Runtime.getRuntime().totalMemory()).getAttribute(attribute.fulfill(1)));
    }
    // -->
    if (attribute.startsWith("ram_max")) {
        event.setReplaced(new Element(Runtime.getRuntime().maxMemory()).getAttribute(attribute.fulfill(1)));
    }
    // -->
    if (attribute.startsWith("ram_free")) {
        event.setReplaced(new Element(Runtime.getRuntime().freeMemory()).getAttribute(attribute.fulfill(1)));
    }
    // -->
    if (attribute.startsWith("available_processors")) {
        event.setReplaced(new Element(Runtime.getRuntime().availableProcessors()).getAttribute(attribute.fulfill(1)));
    }
    // -->
    if (attribute.startsWith("current_time_millis")) {
        event.setReplaced(new Element(System.currentTimeMillis()).getAttribute(attribute.fulfill(1)));
    }
    // -->
    if (attribute.startsWith("has_event") && attribute.hasContext(1)) {
        event.setReplaced(new Element(OldEventManager.eventExists(attribute.getContext(1)) || OldEventManager.eventExists(OldEventManager.StripIdentifiers(attribute.getContext(1)))).getAttribute(attribute.fulfill(1)));
    }
    // -->
    if (attribute.startsWith("get_event_handlers") && attribute.hasContext(1)) {
        String eventName = attribute.getContext(1).toUpperCase();
        List<WorldScriptContainer> EventsOne = OldEventManager.events.get("ON " + eventName);
        List<WorldScriptContainer> EventsTwo = OldEventManager.events.get("ON " + OldEventManager.StripIdentifiers(eventName));
        if (EventsOne == null && EventsTwo == null) {
            dB.echoError("No world scripts will handle the event '" + eventName + "'");
        } else {
            dList list = new dList();
            if (EventsOne != null) {
                for (WorldScriptContainer script : EventsOne) {
                    list.add("s@" + script.getName());
                }
            }
            if (EventsTwo != null) {
                for (WorldScriptContainer script : EventsTwo) {
                    if (!list.contains("s@" + script.getName())) {
                        list.add("s@" + script.getName());
                    }
                }
            }
            event.setReplaced(list.getAttribute(attribute.fulfill(1)));
        }
    }
    // -->
    if (attribute.startsWith("selected_npc")) {
        NPC npc = ((Citizens) Bukkit.getPluginManager().getPlugin("Citizens")).getNPCSelector().getSelected(Bukkit.getConsoleSender());
        if (npc == null) {
            return;
        } else {
            event.setReplaced(new dNPC(npc).getAttribute(attribute.fulfill(1)));
        }
        return;
    }
    // -->
    if (attribute.startsWith("get_npcs_named") && Depends.citizens != null && attribute.hasContext(1)) {
        dList npcs = new dList();
        for (NPC npc : CitizensAPI.getNPCRegistry()) {
            if (npc.getName().equalsIgnoreCase(attribute.getContext(1))) {
                npcs.add(dNPC.mirrorCitizensNPC(npc).identify());
            }
        }
        event.setReplaced(npcs.getAttribute(attribute.fulfill(1)));
        return;
    }
    // -->
    if (attribute.startsWith("has_file") && attribute.hasContext(1)) {
        File f = new File(DenizenAPI.getCurrentInstance().getDataFolder(), attribute.getContext(1));
        try {
            if (!Settings.allowStrangeYAMLSaves() && !f.getCanonicalPath().startsWith(DenizenAPI.getCurrentInstance().getDataFolder().getCanonicalPath())) {
                dB.echoError("Invalid path specified. Invalid paths have been denied by the server administrator.");
                return;
            }
        } catch (Exception e) {
            dB.echoError(e);
            return;
        }
        event.setReplaced(new Element(f.exists()).getAttribute(attribute.fulfill(1)));
        return;
    }
    // -->
    if (attribute.startsWith("list_files") && attribute.hasContext(1)) {
        File folder = new File(DenizenAPI.getCurrentInstance().getDataFolder(), attribute.getContext(1));
        if (!folder.exists() || !folder.isDirectory()) {
            return;
        }
        try {
            if (!Settings.allowStrangeYAMLSaves() && !folder.getCanonicalPath().startsWith(DenizenAPI.getCurrentInstance().getDataFolder().getCanonicalPath())) {
                dB.echoError("Invalid path specified. Invalid paths have been denied by the server administrator.");
                return;
            }
        } catch (Exception e) {
            dB.echoError(e);
            return;
        }
        File[] files = folder.listFiles();
        if (files == null) {
            return;
        }
        dList list = new dList();
        for (File file : files) {
            list.add(file.getName());
        }
        event.setReplaced(list.getAttribute(attribute.fulfill(1)));
        return;
    }
    // -->
    if (attribute.startsWith("has_permissions")) {
        event.setReplaced(new Element(Depends.permissions != null && Depends.permissions.isEnabled()).getAttribute(attribute.fulfill(1)));
        return;
    }
    // -->
    if (attribute.startsWith("has_economy")) {
        event.setReplaced(new Element(Depends.economy != null && Depends.economy.isEnabled()).getAttribute(attribute.fulfill(1)));
        return;
    }
    // -->
    if (attribute.startsWith("denizen_version")) {
        event.setReplaced(new Element(DenizenAPI.getCurrentInstance().getDescription().getVersion()).getAttribute(attribute.fulfill(1)));
        return;
    }
    // -->
    if (attribute.startsWith("bukkit_version")) {
        event.setReplaced(new Element(Bukkit.getBukkitVersion()).getAttribute(attribute.fulfill(1)));
        return;
    }
    // -->
    if (attribute.startsWith("version")) {
        event.setReplaced(new Element(Bukkit.getServer().getVersion()).getAttribute(attribute.fulfill(1)));
        return;
    }
    // -->
    if (attribute.startsWith("java_version")) {
        event.setReplaced(new Element(System.getProperty("java.version")).getAttribute(attribute.fulfill(1)));
        return;
    }
    // -->
    if (attribute.startsWith("max_players")) {
        event.setReplaced(new Element(Bukkit.getServer().getMaxPlayers()).getAttribute(attribute.fulfill(1)));
        return;
    }
    // -->
    if (attribute.startsWith("list_sql_connections")) {
        dList list = new dList();
        for (Map.Entry<String, Connection> entry : SQLCommand.connections.entrySet()) {
            try {
                if (!entry.getValue().isClosed()) {
                    list.add(entry.getKey());
                } else {
                    SQLCommand.connections.remove(entry.getKey());
                }
            } catch (SQLException e) {
                dB.echoError(attribute.getScriptEntry().getResidingQueue(), e);
            }
        }
        event.setReplaced(list.getAttribute(attribute.fulfill(1)));
        return;
    }
    // -->
    if (attribute.startsWith("list_permission_groups")) {
        if (Depends.permissions == null) {
            dB.echoError("No permission system loaded! Have you installed Vault and a compatible permissions plugin?");
            return;
        }
        event.setReplaced(new dList(Arrays.asList(Depends.permissions.getGroups())).getAttribute(attribute.fulfill(1)));
        return;
    }
    // -->
    if (attribute.startsWith("list_plugin_names")) {
        dList plugins = new dList();
        for (Plugin plugin : Bukkit.getServer().getPluginManager().getPlugins()) {
            plugins.add(plugin.getName());
        }
        event.setReplaced(plugins.getAttribute(attribute.fulfill(1)));
        return;
    }
    // -->
    if (attribute.startsWith("list_scripts")) {
        dList scripts = new dList();
        for (String str : ScriptRegistry._getScriptNames()) {
            scripts.add("s@" + str);
        }
        event.setReplaced(scripts.getAttribute(attribute.fulfill(1)));
        return;
    }
    // -->
    if (attribute.startsWith("match_player") && attribute.hasContext(1)) {
        Player matchPlayer = null;
        String matchInput = CoreUtilities.toLowerCase(attribute.getContext(1));
        for (Player player : Bukkit.getOnlinePlayers()) {
            if (CoreUtilities.toLowerCase(player.getName()).equals(matchInput)) {
                matchPlayer = player;
                break;
            } else if (CoreUtilities.toLowerCase(player.getName()).contains(matchInput) && matchPlayer == null) {
                matchPlayer = player;
            }
        }
        if (matchPlayer != null) {
            event.setReplaced(new dPlayer(matchPlayer).getAttribute(attribute.fulfill(1)));
        }
        return;
    }
    // -->
    if (attribute.startsWith("match_offline_player") && attribute.hasContext(1)) {
        UUID matchPlayer = null;
        String matchInput = CoreUtilities.toLowerCase(attribute.getContext(1));
        for (Map.Entry<String, UUID> entry : dPlayer.getAllPlayers().entrySet()) {
            if (CoreUtilities.toLowerCase(entry.getKey()).equals(matchInput)) {
                matchPlayer = entry.getValue();
                break;
            } else if (CoreUtilities.toLowerCase(entry.getKey()).contains(matchInput) && matchPlayer == null) {
                matchPlayer = entry.getValue();
            }
        }
        if (matchPlayer != null) {
            event.setReplaced(new dPlayer(matchPlayer).getAttribute(attribute.fulfill(1)));
        }
        return;
    }
    // -->
    if (attribute.startsWith("get_npcs_assigned") && Depends.citizens != null && attribute.hasContext(1)) {
        dScript script = dScript.valueOf(attribute.getContext(1));
        if (script == null || !(script.getContainer() instanceof AssignmentScriptContainer)) {
            dB.echoError("Invalid script specified.");
        } else {
            dList npcs = new dList();
            for (NPC npc : CitizensAPI.getNPCRegistry()) {
                if (npc.hasTrait(AssignmentTrait.class) && npc.getTrait(AssignmentTrait.class).hasAssignment() && npc.getTrait(AssignmentTrait.class).getAssignment().getName().equalsIgnoreCase(script.getName())) {
                    npcs.add(dNPC.mirrorCitizensNPC(npc).identify());
                }
            }
            event.setReplaced(npcs.getAttribute(attribute.fulfill(1)));
            return;
        }
    }
    // -->
    if (attribute.startsWith("get_online_players_flagged") && attribute.hasContext(1)) {
        String flag = attribute.getContext(1);
        dList players = new dList();
        for (Player player : Bukkit.getOnlinePlayers()) {
            if (DenizenAPI.getCurrentInstance().flagManager().getPlayerFlag(new dPlayer(player), flag).size() > 0) {
                players.add(new dPlayer(player).identify());
            }
        }
        event.setReplaced(players.getAttribute(attribute.fulfill(1)));
        return;
    }
    // -->
    if (attribute.startsWith("get_players_flagged") && attribute.hasContext(1)) {
        String flag = attribute.getContext(1);
        dList players = new dList();
        for (Map.Entry<String, UUID> entry : dPlayer.getAllPlayers().entrySet()) {
            if (DenizenAPI.getCurrentInstance().flagManager().getPlayerFlag(new dPlayer(entry.getValue()), flag).size() > 0) {
                players.add(new dPlayer(entry.getValue()).identify());
            }
        }
        event.setReplaced(players.getAttribute(attribute.fulfill(1)));
        return;
    }
    // -->
    if (attribute.startsWith("get_spawned_npcs_flagged") && Depends.citizens != null && attribute.hasContext(1)) {
        String flag = attribute.getContext(1);
        dList npcs = new dList();
        for (NPC npc : CitizensAPI.getNPCRegistry()) {
            dNPC dNpc = dNPC.mirrorCitizensNPC(npc);
            if (dNpc.isSpawned() && FlagManager.npcHasFlag(dNpc, flag)) {
                npcs.add(dNpc.identify());
            }
        }
        event.setReplaced(npcs.getAttribute(attribute.fulfill(1)));
        return;
    }
    // -->
    if (attribute.startsWith("get_npcs_flagged") && Depends.citizens != null && attribute.hasContext(1)) {
        String flag = attribute.getContext(1);
        dList npcs = new dList();
        for (NPC npc : CitizensAPI.getNPCRegistry()) {
            dNPC dNpc = dNPC.mirrorCitizensNPC(npc);
            if (FlagManager.npcHasFlag(dNpc, flag)) {
                npcs.add(dNpc.identify());
            }
        }
        event.setReplaced(npcs.getAttribute(attribute.fulfill(1)));
        return;
    }
    // -->
    if (attribute.startsWith("list_npcs") && Depends.citizens != null) {
        dList npcs = new dList();
        for (NPC npc : CitizensAPI.getNPCRegistry()) {
            npcs.add(dNPC.mirrorCitizensNPC(npc).identify());
        }
        event.setReplaced(npcs.getAttribute(attribute.fulfill(1)));
        return;
    }
    // -->
    if (attribute.startsWith("list_worlds")) {
        dList worlds = new dList();
        for (World world : Bukkit.getWorlds()) {
            worlds.add(dWorld.mirrorBukkitWorld(world).identify());
        }
        event.setReplaced(worlds.getAttribute(attribute.fulfill(1)));
        return;
    }
    // -->
    if (attribute.startsWith("list_plugins")) {
        dList plugins = new dList();
        for (Plugin plugin : Bukkit.getServer().getPluginManager().getPlugins()) {
            plugins.add(new dPlugin(plugin).identify());
        }
        event.setReplaced(plugins.getAttribute(attribute.fulfill(1)));
        return;
    }
    // -->
    if (attribute.startsWith("list_players")) {
        dList players = new dList();
        for (OfflinePlayer player : Bukkit.getOfflinePlayers()) {
            players.add(dPlayer.mirrorBukkitPlayer(player).identify());
        }
        event.setReplaced(players.getAttribute(attribute.fulfill(1)));
        return;
    }
    // -->
    if (attribute.startsWith("list_online_players")) {
        dList players = new dList();
        for (Player player : Bukkit.getOnlinePlayers()) {
            players.add(dPlayer.mirrorBukkitPlayer(player).identify());
        }
        event.setReplaced(players.getAttribute(attribute.fulfill(1)));
        return;
    }
    // -->
    if (attribute.startsWith("list_offline_players")) {
        dList players = new dList();
        for (OfflinePlayer player : Bukkit.getOfflinePlayers()) {
            if (!player.isOnline()) {
                players.add(dPlayer.mirrorBukkitPlayer(player).identify());
            }
        }
        event.setReplaced(players.getAttribute(attribute.fulfill(1)));
        return;
    }
    // -->
    if (attribute.startsWith("list_banned_players")) {
        dList banned = new dList();
        for (OfflinePlayer player : Bukkit.getBannedPlayers()) {
            banned.add(dPlayer.mirrorBukkitPlayer(player).identify());
        }
        event.setReplaced(banned.getAttribute(attribute.fulfill(1)));
        return;
    }
    // -->
    if (attribute.startsWith("list_ops")) {
        dList players = new dList();
        for (OfflinePlayer player : Bukkit.getOperators()) {
            players.add(dPlayer.mirrorBukkitPlayer(player).identify());
        }
        event.setReplaced(players.getAttribute(attribute.fulfill(1)));
        return;
    }
    // -->
    if (attribute.startsWith("list_online_ops")) {
        dList players = new dList();
        for (OfflinePlayer player : Bukkit.getOperators()) {
            if (player.isOnline()) {
                players.add(dPlayer.mirrorBukkitPlayer(player).identify());
            }
        }
        event.setReplaced(players.getAttribute(attribute.fulfill(1)));
        return;
    }
    // -->
    if (attribute.startsWith("list_offline_ops")) {
        dList players = new dList();
        for (OfflinePlayer player : Bukkit.getOfflinePlayers()) {
            if (player.isOp() && !player.isOnline()) {
                players.add(dPlayer.mirrorBukkitPlayer(player).identify());
            }
        }
        event.setReplaced(players.getAttribute(attribute.fulfill(1)));
        return;
    }
    // -->
    if (attribute.startsWith("motd")) {
        event.setReplaced(new Element(Bukkit.getServer().getMotd()).getAttribute(attribute.fulfill(1)));
        return;
    } else // -->
    if (attribute.startsWith("entity_is_spawned") && attribute.hasContext(1)) {
        dEntity ent = dEntity.valueOf(attribute.getContext(1));
        event.setReplaced(new Element((ent != null && ent.isUnique() && ent.isSpawned()) ? "true" : "false").getAttribute(attribute.fulfill(1)));
    } else // -->
    if (attribute.startsWith("player_is_valid") && attribute.hasContext(1)) {
        event.setReplaced(new Element(dPlayer.playerNameIsValid(attribute.getContext(1))).getAttribute(attribute.fulfill(1)));
    } else // -->
    if (attribute.startsWith("npc_is_valid") && attribute.hasContext(1)) {
        dNPC npc = dNPC.valueOf(attribute.getContext(1));
        event.setReplaced(new Element((npc != null && npc.isValid())).getAttribute(attribute.fulfill(1)));
    } else // -->
    if (attribute.startsWith("current_bossbars")) {
        dList dl = new dList();
        for (String str : BossBarCommand.bossBarMap.keySet()) {
            dl.add(str);
        }
        event.setReplaced(dl.getAttribute(attribute.fulfill(1)));
    } else // -->
    if (attribute.startsWith("recent_tps")) {
        dList list = new dList();
        for (double tps : NMSHandler.getInstance().getRecentTps()) {
            list.add(new Element(tps).identify());
        }
        event.setReplaced(list.getAttribute(attribute.fulfill(1)));
    } else // -->
    if (attribute.startsWith("port")) {
        event.setReplaced(new Element(NMSHandler.getInstance().getPort()).getAttribute(attribute.fulfill(1)));
    }
// TODO: Add everything else from Bukkit.getServer().*
}
Also used : NPC(net.citizensnpcs.api.npc.NPC) net.aufdemrand.denizen.objects.dNPC(net.aufdemrand.denizen.objects.dNPC) Attribute(net.aufdemrand.denizencore.tags.Attribute) SQLException(java.sql.SQLException) WorldScriptContainer(net.aufdemrand.denizencore.scripts.containers.core.WorldScriptContainer) AssignmentScriptContainer(net.aufdemrand.denizen.scripts.containers.core.AssignmentScriptContainer) FlagManager(net.aufdemrand.denizen.flags.FlagManager) net.aufdemrand.denizen.objects.dWorld(net.aufdemrand.denizen.objects.dWorld) net.aufdemrand.denizen.objects.dEntity(net.aufdemrand.denizen.objects.dEntity) UUID(java.util.UUID) Pattern(java.util.regex.Pattern) Player(org.bukkit.entity.Player) net.aufdemrand.denizen.objects.dPlayer(net.aufdemrand.denizen.objects.dPlayer) Connection(java.sql.Connection) net.aufdemrand.denizen.objects.dPlugin(net.aufdemrand.denizen.objects.dPlugin) Notable(net.aufdemrand.denizencore.objects.notable.Notable) SQLException(java.sql.SQLException) AssignmentTrait(net.aufdemrand.denizen.npc.traits.AssignmentTrait) net.aufdemrand.denizen.objects.dNPC(net.aufdemrand.denizen.objects.dNPC) BukkitTagContext(net.aufdemrand.denizen.tags.BukkitTagContext) net.aufdemrand.denizen.objects.dPlayer(net.aufdemrand.denizen.objects.dPlayer) File(java.io.File) Map(java.util.Map) Plugin(org.bukkit.plugin.Plugin) net.aufdemrand.denizen.objects.dPlugin(net.aufdemrand.denizen.objects.dPlugin)

Example 3 with FlagManager

use of net.aufdemrand.denizen.flags.FlagManager in project Denizen-For-Bukkit by DenizenScript.

the class dEntity method getAttribute.

@Override
public String getAttribute(Attribute attribute) {
    if (attribute == null) {
        return null;
    }
    if (entity == null && entity_type == null) {
        if (npc != null) {
            return new Element(identify()).getAttribute(attribute);
        }
        dB.echoError("dEntity has returned null.");
        return null;
    }
    // -->
    if (attribute.startsWith("debug.log")) {
        dB.log(debug());
        return new Element(Boolean.TRUE.toString()).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("type")) {
        return new Element("Entity").getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("entity_type")) {
        return new Element(entity_type.getName()).getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("is_spawned")) {
        return new Element(isSpawned()).getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("eid")) {
        return new Element(entity.getEntityId()).getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("uuid")) {
        return new Element(getUUID().toString()).getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("script")) {
        // TODO: Maybe return legit null?
        return new Element(entityScript == null ? "null" : entityScript).getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("has_flag")) {
        String flag_name;
        if (attribute.hasContext(1)) {
            flag_name = attribute.getContext(1);
        } else {
            return null;
        }
        if (isPlayer() || isCitizensNPC()) {
            dB.echoError("Reading flag for PLAYER or NPC as if it were an ENTITY!");
            return null;
        }
        return new Element(FlagManager.entityHasFlag(this, flag_name)).getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("flag")) {
        String flag_name;
        if (attribute.hasContext(1)) {
            flag_name = attribute.getContext(1);
        } else {
            return null;
        }
        if (isPlayer() || isCitizensNPC()) {
            dB.echoError("Reading flag for PLAYER or NPC as if it were an ENTITY!");
            return null;
        }
        if (attribute.getAttribute(2).equalsIgnoreCase("is_expired") || attribute.startsWith("isexpired")) {
            return new Element(!FlagManager.entityHasFlag(this, flag_name)).getAttribute(attribute.fulfill(2));
        }
        if (attribute.getAttribute(2).equalsIgnoreCase("size") && !FlagManager.entityHasFlag(this, flag_name)) {
            return new Element(0).getAttribute(attribute.fulfill(2));
        }
        if (FlagManager.entityHasFlag(this, flag_name)) {
            FlagManager.Flag flag = DenizenAPI.getCurrentInstance().flagManager().getEntityFlag(this, flag_name);
            return new dList(flag.toString(), true, flag.values()).getAttribute(attribute.fulfill(1));
        }
        return new Element(identify()).getAttribute(attribute);
    }
    // -->
    if (attribute.startsWith("list_flags")) {
        dList allFlags = new dList(DenizenAPI.getCurrentInstance().flagManager().listEntityFlags(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 (entity == null) {
        return new Element(identify()).getAttribute(attribute);
    }
    // -->
    if (attribute.startsWith("custom_id")) {
        if (CustomNBT.hasCustomNBT(getLivingEntity(), "denizen-script-id")) {
            return new dScript(CustomNBT.getCustomNBT(getLivingEntity(), "denizen-script-id")).getAttribute(attribute.fulfill(1));
        } else {
            return new Element(entity.getType().name()).getAttribute(attribute.fulfill(1));
        }
    }
    // -->
    if (attribute.startsWith("name")) {
        return new Element(getName()).getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("saddle")) {
        if (getLivingEntity().getType() == EntityType.HORSE) {
            return new dItem(((Horse) getLivingEntity()).getInventory().getSaddle()).getAttribute(attribute.fulfill(1));
        } else if (getLivingEntity().getType() == EntityType.PIG) {
            return new dItem(((Pig) getLivingEntity()).hasSaddle() ? Material.SADDLE : Material.AIR).getAttribute(attribute.fulfill(1));
        }
    }
    // -->
    if (attribute.startsWith("horse_armor") || attribute.startsWith("horse_armour")) {
        if (getLivingEntity().getType() == EntityType.HORSE) {
            return new dItem(((Horse) getLivingEntity()).getInventory().getArmor()).getAttribute(attribute.fulfill(1));
        }
    }
    // -->
    if (attribute.startsWith("has_saddle")) {
        if (getLivingEntity().getType() == EntityType.HORSE) {
            return new Element(((Horse) getLivingEntity()).getInventory().getSaddle().getType() == Material.SADDLE).getAttribute(attribute.fulfill(1));
        } else if (getLivingEntity().getType() == EntityType.PIG) {
            return new Element(((Pig) getLivingEntity()).hasSaddle()).getAttribute(attribute.fulfill(1));
        }
    }
    // -->
    if (attribute.startsWith("item_in_hand") || attribute.startsWith("iteminhand")) {
        return new dItem(NMSHandler.getInstance().getEntityHelper().getItemInHand(getLivingEntity())).getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("item_in_offhand") || attribute.startsWith("iteminoffhand")) {
        return new dItem(NMSHandler.getInstance().getEntityHelper().getItemInOffHand(getLivingEntity())).getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("map_trace")) {
        EntityHelper.MapTraceResult mtr = NMSHandler.getInstance().getEntityHelper().mapTrace(getLivingEntity(), 200);
        if (mtr != null) {
            double x = 0;
            double y = 0;
            double basex = mtr.hitLocation.getX() - Math.floor(mtr.hitLocation.getX());
            double basey = mtr.hitLocation.getY() - Math.floor(mtr.hitLocation.getY());
            double basez = mtr.hitLocation.getZ() - Math.floor(mtr.hitLocation.getZ());
            if (mtr.angle == BlockFace.NORTH) {
                x = 128f - (basex * 128f);
            } else if (mtr.angle == BlockFace.SOUTH) {
                x = basex * 128f;
            } else if (mtr.angle == BlockFace.WEST) {
                x = basez * 128f;
            } else if (mtr.angle == BlockFace.EAST) {
                x = 128f - (basez * 128f);
            }
            y = 128f - (basey * 128f);
            return new dLocation(null, Math.round(x), Math.round(y)).getAttribute(attribute.fulfill(1));
        }
    }
    // -->
    if (attribute.startsWith("can_see")) {
        if (isLivingEntity() && attribute.hasContext(1) && dEntity.matches(attribute.getContext(1))) {
            dEntity toEntity = dEntity.valueOf(attribute.getContext(1));
            if (toEntity != null && toEntity.isSpawned()) {
                return new Element(getLivingEntity().hasLineOfSight(toEntity.getBukkitEntity())).getAttribute(attribute.fulfill(1));
            }
        }
    }
    // -->
    if (attribute.startsWith("eye_location")) {
        return new dLocation(getEyeLocation()).getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("eye_height")) {
        if (isLivingEntity()) {
            return new Element(getLivingEntity().getEyeHeight()).getAttribute(attribute.fulfill(1));
        }
    }
    // -->
    if (attribute.startsWith("location.cursor_on")) {
        int range = attribute.getIntContext(2);
        if (range < 1) {
            range = 50;
        }
        Set<Material> set = new HashSet<Material>();
        set.add(Material.AIR);
        attribute = attribute.fulfill(2);
        if (attribute.startsWith("ignore") && attribute.hasContext(1)) {
            List<dMaterial> ignoreList = dList.valueOf(attribute.getContext(1)).filter(dMaterial.class);
            for (dMaterial material : ignoreList) {
                set.add(material.getMaterial());
            }
            attribute = attribute.fulfill(1);
        }
        return new dLocation(getLivingEntity().getTargetBlock(set, range).getLocation()).getAttribute(attribute);
    }
    // -->
    if (attribute.startsWith("location.standing_on")) {
        return new dLocation(entity.getLocation().clone().add(0, -0.5f, 0)).getAttribute(attribute.fulfill(2));
    }
    // -->
    if (attribute.startsWith("location")) {
        return new dLocation(entity.getLocation()).getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("velocity")) {
        return new dLocation(entity.getVelocity().toLocation(entity.getWorld())).getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("world")) {
        return new dWorld(entity.getWorld()).getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("can_pickup_items")) {
        if (isLivingEntity()) {
            return new Element(getLivingEntity().getCanPickupItems()).getAttribute(attribute.fulfill(1));
        }
    }
    // -->
    if (attribute.startsWith("fallingblock_material")) {
        return dMaterial.getMaterialFrom(((FallingBlock) entity).getMaterial()).getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("fall_distance")) {
        return new Element(entity.getFallDistance()).getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("fire_time")) {
        return new Duration(entity.getFireTicks() / 20).getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("on_fire")) {
        return new Element(entity.getFireTicks() > 0).getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("leash_holder") || attribute.startsWith("get_leash_holder")) {
        if (isLivingEntity() && getLivingEntity().isLeashed()) {
            return new dEntity(getLivingEntity().getLeashHolder()).getAttribute(attribute.fulfill(1));
        }
    }
    // -->
    if (attribute.startsWith("passenger") || attribute.startsWith("get_passenger")) {
        if (!entity.isEmpty()) {
            return new dEntity(entity.getPassenger()).getAttribute(attribute.fulfill(1));
        }
    }
    // -->
    if (attribute.startsWith("shooter") || attribute.startsWith("get_shooter")) {
        if (isProjectile() && hasShooter()) {
            return getShooter().getAttribute(attribute.fulfill(1));
        }
    }
    // -->
    if (attribute.startsWith("vehicle") || attribute.startsWith("get_vehicle")) {
        if (entity.isInsideVehicle()) {
            return new dEntity(entity.getVehicle()).getAttribute(attribute.fulfill(1));
        }
    }
    // -->
    if (attribute.startsWith("has_effect")) {
        boolean returnElement = false;
        if (attribute.hasContext(1)) {
            PotionEffectType effectType = PotionEffectType.getByName(attribute.getContext(1));
            for (org.bukkit.potion.PotionEffect effect : getLivingEntity().getActivePotionEffects()) {
                if (effect.getType().equals(effectType)) {
                    returnElement = true;
                }
            }
        } else if (!getLivingEntity().getActivePotionEffects().isEmpty()) {
            returnElement = true;
        }
        return new Element(returnElement).getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("can_breed")) {
        return new Element(((Ageable) getLivingEntity()).canBreed()).getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("breeding") || attribute.startsWith("is_breeding")) {
        return new Element(NMSHandler.getInstance().getEntityHelper().isBreeding((Animals) getLivingEntity())).getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("has_passenger")) {
        return new Element(!entity.isEmpty()).getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("empty") || attribute.startsWith("is_empty")) {
        return new Element(entity.isEmpty()).getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("inside_vehicle") || attribute.startsWith("is_inside_vehicle")) {
        return new Element(entity.isInsideVehicle()).getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("leashed") || attribute.startsWith("is_leashed")) {
        if (isLivingEntity()) {
            return new Element(getLivingEntity().isLeashed()).getAttribute(attribute.fulfill(1));
        } else {
            return Element.FALSE.getAttribute(attribute.fulfill(1));
        }
    }
    // -->
    if (attribute.startsWith("on_ground") || attribute.startsWith("is_on_ground")) {
        return new Element(entity.isOnGround()).getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("persistent") || attribute.startsWith("is_persistent")) {
        if (isLivingEntity()) {
            return new Element(!getLivingEntity().getRemoveWhenFarAway()).getAttribute(attribute.fulfill(1));
        } else {
            return Element.FALSE.getAttribute(attribute.fulfill(1));
        }
    }
    // -->
    if (attribute.startsWith("killer")) {
        return getPlayerFrom(getLivingEntity().getKiller()).getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("last_damage.amount")) {
        return new Element(getLivingEntity().getLastDamage()).getAttribute(attribute.fulfill(2));
    }
    // -->
    if (attribute.startsWith("last_damage.cause") && entity.getLastDamageCause() != null) {
        return new Element(entity.getLastDamageCause().getCause().name()).getAttribute(attribute.fulfill(2));
    }
    // -->
    if (attribute.startsWith("last_damage.duration")) {
        return new Duration((long) getLivingEntity().getNoDamageTicks()).getAttribute(attribute.fulfill(2));
    }
    // -->
    if (attribute.startsWith("oxygen.max")) {
        return new Duration((long) getLivingEntity().getMaximumAir()).getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("oxygen")) {
        return new Duration((long) getLivingEntity().getRemainingAir()).getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("remove_when_far")) {
        return new Element(getLivingEntity().getRemoveWhenFarAway()).getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("target")) {
        if (getBukkitEntity() instanceof Creature) {
            Entity target = ((Creature) getLivingEntity()).getTarget();
            if (target != null) {
                return new dEntity(target).getAttribute(attribute.fulfill(1));
            }
        }
        return null;
    }
    // -->
    if (attribute.startsWith("time_lived")) {
        return new Duration(entity.getTicksLived() / 20).getAttribute(attribute.fulfill(1));
    }
    // -->
    if ((attribute.startsWith("pickup_delay") || attribute.startsWith("pickupdelay")) && getBukkitEntity() instanceof Item) {
        return new Duration(((Item) getBukkitEntity()).getPickupDelay() * 20).getAttribute(attribute.fulfill(1));
    }
    // -->
    if (NMSHandler.getVersion().isAtLeast(NMSVersion.v1_9_R2) && attribute.startsWith("gliding")) {
        return new Element(getLivingEntity().isGliding()).getAttribute(attribute.fulfill(1));
    }
    // -->
    if (NMSHandler.getVersion().isAtLeast(NMSVersion.v1_9_R2) && attribute.startsWith("glowing")) {
        return new Element(getLivingEntity().isGlowing()).getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("is_living")) {
        return new Element(isLivingEntity()).getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("is_mob")) {
        if (!isPlayer() && !isNPC()) {
            return Element.TRUE.getAttribute(attribute.fulfill(1));
        } else {
            return Element.FALSE.getAttribute(attribute.fulfill(1));
        }
    }
    // -->
    if (attribute.startsWith("is_npc")) {
        return new Element(isCitizensNPC()).getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("is_player")) {
        return new Element(isPlayer()).getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("is_projectile")) {
        return new Element(isProjectile()).getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("tameable") || attribute.startsWith("is_tameable")) {
        return new Element(EntityTame.describes(this)).getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("ageable") || attribute.startsWith("is_ageable")) {
        return new Element(EntityAge.describes(this)).getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("colorable") || attribute.startsWith("is_colorable")) {
        return new Element(EntityColor.describes(this)).getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("experience") && getBukkitEntity() instanceof ExperienceOrb) {
        return new Element(((ExperienceOrb) getBukkitEntity()).getExperience()).getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("dragon_phase") && getBukkitEntity() instanceof EnderDragon) {
        return new Element(((EnderDragon) getLivingEntity()).getPhase().name()).getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("describe")) {
        String escript = getEntityScript();
        return new Element("e@" + (escript != null && escript.length() > 0 ? escript : getEntityType().getLowercaseName()) + PropertyParser.getPropertiesString(this)).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 Element(identify()).getAttribute(attribute);
}
Also used : FlagManager(net.aufdemrand.denizen.flags.FlagManager) Property(net.aufdemrand.denizencore.objects.properties.Property) Pattern(java.util.regex.Pattern) PotionEffectType(org.bukkit.potion.PotionEffectType) EntityHelper(net.aufdemrand.denizen.nms.interfaces.EntityHelper) Material(org.bukkit.Material) PotionEffect(org.bukkit.potion.PotionEffect)

Example 4 with FlagManager

use of net.aufdemrand.denizen.flags.FlagManager in project Denizen-For-Bukkit by DenizenScript.

the class dNPC method getAttribute.

@Override
public String getAttribute(Attribute attribute) {
    if (attribute == null) {
        return null;
    }
    // Defined in dEntity
    if (attribute.startsWith("is_npc")) {
        return Element.TRUE.getAttribute(attribute.fulfill(1));
    }
    // Defined in dEntity
    if (attribute.startsWith("location") && !isSpawned()) {
        return getLocation().getAttribute(attribute.fulfill(1));
    }
    // Defined in dEntity
    if (attribute.startsWith("eye_location")) {
        return getEyeLocation().getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("has_nickname")) {
        NPC citizen = getCitizen();
        return new Element(citizen.hasTrait(NicknameTrait.class) && citizen.getTrait(NicknameTrait.class).hasNickname()).getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("name.nickname")) {
        return new Element(getCitizen().hasTrait(NicknameTrait.class) ? getCitizen().getTrait(NicknameTrait.class).getNickname() : getName()).getAttribute(attribute.fulfill(2));
    }
    // -->
    if (attribute.startsWith("name")) {
        return new Element(getName()).getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("list_traits")) {
        List<String> list = new ArrayList<String>();
        for (Trait trait : getCitizen().getTraits()) {
            list.add(trait.getName());
        }
        return new dList(list).getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("has_trait")) {
        if (attribute.hasContext(1)) {
            Class<? extends Trait> trait = CitizensAPI.getTraitFactory().getTraitClass(attribute.getContext(1));
            if (trait != null) {
                return new Element(getCitizen().hasTrait(trait)).getAttribute(attribute.fulfill(1));
            }
        }
    }
    // -->
    if (attribute.startsWith("pushable") || attribute.startsWith("is_pushable")) {
        return new Element(getPushableTrait().isPushable()).getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("has_trigger") && attribute.hasContext(1)) {
        if (!getCitizen().hasTrait(TriggerTrait.class)) {
            return Element.FALSE.getAttribute(attribute.fulfill(1));
        }
        TriggerTrait trait = getCitizen().getTrait(TriggerTrait.class);
        return new Element(trait.hasTrigger(attribute.getContext(1))).getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("anchor.list") || attribute.startsWith("anchors.list")) {
        List<String> list = new ArrayList<String>();
        for (Anchor anchor : getCitizen().getTrait(Anchors.class).getAnchors()) {
            list.add(anchor.getName());
        }
        return new dList(list).getAttribute(attribute.fulfill(2));
    }
    // -->
    if (attribute.startsWith("has_anchors")) {
        return (new Element(getCitizen().getTrait(Anchors.class).getAnchors().size() > 0)).getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("anchor")) {
        if (attribute.hasContext(1) && getCitizen().getTrait(Anchors.class).getAnchor(attribute.getContext(1)) != null) {
            return new dLocation(getCitizen().getTrait(Anchors.class).getAnchor(attribute.getContext(1)).getLocation()).getAttribute(attribute.fulfill(1));
        }
    }
    // -->
    if (attribute.startsWith("has_flag")) {
        String flag_name;
        if (attribute.hasContext(1)) {
            flag_name = attribute.getContext(1);
        } else {
            return null;
        }
        return new Element(FlagManager.npcHasFlag(this, flag_name)).getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("flag")) {
        String flag_name;
        if (attribute.hasContext(1)) {
            flag_name = attribute.getContext(1);
        } else {
            return null;
        }
        if (attribute.getAttribute(2).equalsIgnoreCase("is_expired") || attribute.startsWith("isexpired")) {
            return new Element(!FlagManager.npcHasFlag(this, flag_name)).getAttribute(attribute.fulfill(2));
        }
        if (attribute.getAttribute(2).equalsIgnoreCase("size") && !FlagManager.npcHasFlag(this, flag_name)) {
            return new Element(0).getAttribute(attribute.fulfill(2));
        }
        if (FlagManager.npcHasFlag(this, flag_name)) {
            FlagManager.Flag flag = DenizenAPI.getCurrentInstance().flagManager().getNPCFlag(getId(), flag_name);
            return new dList(flag.toString(), true, flag.values()).getAttribute(attribute.fulfill(1));
        }
        return new Element(identify()).getAttribute(attribute);
    }
    // -->
    if (attribute.startsWith("list_flags")) {
        dList allFlags = new dList(DenizenAPI.getCurrentInstance().flagManager().listNPCFlags(getId()));
        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("constant")) {
        if (attribute.hasContext(1)) {
            if (getCitizen().hasTrait(ConstantsTrait.class) && getCitizen().getTrait(ConstantsTrait.class).getConstant(attribute.getContext(1)) != null) {
                return new Element(getCitizen().getTrait(ConstantsTrait.class).getConstant(attribute.getContext(1))).getAttribute(attribute.fulfill(1));
            } else {
                return null;
            }
        }
    }
    // -->
    if (attribute.startsWith("has_pose")) {
        if (attribute.hasContext(1)) {
            return new Element(getCitizen().getTrait(Poses.class).hasPose(attribute.getContext(1))).getAttribute(attribute.fulfill(1));
        } else {
            return null;
        }
    }
    // -->
    if (attribute.startsWith("pose") || attribute.startsWith("get_pose")) {
        if (attribute.hasContext(1)) {
            Pose pose = getCitizen().getTrait(Poses.class).getPose(attribute.getContext(1));
            return new dLocation(org.bukkit.Bukkit.getWorlds().get(0), 0, 0, 0, pose.getYaw(), pose.getPitch()).getAttribute(attribute.fulfill(1));
        } else {
            return null;
        }
    }
    // -->
    if (attribute.startsWith("engaged") || attribute.startsWith("is_engaged")) {
        return new Element(isEngaged()).getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("invulnerable") || attribute.startsWith("vulnerable")) {
        return new Element(getCitizen().data().get(NPC.DEFAULT_PROTECTED_METADATA, true)).getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("id")) {
        return new Element(getId()).getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("owner")) {
        String owner = getOwner();
        dPlayer player = null;
        if (!owner.equalsIgnoreCase("server")) {
            player = dPlayer.valueOfInternal(owner, false);
        }
        if (player != null) {
            return player.getAttribute(attribute.fulfill(1));
        } else {
            return new Element(owner).getAttribute(attribute.fulfill(1));
        }
    }
    // -->
    if (attribute.startsWith("has_skin")) {
        return new Element(getCitizen().data().has(NPC.PLAYER_SKIN_UUID_METADATA)).getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("skin_blob")) {
        if (getCitizen().data().has(NPC.PLAYER_SKIN_TEXTURE_PROPERTIES_METADATA)) {
            String tex = getCitizen().data().get(NPC.PLAYER_SKIN_TEXTURE_PROPERTIES_METADATA).toString();
            String sign = "";
            if (getCitizen().data().has(NPC.PLAYER_SKIN_TEXTURE_PROPERTIES_SIGN_METADATA)) {
                sign = ";" + getCitizen().data().get(NPC.PLAYER_SKIN_TEXTURE_PROPERTIES_SIGN_METADATA).toString();
            }
            return new Element(tex + sign).getAttribute(attribute.fulfill(1));
        }
    }
    // -->
    if (attribute.startsWith("skin")) {
        if (getCitizen().data().has(NPC.PLAYER_SKIN_UUID_METADATA)) {
            return new Element(getCitizen().data().get(NPC.PLAYER_SKIN_UUID_METADATA).toString()).getAttribute(attribute.fulfill(1));
        }
    }
    // -->
    if (attribute.startsWith("inventory")) {
        return getDenizenInventory().getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("is_spawned")) {
        return new Element(isSpawned()).getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("is_protected")) {
        return new Element(getCitizen().isProtected()).getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("lookclose")) {
        NPC citizen = getCitizen();
        if (citizen.hasTrait(LookClose.class)) {
            // There is no method to check if the NPC has LookClose enabled...
            // LookClose.toString() returns "LookClose{" + enabled + "}"
            String lookclose = citizen.getTrait(LookClose.class).toString();
            lookclose = lookclose.substring(10, lookclose.length() - 1);
            return new Element(Boolean.valueOf(lookclose)).getAttribute(attribute.fulfill(1));
        }
        return Element.FALSE.getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("location.previous_location")) {
        return (NPCTags.previousLocations.containsKey(getId()) ? NPCTags.previousLocations.get(getId()).getAttribute(attribute.fulfill(2)) : null);
    }
    // -->
    if (attribute.startsWith("teleport_on_stuck")) {
        return new Element(getNavigator().getDefaultParameters().stuckAction() == TeleportStuckAction.INSTANCE).getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("has_script")) {
        NPC citizen = getCitizen();
        return new Element(citizen.hasTrait(AssignmentTrait.class) && citizen.getTrait(AssignmentTrait.class).hasAssignment()).getAttribute(attribute.fulfill(1));
    }
    // -->
    if (attribute.startsWith("script")) {
        NPC citizen = getCitizen();
        if (!citizen.hasTrait(AssignmentTrait.class) || !citizen.getTrait(AssignmentTrait.class).hasAssignment()) {
            return null;
        } else {
            return new dScript(citizen.getTrait(AssignmentTrait.class).getAssignment().getName()).getAttribute(attribute.fulfill(1));
        }
    }
    // -->
    if (attribute.startsWith("navigator.is_navigating")) {
        return new Element(getNavigator().isNavigating()).getAttribute(attribute.fulfill(2));
    }
    // -->
    if (attribute.startsWith("navigator.speed")) {
        return new Element(getNavigator().getLocalParameters().speed()).getAttribute(attribute.fulfill(2));
    }
    // -->
    if (attribute.startsWith("navigator.range")) {
        return new Element(getNavigator().getLocalParameters().range()).getAttribute(attribute.fulfill(2));
    }
    // -->
    if (attribute.startsWith("navigator.attack_range")) {
        return new Element(getNavigator().getLocalParameters().attackRange()).getAttribute(attribute.fulfill(2));
    }
    // -->
    if (attribute.startsWith("navigator.attack_strategy")) {
        return new Element(getNavigator().getLocalParameters().attackStrategy().toString()).getAttribute(attribute.fulfill(2));
    }
    // -->
    if (attribute.startsWith("navigator.speed_modifier")) {
        return new Element(getNavigator().getLocalParameters().speedModifier()).getAttribute(attribute.fulfill(2));
    }
    // -->
    if (attribute.startsWith("navigator.base_speed")) {
        return new Element(getNavigator().getLocalParameters().baseSpeed()).getAttribute(attribute.fulfill(2));
    }
    // -->
    if (attribute.startsWith("navigator.avoid_water")) {
        return new Element(getNavigator().getLocalParameters().avoidWater()).getAttribute(attribute.fulfill(2));
    }
    // -->
    if (attribute.startsWith("navigator.target_location")) {
        return (getNavigator().getTargetAsLocation() != null ? new dLocation(getNavigator().getTargetAsLocation()).getAttribute(attribute.fulfill(2)) : null);
    }
    // -->
    if (attribute.startsWith("navigator.is_fighting")) {
        return new Element(getNavigator().getEntityTarget() != null && getNavigator().getEntityTarget().isAggressive()).getAttribute(attribute.fulfill(2));
    }
    // -->
    if (attribute.startsWith("navigator.target_type")) // TODO: IMPROVE
    {
        return new Element(getNavigator().getTargetType() == null ? "null" : getNavigator().getTargetType().toString()).getAttribute(attribute.fulfill(2));
    }
    // -->
    if (attribute.startsWith("navigator.target_entity")) {
        return (getNavigator().getEntityTarget() != null && getNavigator().getEntityTarget().getTarget() != null ? new dEntity(getNavigator().getEntityTarget().getTarget()).getAttribute(attribute.fulfill(2)) : null);
    }
    // -->
    if (attribute.startsWith("type")) {
        return new Element("NPC").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 (getEntity() != null ? new dEntity(this).getAttribute(attribute) : new Element(identify()).getAttribute(attribute));
}
Also used : NPC(net.citizensnpcs.api.npc.NPC) ArrayList(java.util.ArrayList) FlagManager(net.aufdemrand.denizen.flags.FlagManager) Pose(net.citizensnpcs.util.Pose) Property(net.aufdemrand.denizencore.objects.properties.Property) Pattern(java.util.regex.Pattern) Anchors(net.citizensnpcs.trait.Anchors) Anchor(net.citizensnpcs.util.Anchor) Poses(net.citizensnpcs.trait.Poses) LookClose(net.citizensnpcs.trait.LookClose) Trait(net.citizensnpcs.api.trait.Trait)

Example 5 with FlagManager

use of net.aufdemrand.denizen.flags.FlagManager in project Denizen-For-Bukkit by DenizenScript.

the class FlaggedRequirement method check.

@Override
public boolean check(RequirementsContext context, List<String> args) throws RequirementCheckException {
    boolean outcome = false;
    String name = null;
    String value = "true";
    String index = "";
    Type type = Type.PLAYER;
    for (String arg : args) {
        if (aH.matchesArg("GLOBAL, NPC, DENIZEN, GLOBAL", arg)) {
            type = Type.valueOf(arg.toUpperCase().replace("DENIZEN", "NPC"));
        } else if (arg.split(":", 2).length > 1) {
            String[] flagArgs = arg.split(":");
            value = flagArgs[1].toUpperCase();
            if (flagArgs[0].contains("[")) {
                name = flagArgs[0].split("\\[", 2)[0].trim();
                index = flagArgs[0].split("\\[", 2)[1].split("\\]", 2)[0].trim();
            } else {
                name = flagArgs[0].toUpperCase();
            }
        } else {
            name = arg.toUpperCase();
        }
    }
    FlagManager flagMng = DenizenAPI.getCurrentInstance().flagManager();
    FlagManager.Flag flag = null;
    switch(type) {
        case NPC:
            flag = flagMng.getNPCFlag(context.getNPC().getId(), name);
            break;
        case PLAYER:
            flag = flagMng.getPlayerFlag(context.getPlayer(), name);
            break;
        case GLOBAL:
            flag = flagMng.getGlobalFlag(name);
            break;
    }
    if (index.length() == 0) {
        if (flag.getLast().asString().equalsIgnoreCase(value)) {
            outcome = true;
        } else {
            dB.echoDebug(context.getScriptContainer(), "... does not match '" + flag.getLast().asString() + "'.");
        }
    } else if (index.matches("\\d+")) {
        if (flag.get(Integer.parseInt(index)).asString().equalsIgnoreCase(value)) {
            outcome = true;
        } else {
            dB.echoDebug(context.getScriptContainer(), "... does not match '" + flag.get(Integer.parseInt(index)).asString() + "'.");
        }
    }
    return outcome;
}
Also used : FlagManager(net.aufdemrand.denizen.flags.FlagManager)

Aggregations

FlagManager (net.aufdemrand.denizen.flags.FlagManager)7 Pattern (java.util.regex.Pattern)4 Property (net.aufdemrand.denizencore.objects.properties.Property)3 ArrayList (java.util.ArrayList)2 net.aufdemrand.denizen.objects.dEntity (net.aufdemrand.denizen.objects.dEntity)2 net.aufdemrand.denizen.objects.dNPC (net.aufdemrand.denizen.objects.dNPC)2 net.aufdemrand.denizen.objects.dPlayer (net.aufdemrand.denizen.objects.dPlayer)2 NPC (net.citizensnpcs.api.npc.NPC)2 Player (org.bukkit.entity.Player)2 PotionEffect (org.bukkit.potion.PotionEffect)2 File (java.io.File)1 Connection (java.sql.Connection)1 SQLException (java.sql.SQLException)1 Date (java.util.Date)1 Map (java.util.Map)1 UUID (java.util.UUID)1 Flag (net.aufdemrand.denizen.flags.FlagManager.Flag)1 ImprovedOfflinePlayer (net.aufdemrand.denizen.nms.abstracts.ImprovedOfflinePlayer)1 Sidebar (net.aufdemrand.denizen.nms.abstracts.Sidebar)1 EntityHelper (net.aufdemrand.denizen.nms.interfaces.EntityHelper)1