use of net.aufdemrand.denizen.tags.BukkitTagContext 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;
}
}
use of net.aufdemrand.denizen.tags.BukkitTagContext in project Denizen-For-Bukkit by DenizenScript.
the class Denizen method handleCustomArgs.
@Override
public boolean handleCustomArgs(ScriptEntry scriptEntry, aH.Argument arg, boolean if_ignore) {
// Fill player/off-line player
if (arg.matchesPrefix("player") && !if_ignore) {
dB.echoDebug(scriptEntry, "...replacing the linked player with " + arg.getValue());
String value = TagManager.tag(arg.getValue(), new BukkitTagContext(scriptEntry, false));
dPlayer player = dPlayer.valueOf(value);
if (player == null || !player.isValid()) {
dB.echoError(scriptEntry.getResidingQueue(), value + " is an invalid player!");
}
((BukkitScriptEntryData) scriptEntry.entryData).setPlayer(player);
return true;
} else // Fill NPCID/NPC argument
if (arg.matchesPrefix("npc, npcid") && !if_ignore) {
dB.echoDebug(scriptEntry, "...replacing the linked NPC with " + arg.getValue());
String value = TagManager.tag(arg.getValue(), new BukkitTagContext(scriptEntry, false));
dNPC npc = dNPC.valueOf(value);
if (npc == null || !npc.isValid()) {
dB.echoError(scriptEntry.getResidingQueue(), value + " is an invalid NPC!");
return false;
}
((BukkitScriptEntryData) scriptEntry.entryData).setNPC(npc);
return true;
}
return false;
}
use of net.aufdemrand.denizen.tags.BukkitTagContext 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().*
}
use of net.aufdemrand.denizen.tags.BukkitTagContext in project Denizen-For-Bukkit by DenizenScript.
the class ChatTrigger method process.
// Technically defined in TriggerTrait, but placing here instead.
// <--[action]
// @Actions
// chat
//
// @Triggers when a player chats to the NPC.
//
// @Context
// <context.message> returns the triggering message
// <context.keyword> returns the keyword matched by a RegEx trigger
//
// @Determine
// "CANCELLED" to stop the player from chatting.
// Element to change the message.
//
// -->
public ChatContext process(Player player, String message) {
// Check if there is an NPC within range of a player to chat to.
dNPC npc = Utilities.getClosestNPC_ChatTrigger(player.getLocation(), 25);
dPlayer denizenPlayer = dPlayer.mirrorBukkitPlayer(player);
if (HyperDebug) {
dB.log("Processing chat trigger: valid npc? " + (npc != null));
}
// No NPC? Nothing else to do here.
if (npc == null) {
return new ChatContext(false);
}
if (HyperDebug) {
dB.log("Has trait? " + npc.getCitizen().hasTrait(TriggerTrait.class));
}
// just return false.
if (!npc.getCitizen().hasTrait(TriggerTrait.class)) {
return new ChatContext(false);
}
if (HyperDebug) {
dB.log("enabled? " + npc.getCitizen().getTrait(TriggerTrait.class).isEnabled(name));
}
if (!npc.getCitizen().getTrait(TriggerTrait.class).isEnabled(name)) {
return new ChatContext(false);
}
// Check range
if (npc.getTriggerTrait().getRadius(name) < npc.getLocation().distance(player.getLocation())) {
if (HyperDebug) {
dB.log("Not in range");
}
return new ChatContext(false);
}
if (Settings.chatMustSeeNPC()) {
if (!player.hasLineOfSight(npc.getEntity())) {
if (HyperDebug) {
dB.log("no LOS");
}
return new ChatContext(false);
}
}
if (Settings.chatMustLookAtNPC()) {
if (!NMSHandler.getInstance().getEntityHelper().isFacingEntity(player, npc.getEntity(), 45)) {
if (HyperDebug) {
dB.log("Not facing");
}
return new ChatContext(false);
}
}
Boolean ret = false;
// Denizen should be good to interact with. Let's get the script.
InteractScriptContainer script = npc.getInteractScript(denizenPlayer, ChatTrigger.class);
Map<String, dObject> context = new HashMap<String, dObject>();
context.put("message", new Element(message));
//
// Fire the Actions!
//
// If engaged or not cool, calls On Unavailable, if cool, calls On Chat
// If available (not engaged, and cool) sets cool down and returns true.
TriggerTrait.TriggerContext trigger = npc.getTriggerTrait().trigger(ChatTrigger.this, denizenPlayer, context);
// Return false if determine cancelled
if (trigger.hasDetermination()) {
if (trigger.getDetermination().equalsIgnoreCase("cancelled")) {
if (HyperDebug) {
dB.log("Cancelled");
}
// Mark as handled, the event will cancel.
return new ChatContext(true);
}
}
// Return false if trigger was unable to fire
if (!trigger.wasTriggered()) {
// through. Check the Settings if this is enabled.
if (Settings.chatGloballyIfUninteractable()) {
dB.echoDebug(script, ChatColor.YELLOW + "Resuming. " + ChatColor.WHITE + "The NPC is currently cooling down or engaged.");
return new ChatContext(false);
} else {
ret = true;
}
}
// Debugger
dB.report(script, name, aH.debugObj("Player", player.getName()) + aH.debugObj("NPC", npc.toString()) + aH.debugObj("Radius(Max)", npc.getLocation().distance(player.getLocation()) + "(" + npc.getTriggerTrait().getRadius(name) + ")") + aH.debugObj("Trigger text", message) + aH.debugObj("LOS", String.valueOf(player.hasLineOfSight(npc.getEntity()))) + aH.debugObj("Facing", String.valueOf(NMSHandler.getInstance().getEntityHelper().isFacingEntity(player, npc.getEntity(), 45))));
// Change the text if it's in the determination
if (trigger.hasDetermination()) {
message = trigger.getDetermination();
}
if (script == null) {
if (HyperDebug) {
dB.log("null script");
}
return new ChatContext(message, false);
}
// Check if the NPC has Chat Triggers for this step.
if (!script.containsTriggerInStep(InteractScriptHelper.getCurrentStep(denizenPlayer, script.getName()), ChatTrigger.class)) {
// it has no chat triggers for this step
if (npc.getCitizen().hasTrait(ChatbotTrait.class)) {
Utilities.talkToNPC(message, denizenPlayer, npc, Settings.chatToNpcOverhearingRange());
npc.getCitizen().getTrait(ChatbotTrait.class).chatTo(player, message);
if (HyperDebug) {
dB.log("chatbot");
}
return new ChatContext(false);
} else // No chat trigger for this step.. do we chat globally, or to the NPC?
if (!Settings.chatGloballyIfNoChatTriggers()) {
dB.echoDebug(script, player.getName() + " says to " + npc.getNicknameTrait().getNickname() + ", " + message);
return new ChatContext(false);
} else {
if (HyperDebug) {
dB.log("No trigger in step, chatting globally");
}
return new ChatContext(message, ret);
}
}
// Parse the script and match Triggers.. if found, cancel the text! The
// parser will take care of everything else.
String id = null;
boolean matched = false;
String replacementText = null;
String regexId = null;
String regexMessage = null;
// Use TreeMap to sort chat triggers alphabetically
TreeMap<String, String> idMap = new TreeMap<String, String>();
idMap.putAll(script.getIdMapFor(ChatTrigger.class, denizenPlayer));
if (!idMap.isEmpty()) {
// Iterate through the different id entries in the step's chat trigger
List<Map.Entry<String, String>> entries = new ArrayList<Map.Entry<String, String>>(idMap.entrySet());
Collections.sort(entries, new Comparator<Map.Entry<String, String>>() {
@Override
public int compare(Map.Entry<String, String> o1, Map.Entry<String, String> o2) {
if (o1 == null || o2 == null) {
return 0;
}
return o1.getKey().compareToIgnoreCase(o2.getKey());
}
});
for (Map.Entry<String, String> entry : entries) {
// Check if the chat trigger specified in the specified id's 'trigger:' key
// matches the text the player has said
// TODO: script arg?
String triggerText = TagManager.tag(entry.getValue(), new BukkitTagContext(denizenPlayer, npc, false, null, false, null));
Matcher matcher = triggerPattern.matcher(triggerText);
while (matcher.find()) {
if (!script.checkSpecificTriggerScriptRequirementsFor(ChatTrigger.class, denizenPlayer, npc, entry.getKey())) {
continue;
}
// TODO: script arg?
String keyword = TagManager.tag(matcher.group().replace("/", ""), new BukkitTagContext(denizenPlayer, npc, false, null, false, null));
String[] split = keyword.split("\\\\\\+REPLACE:", 2);
String replace = null;
if (split.length == 2) {
keyword = split[0];
replace = split[1];
}
// match already (thus using alphabetical priority for triggers)
if (regexId == null && isKeywordRegex(keyword)) {
Pattern pattern = Pattern.compile(keyword.substring(6));
Matcher m = pattern.matcher(message);
if (m.find()) {
// REGEX matches are left for last, so save it in case non-REGEX
// matches don't exist
regexId = entry.getKey();
regexMessage = triggerText.replace(matcher.group(), m.group());
dB.log("entry value: " + triggerText + " keyword: " + keyword + " m.group: " + m.group() + " matcher.group: " + matcher.group());
context.put("keyword", new Element(m.group()));
if (replace != null) {
regexMessage = replace;
}
}
} else if (isKeywordStrict(keyword)) {
if (message.toUpperCase().equalsIgnoreCase(keyword.toUpperCase())) {
// Trigger matches
id = entry.getKey();
replacementText = triggerText.replace("/", "");
matched = true;
if (replace != null) {
replacementText = replace;
}
}
} else if (message.toUpperCase().contains(keyword.toUpperCase())) {
// Trigger matches
id = entry.getKey();
replacementText = triggerText.replace("/", "");
matched = true;
if (replace != null) {
replacementText = replace;
}
}
}
if (matched) {
break;
}
}
}
if (!matched && regexId != null) {
id = regexId;
replacementText = regexMessage;
}
// If there was a match, the id of the match should have been returned.
if (id != null) {
Utilities.talkToNPC(replacementText, denizenPlayer, npc, Settings.chatToNpcOverhearingRange());
parse(npc, denizenPlayer, script, id, context);
if (HyperDebug) {
dB.log("chat to NPC");
}
return new ChatContext(true);
} else {
// none of its chat triggers worked
if (npc.getCitizen().hasTrait(ChatbotTrait.class)) {
Utilities.talkToNPC(message, denizenPlayer, npc, Settings.chatToNpcOverhearingRange());
npc.getCitizen().getTrait(ChatbotTrait.class).chatTo(player, message);
if (HyperDebug) {
dB.log("Chatbot");
}
return new ChatContext(true);
} else if (!Settings.chatGloballyIfFailedChatTriggers()) {
Utilities.talkToNPC(message, denizenPlayer, npc, Settings.chatToNpcOverhearingRange());
if (HyperDebug) {
dB.log("Chat globally");
}
return new ChatContext(true);
}
// No matching chat triggers, and the config.yml says we
// should just ignore the interaction...
}
if (HyperDebug) {
dB.log("Finished calculating");
}
return new ChatContext(message, ret);
}
use of net.aufdemrand.denizen.tags.BukkitTagContext in project Denizen-For-Bukkit by DenizenScript.
the class Utilities method talkToNPC.
/**
* @param player the player doing the talking
* @param npc the npc being talked to
* @param range the range, in blocks, that 'bystanders' will hear he chat
*/
public static void talkToNPC(String message, dPlayer player, dNPC npc, double range) {
String replacer = String.valueOf((char) 0x04);
// Get formats from Settings, and fill in <TEXT>
String talkFormat = Settings.chatToNpcFormat().replaceAll("(?i)<TEXT>", replacer);
String bystanderFormat = Settings.chatToNpcOverheardFormat().replaceAll("(?i)<TEXT>", replacer);
// Fill in tags // TODO: Debug option?
talkFormat = TagManager.tag(talkFormat, new BukkitTagContext(player, npc, false, null, true, null)).replace(replacer, message);
bystanderFormat = TagManager.tag(bystanderFormat, new BukkitTagContext(player, npc, false, null, true, null)).replace(replacer, message);
// Send message to player
player.getPlayerEntity().sendMessage(talkFormat);
// Send message to bystanders
for (Player target : Bukkit.getOnlinePlayers()) {
if (target != player.getPlayerEntity()) {
if (target.getWorld().equals(player.getPlayerEntity().getWorld()) && target.getLocation().distance(player.getPlayerEntity().getLocation()) <= range) {
target.sendMessage(bystanderFormat);
}
}
}
}
Aggregations