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);
}
}
}
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));
}
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);
}
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();
}
}
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;
}
}
Aggregations