Search in sources :

Example 76 with Projectile

use of org.bukkit.entity.Projectile in project AuthMeReloaded by AuthMe.

the class EntityListenerTest method shouldAllowProjectileLaunchFromNonHuman.

@Test
public void shouldAllowProjectileLaunchFromNonHuman() {
    // given
    Projectile projectile = mock(Projectile.class);
    ProjectileSource source = mock(ProjectileSource.class);
    given(projectile.getShooter()).willReturn(source);
    ProjectileLaunchEvent event = mock(ProjectileLaunchEvent.class);
    given(event.getEntity()).willReturn(projectile);
    // when
    listener.onProjectileLaunch(event);
    // then
    verifyNoInteractions(listenerService);
    verify(event, never()).setCancelled(anyBoolean());
}
Also used : ProjectileLaunchEvent(org.bukkit.event.entity.ProjectileLaunchEvent) ProjectileSource(org.bukkit.projectiles.ProjectileSource) Projectile(org.bukkit.entity.Projectile) Test(org.junit.Test)

Example 77 with Projectile

use of org.bukkit.entity.Projectile in project Denizen-For-Bukkit by DenizenScript.

the class ShootCommand method execute.

@Override
public void execute(final ScriptEntry scriptEntry) {
    EntityTag originEntity = scriptEntry.getObjectTag("origin_entity");
    LocationTag originLocation = scriptEntry.hasObject("origin_location") ? (LocationTag) scriptEntry.getObject("origin_location") : new LocationTag(originEntity.getEyeLocation().add(originEntity.getEyeLocation().getDirection()));
    boolean no_rotate = scriptEntry.hasObject("no_rotate") && scriptEntry.getElement("no_rotate").asBoolean();
    // If there is no destination set, but there is a shooter, get a point
    // in front of the shooter and set it as the destination
    final LocationTag destination = scriptEntry.hasObject("destination") ? (LocationTag) scriptEntry.getObject("destination") : (originEntity != null ? new LocationTag(originEntity.getEyeLocation().clone().add(originEntity.getEyeLocation().clone().getDirection().multiply(30))) : (originLocation != null ? new LocationTag(originLocation.clone().add(originLocation.getDirection().multiply(30))) : null));
    // TODO: Same as PUSH -- is this the place to do this?
    if (destination == null) {
        Debug.echoError("No destination specified!");
        return;
    }
    final List<EntityTag> entities = (List<EntityTag>) scriptEntry.getObject("entities");
    final ScriptTag script = scriptEntry.getObjectTag("script");
    final ElementTag subPath = scriptEntry.getElement("path");
    final ListTag definitions = scriptEntry.getObjectTag("definitions");
    EntityTag shooter = scriptEntry.getObjectTag("shooter");
    ElementTag height = scriptEntry.getElement("height");
    ElementTag gravity = scriptEntry.getElement("gravity");
    ElementTag speed = scriptEntry.getElement("speed");
    ElementTag spread = scriptEntry.getElement("spread");
    LocationTag lead = scriptEntry.getObjectTag("lead");
    if (scriptEntry.dbCallShouldDebug()) {
        Debug.report(scriptEntry, getName(), originEntity, originLocation, db("entities", entities), destination, height, gravity, speed, script, subPath, shooter, spread, lead, (no_rotate ? db("no_rotate", "true") : ""), definitions);
    }
    final ListTag entityList = new ListTag();
    if (!no_rotate) {
        originLocation = new LocationTag(NMSHandler.getEntityHelper().faceLocation(originLocation, destination));
    }
    for (EntityTag entity : entities) {
        if (!entity.isSpawned() || !no_rotate) {
            entity.spawnAt(originLocation);
        }
        entityList.addObject(entity);
        if (entity.isProjectile()) {
            if (shooter != null || originEntity != null) {
                entity.setShooter(shooter != null ? shooter : originEntity);
            }
            if (script != null || scriptEntry.shouldWaitFor()) {
                arrows.put(entity.getUUID(), null);
            }
        }
    }
    scriptEntry.addObject("shot_entities", entityList);
    if (entityList.size() == 1) {
        scriptEntry.addObject("shot_entity", entityList.getObject(0));
    }
    if (spread == null) {
        Position.mount(Conversion.convertEntities(entities));
    }
    final EntityTag lastEntity = entities.get(entities.size() - 1);
    if (speed == null) {
        if (gravity == null) {
            gravity = new ElementTag(lastEntity.getEntityType().getGravity());
        }
        Vector v1 = lastEntity.getLocation().toVector();
        Vector v2 = destination.toVector();
        Vector v3 = Velocity.calculate(v1, v2, gravity.asDouble(), height.asDouble());
        lastEntity.setVelocity(v3);
    } else if (lead == null) {
        Vector relative = destination.clone().subtract(originLocation).toVector();
        lastEntity.setVelocity(relative.normalize().multiply(speed.asDouble()));
    } else {
        double g = 20;
        double v = speed.asDouble();
        Vector relative = destination.clone().subtract(originLocation).toVector();
        double testAng = Velocity.launchAngle(originLocation, destination.toVector(), v, relative.getY(), g);
        double hangTime = Velocity.hangtime(testAng, v, relative.getY(), g);
        Vector to = destination.clone().add(lead.clone().multiply(hangTime)).toVector();
        relative = to.clone().subtract(originLocation.toVector());
        double dist = Math.sqrt(relative.getX() * relative.getX() + relative.getZ() * relative.getZ());
        if (dist == 0) {
            dist = 0.1d;
        }
        testAng = Velocity.launchAngle(originLocation, to, v, relative.getY(), g);
        relative.setY(Math.tan(testAng) * dist);
        relative = relative.normalize();
        v = v + (1.188 * Math.pow(hangTime, 2));
        relative = relative.multiply(v / 20.0d);
        lastEntity.setVelocity(relative);
    }
    if (spread != null) {
        Vector base = lastEntity.getVelocity().clone();
        float sf = spread.asFloat();
        for (EntityTag entity : entities) {
            Vector newvel = Velocity.spread(base, (CoreUtilities.getRandom().nextDouble() > 0.5f ? 1 : -1) * Math.toRadians(CoreUtilities.getRandom().nextDouble() * sf), (CoreUtilities.getRandom().nextDouble() > 0.5f ? 1 : -1) * Math.toRadians(CoreUtilities.getRandom().nextDouble() * sf));
            entity.setVelocity(newvel);
        }
    }
    final LocationTag start = new LocationTag(lastEntity.getLocation());
    // A task used to trigger a script if the entity is no longer
    // being shot, when the script argument is used
    BukkitRunnable task = new BukkitRunnable() {

        boolean flying = true;

        LocationTag lastLocation = null;

        Vector lastVelocity = null;

        public void run() {
            // If the entity is no longer spawned, stop the task
            if (!lastEntity.isSpawned()) {
                if (Debug.verbose) {
                    Debug.log("Shoot ended because entity not spawned");
                }
                flying = false;
            } else // the air, stop the task
            if (lastLocation != null && lastVelocity != null && !(lastEntity.getBukkitEntity() instanceof Projectile)) {
                if (lastLocation.getWorld() != lastEntity.getBukkitEntity().getWorld() || (lastLocation.distanceSquared(lastEntity.getBukkitEntity().getLocation()) < 0.1 && lastVelocity.distanceSquared(lastEntity.getBukkitEntity().getVelocity()) < 0.1)) {
                    if (Debug.verbose) {
                        Debug.log("Shoot ended because distances short - locations: " + (lastLocation.distanceSquared(lastEntity.getBukkitEntity().getLocation())) + ", velocity: " + (lastVelocity.distanceSquared(lastEntity.getBukkitEntity().getVelocity()) < 0.1));
                    }
                    flying = false;
                }
            }
            if (!arrows.containsKey(lastEntity.getUUID()) || arrows.get(lastEntity.getUUID()) != null) {
                if (Debug.verbose) {
                    Debug.log("Shoot ended because uuid was updated (hit entity?)");
                }
                flying = false;
            }
            // are met
            if (!flying) {
                this.cancel();
                ListTag hitEntities = new ListTag();
                for (EntityTag entity : entities) {
                    if (arrows.containsKey(entity.getUUID())) {
                        EntityTag hit = arrows.get(entity.getUUID());
                        arrows.remove(entity.getUUID());
                        if (hit != null) {
                            hitEntities.addObject(hit);
                        }
                    }
                }
                if (lastLocation == null) {
                    lastLocation = start;
                }
                scriptEntry.addObject("location", new LocationTag(lastLocation));
                scriptEntry.addObject("hit_entities", hitEntities);
                if (script != null) {
                    Consumer<ScriptQueue> configure = (queue) -> {
                        queue.addDefinition("location", new LocationTag(lastLocation));
                        queue.addDefinition("shot_entities", entityList);
                        queue.addDefinition("last_entity", lastEntity);
                        queue.addDefinition("hit_entities", hitEntities);
                    };
                    ScriptUtilities.createAndStartQueue(script.getContainer(), subPath == null ? null : subPath.asString(), scriptEntry.entryData, null, configure, null, null, definitions, scriptEntry);
                }
                scriptEntry.setFinished(true);
            } else {
                // Record its position in case the entity dies
                lastLocation = lastEntity.getLocation();
                lastVelocity = lastEntity.getVelocity();
            }
        }
    };
    if (script != null || scriptEntry.shouldWaitFor()) {
        task.runTaskTimer(Denizen.getInstance(), 1, 2);
    }
}
Also used : Utilities(com.denizenscript.denizen.utilities.Utilities) LocationTag(com.denizenscript.denizen.objects.LocationTag) NMSHandler(com.denizenscript.denizen.nms.NMSHandler) Projectile(org.bukkit.entity.Projectile) HashMap(java.util.HashMap) Debug(com.denizenscript.denizencore.utilities.debugging.Debug) InvalidArgumentsException(com.denizenscript.denizencore.exceptions.InvalidArgumentsException) Holdable(com.denizenscript.denizencore.scripts.commands.Holdable) EventHandler(org.bukkit.event.EventHandler) ScriptQueue(com.denizenscript.denizencore.scripts.queues.ScriptQueue) Map(java.util.Map) ScriptEntry(com.denizenscript.denizencore.scripts.ScriptEntry) ElementTag(com.denizenscript.denizencore.objects.core.ElementTag) EntityDamageByEntityEvent(org.bukkit.event.entity.EntityDamageByEntityEvent) Bukkit(org.bukkit.Bukkit) Listener(org.bukkit.event.Listener) ListTag(com.denizenscript.denizencore.objects.core.ListTag) Velocity(com.denizenscript.denizen.utilities.entity.Velocity) Entity(org.bukkit.entity.Entity) BukkitRunnable(org.bukkit.scheduler.BukkitRunnable) UUID(java.util.UUID) Position(com.denizenscript.denizen.utilities.entity.Position) Denizen(com.denizenscript.denizen.Denizen) Vector(org.bukkit.util.Vector) Consumer(java.util.function.Consumer) List(java.util.List) ScriptUtilities(com.denizenscript.denizencore.utilities.ScriptUtilities) EventPriority(org.bukkit.event.EventPriority) EntityTag(com.denizenscript.denizen.objects.EntityTag) com.denizenscript.denizencore.objects(com.denizenscript.denizencore.objects) ScriptTag(com.denizenscript.denizencore.objects.core.ScriptTag) AbstractCommand(com.denizenscript.denizencore.scripts.commands.AbstractCommand) CoreUtilities(com.denizenscript.denizencore.utilities.CoreUtilities) Conversion(com.denizenscript.denizen.utilities.Conversion) TaskScriptContainer(com.denizenscript.denizencore.scripts.containers.core.TaskScriptContainer) ProjectileHitEvent(org.bukkit.event.entity.ProjectileHitEvent) BukkitRunnable(org.bukkit.scheduler.BukkitRunnable) ListTag(com.denizenscript.denizencore.objects.core.ListTag) Projectile(org.bukkit.entity.Projectile) LocationTag(com.denizenscript.denizen.objects.LocationTag) ScriptTag(com.denizenscript.denizencore.objects.core.ScriptTag) EntityTag(com.denizenscript.denizen.objects.EntityTag) List(java.util.List) ElementTag(com.denizenscript.denizencore.objects.core.ElementTag) Vector(org.bukkit.util.Vector) ScriptQueue(com.denizenscript.denizencore.scripts.queues.ScriptQueue)

Example 78 with Projectile

use of org.bukkit.entity.Projectile in project Denizen-For-Bukkit by DenizenScript.

the class ShootCommand method arrowDamage.

@EventHandler
public void arrowDamage(EntityDamageByEntityEvent event) {
    Entity arrow = event.getDamager();
    if (!(arrow instanceof Projectile)) {
        return;
    }
    if (!arrows.containsKey(arrow.getUniqueId())) {
        return;
    }
    if (Debug.verbose) {
        Debug.log("Shoot ending because damage");
    }
    arrows.put(arrow.getUniqueId(), new EntityTag(event.getEntity()));
}
Also used : Entity(org.bukkit.entity.Entity) EntityTag(com.denizenscript.denizen.objects.EntityTag) Projectile(org.bukkit.entity.Projectile) EventHandler(org.bukkit.event.EventHandler)

Example 79 with Projectile

use of org.bukkit.entity.Projectile in project Denizen-For-Bukkit by DenizenScript.

the class NPCTagBase method onDeath.

// <--[action]
// @Actions
// death
// death by entity
// death by <entity>
// death by block
// death by <cause>
// 
// @Triggers when the NPC dies.
// 
// @Context
// <context.killer> returns the entity that killed the NPC (if any)
// <context.shooter> returns the shooter of the killing projectile (if any)
// <context.damage> returns the last amount of damage applied (if any)
// <context.death_cause> returns the last damage cause (if any)
// 
// -->
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onDeath(EntityDeathEvent deathEvent) {
    NPC citizen = CitizensAPI.getNPCRegistry().getNPC(deathEvent.getEntity());
    if (citizen == null || !citizen.hasTrait(AssignmentTrait.class)) {
        return;
    }
    NPCTag npc = new NPCTag(citizen);
    EntityDamageEvent event = deathEvent.getEntity().getLastDamageCause();
    String deathCause = event == null ? "unknown" : CoreUtilities.toLowerCase(event.getCause().toString()).replace('_', ' ');
    Map<String, ObjectTag> context = new HashMap<>();
    context.put("damage", new ElementTag(event == null ? 0 : event.getDamage()));
    context.put("death_cause", new ElementTag(deathCause));
    PlayerTag player = null;
    if (event instanceof EntityDamageByEntityEvent) {
        Entity killerEntity = ((EntityDamageByEntityEvent) event).getDamager();
        context.put("killer", new EntityTag(killerEntity).getDenizenObject());
        if (killerEntity instanceof Player) {
            player = PlayerTag.mirrorBukkitPlayer((Player) killerEntity);
        } else if (killerEntity instanceof Projectile) {
            ProjectileSource shooter = ((Projectile) killerEntity).getShooter();
            if (shooter instanceof LivingEntity) {
                context.put("shooter", new EntityTag((LivingEntity) shooter).getDenizenObject());
                if (shooter instanceof Player) {
                    player = PlayerTag.mirrorBukkitPlayer((Player) shooter);
                }
                npc.action("death by " + ((LivingEntity) shooter).getType().toString(), player, context);
            }
        }
        npc.action("death by entity", player, context);
        npc.action("death by " + killerEntity.getType().toString(), player, context);
    } else if (event instanceof EntityDamageByBlockEvent) {
        npc.action("death by block", null, context);
    }
    npc.action("death", player, context);
    npc.action("death by " + deathCause, player, context);
}
Also used : NPC(net.citizensnpcs.api.npc.NPC) Entity(org.bukkit.entity.Entity) LivingEntity(org.bukkit.entity.LivingEntity) Player(org.bukkit.entity.Player) HashMap(java.util.HashMap) PlayerTag(com.denizenscript.denizen.objects.PlayerTag) EntityDamageByBlockEvent(org.bukkit.event.entity.EntityDamageByBlockEvent) Projectile(org.bukkit.entity.Projectile) LivingEntity(org.bukkit.entity.LivingEntity) ObjectTag(com.denizenscript.denizencore.objects.ObjectTag) EntityDamageByEntityEvent(org.bukkit.event.entity.EntityDamageByEntityEvent) NPCTag(com.denizenscript.denizen.objects.NPCTag) EntityTag(com.denizenscript.denizen.objects.EntityTag) ElementTag(com.denizenscript.denizencore.objects.core.ElementTag) ProjectileSource(org.bukkit.projectiles.ProjectileSource) EntityDamageEvent(org.bukkit.event.entity.EntityDamageEvent) EventHandler(org.bukkit.event.EventHandler)

Example 80 with Projectile

use of org.bukkit.entity.Projectile in project FunnyGuilds by FunnyGuilds.

the class EntityDamage method onDamage.

@EventHandler(priority = EventPriority.HIGHEST)
public void onDamage(EntityDamageByEntityEvent event) {
    Entity entity = event.getEntity();
    Entity damager = event.getDamager();
    if (!(entity instanceof Player)) {
        return;
    }
    Player attacker = null;
    if (damager instanceof Player) {
        attacker = (Player) damager;
    } else if (damager instanceof Projectile) {
        ProjectileSource shooter = ((Projectile) damager).getShooter();
        if (shooter instanceof Player) {
            attacker = (Player) shooter;
        }
    }
    if (attacker == null) {
        return;
    }
    PluginConfig config = Settings.getConfig();
    User victimUser = User.get((Player) event.getEntity());
    User attackerUser = User.get(attacker);
    if (victimUser.hasGuild() && attackerUser.hasGuild()) {
        if (victimUser.getUUID().equals(attackerUser.getUUID())) {
            return;
        }
        if (victimUser.getGuild().equals(attackerUser.getGuild())) {
            if (!victimUser.getGuild().getPvP()) {
                event.setCancelled(true);
                return;
            }
        }
        if (victimUser.getGuild().getAllies().contains(attackerUser.getGuild())) {
            if (!config.damageAlly) {
                event.setCancelled(true);
                return;
            }
        }
    }
    if (config.assistEnable && !event.isCancelled()) {
        victimUser.addDamage(attackerUser, event.getDamage());
    }
}
Also used : PluginConfig(net.dzikoysk.funnyguilds.data.configs.PluginConfig) Entity(org.bukkit.entity.Entity) Player(org.bukkit.entity.Player) User(net.dzikoysk.funnyguilds.basic.User) ProjectileSource(org.bukkit.projectiles.ProjectileSource) Projectile(org.bukkit.entity.Projectile) EventHandler(org.bukkit.event.EventHandler)

Aggregations

Projectile (org.bukkit.entity.Projectile)86 EventHandler (org.bukkit.event.EventHandler)55 Player (org.bukkit.entity.Player)44 Entity (org.bukkit.entity.Entity)37 LivingEntity (org.bukkit.entity.LivingEntity)28 ProjectileSource (org.bukkit.projectiles.ProjectileSource)18 EntityDamageByEntityEvent (org.bukkit.event.entity.EntityDamageByEntityEvent)13 Location (org.bukkit.Location)9 Animals (org.bukkit.entity.Animals)9 Monster (org.bukkit.entity.Monster)9 Vector (org.bukkit.util.Vector)9 Villager (org.bukkit.entity.Villager)8 ItemStack (org.bukkit.inventory.ItemStack)8 Hanging (org.bukkit.entity.Hanging)7 EntityDamageEvent (org.bukkit.event.entity.EntityDamageEvent)7 PotionEffect (org.bukkit.potion.PotionEffect)7 Block (org.bukkit.block.Block)6 Arrow (org.bukkit.entity.Arrow)6 Region (br.net.fabiozumbi12.RedProtect.Bukkit.Region)4 NotRegisteredException (com.palmergames.bukkit.towny.exceptions.NotRegisteredException)4