Search in sources :

Example 61 with EntityTag

use of com.denizenscript.denizen.objects.EntityTag 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 62 with EntityTag

use of com.denizenscript.denizen.objects.EntityTag in project Denizen-For-Bukkit by DenizenScript.

the class EnchantmentScriptContainer method getDamageProtection.

public int getDamageProtection(int level, String causeName, Entity attacker) {
    ContextSource.SimpleMap src = new ContextSource.SimpleMap();
    src.contexts = new HashMap<>();
    src.contexts.put("level", new ElementTag(level));
    src.contexts.put("cause", new ElementTag(causeName));
    if (attacker != null) {
        src.contexts.put("attacker", new EntityTag(attacker));
    }
    return Integer.parseInt(autoTag(damageProtectionTaggable, src));
}
Also used : ContextSource(com.denizenscript.denizencore.scripts.queues.ContextSource) EntityTag(com.denizenscript.denizen.objects.EntityTag) ElementTag(com.denizenscript.denizencore.objects.core.ElementTag)

Example 63 with EntityTag

use of com.denizenscript.denizen.objects.EntityTag in project Denizen-For-Bukkit by DenizenScript.

the class EntityScriptHelper method onEntityDeath.

@EventHandler(priority = EventPriority.MONITOR)
public void onEntityDeath(EntityDeathEvent event) {
    Entity entity = event.getEntity();
    EntityTag.rememberEntity(entity);
    EntityDespawnScriptEvent.instance.entity = new EntityTag(entity);
    EntityDespawnScriptEvent.instance.cause = new ElementTag("DEATH");
    EntityDespawnScriptEvent.instance.cancelled = false;
    EntityDespawnScriptEvent.instance.fire();
    EntityTag.forgetEntity(entity);
}
Also used : Entity(org.bukkit.entity.Entity) LivingEntity(org.bukkit.entity.LivingEntity) EntityTag(com.denizenscript.denizen.objects.EntityTag) ElementTag(com.denizenscript.denizencore.objects.core.ElementTag) EventHandler(org.bukkit.event.EventHandler)

Example 64 with EntityTag

use of com.denizenscript.denizen.objects.EntityTag in project Denizen-For-Bukkit by DenizenScript.

the class NoteBlockReceiver method playNote.

// Note that this may run async
public void playNote(ShortMessage message) {
    // if this isn't a NOTE_ON message, we can't play it
    if (ShortMessage.NOTE_ON != message.getCommand()) {
        return;
    }
    int channel = message.getChannel();
    // If this is a percussion channel, return
    if (channel == 9) {
        return;
    }
    if (channelPatches == null) {
        Debug.echoError("Trying to play notes on closed midi NoteBlockReceiver!");
        return;
    }
    // get the correct instrument
    Integer patch = channelPatches.get(channel);
    // get pitch and volume from the midi message
    float pitch = (float) ToneUtil.midiToPitch(message);
    float volume = VOLUME_RANGE * (message.getData2() / 127.0f);
    SoundHelper soundHelper = NMSHandler.getSoundHelper();
    Sound instrument = soundHelper.getDefaultMidiInstrument();
    if (patch != null) {
        instrument = soundHelper.getMidiInstrumentFromPatch(patch);
    }
    if (location != null) {
        location.getWorld().playSound(location, instrument, volume, pitch);
    } else if (entities != null && !entities.isEmpty()) {
        for (int i = 0; i < entities.size(); i++) {
            EntityTag entity = entities.get(i);
            if (entity.isSpawned()) {
                if (entity.isPlayer()) {
                    NMSHandler.getSoundHelper().playSound(entity.getPlayer(), entity.getLocation(), instrument, volume, pitch, "RECORDS");
                } else {
                    NMSHandler.getSoundHelper().playSound(null, entity.getLocation(), instrument, volume, pitch, "RECORDS");
                }
            } else {
                entities.remove(i);
                i--;
            }
        }
    } else {
        this.close();
    }
}
Also used : SoundHelper(com.denizenscript.denizen.nms.interfaces.SoundHelper) Sound(org.bukkit.Sound) EntityTag(com.denizenscript.denizen.objects.EntityTag)

Example 65 with EntityTag

use of com.denizenscript.denizen.objects.EntityTag in project Denizen-For-Bukkit by DenizenScript.

the class MidiUtil method playMidi.

public static NoteBlockReceiver playMidi(File file, float tempo, float volume, List<EntityTag> entities) {
    try {
        NoteBlockReceiver receiver = new NoteBlockReceiver(entities, entities.get(0).getUUID().toString());
        receiver.VOLUME_RANGE = volume;
        // stop playing it
        for (EntityTag entity : entities) {
            stopMidi(entity.getUUID().toString());
        }
        receivers.put(entities.get(0).getUUID().toString(), receiver);
        startSequencer(file, tempo, receiver);
        return receiver;
    } catch (Exception e) {
        Debug.echoError(e);
        return null;
    }
}
Also used : EntityTag(com.denizenscript.denizen.objects.EntityTag) InvalidMidiDataException(javax.sound.midi.InvalidMidiDataException) IOException(java.io.IOException) MidiUnavailableException(javax.sound.midi.MidiUnavailableException)

Aggregations

EntityTag (com.denizenscript.denizen.objects.EntityTag)142 EventHandler (org.bukkit.event.EventHandler)81 ElementTag (com.denizenscript.denizencore.objects.core.ElementTag)56 LocationTag (com.denizenscript.denizen.objects.LocationTag)45 List (java.util.List)27 Entity (org.bukkit.entity.Entity)25 ItemTag (com.denizenscript.denizen.objects.ItemTag)17 ListTag (com.denizenscript.denizencore.objects.core.ListTag)17 PlayerTag (com.denizenscript.denizen.objects.PlayerTag)15 InvalidArgumentsException (com.denizenscript.denizencore.exceptions.InvalidArgumentsException)12 DurationTag (com.denizenscript.denizencore.objects.core.DurationTag)11 NPCTag (com.denizenscript.denizen.objects.NPCTag)10 ArrayList (java.util.ArrayList)10 BukkitRunnable (org.bukkit.scheduler.BukkitRunnable)10 ScriptEntry (com.denizenscript.denizencore.scripts.ScriptEntry)9 Player (org.bukkit.entity.Player)9 MaterialTag (com.denizenscript.denizen.objects.MaterialTag)8 Argument (com.denizenscript.denizencore.objects.Argument)8 LivingEntity (org.bukkit.entity.LivingEntity)8 Item (org.bukkit.entity.Item)7