use of com.denizenscript.denizen.tags.BukkitTagContext 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);
}
}
}
use of com.denizenscript.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, PlayerTag player, NPCTag npc, double range, ScriptTag script) {
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
talkFormat = TagManager.tag(talkFormat, new BukkitTagContext(player, npc, script)).replace(replacer, message);
bystanderFormat = TagManager.tag(bystanderFormat, new BukkitTagContext(player, npc, script)).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);
}
}
}
}
use of com.denizenscript.denizen.tags.BukkitTagContext in project Denizen-For-Bukkit by DenizenScript.
the class BookScriptContainer method writeBookTo.
public ItemTag writeBookTo(ItemTag book, TagContext context) {
if (context == null) {
context = new BukkitTagContext(null, null, new ScriptTag(this));
}
// Get current ItemMeta from the book
BookMeta bookInfo = (BookMeta) book.getItemMeta();
if (contains("title", String.class)) {
String title = getString("title");
title = TagManager.tag(title, context);
bookInfo.setTitle(title);
}
if (contains("signed", String.class)) {
if (getString("signed").equalsIgnoreCase("false")) {
book.getItemStack().setType(Material.WRITABLE_BOOK);
}
}
if (contains("author", String.class)) {
String author = getString("author");
author = TagManager.tag(author, context);
bookInfo.setAuthor(author);
}
if (contains("text", List.class)) {
List<String> pages = getStringList("text");
for (String page : pages) {
page = TagManager.tag(page, context);
bookInfo.spigot().addPage(FormattedTextHelper.parse(page, ChatColor.BLACK));
}
}
book.setItemMeta(bookInfo);
return book;
}
use of com.denizenscript.denizen.tags.BukkitTagContext in project Denizen-For-Bukkit by DenizenScript.
the class CommandScriptContainer method runTabCompleteProcedure.
public List<String> runTabCompleteProcedure(PlayerTag player, NPCTag npc, Map<String, ObjectTag> context, String[] originalArguments) {
BukkitTagContext tagContext = new BukkitTagContext(player, npc, new ScriptTag(this));
ContextSource contextSrc = null;
if (context != null) {
ContextSource.SimpleMap src = new ContextSource.SimpleMap();
src.contexts = context;
tagContext.contextSource = src;
contextSrc = src;
}
List<String> list = new ArrayList<>();
if (tabCompletionTaggables != null) {
int argCount = Math.max(originalArguments.length, 1);
String taggable = tabCompletionTaggables.get(argCount);
if (taggable == null) {
taggable = tabCompletionTaggables.get(-1);
}
if (taggable != null) {
String argLow = originalArguments.length == 0 ? "" : CoreUtilities.toLowerCase(originalArguments[originalArguments.length - 1]);
for (String value : ListTag.getListFor(TagManager.tagObject(taggable, tagContext), tagContext)) {
if (CoreUtilities.toLowerCase(value).startsWith(argLow)) {
list.add(value);
}
}
}
}
if (hasProcStyleTabComplete) {
List<ScriptEntry> entries = getEntries(new BukkitScriptEntryData(player, npc), "tab complete");
ScriptQueue queue = new InstantQueue(getName());
queue.addEntries(entries);
if (contextSrc != null) {
queue.setContextSource(contextSrc);
}
queue.start();
if (queue.determinations != null && queue.determinations.size() > 0) {
list.addAll(ListTag.getListFor(queue.determinations.getObject(0), tagContext));
}
}
return list;
}
use of com.denizenscript.denizen.tags.BukkitTagContext in project Denizen-For-Bukkit by DenizenScript.
the class EnchantmentScriptContainer method autoTag.
public String autoTag(String value, ContextSource src) {
if (value == null) {
return null;
}
validateThread();
TagContext context = new BukkitTagContext(null, new ScriptTag(this));
context.contextSource = src;
return TagManager.tag(value, context);
}
Aggregations