use of com.denizenscript.denizencore.objects.core.ScriptTag in project Denizen-For-Bukkit by DenizenScript.
the class ActionBarCommand method parseArgs.
// <--[command]
// @Name ActionBar
// @Syntax actionbar [<text>] (targets:<player>|...) (format:<name>) (per_player)
// @Required 1
// @Maximum 4
// @Short Sends a message to a player's action bar.
// @group player
//
// @Description
// Sends a message to the target's action bar area.
// If no target is specified it will default to the attached player.
// Accepts the 'format:<name>' argument, which will reformat the text according to the specified format script. See <@link language Format Script Containers>.
//
// Optionally use 'per_player' with a list of player targets, to have the tags in the text input be reparsed for each and every player.
// So, for example, "- actionbar 'hello <player.name>' targets:<server.online_players>"
// would normally show "hello bob" to every player (every player sees the exact same name in the text, ie bob sees "hello bob", steve also sees "hello bob", etc)
// but if you use "per_player", each player online would see their own name (so bob sees "hello bob", steve sees "hello steve", etc).
//
// @Tags
// None
//
// @Usage
// Use to send a message to the player's action bar.
// - actionbar "Hey there <player.name>!"
//
// @Usage
// Use to send a message to a list of players.
// - actionbar "Hey, welcome to the server!" targets:<[thatplayer]>|<[player]>|<[someplayer]>
//
// @Usage
// Use to send a message to a list of players, with a formatted message.
// - actionbar "Hey there!" targets:<[thatplayer]>|<[player]> format:ServerChat
// -->
@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {
for (Argument arg : ArgumentHelper.interpret(scriptEntry, scriptEntry.getOriginalArguments())) {
if (!scriptEntry.hasObject("format") && arg.matchesPrefix("format", "f")) {
String formatStr = TagManager.tag(arg.getValue(), scriptEntry.getContext());
FormatScriptContainer format = ScriptRegistry.getScriptContainer(formatStr);
if (format == null) {
Debug.echoError("Could not find format script matching '" + formatStr + "'");
}
scriptEntry.addObject("format", new ScriptTag(format));
} else if (!scriptEntry.hasObject("targets") && arg.matchesPrefix("targets", "target")) {
scriptEntry.addObject("targets", ListTag.getListFor(TagManager.tagObject(arg.getValue(), scriptEntry.getContext()), scriptEntry.getContext()).filter(PlayerTag.class, scriptEntry));
} else if (!scriptEntry.hasObject("per_player") && arg.matches("per_player")) {
scriptEntry.addObject("per_player", new ElementTag(true));
} else if (!scriptEntry.hasObject("text")) {
scriptEntry.addObject("text", arg.getRawElement());
} else {
arg.reportUnhandled();
}
}
if (!scriptEntry.hasObject("text")) {
throw new InvalidArgumentsException("Must specify a message!");
}
if (!scriptEntry.hasObject("targets") && !Utilities.entryHasPlayer(scriptEntry)) {
throw new InvalidArgumentsException("Must specify target(s).");
}
if (!scriptEntry.hasObject("targets")) {
if (!Utilities.entryHasPlayer(scriptEntry)) {
throw new InvalidArgumentsException("Must specify valid player Targets!");
} else {
scriptEntry.addObject("targets", Collections.singletonList(Utilities.getEntryPlayer(scriptEntry)));
}
}
}
use of com.denizenscript.denizencore.objects.core.ScriptTag in project Denizen-For-Bukkit by DenizenScript.
the class NarrateCommand method execute.
@Override
public void execute(ScriptEntry scriptEntry) {
if (scriptEntry.getResidingQueue().procedural) {
Debug.echoError("'Narrate' should not be used in a procedure script. Consider the 'debug' command instead.");
}
List<PlayerTag> targets = (List<PlayerTag>) scriptEntry.getObject("targets");
String text = scriptEntry.getElement("text").asString();
ScriptTag formatObj = scriptEntry.getObjectTag("format");
ElementTag perPlayerObj = scriptEntry.getElement("per_player");
ElementTag from = scriptEntry.getElement("from");
boolean perPlayer = perPlayerObj != null && perPlayerObj.asBoolean();
BukkitTagContext context = (BukkitTagContext) scriptEntry.getContext();
if (!perPlayer || targets == null) {
text = TagManager.tag(text, context);
}
if (scriptEntry.dbCallShouldDebug()) {
Debug.report(scriptEntry, getName(), db("Narrating", text), db("Targets", targets), formatObj, perPlayerObj, from);
}
UUID fromId = null;
if (from != null) {
if (from.asString().startsWith("p@")) {
fromId = UUID.fromString(from.asString().substring("p@".length()));
} else {
fromId = UUID.fromString(from.asString());
}
}
FormatScriptContainer format = formatObj == null ? null : (FormatScriptContainer) formatObj.getContainer();
if (targets == null) {
Bukkit.getServer().getConsoleSender().spigot().sendMessage(FormattedTextHelper.parse(format != null ? format.getFormattedText(text, scriptEntry) : text, ChatColor.WHITE));
return;
}
for (PlayerTag player : targets) {
if (player != null) {
if (!player.isOnline()) {
Debug.echoDebug(scriptEntry, "Player is offline, can't narrate to them. Skipping.");
continue;
}
String personalText = text;
if (perPlayer) {
context.player = player;
personalText = TagManager.tag(personalText, context);
}
BaseComponent[] component = FormattedTextHelper.parse(format != null ? format.getFormattedText(personalText, scriptEntry) : personalText, ChatColor.WHITE);
if (fromId == null) {
player.getPlayerEntity().spigot().sendMessage(component);
} else {
player.getPlayerEntity().spigot().sendMessage(ChatMessageType.CHAT, fromId, component);
}
} else {
Debug.echoError("Narrated to non-existent player!?");
}
}
}
use of com.denizenscript.denizencore.objects.core.ScriptTag in project Denizen-For-Bukkit by DenizenScript.
the class ModifyBlockCommand method execute.
@Override
public void execute(final ScriptEntry scriptEntry) {
final ListTag materials = scriptEntry.getObjectTag("materials");
final List<LocationTag> locations = (List<LocationTag>) scriptEntry.getObject("locations");
final ListTag location_list = scriptEntry.getObjectTag("location_list");
final ElementTag physics = scriptEntry.getElement("physics");
final ItemTag natural = scriptEntry.getObjectTag("natural");
final ElementTag delayed = scriptEntry.getElement("delayed");
final ElementTag maxDelayMs = scriptEntry.getElement("max_delay_ms");
final ElementTag radiusElement = scriptEntry.getElement("radius");
final ElementTag heightElement = scriptEntry.getElement("height");
final ElementTag depthElement = scriptEntry.getElement("depth");
final ScriptTag script = scriptEntry.getObjectTag("script");
final PlayerTag source = scriptEntry.getObjectTag("source");
ListTag percents = scriptEntry.getObjectTag("percents");
if (percents != null && percents.size() != materials.size()) {
Debug.echoError(scriptEntry, "Percents length != materials length");
percents = null;
}
final List<MaterialTag> materialList = materials.filter(MaterialTag.class, scriptEntry);
for (MaterialTag mat : materialList) {
if (!mat.getMaterial().isBlock()) {
Debug.echoError("Material '" + mat.getMaterial().name() + "' is not a block material");
return;
}
}
if (scriptEntry.dbCallShouldDebug()) {
Debug.report(scriptEntry, getName(), materials, physics, radiusElement, heightElement, depthElement, natural, delayed, maxDelayMs, script, percents, source, (locations == null ? location_list : db("locations", locations)));
}
Player sourcePlayer = source == null ? null : source.getPlayerEntity();
final boolean doPhysics = physics.asBoolean();
final int radius = radiusElement.asInt();
final int height = heightElement.asInt();
final int depth = depthElement.asInt();
List<Float> percentages = null;
if (percents != null) {
percentages = new ArrayList<>();
for (String str : percents) {
percentages.add(new ElementTag(str).asFloat());
}
}
final List<Float> percs = percentages;
if (locations == null && location_list == null) {
Debug.echoError("Must specify a valid location!");
return;
}
if ((location_list != null && location_list.isEmpty()) || (locations != null && locations.isEmpty())) {
return;
}
if (materialList.isEmpty()) {
Debug.echoError("Must specify a valid material!");
return;
}
no_physics = !doPhysics;
if (delayed.asBoolean()) {
final long maxDelay = maxDelayMs.asLong();
new BukkitRunnable() {
int index = 0;
@Override
public void run() {
try {
long start = System.currentTimeMillis();
LocationTag loc;
if (locations != null) {
loc = locations.get(0);
} else {
loc = getLocAt(location_list, 0, scriptEntry);
}
if (isLocationBad(scriptEntry, loc)) {
scriptEntry.setFinished(true);
cancel();
return;
}
boolean was_static = preSetup(loc);
while ((locations != null && locations.size() > index) || (location_list != null && location_list.size() > index)) {
LocationTag nLoc;
if (locations != null) {
nLoc = locations.get(index);
} else {
nLoc = getLocAt(location_list, index, scriptEntry);
}
if (isLocationBad(scriptEntry, nLoc)) {
scriptEntry.setFinished(true);
cancel();
return;
}
handleLocation(nLoc, index, materialList, doPhysics, natural, radius, height, depth, percs, sourcePlayer, scriptEntry);
index++;
if (System.currentTimeMillis() - start > maxDelay) {
break;
}
}
postComplete(loc, was_static);
if ((locations != null && locations.size() == index) || (location_list != null && location_list.size() == index)) {
if (script != null) {
ScriptUtilities.createAndStartQueue(script.getContainer(), null, scriptEntry.entryData, null, null, null, null, null, scriptEntry);
}
scriptEntry.setFinished(true);
cancel();
}
} catch (Throwable ex) {
Debug.echoError(ex);
}
}
}.runTaskTimer(Denizen.getInstance(), 1, 1);
} else {
LocationTag loc;
if (locations != null) {
loc = locations.get(0);
} else {
loc = getLocAt(location_list, 0, scriptEntry);
}
if (isLocationBad(scriptEntry, loc)) {
return;
}
boolean was_static = preSetup(loc);
int index = 0;
if (locations != null) {
for (LocationTag obj : locations) {
if (isLocationBad(scriptEntry, obj)) {
return;
}
handleLocation(obj, index, materialList, doPhysics, natural, radius, height, depth, percentages, sourcePlayer, scriptEntry);
index++;
}
} else {
for (int i = 0; i < location_list.size(); i++) {
LocationTag obj = getLocAt(location_list, i, scriptEntry);
if (isLocationBad(scriptEntry, obj)) {
return;
}
handleLocation(obj, index, materialList, doPhysics, natural, radius, height, depth, percentages, sourcePlayer, scriptEntry);
index++;
}
}
postComplete(loc, was_static);
scriptEntry.setFinished(true);
}
}
use of com.denizenscript.denizencore.objects.core.ScriptTag in project Denizen-For-Bukkit by DenizenScript.
the class ChatTrigger method processSingle.
public void processSingle(String message, Player player, NPCTag npc, Map<String, ObjectTag> context, InteractScriptContainer script, ChatContext returnable) {
context = new HashMap<>(context);
PlayerTag denizenPlayer = PlayerTag.mirrorBukkitPlayer(player);
if (script.shouldDebug()) {
Debug.report(script, name, ArgumentHelper.debugObj("Player", player.getName()) + ArgumentHelper.debugObj("NPC", npc.toString()) + ArgumentHelper.debugObj("Radius(Max)", npc.getLocation().distance(player.getLocation()) + "(" + npc.getTriggerTrait().getRadius(name) + ")") + ArgumentHelper.debugObj("Trigger text", message) + ArgumentHelper.debugObj("LOS", String.valueOf(player.hasLineOfSight(npc.getEntity()))) + ArgumentHelper.debugObj("Facing", String.valueOf(NMSHandler.getEntityHelper().isFacingEntity(player, npc.getEntity(), 45))));
}
String step = InteractScriptHelper.getCurrentStep(denizenPlayer, script.getName());
if (!script.containsTriggerInStep(step, ChatTrigger.class)) {
if (!Settings.chatGloballyIfNoChatTriggers()) {
Debug.echoDebug(script, player.getName() + " says to " + npc.getNicknameTrait().getNickname() + ", " + message);
return;
} else {
if (Debug.verbose) {
Debug.log("No trigger in step, chatting globally");
}
return;
}
}
String id = null;
boolean matched = false;
String replacementText = null;
String regexId = null;
String regexMessage = null;
String messageLow = CoreUtilities.toLowerCase(message);
Map<String, String> idMap = script.getIdMapFor(ChatTrigger.class, denizenPlayer);
if (!idMap.isEmpty()) {
for (Map.Entry<String, String> entry : idMap.entrySet()) {
// 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, null, false, null));
Matcher matcher = triggerPattern.matcher(triggerText);
while (matcher.find()) {
// TODO: script arg?
String keyword = TagManager.tag(matcher.group().replace("/", ""), new BukkitTagContext(denizenPlayer, npc, null, false, null));
String[] split = keyword.split("\\\\\\+REPLACE:", 2);
String replace = null;
if (split.length == 2) {
keyword = split[0];
replace = split[1];
}
String keywordLow = CoreUtilities.toLowerCase(keyword);
// match already (thus using alphabetical priority for triggers)
if (regexId == null && keywordLow.startsWith("regex:")) {
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());
context.put("keyword", new ElementTag(m.group()));
if (replace != null) {
regexMessage = replace;
}
}
} else if (keyword.contains("|")) {
for (String subkeyword : CoreUtilities.split(keyword, '|')) {
if (messageLow.contains(CoreUtilities.toLowerCase(subkeyword))) {
id = entry.getKey();
replacementText = triggerText.replace(matcher.group(), subkeyword);
matched = true;
context.put("keyword", new ElementTag(subkeyword));
if (replace != null) {
replacementText = replace;
}
}
}
} else if (keyword.equals("*")) {
id = entry.getKey();
replacementText = triggerText.replace("/*/", message);
matched = true;
if (replace != null) {
replacementText = replace;
}
} else if (keywordLow.startsWith("strict:") && messageLow.equals(keywordLow.substring("strict:".length()))) {
id = entry.getKey();
replacementText = triggerText.replace(matcher.group(), keyword.substring("strict:".length()));
matched = true;
if (replace != null) {
replacementText = replace;
}
} else if (messageLow.contains(keywordLow)) {
id = entry.getKey();
replacementText = triggerText.replace(matcher.group(), keyword);
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.
String showNormalChat = script.getString("STEPS." + step + ".CHAT TRIGGER." + id + ".SHOW AS NORMAL CHAT", "false");
if (id != null) {
String hideTriggerMessage = script.getString("STEPS." + step + ".CHAT TRIGGER." + id + ".HIDE TRIGGER MESSAGE", "false");
if (!hideTriggerMessage.equalsIgnoreCase("true")) {
Utilities.talkToNPC(replacementText, denizenPlayer, npc, Settings.chatToNpcOverhearingRange(), new ScriptTag(script));
}
parse(npc, denizenPlayer, script, id, context);
if (Debug.verbose) {
Debug.log("chat to NPC");
}
if (!showNormalChat.equalsIgnoreCase("true")) {
returnable.triggered = true;
}
return;
} else {
if (!Settings.chatGloballyIfFailedChatTriggers()) {
Utilities.talkToNPC(message, denizenPlayer, npc, Settings.chatToNpcOverhearingRange(), new ScriptTag(script));
if (Debug.verbose) {
Debug.log("Chat globally");
}
if (!showNormalChat.equalsIgnoreCase("true")) {
returnable.triggered = true;
}
return;
}
// No matching chat triggers, and the config.yml says we
// should just ignore the interaction...
}
if (Debug.verbose) {
Debug.log("Finished calculating");
}
returnable.changed_text = message;
}
use of com.denizenscript.denizencore.objects.core.ScriptTag in project Denizen-For-Bukkit by DenizenScript.
the class DamageTrigger method damageTrigger.
// <--[language]
// @name Damage Triggers
// @group NPC Interact Scripts
// @description
// Damage Triggers are triggered when when a player left clicks the NPC.
// Despite the name, these do not actually require the NPC take any damage, only that the player left clicks the NPC.
//
// In scripts, use <context.damage> to measure how much damage was done to the NPC
// (though note that invincible NPCs don't necessarily take any damage even when this is non-zero).
//
// These are very basic with no extraneous complexity.
//
// -->
// <--[action]
// @Actions
// no damage trigger
//
// @Triggers when the NPC is damaged by a player but no damage trigger fires.
//
// @Context
// None
//
// -->
// Technically defined in TriggerTrait, but placing here instead.
// <--[action]
// @Actions
// damage
//
// @Triggers when the NPC is damaged by a player.
//
// @Context
// <context.damage> returns how much damage was done.
//
// @Determine
// "cancelled" to cancel the damage event.
//
// -->
// <--[action]
// @Actions
// damaged
//
// @Triggers when the NPC is damaged by an entity.
//
// @Context
// <context.damage> returns how much damage was done.
// <context.damager> returns the entity that did the damage.
//
// @Determine
// "cancelled" to cancel the damage event.
//
// -->
@EventHandler
public void damageTrigger(EntityDamageByEntityEvent event) {
Map<String, ObjectTag> context = new HashMap<>();
context.put("damage", new ElementTag(event.getDamage()));
if (CitizensAPI.getNPCRegistry().isNPC(event.getEntity())) {
NPCTag npc = new NPCTag(CitizensAPI.getNPCRegistry().getNPC(event.getEntity()));
if (npc == null) {
return;
}
if (npc.getCitizen() == null) {
return;
}
EntityTag damager = new EntityTag(event.getDamager());
if (damager.isProjectile() && damager.hasShooter()) {
damager = damager.getShooter();
}
context.put("damager", damager.getDenizenObject());
ListTag determ = npc.action("damaged", null, context);
if (determ != null && determ.containsCaseInsensitive("cancelled")) {
event.setCancelled(true);
return;
}
if (!damager.isPlayer()) {
return;
}
PlayerTag dplayer = damager.getDenizenPlayer();
if (!npc.getCitizen().hasTrait(TriggerTrait.class)) {
return;
}
if (!npc.getTriggerTrait().isEnabled(name)) {
return;
}
TriggerTrait.TriggerContext trigger = npc.getTriggerTrait().trigger(this, dplayer);
if (!trigger.wasTriggered()) {
return;
}
if (trigger.hasDetermination() && trigger.getDeterminations().containsCaseInsensitive("cancelled")) {
event.setCancelled(true);
return;
}
List<InteractScriptContainer> scripts = InteractScriptHelper.getInteractScripts(npc, dplayer, true, ClickTrigger.class);
boolean any = false;
if (scripts != null) {
for (InteractScriptContainer script : scripts) {
String id = null;
Map<String, String> idMap = script.getIdMapFor(ClickTrigger.class, dplayer);
if (!idMap.isEmpty()) {
for (Map.Entry<String, String> entry : idMap.entrySet()) {
String entry_value = TagManager.tag(entry.getValue(), new BukkitTagContext(dplayer, npc, null, false, new ScriptTag(script)));
if (ItemTag.valueOf(entry_value, script).comparesTo(dplayer.getPlayerEntity().getEquipment().getItemInMainHand()) >= 0) {
id = entry.getKey();
}
}
}
if (parse(npc, dplayer, script, id, context)) {
any = true;
}
}
}
if (!any) {
npc.action("no damage trigger", dplayer);
}
}
}
Aggregations