@EventHandler
public void serverTag(ReplaceableTagEvent event) {
if (!event.matches("server", "svr", "global") || event.replaced()) return;
Attribute attribute =
new Attribute(event.raw_tag, event.getScriptEntry()).fulfill(1);
// <--[tag]
// @attribute <server.has_flag[<flag_name>]>
// @returns Element(boolean)
// @description
// returns true if the server has the specified flag, otherwise returns false.
// -->
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)));
}
// <--[tag]
// @attribute <server.flag[<name>]>
// @returns Flag dList
// @description
// returns the specified flag from the server.
// -->
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))
event.setReplaced(new dList(DenizenAPI.getCurrentInstance().flagManager()
.getGlobalFlag(flag_name))
.getAttribute(attribute));
return;
}
// <--[tag]
// @attribute <server.list_materials>
// @returns dList
// @description
// Returns a list of all materials known to the server (only their Bukkit enum names).
// -->
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)));
}
// <--[tag]
// @attribute <server.list_flags[(regex:)<search>]>
// @returns dList
// @description
// Returns a list of the server's flag names, with an optional search for
// names containing a certain pattern.
// -->
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).toLowerCase();
if (search.startsWith("regex:")) {
String regex = search.substring(6);
try {
Pattern pattern = Pattern.compile(search.substring(6));
for (String flag : allFlags)
if (pattern.matcher(flag).matches())
searchFlags.add(flag);
} catch (Exception e) {
dB.echoError(e);
}
}
else {
for (String flag : allFlags)
if (flag.toLowerCase().contains(search))
searchFlags.add(flag);
}
}
event.setReplaced(searchFlags == null ? allFlags.getAttribute(attribute.fulfill(1))
: searchFlags.getAttribute(attribute.fulfill(1)));
}
// <--[tag]
// @attribute <server.current_time_millis>
// @returns Element(Number)
// @description
// Returns the number of milliseconds since Jan 1, 1970.
// -->
if (attribute.startsWith("current_time_millis")) {
event.setReplaced(new Element(System.currentTimeMillis())
.getAttribute(attribute.fulfill(1)));
}
// <--[tag]
// @attribute <server.has_event[<event_name>]>
// @returns Element(Number)
// @description
// Returns whether a world event exists on the server.
// This tag will ignore dObject identifiers (see <@link language dobject>).
// -->
if (attribute.startsWith("has_event")
&& attribute.hasContext(1)) {
event.setReplaced(new Element(EventManager.eventExists(attribute.getContext(1))
|| EventManager.eventExists(EventManager.StripIdentifiers(attribute.getContext(1))))
.getAttribute(attribute.fulfill(1)));
}
// <--[tag]
// @attribute <server.get_event_handlers[<event_name>]>
// @returns dList<dScript>
// @description
// Returns a list of all world scripts that will handle a given event name.
// This tag will ignore dObject identifiers (see <@link language dobject>).
// For use with <@link tag server.has_event[<event_name>]>
// -->
if (attribute.startsWith("get_event_handlers")
&& attribute.hasContext(1)) {
String eventName = attribute.getContext(1).toUpperCase();
List<WorldScriptContainer> EventsOne = EventManager.events.get("ON " + eventName);
List<WorldScriptContainer> EventsTwo = EventManager.events.get("ON " + EventManager.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)));
}
}
// <--[tag]
// @attribute <server.selected_npc>
// @returns dNPC
// @description
// Returns the server's currently selected NPC.
// -->
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;
}
// <--[tag]
// @attribute <server.get_npcs_named[<name>]>
// @returns dList(dNPC)
// @description
// Returns a list of NPCs with a certain name.
// -->
if (attribute.startsWith("get_npcs_named") && Depends.citizens != null && attribute.hasContext(1)) {
ArrayList<dNPC> npcs = new ArrayList<dNPC>();
for (NPC npc : CitizensAPI.getNPCRegistry())
if (npc.getName().equalsIgnoreCase(attribute.getContext(1)))
npcs.add(dNPC.mirrorCitizensNPC(npc));
event.setReplaced(new dList(npcs).getAttribute(attribute.fulfill(1)));
return;
}
// <--[tag]
// @attribute <server.has_file[<name>]>
// @returns Element(Boolean)
// @description
// Returns true if the specified file exists. The starting path is /plugins/Denizen.
// -->
if (attribute.startsWith("has_file") && attribute.hasContext(1)) {
event.setReplaced(new Element(new File(DenizenAPI.getCurrentInstance().getDataFolder(),
attribute.getContext(1)).exists()).getAttribute(attribute.fulfill(1)));
return;
}
// <--[tag]
// @attribute <server.denizen_version>
// @returns Element
// @description
// Returns the version of Denizen currently being used.
// -->
if (attribute.startsWith("denizen_version")) {
event.setReplaced(new Element(DenizenAPI.getCurrentInstance().getDescription().getVersion())
.getAttribute(attribute.fulfill(1)));
return;
}
// <--[tag]
// @attribute <server.bukkit_version>
// @returns Element
// @description
// Returns the version of Bukkit currently being used.
// -->
if (attribute.startsWith("bukkit_version")) {
event.setReplaced(new Element(Bukkit.getBukkitVersion())
.getAttribute(attribute.fulfill(1)));
return;
}
// <--[tag]
// @attribute <server.version>
// @returns Element
// @description
// Returns the version string of the server.
// -->
if (attribute.startsWith("version")) {
event.setReplaced(new Element(Bukkit.getServer().getVersion())
.getAttribute(attribute.fulfill(1)));
return;
}
// <--[tag]
// @attribute <server.java_version>
// @returns Element
// @description
// Returns the current Java version of the server.
// -->
if (attribute.startsWith("java_version")) {
event.setReplaced(new Element(System.getProperty("java.version"))
.getAttribute(attribute.fulfill(1)));
return;
}
// <--[tag]
// @attribute <server.max_players>
// @returns Element(Number)
// @description
// Returns the maximum number of players allowed on the server.
// -->
if (attribute.startsWith("max_players")) {
event.setReplaced(new Element(Bukkit.getServer().getMaxPlayers())
.getAttribute(attribute.fulfill(1)));
return;
}
// <--[tag]
// @attribute <server.list_sql_connections>
// @returns dList
// @description
// Returns a list of all SQL connections opened by <@link command sql>.
// -->
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;
}
// <--[tag]
// @attribute <server.list_permission_groups>
// @returns dList
// @description
// Returns a list of all permission groups on the server.
// -->
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;
}
// <--[tag]
// @attribute <server.list_plugin_names>
// @returns dList
// @description
// Gets a list of currently enabled plugin names from the server.
// -->
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;
}
// <--[tag]
// @attribute <server.list_scripts>
// @returns dList(dScript)
// @description
// Gets a list of all scripts currently loaded into Denizen.
// -->
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;
}
// <--[tag]
// @attribute <server.match_player[<name>]>
// @returns dPlayer
// @description
// Returns the online player that best matches the input name.
// EG, in a group of 'bo', 'bob', and 'bobby'... input 'bob' returns p@bob,
// input 'bobb' returns p@bobby, and input 'b' returns p@bo.
// -->
if (attribute.startsWith("match_player") && attribute.hasContext(1)) {
Player matchPlayer = null;
String matchInput = attribute.getContext(1).toLowerCase();
for (Player player: Bukkit.getOnlinePlayers()) {
if (player.getName().toLowerCase().equals(matchInput)) {
matchPlayer = player;
break;
}
else if (player.getName().toLowerCase().contains(matchInput) && matchPlayer == null) {
matchPlayer = player;
}
}
if (matchPlayer == null) {
event.setReplaced("null");
} else {
event.setReplaced(new dPlayer(matchPlayer).getAttribute(attribute.fulfill(1)));
}
return;
}
// <--[tag]
// @attribute <server.get_npcs_assigned[<assignment_script>]>
// @returns dList(dNPC)
// @description
// Returns a list of all NPCs assigned to a specified script.
// -->
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 {
ArrayList<dNPC> npcs = new ArrayList<dNPC>();
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));
}
event.setReplaced(new dList(npcs).getAttribute(attribute.fulfill(1)));
return;
}
}
// <--[tag]
// @attribute <server.get_online_players_flagged[<flag_name>]>
// @returns dList(dPlayer)
// @description
// Returns a list of all online players with a specified flag set.
// -->
if (attribute.startsWith("get_online_players_flagged")
&& attribute.hasContext(1)) {
String flag = attribute.getContext(1);
ArrayList<dPlayer> players = new ArrayList<dPlayer>();
for (Player player: Bukkit.getOnlinePlayers()) {
if (DenizenAPI.getCurrentInstance().flagManager().getPlayerFlag(new dPlayer(player), flag).size() > 0)
players.add(new dPlayer(player));
}
event.setReplaced(new dList(players).getAttribute(attribute.fulfill(1)));
return;
}
// <--[tag]
// @attribute <server.get_players_flagged[<flag_name>]>
// @returns dList(dPlayer)
// @description
// Returns a list of all players with a specified flag set.
// -->
if (attribute.startsWith("get_players_flagged")
&& attribute.hasContext(1)) {
String flag = attribute.getContext(1);
ArrayList<dPlayer> players = new ArrayList<dPlayer>();
for (Map.Entry<String, UUID> entry : dPlayer.getAllPlayers().entrySet()) {
if (DenizenAPI.getCurrentInstance().flagManager().getPlayerFlag(entry.getValue(), flag).size() > 0)
players.add(new dPlayer(entry.getValue()));
}
event.setReplaced(new dList(players).getAttribute(attribute.fulfill(1)));
return;
}
// <--[tag]
// @attribute <server.get_spawned_npcs_flagged[<flag_name>]>
// @returns dList(dNPC)
// @description
// Returns a list of all spawned NPCs with a specified flag set.
// -->
if (attribute.startsWith("get_spawned_npcs_flagged") && Depends.citizens != null
&& attribute.hasContext(1)) {
String flag = attribute.getContext(1);
ArrayList<dNPC> npcs = new ArrayList<dNPC>();
for (NPC npc : CitizensAPI.getNPCRegistry()) {
dNPC dNpc = dNPC.mirrorCitizensNPC(npc);
if (dNpc.isSpawned() && FlagManager.npcHasFlag(dNpc, flag))
npcs.add(dNpc);
}
event.setReplaced(new dList(npcs).getAttribute(attribute.fulfill(1)));
return;
}
// <--[tag]
// @attribute <server.get_npcs_flagged[<flag_name>]>
// @returns dList(dNPC)
// @description
// Returns a list of all NPCs with a specified flag set.
// -->
if (attribute.startsWith("get_npcs_flagged") && Depends.citizens != null
&& attribute.hasContext(1)) {
String flag = attribute.getContext(1);
ArrayList<dNPC> npcs = new ArrayList<dNPC>();
for (NPC npc : CitizensAPI.getNPCRegistry()) {
dNPC dNpc = dNPC.mirrorCitizensNPC(npc);
if (FlagManager.npcHasFlag(dNpc, flag))
npcs.add(dNpc);
}
event.setReplaced(new dList(npcs).getAttribute(attribute.fulfill(1)));
return;
}
// <--[tag]
// @attribute <server.list_npcs>
// @returns dList(dNPC)
// @description
// Returns a list of all NPCs.
// -->
if (attribute.startsWith("list_npcs") && Depends.citizens != null) {
ArrayList<dNPC> npcs = new ArrayList<dNPC>();
for (NPC npc : CitizensAPI.getNPCRegistry())
npcs.add(dNPC.mirrorCitizensNPC(npc));
event.setReplaced(new dList(npcs).getAttribute(attribute.fulfill(1)));
return;
}
// <--[tag]
// @attribute <server.list_worlds>
// @returns dList(dWorld)
// @description
// Returns a list of all worlds.
// -->
if (attribute.startsWith("list_worlds")) {
ArrayList<dWorld> worlds = new ArrayList<dWorld>();
for (World world : Bukkit.getWorlds())
worlds.add(dWorld.mirrorBukkitWorld(world));
event.setReplaced(new dList(worlds).getAttribute(attribute.fulfill(1)));
return;
}
// <--[tag]
// @attribute <server.list_plugins>
// @returns dList(dPlugin)
// @description
// Gets a list of currently enabled dPlugins from the server.
// -->
if (attribute.startsWith("list_plugins")) {
ArrayList<dPlugin> plugins = new ArrayList<dPlugin>();
for (Plugin plugin : Bukkit.getServer().getPluginManager().getPlugins())
plugins.add(new dPlugin(plugin));
event.setReplaced(new dList(plugins).getAttribute(attribute.fulfill(1)));
return;
}
// <--[tag]
// @attribute <server.list_players>
// @returns dList(dPlayer)
// @description
// Returns a list of all players that have ever played on the server, online or not.
// -->
if (attribute.startsWith("list_players")) {
ArrayList<dPlayer> players = new ArrayList<dPlayer>();
for (OfflinePlayer player : Bukkit.getOfflinePlayers())
players.add(dPlayer.mirrorBukkitPlayer(player));
event.setReplaced(new dList(players).getAttribute(attribute.fulfill(1)));
return;
}
// <--[tag]
// @attribute <server.list_online_players>
// @returns dList(dPlayer)
// @description
// Returns a list of all online players.
// -->
if (attribute.startsWith("list_online_players")) {
ArrayList<dPlayer> players = new ArrayList<dPlayer>();
for (Player player : Bukkit.getOnlinePlayers())
players.add(dPlayer.mirrorBukkitPlayer(player));
event.setReplaced(new dList(players).getAttribute(attribute.fulfill(1)));
return;
}
// <--[tag]
// @attribute <server.list_offline_players>
// @returns dList(dPlayer)
// @description
// Returns a list of all offline players.
// -->
if (attribute.startsWith("list_offline_players")) {
ArrayList<dPlayer> players = new ArrayList<dPlayer>();
for (OfflinePlayer player : Bukkit.getOfflinePlayers())
if (!player.isOnline()) players.add(dPlayer.mirrorBukkitPlayer(player));
event.setReplaced(new dList(players).getAttribute(attribute.fulfill(1)));
return;
}
// <--[tag]
// @attribute <server.list_ops>
// @returns dList(dPlayer)
// @description
// Returns a list of all ops, online or not.
// -->
if (attribute.startsWith("list_ops")) {
ArrayList<dPlayer> players = new ArrayList<dPlayer>();
for (OfflinePlayer player : Bukkit.getOfflinePlayers())
if (player.isOp()) players.add(dPlayer.mirrorBukkitPlayer(player));
event.setReplaced(new dList(players).getAttribute(attribute.fulfill(1)));
return;
}
// <--[tag]
// @attribute <server.list_online_ops>
// @returns dList(dPlayer)
// @description
// Returns a list of all online ops.
// -->
if (attribute.startsWith("list_online_ops")) {
ArrayList<dPlayer> players = new ArrayList<dPlayer>();
for (Player player : Bukkit.getOnlinePlayers())
if (player.isOp()) players.add(dPlayer.mirrorBukkitPlayer(player));
event.setReplaced(new dList(players).getAttribute(attribute.fulfill(1)));
return;
}
// <--[tag]
// @attribute <server.list_offline_ops>
// @returns dList(dPlayer)
// @description
// Returns a list of all offline ops.
// -->
if (attribute.startsWith("list_offline_ops")) {
ArrayList<dPlayer> players = new ArrayList<dPlayer>();
for (OfflinePlayer player : Bukkit.getOfflinePlayers())
if (player.isOp() && !player.isOnline()) players.add(dPlayer.mirrorBukkitPlayer(player));
event.setReplaced(new dList(players).getAttribute(attribute.fulfill(1)));
return;
}
// <--[tag]
// @attribute <server.motd>
// @returns Element
// @description
// Returns the server's current MOTD
// -->
if (attribute.startsWith("motd")) {
event.setReplaced(new Element(Bukkit.getServer().getMotd()).getAttribute(attribute.fulfill(1)));
return;
}
// TODO: Add everything else from Bukkit.getServer().*
}