Search in sources :

Example 11 with BukkitTagContext

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);
        }
    }
}
Also used : TriggerTrait(com.denizenscript.denizen.npc.traits.TriggerTrait) HashMap(java.util.HashMap) PlayerTag(com.denizenscript.denizen.objects.PlayerTag) ListTag(com.denizenscript.denizencore.objects.core.ListTag) ObjectTag(com.denizenscript.denizencore.objects.ObjectTag) BukkitTagContext(com.denizenscript.denizen.tags.BukkitTagContext) NPCTag(com.denizenscript.denizen.objects.NPCTag) ScriptTag(com.denizenscript.denizencore.objects.core.ScriptTag) EntityTag(com.denizenscript.denizen.objects.EntityTag) InteractScriptContainer(com.denizenscript.denizen.scripts.containers.core.InteractScriptContainer) ElementTag(com.denizenscript.denizencore.objects.core.ElementTag) HashMap(java.util.HashMap) Map(java.util.Map) EventHandler(org.bukkit.event.EventHandler)

Example 12 with BukkitTagContext

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);
            }
        }
    }
}
Also used : Player(org.bukkit.entity.Player) BukkitTagContext(com.denizenscript.denizen.tags.BukkitTagContext)

Example 13 with BukkitTagContext

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;
}
Also used : BukkitTagContext(com.denizenscript.denizen.tags.BukkitTagContext) ScriptTag(com.denizenscript.denizencore.objects.core.ScriptTag) BookMeta(org.bukkit.inventory.meta.BookMeta)

Example 14 with BukkitTagContext

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;
}
Also used : ArrayList(java.util.ArrayList) ScriptEntry(com.denizenscript.denizencore.scripts.ScriptEntry) BukkitTagContext(com.denizenscript.denizen.tags.BukkitTagContext) BukkitScriptEntryData(com.denizenscript.denizen.utilities.implementation.BukkitScriptEntryData) ScriptTag(com.denizenscript.denizencore.objects.core.ScriptTag) ContextSource(com.denizenscript.denizencore.scripts.queues.ContextSource) InstantQueue(com.denizenscript.denizencore.scripts.queues.core.InstantQueue) ScriptQueue(com.denizenscript.denizencore.scripts.queues.ScriptQueue)

Example 15 with BukkitTagContext

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);
}
Also used : BukkitTagContext(com.denizenscript.denizen.tags.BukkitTagContext) TagContext(com.denizenscript.denizencore.tags.TagContext) BukkitTagContext(com.denizenscript.denizen.tags.BukkitTagContext) ScriptTag(com.denizenscript.denizencore.objects.core.ScriptTag)

Aggregations

BukkitTagContext (com.denizenscript.denizen.tags.BukkitTagContext)28 ScriptTag (com.denizenscript.denizencore.objects.core.ScriptTag)16 PlayerTag (com.denizenscript.denizen.objects.PlayerTag)9 ElementTag (com.denizenscript.denizencore.objects.core.ElementTag)9 ObjectTag (com.denizenscript.denizencore.objects.ObjectTag)5 TagContext (com.denizenscript.denizencore.tags.TagContext)5 Player (org.bukkit.entity.Player)5 EntityTag (com.denizenscript.denizen.objects.EntityTag)4 ItemTag (com.denizenscript.denizen.objects.ItemTag)4 NPCTag (com.denizenscript.denizen.objects.NPCTag)4 ListTag (com.denizenscript.denizencore.objects.core.ListTag)4 ScriptEntry (com.denizenscript.denizencore.scripts.ScriptEntry)4 List (java.util.List)4 EventHandler (org.bukkit.event.EventHandler)4 FormatScriptContainer (com.denizenscript.denizen.scripts.containers.core.FormatScriptContainer)3 ScriptQueue (com.denizenscript.denizencore.scripts.queues.ScriptQueue)3 YamlConfiguration (com.denizenscript.denizencore.utilities.YamlConfiguration)3 StringHolder (com.denizenscript.denizencore.utilities.text.StringHolder)3 NMSHandler (com.denizenscript.denizen.nms.NMSHandler)2 TriggerTrait (com.denizenscript.denizen.npc.traits.TriggerTrait)2