Search in sources :

Example 1 with InteractScriptContainer

use of com.denizenscript.denizen.scripts.containers.core.InteractScriptContainer in project Denizen-For-Bukkit by DenizenScript.

the class BukkitScriptProperties method registerTags.

public static void registerTags() {
    // <--[tag]
    // @attribute <ScriptTag.cooled_down[<player>]>
    // @returns ElementTag(Boolean)
    // @description
    // Returns whether the script is currently cooled down for the player. Any global
    // cooldown present on the script will also be taken into account. Not specifying a player will result in
    // using the attached player available in the script entry. Not having a valid player will result in 'null'.
    // -->
    PropertyParser.<BukkitScriptProperties, ElementTag>registerTag(ElementTag.class, "cooled_down", (attribute, script) -> {
        PlayerTag player = (attribute.hasParam() ? attribute.paramAsType(PlayerTag.class) : ((BukkitScriptEntryData) attribute.getScriptEntry().entryData).getPlayer());
        if (player != null && player.isValid()) {
            return new ElementTag(CooldownCommand.checkCooldown(player, script.script.getContainer().getName()));
        } else {
            return null;
        }
    });
    // <--[tag]
    // @attribute <ScriptTag.cooldown[<player>]>
    // @returns DurationTag
    // @description
    // Returns the time left for the player to cooldown for the script.
    // -->
    PropertyParser.<BukkitScriptProperties, DurationTag>registerTag(DurationTag.class, "cooldown", (attribute, script) -> {
        PlayerTag player = (attribute.hasParam() ? attribute.paramAsType(PlayerTag.class) : ((BukkitScriptEntryData) attribute.getScriptEntry().entryData).getPlayer());
        return CooldownCommand.getCooldownDuration(player, script.script.getName());
    });
    // <--[tag]
    // @attribute <ScriptTag.step[(<player>)]>
    // @returns ElementTag
    // @description
    // Returns the name of a script step that the player is currently on.
    // Must be an INTERACT script.
    // -->
    PropertyParser.<BukkitScriptProperties, ElementTag>registerTag(ElementTag.class, "step", (attribute, script) -> {
        PlayerTag player = attribute.hasParam() ? attribute.paramAsType(PlayerTag.class) : ((BukkitScriptEntryData) attribute.getScriptEntry().entryData).getPlayer();
        if (player != null && player.isValid()) {
            return new ElementTag(InteractScriptHelper.getCurrentStep(player, script.script.getContainer().getName()));
        } else {
            return null;
        }
    });
    // <--[tag]
    // @attribute <ScriptTag.step_expiration[(<player>)]>
    // @returns TimeTag
    // @description
    // Returns the time that an interact script step expires at, if applied by <@link command zap> with a duration limit.
    // -->
    PropertyParser.<BukkitScriptProperties, TimeTag>registerTag(TimeTag.class, "step_expiration", (attribute, script) -> {
        PlayerTag player = attribute.hasParam() ? attribute.paramAsType(PlayerTag.class) : ((BukkitScriptEntryData) attribute.getScriptEntry().entryData).getPlayer();
        if (player != null && player.isValid()) {
            return InteractScriptHelper.getStepExpiration(player, script.script.getContainer().getName());
        } else {
            return null;
        }
    });
    // <--[tag]
    // @attribute <ScriptTag.default_step>
    // @returns ElementTag
    // @description
    // Returns the name of the default step of an interact script.
    // -->
    PropertyParser.<BukkitScriptProperties, ElementTag>registerStaticTag(ElementTag.class, "default_step", (attribute, script) -> {
        String step = ((InteractScriptContainer) script.script.getContainer()).getDefaultStepName();
        return new ElementTag(step);
    });
}
Also used : PlayerTag(com.denizenscript.denizen.objects.PlayerTag) BukkitScriptEntryData(com.denizenscript.denizen.utilities.implementation.BukkitScriptEntryData) TimeTag(com.denizenscript.denizencore.objects.core.TimeTag) InteractScriptContainer(com.denizenscript.denizen.scripts.containers.core.InteractScriptContainer) ElementTag(com.denizenscript.denizencore.objects.core.ElementTag) DurationTag(com.denizenscript.denizencore.objects.core.DurationTag)

Example 2 with InteractScriptContainer

use of com.denizenscript.denizen.scripts.containers.core.InteractScriptContainer 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 3 with InteractScriptContainer

use of com.denizenscript.denizen.scripts.containers.core.InteractScriptContainer in project Denizen-For-Bukkit by DenizenScript.

the class ClickTrigger method clickTrigger.

// <--[language]
// @name Click Triggers
// @group NPC Interact Scripts
// @description
// Click Triggers are triggered when a player right clicks the NPC.
// 
// These are very basic with no extraneous complexity.
// 
// <code>
// click trigger:
// script:
// - narrate "hi <player.name>"
// </code>
// 
// They can optionally have an item matcher with multiple triggers, for the item in the player's hand. For example:
// <code>
// click trigger:
// 1:
// trigger: my_item_script
// script:
// - narrate "Nice item script"
// 2:
// trigger: stone
// script:
// - narrate "Nice vanilla item"
// 3:
// script:
// - narrate "I don't recognize your held item"
// </code>
// 
// -->
// <--[action]
// @Actions
// no click trigger
// 
// @Triggers when the NPC is clicked but no click trigger fires.
// 
// @Context
// None
// 
// -->
// Technically defined in TriggerTrait, but placing here instead.
// <--[action]
// @Actions
// click
// 
// @Triggers when the NPC is clicked by a player.
// 
// @Context
// None
// 
// @Determine
// "cancelled" to cancel the click event completely.
// 
// -->
@EventHandler
public void clickTrigger(NPCRightClickEvent event) {
    if (!event.getNPC().hasTrait(TriggerTrait.class)) {
        return;
    }
    NPCTag npc = new NPCTag(event.getNPC());
    if (!npc.getTriggerTrait().isEnabled(name)) {
        return;
    }
    PlayerTag player = PlayerTag.mirrorBukkitPlayer(event.getClicker());
    TriggerTrait.TriggerContext trigger = npc.getTriggerTrait().trigger(this, player);
    if (!trigger.wasTriggered()) {
        return;
    }
    if (trigger.hasDetermination() && trigger.getDeterminations().containsCaseInsensitive("cancelled")) {
        event.setCancelled(true);
        return;
    }
    List<InteractScriptContainer> scripts = npc.getInteractScripts(player, ClickTrigger.class);
    boolean any = false;
    if (scripts != null) {
        for (InteractScriptContainer script : scripts) {
            String id = null;
            Map<String, String> idMap = script.getIdMapFor(ClickTrigger.class, player);
            if (!idMap.isEmpty()) {
                ItemTag heldItem = new ItemTag(player.getPlayerEntity().getEquipment().getItemInMainHand());
                for (Map.Entry<String, String> entry : idMap.entrySet()) {
                    String entry_value = TagManager.tag(entry.getValue(), new BukkitTagContext(player, npc, null, false, null));
                    boolean isMatch = entry_value.isEmpty() || BukkitScriptEvent.tryItem(heldItem, entry_value);
                    if (script.shouldDebug()) {
                        Debug.echoDebug(script, "Comparing click trigger '<A>" + entry_value + "<W>' with item '<A>" + heldItem.debuggable() + "<W>': " + (isMatch ? "<GR>Match!" : "<Y>Not a match"));
                    }
                    if (isMatch) {
                        id = entry.getKey();
                        break;
                    }
                }
            }
            if (parse(npc, player, script, id)) {
                any = true;
            }
        }
    }
    if (!any) {
        npc.action("no click trigger", player);
    }
}
Also used : TriggerTrait(com.denizenscript.denizen.npc.traits.TriggerTrait) PlayerTag(com.denizenscript.denizen.objects.PlayerTag) BukkitTagContext(com.denizenscript.denizen.tags.BukkitTagContext) NPCTag(com.denizenscript.denizen.objects.NPCTag) InteractScriptContainer(com.denizenscript.denizen.scripts.containers.core.InteractScriptContainer) ItemTag(com.denizenscript.denizen.objects.ItemTag) Map(java.util.Map) EventHandler(org.bukkit.event.EventHandler)

Example 4 with InteractScriptContainer

use of com.denizenscript.denizen.scripts.containers.core.InteractScriptContainer in project Denizen-For-Bukkit by DenizenScript.

the class ZapCommand method parseArgs.

@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {
    for (Argument arg : scriptEntry) {
        if (!scriptEntry.hasObject("script") && !scriptEntry.hasObject("step") && arg.hasPrefix() && arg.getPrefix().matchesArgumentType(ScriptTag.class)) {
            Deprecations.zapPrefix.warn(scriptEntry);
            scriptEntry.addObject("script", arg.getPrefix().asType(ScriptTag.class));
            scriptEntry.addObject("step", arg.asElement());
        } else if (!scriptEntry.hasObject("script") && arg.matchesArgumentType(ScriptTag.class) && arg.asType(ScriptTag.class).getContainer() instanceof InteractScriptContainer && arg.limitToOnlyPrefix("script")) {
            scriptEntry.addObject("script", arg.asType(ScriptTag.class));
        } else if (!scriptEntry.hasObject("step") && arg.limitToOnlyPrefix("step")) {
            scriptEntry.addObject("step", arg.asElement());
        } else if (!scriptEntry.hasObject("duration") && arg.matchesArgumentType(DurationTag.class) && arg.limitToOnlyPrefix("duration")) {
            scriptEntry.addObject("duration", arg.asType(DurationTag.class));
        } else {
            arg.reportUnhandled();
        }
    }
    PlayerTag player = Utilities.getEntryPlayer(scriptEntry);
    if (player == null || !player.isValid()) {
        throw new InvalidArgumentsException("Must have player context!");
    }
    if (!scriptEntry.hasObject("script")) {
        ScriptTag script = scriptEntry.getScript();
        if (script != null) {
            if (script.getContainer() instanceof InteractScriptContainer) {
                scriptEntry.addObject("script", script);
            } else if (script.getContainer() instanceof AssignmentScriptContainer) {
                InteractScriptContainer interact = ((AssignmentScriptContainer) script.getContainer()).interact;
                if (interact != null) {
                    scriptEntry.addObject("script", new ScriptTag(interact));
                }
            }
        }
        if (!scriptEntry.hasObject("script")) {
            NPCTag npc = Utilities.getEntryNPC(scriptEntry);
            if (npc != null && npc.getCitizen().hasTrait(AssignmentTrait.class)) {
                AssignmentTrait trait = npc.getCitizen().getOrAddTrait(AssignmentTrait.class);
                for (AssignmentScriptContainer container : trait.containerCache) {
                    if (container != null && container.getInteract() != null) {
                        scriptEntry.addObject("script", new ScriptTag(container.getInteract()));
                        break;
                    }
                }
            }
        }
        if (!scriptEntry.hasObject("script")) {
            throw new InvalidArgumentsException("No script to zap! Must be in an interact script, or have a linked NPC with an associated interact script.");
        }
    }
}
Also used : AssignmentTrait(com.denizenscript.denizen.npc.traits.AssignmentTrait) PlayerTag(com.denizenscript.denizen.objects.PlayerTag) ScriptTag(com.denizenscript.denizencore.objects.core.ScriptTag) NPCTag(com.denizenscript.denizen.objects.NPCTag) InteractScriptContainer(com.denizenscript.denizen.scripts.containers.core.InteractScriptContainer) AssignmentScriptContainer(com.denizenscript.denizen.scripts.containers.core.AssignmentScriptContainer) DurationTag(com.denizenscript.denizencore.objects.core.DurationTag) InvalidArgumentsException(com.denizenscript.denizencore.exceptions.InvalidArgumentsException)

Example 5 with InteractScriptContainer

use of com.denizenscript.denizen.scripts.containers.core.InteractScriptContainer in project Denizen-For-Bukkit by DenizenScript.

the class ZapCommand method execute.

@Override
public void execute(final ScriptEntry scriptEntry) {
    final ScriptTag script = scriptEntry.getObjectTag("script");
    DurationTag duration = scriptEntry.getObjectTag("duration");
    ElementTag stepElement = scriptEntry.getElement("step");
    if (scriptEntry.dbCallShouldDebug()) {
        Debug.report(scriptEntry, getName(), Utilities.getEntryPlayer(scriptEntry), script, stepElement != null ? stepElement : db("step", "++ (inc)"), duration);
    }
    String step = stepElement == null ? null : stepElement.asString();
    String currentStep = InteractScriptHelper.getCurrentStep(Utilities.getEntryPlayer(scriptEntry), script.getName());
    // Special-case for backwards compatibility: ability to use ZAP to count up steps.
    if (step == null) {
        // to '1' so it can be incremented next time.
        if (ArgumentHelper.matchesInteger(currentStep)) {
            step = String.valueOf(Integer.parseInt(currentStep) + 1);
        } else {
            step = "1";
        }
    } else if (step.equals("*")) {
        step = ((InteractScriptContainer) script.getContainer()).getDefaultStepName();
    }
    if (step.equalsIgnoreCase(currentStep)) {
        Debug.echoError(scriptEntry, "Zapping to own current step!");
        return;
    }
    TimeTag expiration = null;
    if (duration != null && duration.getSeconds() > 0) {
        expiration = new TimeTag(TimeTag.now().millis() + duration.getMillis());
    }
    Utilities.getEntryPlayer(scriptEntry).getFlagTracker().setFlag("__interact_step." + script.getName(), new ElementTag(step), expiration);
}
Also used : ScriptTag(com.denizenscript.denizencore.objects.core.ScriptTag) InteractScriptContainer(com.denizenscript.denizen.scripts.containers.core.InteractScriptContainer) TimeTag(com.denizenscript.denizencore.objects.core.TimeTag) ElementTag(com.denizenscript.denizencore.objects.core.ElementTag) DurationTag(com.denizenscript.denizencore.objects.core.DurationTag)

Aggregations

InteractScriptContainer (com.denizenscript.denizen.scripts.containers.core.InteractScriptContainer)6 PlayerTag (com.denizenscript.denizen.objects.PlayerTag)5 NPCTag (com.denizenscript.denizen.objects.NPCTag)4 ElementTag (com.denizenscript.denizencore.objects.core.ElementTag)4 TriggerTrait (com.denizenscript.denizen.npc.traits.TriggerTrait)3 DurationTag (com.denizenscript.denizencore.objects.core.DurationTag)3 ScriptTag (com.denizenscript.denizencore.objects.core.ScriptTag)3 BukkitTagContext (com.denizenscript.denizen.tags.BukkitTagContext)2 ObjectTag (com.denizenscript.denizencore.objects.ObjectTag)2 TimeTag (com.denizenscript.denizencore.objects.core.TimeTag)2 Map (java.util.Map)2 EventHandler (org.bukkit.event.EventHandler)2 AssignmentTrait (com.denizenscript.denizen.npc.traits.AssignmentTrait)1 EntityTag (com.denizenscript.denizen.objects.EntityTag)1 ItemTag (com.denizenscript.denizen.objects.ItemTag)1 AssignmentScriptContainer (com.denizenscript.denizen.scripts.containers.core.AssignmentScriptContainer)1 BukkitScriptEntryData (com.denizenscript.denizen.utilities.implementation.BukkitScriptEntryData)1 InvalidArgumentsException (com.denizenscript.denizencore.exceptions.InvalidArgumentsException)1 ListTag (com.denizenscript.denizencore.objects.core.ListTag)1 HashMap (java.util.HashMap)1