Java Code Examples for org.bukkit.entity.Entity#setVelocity()

The following examples show how to use org.bukkit.entity.Entity#setVelocity() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Vectors.java    From TabooLib with MIT License 6 votes vote down vote up
public static void entityPush(Entity entity, Location to, double velocity) {
    Location from = entity.getLocation();
    Vector test = to.clone().subtract(from).toVector();
    double elevation = test.getY();
    Double launchAngle = calculateLaunchAngle(from, to, velocity, elevation, 20.0D);
    double distance = Math.sqrt(Math.pow(test.getX(), 2.0D) + Math.pow(test.getZ(), 2.0D));
    if (distance != 0.0D) {
        if (launchAngle == null) {
            launchAngle = Math.atan((40.0D * elevation + Math.pow(velocity, 2.0D)) / (40.0D * elevation + 2.0D * Math.pow(velocity, 2.0D)));
        }
        double hangTime = calculateHangTime(launchAngle, velocity, elevation, 20.0D);
        test.setY(Math.tan(launchAngle) * distance);
        test = normalizeVector(test);
        Vector noise = Vector.getRandom();
        noise = noise.multiply(0.1D);
        test.add(noise);
        velocity = velocity + 1.188D * Math.pow(hangTime, 2.0D) + (Numbers.getRandom().nextDouble() - 0.8D) / 2.0D;
        test = test.multiply(velocity / 20.0D);
        entity.setVelocity(test);
    }
}
 
Example 2
Source File: Scout.java    From AnnihilationPro with MIT License 6 votes vote down vote up
private void pullEntityToLocation(Entity e, Location loc)
{
	Location entityLoc = e.getLocation();

	entityLoc.setY(entityLoc.getY() + 0.5D);
	e.teleport(entityLoc);

	double g = -0.08D;
	double d = loc.distance(entityLoc);
	double t = d;
	double v_x = (1.0D + 0.07000000000000001D * t)
			* (loc.getX() - entityLoc.getX()) / t;
	double v_y = (1.0D + 0.03D * t) * (loc.getY() - entityLoc.getY()) / t
			- 0.5D * g * t;
	double v_z = (1.0D + 0.07000000000000001D * t)
			* (loc.getZ() - entityLoc.getZ()) / t;

	Vector v = e.getVelocity();
	v.setX(v_x);
	v.setY(v_y);
	v.setZ(v_z);
	e.setVelocity(v);

	//addNoFall(e, 100);
}
 
Example 3
Source File: SlimefunBootsListener.java    From Slimefun4 with GNU General Public License v3.0 6 votes vote down vote up
private void stomp(EntityDamageEvent e) {
    Player p = (Player) e.getEntity();
    p.getWorld().playSound(p.getLocation(), Sound.ENTITY_ZOMBIE_BREAK_WOODEN_DOOR, 1F, 2F);
    p.setVelocity(new Vector(0.0, 0.7, 0.0));

    for (Entity n : p.getNearbyEntities(4, 4, 4)) {
        if (n instanceof LivingEntity && !n.getUniqueId().equals(p.getUniqueId())) {
            Vector velocity = n.getLocation().toVector().subtract(p.getLocation().toVector()).normalize().multiply(1.4);
            n.setVelocity(velocity);

            if (!(n instanceof Player) || (p.getWorld().getPVP() && SlimefunPlugin.getProtectionManager().hasPermission(p, n.getLocation(), ProtectableAction.PVP))) {
                EntityDamageByEntityEvent event = new EntityDamageByEntityEvent(p, n, DamageCause.ENTITY_ATTACK, e.getDamage() / 2);
                Bukkit.getPluginManager().callEvent(event);
                if (!event.isCancelled()) ((LivingEntity) n).damage(e.getDamage() / 2);
            }
        }
    }

    for (BlockFace face : BlockFace.values()) {
        Block b = p.getLocation().getBlock().getRelative(BlockFace.DOWN).getRelative(face);
        p.getWorld().playEffect(b.getLocation(), Effect.STEP_SOUND, b.getType());
    }
}
 
Example 4
Source File: PushbackCharger.java    From QualityArmory with GNU General Public License v3.0 5 votes vote down vote up
@SuppressWarnings("deprecation")
@Override
public boolean shoot(Gun g, Player p, ItemStack i) {

	Location start = p.getEyeLocation().clone();
	Vector go = p.getLocation().getDirection().normalize();
	//go.add(new Vector((Math.random() * 2 * sway) - sway, (Math.random() * 2 * sway) - sway,
	//		(Math.random() * 2 * sway) - sway));
	GunUtil.playShoot(g,  p);

	boolean lookup = (go.getY() > go.getX() && go.getY() > go.getZ());
	boolean lookdown = (-go.getY() > go.getX() && -go.getY() > go.getZ());
	
	double degreeVector = Math.atan2(go.getX(), go.getZ());
	if (degreeVector > Math.PI)
		degreeVector = 2 * Math.PI - degreeVector;
	for (Entity e : p.getNearbyEntities(g.getMaxDistance(), g.getMaxDistance(), g.getMaxDistance())) {
		double dis = e.getLocation().distance(start);
		if (e instanceof Damageable)
			if (e != p && e != p.getVehicle() && e != p.getPassenger()) {
				double degreeEntity = Math.atan2(e.getLocation().getX() - start.getX(),
						e.getLocation().getZ() - start.getZ());
				if (degreeEntity > Math.PI)
					degreeEntity = 2 * Math.PI - degreeEntity;
				
				if ((lookup && e.getLocation().getY() > start.getY()) || (lookdown && e.getLocation().getY() < start.getY()) ||
						
						
						(!lookdown&&!lookup&&Math.max(degreeEntity, degreeVector)
						- Math.min(degreeEntity, degreeVector) < (dis > 10 ? Math.PI / 4 : Math.PI / 2))) {
					
					Vector pushback = new Vector(e.getLocation().getX() - start.getX(),e.getLocation().getY() - start.getY(),
							e.getLocation().getZ() - start.getZ());
					pushback.normalize().multiply(g.getMaxDistance()/(dis));
					e.setVelocity(pushback);
				}
			}
	}
	return false;
}
 
Example 5
Source File: InfusedHopper.java    From Slimefun4 with GNU General Public License v3.0 5 votes vote down vote up
@Override
public BlockTicker getItemHandler() {
    return new BlockTicker() {

        @Override
        public void tick(Block b, SlimefunItem sfItem, Config data) {
            if (b.getType() != Material.HOPPER) {
                // we're no longer a hopper, we were probably destroyed. skipping this tick.
                BlockStorage.clearBlockInfo(b);
                return;
            }

            Location l = b.getLocation().add(0.5, 1.2, 0.5);
            boolean sound = false;
            double range = radius.getValue();

            for (Entity item : b.getWorld().getNearbyEntities(l, range, range, range, n -> isValidItem(l, n))) {
                item.setVelocity(new Vector(0, 0.1, 0));
                item.teleport(l);
                sound = true;
            }

            if (sound && !silent.getValue().booleanValue()) {
                b.getWorld().playSound(b.getLocation(), Sound.ENTITY_ENDERMAN_TELEPORT, 1F, 2F);
            }
        }

        @Override
        public boolean isSynchronized() {
            return true;
        }
    };
}
 
Example 6
Source File: SeismicAxe.java    From Slimefun4 with GNU General Public License v3.0 5 votes vote down vote up
private void pushEntity(Player p, Entity entity) {
    Vector vector = entity.getLocation().toVector().subtract(p.getLocation().toVector()).normalize();
    vector.multiply(STRENGTH);
    vector.setY(0.9);
    entity.setVelocity(vector);

    if (entity.getType() != EntityType.PLAYER || p.getWorld().getPVP()) {
        EntityDamageByEntityEvent event = new EntityDamageByEntityEvent(p, entity, DamageCause.ENTITY_ATTACK, 6D);
        Bukkit.getPluginManager().callEvent(event);

        if (!event.isCancelled()) {
            ((LivingEntity) entity).damage(DAMAGE);
        }
    }
}
 
Example 7
Source File: SafeBoat.java    From askyblock with GNU General Public License v2.0 5 votes vote down vote up
/**
 * @param e - event
 *            This event check throws the boat at a player when they hit it
 *            unless someone is in it
 */
@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
public void onClick(VehicleDamageEvent e) {
    // plugin.getLogger().info("Damage event " + e.getDamage());
    // Find out what block is being clicked
    Vehicle boat = e.getVehicle();
    if (!(boat instanceof Boat)) {
        return;
    }
    if (!boat.isEmpty()) {
        return;
    }
    final World playerWorld = boat.getWorld();
    if (!playerWorld.getName().equalsIgnoreCase(Settings.worldName)) {
        // Not the right world
        return;
    }
    // plugin.getLogger().info("Boat ");
    // Find out who is doing the clicking
    if (!(e.getAttacker() instanceof Player)) {
        // If a creeper blows up the boat, tough cookies!
        return;
    }
    Player p = (Player) e.getAttacker();
    if (p == null) {
        return;
    }
    // Try to remove the boat and throw it at the player
    Location boatSpot = new Location(boat.getWorld(), boat.getLocation().getX(), boat.getLocation().getY() + 2, boat.getLocation().getZ());
    Location throwTo = new Location(boat.getWorld(), p.getLocation().getX(), p.getLocation().getY() + 1, p.getLocation().getZ());
    ItemStack newBoat = new ItemStack(Material.BOAT, 1);
    // Find the direction the boat should move in
    Vector dir = throwTo.toVector().subtract(boatSpot.toVector()).normalize();
    dir = dir.multiply(0.5);
    Entity newB = boat.getWorld().dropItem(boatSpot, newBoat);
    newB.setVelocity(dir);
    boat.remove();
    e.setCancelled(true);
}
 
Example 8
Source File: JumpEffect.java    From EffectLib with MIT License 5 votes vote down vote up
@Override
public void onRun() {
    Entity entity = getEntity();
    if (entity == null) {
        cancel();
        return;
    }
    Vector v = entity.getVelocity();
    v.setY(v.getY() + power);
    entity.setVelocity(v);
}
 
Example 9
Source File: Projectiles.java    From CardinalPGM with MIT License 5 votes vote down vote up
@EventHandler
public void onEntityHitByProjectile(EntityDamageByEntityEvent event) {
    if (event.isCancelled()) return;
    if (event.getCause().equals(DamageCause.PROJECTILE)) {
        ProjectileSource source = ((Projectile) event.getDamager()).getShooter();
        if (source instanceof Player) {
            ((Player) source).playSound(((Player) source).getLocation(), Sound.ENTITY_ARROW_HIT_PLAYER, 0.2F, 0.5F);
        }
        if (event.getDamager().getType().equals(projectile) && event.getDamager().hasMetadata("custom")) {
            Entity arrow = event.getEntity().getWorld().spawnEntity(event.getDamager().getLocation(), EntityType.ARROW);
            ((Projectile) arrow).setShooter(source);
            arrow.setVelocity(event.getDamager().getVelocity());
            event.getDamager().remove();
            if (event.getEntity() instanceof LivingEntity) {
                for (PotionEffect effect : potionEffects) {
                    ((LivingEntity) event.getEntity()).addPotionEffect(effect);
                }
                final Entity entity = event.getEntity();
                Bukkit.getServer().getScheduler().runTaskLater(GameHandler.getGameHandler().getPlugin(), new Runnable() {
                    @Override
                    public void run() {
                        ((LivingEntity) entity).setArrowsStuck(((LivingEntity) entity).getArrowsStuck() - 1);
                    }
                }, 0);
            }
        }
    }
}
 
Example 10
Source File: ModifyBowProjectileMatchModule.java    From PGM with GNU Affero General Public License v3.0 4 votes vote down vote up
@EventHandler(ignoreCancelled = true)
public void changeBowProjectile(EntityShootBowEvent event) {
  Plugin plugin = PGM.get();
  Entity newProjectile;

  if (this.cls == Arrow.class && event.getProjectile() instanceof Arrow) {
    // Don't change the projectile if it's an Arrow and the custom entity type is also Arrow
    newProjectile = event.getProjectile();
  } else {
    // Replace the projectile
    Projectile oldEntity = (Projectile) event.getProjectile();
    if (Projectile.class.isAssignableFrom(this.cls)) {
      newProjectile = event.getEntity().launchProjectile((Class<? extends Projectile>) this.cls);
    } else {
      World world = event.getEntity().getWorld();
      newProjectile = world.spawn(oldEntity.getLocation(), this.cls);
    }
    event.setProjectile(newProjectile);

    // Copy some things from the old projectile
    newProjectile.setVelocity(oldEntity.getVelocity());
    newProjectile.setFallDistance(oldEntity.getFallDistance());
    newProjectile.setFireTicks(oldEntity.getFireTicks());

    if (newProjectile instanceof Projectile) {
      ((Projectile) newProjectile).setShooter(oldEntity.getShooter());
      ((Projectile) newProjectile).setBounce(oldEntity.doesBounce());
    }

    // Save some special properties of Arrows
    if (oldEntity instanceof Arrow) {
      Arrow arrow = (Arrow) oldEntity;
      newProjectile.setMetadata("critical", new FixedMetadataValue(plugin, arrow.isCritical()));
      newProjectile.setMetadata(
          "knockback", new FixedMetadataValue(plugin, arrow.getKnockbackStrength()));
      newProjectile.setMetadata(
          "damage", new FixedMetadataValue(plugin, arrow.spigot().getDamage()));
    }
  }

  // Tag the projectile as custom
  newProjectile.setMetadata("customProjectile", new FixedMetadataValue(plugin, true));

  match.callEvent(new EntityLaunchEvent(newProjectile, event.getEntity()));
}
 
Example 11
Source File: ModifyBowProjectileMatchModule.java    From ProjectAres with GNU Affero General Public License v3.0 4 votes vote down vote up
@EventHandler(ignoreCancelled = true)
public void changeBowProjectile(EntityShootBowEvent event) {
    Plugin plugin = this.getMatch().getPlugin();
    Entity newProjectile;

    if(this.projectile == Arrow.class && event.getProjectile() instanceof Arrow) {
        // Don't change the projectile if it's an Arrow and the custom entity type is also Arrow
        newProjectile = event.getProjectile();
    } else {
        // Replace the projectile
        World world = event.getEntity().getWorld();
        Projectile oldEntity = (Projectile) event.getProjectile();

        if(Projectile.class.isAssignableFrom(projectile)) {
            // Ender Pearls need to be soawned this way, otherwise they will hit the shooter sometimes
            newProjectile = event.getEntity().launchProjectile(projectile.asSubclass(Projectile.class), oldEntity.getVelocity());
        } else {
            newProjectile = world.spawn(oldEntity.getLocation(), projectile);
            newProjectile.setVelocity(oldEntity.getVelocity());
        }

        event.setProjectile(newProjectile);

        // Copy some things from the old projectile
        newProjectile.setFallDistance(oldEntity.getFallDistance());
        newProjectile.setFireTicks(oldEntity.getFireTicks());

        if(newProjectile instanceof Projectile) {
            ((Projectile) newProjectile).setShooter(oldEntity.getShooter());
            ((Projectile) newProjectile).setBounce(oldEntity.doesBounce());
        }

        // Save some special properties of Arrows
        if(oldEntity instanceof Arrow) {
            Arrow arrow = (Arrow) oldEntity;
            newProjectile.setMetadata("critical", new FixedMetadataValue(plugin, arrow.isCritical()));
            newProjectile.setMetadata("knockback", new FixedMetadataValue(plugin, arrow.getKnockbackStrength()));
            newProjectile.setMetadata("damage", new FixedMetadataValue(plugin, arrow.getDamage()));
        }
    }

    // Tag the projectile as custom
    newProjectile.setMetadata("customProjectile", new FixedMetadataValue(plugin, true));

    getMatch().callEvent(new EntityLaunchEvent(newProjectile, event.getEntity()));
}
 
Example 12
Source File: Powergloves.java    From ce with GNU Lesser General Public License v3.0 4 votes vote down vote up
@Override
public boolean effect(Event event, final Player player) {
	if(event instanceof PlayerInteractEntityEvent) {
		PlayerInteractEntityEvent e = (PlayerInteractEntityEvent) event;
		e.setCancelled(true);
		final Entity clicked = e.getRightClicked();
		if(!player.hasMetadata("ce." + getOriginalName()))
			if(!clicked.getType().equals(EntityType.PAINTING) && !clicked.getType().equals(EntityType.ITEM_FRAME) && clicked.getPassenger() != player && player.getPassenger() == null) {
				player.setMetadata("ce." + getOriginalName(), new FixedMetadataValue(main, false));

				player.setPassenger(clicked);

				player.getWorld().playEffect(player.getLocation(), Effect.ZOMBIE_CHEW_IRON_DOOR, 10);

				new BukkitRunnable() {

					@Override
					public void run() {
						player.getWorld().playEffect(player.getLocation(), Effect.CLICK2, 10);
						player.setMetadata("ce." + getOriginalName(), new FixedMetadataValue(main, true));
						this.cancel();
					}
				}.runTaskLater(main, ThrowDelayAfterGrab);

				new BukkitRunnable() {

					int			GrabTime	= MaxGrabtime;
					ItemStack	current		= player.getItemInHand();

					@Override
					public void run() {
						if(current.equals(player.getItemInHand())) {
							current = player.getItemInHand();
						if(GrabTime > 0) {
							if(!player.hasMetadata("ce." + getOriginalName())) {
								this.cancel();
							}
							GrabTime--;
						} else if(GrabTime <= 0) {
							if(player.hasMetadata("ce." + getOriginalName())) {
								player.getWorld().playEffect(player.getLocation(), Effect.CLICK1, 10);
								player.removeMetadata("ce." + getOriginalName(), main);
								generateCooldown(player, getCooldown());
							}
							clicked.leaveVehicle();
							this.cancel();
						}
					  } else {
						  player.removeMetadata("ce." + getOriginalName(), main);
						  generateCooldown(player, getCooldown());
						  this.cancel();
					  }
					}
				}.runTaskTimer(main, 0l, 10l);
			}
	} else if(event instanceof PlayerInteractEvent) {
		if(player.hasMetadata("ce." + getOriginalName()) && player.getMetadata("ce." + getOriginalName()).get(0).asBoolean())
				if(player.getPassenger() != null) {
					Entity passenger = player.getPassenger();
					player.getPassenger().leaveVehicle();
					passenger.setVelocity(player.getLocation().getDirection().multiply(ThrowSpeedMultiplier));
					player.getWorld().playEffect(player.getLocation(), Effect.ZOMBIE_DESTROY_DOOR, 10);
					player.removeMetadata("ce." + getOriginalName(), main);
					return true;
				}
	}
	return false;
}
 
Example 13
Source File: CEListener.java    From ce with GNU Lesser General Public License v3.0 4 votes vote down vote up
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void EntityDamageEvent(EntityDamageEvent e) {

    Entity damaged = e.getEntity();

    if (damaged instanceof Player) {

        CEventHandler.handleEvent((Player) damaged, e, damageNature);

        if (damaged.hasMetadata("ce.springs")) {
            e.setCancelled(true);
            Vector vel = damaged.getVelocity();
            vel.setY((vel.getY() * -0.75) > 1 ? vel.getY() * -0.75 : 0);
            damaged.setVelocity(vel);
        }

    }

}
 
Example 14
Source File: EnderBow.java    From NBTEditor with GNU General Public License v3.0 4 votes vote down vote up
@Override
public void onShootBow(EntityShootBowEvent event, DelayedPlayerDetails details) {
	Entity perl = event.getEntity().launchProjectile(EnderPearl.class);
	perl.setVelocity(event.getProjectile().getVelocity());
	event.setProjectile(perl);
}
 
Example 15
Source File: WitherBow.java    From NBTEditor with GNU General Public License v3.0 4 votes vote down vote up
@Override
public void onShootBow(EntityShootBowEvent event, DelayedPlayerDetails details) {
	Entity skull = event.getEntity().launchProjectile(WitherSkull.class);
	skull.setVelocity(event.getProjectile().getVelocity());
	event.setProjectile(skull);
}