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

The following examples show how to use org.bukkit.entity.Player#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: JumpPads.java    From HubBasics with GNU Lesser General Public License v3.0 7 votes vote down vote up
@EventHandler
public void onPlayerMove(PlayerMoveEvent event) {
    if (needsPressurePlate(event.getPlayer()) || !isBlockRequired(event.getPlayer())) return;
    Player player = event.getPlayer();
    if (!isEnabledInWorld(player.getWorld())) return;
    Location loc = player.getLocation().subtract(0, 1, 0);
    if (loc.getBlock().getType() == getMaterial(player)) {
        player.setVelocity(calculateVector(player));
        if (getSound(player) != null) {
            player.playSound(player.getLocation(), getSound(player), 1, 1);
        }
        if (getEffect(player) != null) {
            Effect effect = this.getEffect(player);
            ReflectionUtils.invokeMethod(player.spigot(), this.playEffectMethod, player.getLocation(),
                    getEffect(player), effect.getId(), 0, 1, 1, 1, 1, 40, 3);
        }
    }
}
 
Example 2
Source File: WindStaff.java    From Slimefun4 with GNU General Public License v3.0 6 votes vote down vote up
@Override
public ItemUseHandler getItemHandler() {
    return e -> {
        Player p = e.getPlayer();

        if (p.getFoodLevel() >= 2) {
            if (p.getInventory().getItemInMainHand().getType() != Material.SHEARS && p.getGameMode() != GameMode.CREATIVE) {
                FoodLevelChangeEvent event = new FoodLevelChangeEvent(p, p.getFoodLevel() - 2);
                Bukkit.getPluginManager().callEvent(event);
                p.setFoodLevel(event.getFoodLevel());
            }

            p.setVelocity(p.getEyeLocation().getDirection().multiply(4));
            p.getWorld().playSound(p.getLocation(), Sound.ENTITY_TNT_PRIMED, 1, 1);
            p.getWorld().playEffect(p.getLocation(), Effect.SMOKE, 1);
            p.setFallDistance(0F);
        }
        else {
            SlimefunPlugin.getLocalization().sendMessage(p, "messages.hungry", true);
        }
    };
}
 
Example 3
Source File: JumpPads.java    From HubBasics with GNU Lesser General Public License v3.0 6 votes vote down vote up
@EventHandler
public void onPlayerInteract(PlayerInteractEvent event) {
    if (!needsPressurePlate(event.getPlayer())) return;
    Player player = event.getPlayer();
    if (event.getAction() == Action.PHYSICAL && !event.isCancelled() && isEnabledInWorld(player.getWorld())) {
        if (event.getClickedBlock().getType() == getPlateType(player)) {
            boolean apply = true;
            if (isBlockRequired(player)) {
                Location loc = event.getClickedBlock().getLocation().subtract(0, 1, 0);
                apply = loc.getWorld().getBlockAt(loc).getType() == getMaterial(player);
            }
            if (apply) {
                player.setVelocity(calculateVector(player));
                if (getSound(player) != null) {
                    player.playSound(player.getLocation(), getSound(player), 1, 1);
                }
                if (getEffect(player) != null) {
                    particleEffect.display(getEffect(player), player.getLocation(), 1, 1, 1, 1, 40);
                }
                event.setCancelled(true);
            }
        }
    }
}
 
Example 4
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 5
Source File: JumpPads.java    From HubBasics with GNU Lesser General Public License v3.0 6 votes vote down vote up
@EventHandler
public void onPlayerInteract(PlayerInteractEvent event) {
    if (!needsPressurePlate(event.getPlayer())) return;
    Player player = event.getPlayer();
    if (event.getAction() == Action.PHYSICAL && !event.isCancelled() && isEnabledInWorld(player.getWorld())) {
        if (event.getClickedBlock().getType() == getPlateType(player)) {
            boolean apply = true;
            if (isBlockRequired(player)) {
                Location loc = event.getClickedBlock().getLocation().subtract(0, 1, 0);
                apply = loc.getWorld().getBlockAt(loc).getType() == getMaterial(player);
            }
            if (apply) {
                player.setVelocity(calculateVector(player));
                if (getSound(player) != null) {
                    player.playSound(player.getLocation(), getSound(player), 1, 1);
                }
                if (getEffect(player) != null) {
                    Effect effect = this.getEffect(player);
                    ReflectionUtils.invokeMethod(player.spigot(), this.playEffectMethod, player.getLocation(),
                            getEffect(player), effect.getId(), 0, 1, 1, 1, 1, 40, 3);
                }
                event.setCancelled(true);
            }
        }
    }
}
 
Example 6
Source File: Acrobat.java    From AnnihilationPro with MIT License 6 votes vote down vote up
@EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true)
public void AcrobatDoubleJump(PlayerToggleFlightEvent event) 
{  
	Player player = event.getPlayer();
	if(player.getGameMode() != GameMode.CREATIVE) 
	{
		AnniPlayer p = AnniPlayer.getPlayer(player.getUniqueId());
		if(Game.isGameRunning() && p != null && p.getKit().equals(this))
		{
			Delays.getInstance().addDelay(player, System.currentTimeMillis()+10000, this.getInternalName());
		    event.setCancelled(true);
		    player.setAllowFlight(false);
		    player.setFlying(false);		    
		    player.setVelocity(player.getLocation().getDirection().setY(1).multiply(1));
		    player.playSound(player.getLocation(), Sound.ZOMBIE_INFECT, 1.0F, 2.0F);
		}
		else
		{
			player.setAllowFlight(false);
			player.setFlying(false);
		}
	}
}
 
Example 7
Source File: JumpPads.java    From HubBasics with GNU Lesser General Public License v3.0 6 votes vote down vote up
@EventHandler
public void onPlayerMove(PlayerMoveEvent event) {
    if (needsPressurePlate(event.getPlayer()) || !isBlockRequired(event.getPlayer())) return;
    Player player = event.getPlayer();
    if (!isEnabledInWorld(player.getWorld())) return;
    Location loc = player.getLocation().subtract(0, 1, 0);
    if (loc.getBlock().getType() == getMaterial(player)) {
        player.setVelocity(calculateVector(player));
        if (getSound(player) != null) {
            player.playSound(player.getLocation(), getSound(player), 1, 1);
        }
        if (getEffect(player) != null) {
            particleEffect.display(getEffect(player), player.getLocation(), 1, 1, 1, 1, 40);
        }
    }
}
 
Example 8
Source File: Scout.java    From AnnihilationPro with MIT License 5 votes vote down vote up
private void pullPlayerSlightly(Player p, Location loc)
{
	if (loc.getY() > p.getLocation().getY())
	{
		p.setVelocity(new Vector(0.0D, 0.25D, 0.0D));
		return;
	}

	Location playerLoc = p.getLocation();

	Vector vector = loc.toVector().subtract(playerLoc.toVector());
	p.setVelocity(vector);
}
 
Example 9
Source File: CrateControl.java    From Crazy-Crates with MIT License 5 votes vote down vote up
public static void knockBack(Player player, Location location) {
    Vector vector = player.getLocation().toVector().subtract(location.toVector()).normalize().multiply(1).setY(.1);
    if (player.isInsideVehicle()) {
        player.getVehicle().setVelocity(vector);
        return;
    }
    player.setVelocity(vector);
}
 
Example 10
Source File: Game.java    From BedwarsRel with GNU General Public License v3.0 5 votes vote down vote up
private void teleportPlayersToTeamSpawn() {
  for (Team team : this.teams.values()) {
    for (Player player : team.getPlayers()) {
      if (!player.getWorld().equals(team.getSpawnLocation().getWorld())) {
        this.getPlayerSettings(player).setTeleporting(true);
      }
      player.setVelocity(new Vector(0, 0, 0));
      player.setFallDistance(0.0F);
      player.teleport(team.getSpawnLocation());
      if (this.getPlayerStorage(player) != null) {
        this.getPlayerStorage(player).clean();
      }
    }
  }
}
 
Example 11
Source File: CFIPacketListener.java    From FastAsyncWorldedit with GNU General Public License v3.0 5 votes vote down vote up
@EventHandler
public void onTeleport(PlayerTeleportEvent event) {
    final Player player = event.getPlayer();
    VirtualWorld gen = getGenerator(player);
    if (gen != null) {
        Location from = event.getFrom();
        Location to = event.getTo();
        if (to.getWorld().equals(from.getWorld()) && to.distanceSquared(from) < 8) {
            event.setTo(player.getLocation());
            event.setCancelled(true);
            player.setVelocity(player.getVelocity());
        }
    }
}
 
Example 12
Source File: Leaper.java    From ZombieEscape with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void interact(PlayerInteractEvent event, Player player, ItemStack itemStack) {
    if (Cooldowns.tryCooldown(player, "leaper", 1000 * 3)) {
        player.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, 20 * 5, 1));
        Vector vector = player.getLocation().getDirection().multiply(1.0);
        vector.setY(0.35);
        player.setVelocity(vector);
    } else {
        Messages.COOLDOWN.send(player);
    }
}
 
Example 13
Source File: WorldGuardEvents.java    From uSkyBlock with GNU General Public License v3.0 5 votes vote down vote up
@EventHandler
@SuppressWarnings("unused")
public void onPlayerMove(PlayerMoveEvent e) {
    if (e.getTo() == null || !plugin.getWorldManager().isSkyAssociatedWorld(e.getTo().getWorld())) {
        return;
    }
    String islandNameAt = WorldGuardHandler.getIslandNameAt(e.getTo());
    if (islandNameAt == null) {
        return;
    }
    IslandInfo islandInfo = plugin.getIslandInfo(islandNameAt);
    if (islandInfo == null || islandInfo.getBans().isEmpty()) {
        return;
    }
    Player player = e.getPlayer();
    if (!player.isOp() && !player.hasPermission("usb.mod.bypassprotection") && isBlockedFromEntry(player, islandInfo)) {
        e.setCancelled(true);
        Location l = e.getTo().clone();
        l.subtract(islandInfo.getIslandLocation());
        Vector v = new Vector(l.getX(), l.getY(), l.getZ());
        v.normalize();
        v.multiply(1.5); // Bounce
        player.setVelocity(v);
        if (islandInfo.isBanned(player)) {
            plugin.notifyPlayer(player, tr("\u00a7cBanned:\u00a7e You are banned from this island."));
        } else {
            plugin.notifyPlayer(player, tr("\u00a7cLocked:\u00a7e That island is locked! No entry allowed."));
        }
    }
}
 
Example 14
Source File: QuadCrate.java    From Crazy-Crates with MIT License 5 votes vote down vote up
@EventHandler
public void onPlayerMove(PlayerMoveEvent e) {
    Player player = e.getPlayer();
    if (QuadCrateSession.inSession(player)) {//Player tries to walk away from the crate area
        Location from = e.getFrom();
        Location to = e.getTo();
        if (from.getBlockX() != to.getBlockX() || from.getBlockZ() != to.getBlockZ()) {
            e.setCancelled(true);
            player.teleport(from);
            return;
        }
    }
    for (Entity en : player.getNearbyEntities(2, 2, 2)) {//Someone tries to enter the crate area
        if (en instanceof Player) {
            Player p = (Player) en;
            if (QuadCrateSession.inSession(p)) {
                Vector v = player.getLocation().toVector().subtract(p.getLocation().toVector()).normalize().setY(1);
                if (player.isInsideVehicle()) {
                    player.getVehicle().setVelocity(v);
                } else {
                    player.setVelocity(v);
                }
                break;
            }
        }
    }
}
 
Example 15
Source File: CitizensNpcFactory.java    From helper with MIT License 5 votes vote down vote up
private void tickNpcs() {
    for (NPC npc : this.npcRegistry) {
        if (!npc.isSpawned() || !npc.hasTrait(ClickableTrait.class)) continue;

        Npc helperNpc = npc.getTrait(ClickableTrait.class).npc;

        // ensure npcs stay in the same position
        Location loc = npc.getEntity().getLocation();
        if (loc.getBlockX() != helperNpc.getInitialSpawn().getBlockX() || loc.getBlockZ() != helperNpc.getInitialSpawn().getBlockZ()) {
            npc.teleport(helperNpc.getInitialSpawn().clone(), PlayerTeleportEvent.TeleportCause.PLUGIN);
        }

        // don't let players stand near npcs
        for (Entity entity : npc.getStoredLocation().getWorld().getNearbyEntities(npc.getStoredLocation(), 1.0, 1.0, 1.0)) {
            if (!(entity instanceof Player) || this.npcRegistry.isNPC(entity)) continue;

            final Player p = (Player) entity;

            if (p.getGameMode() == GameMode.CREATIVE || p.getGameMode() == GameMode.SPECTATOR) {
                continue;
            }

            if (npc.getEntity().getLocation().distance(p.getLocation()) < 3.5) {
                p.setVelocity(p.getLocation().getDirection().multiply(-0.5).setY(0.4));
            }
        }
    }
}
 
Example 16
Source File: AttackPush.java    From EliteMobs with GNU General Public License v3.0 4 votes vote down vote up
@EventHandler
public void attackPush(EntityDamageByEntityEvent event) {

    EliteMobEntity eliteMobEntity = EventValidator.getEventEliteMob(this, event);
    if (eliteMobEntity == null) return;
    Player player = EntityFinder.findPlayer(event);
    if (PowerCooldown.isInCooldown(eliteMobEntity, cooldownList)) return;

    Vector pushbackDirection = player.getLocation().subtract(eliteMobEntity.getLivingEntity().getLocation()).toVector();
    Vector pushbackApplied = pushbackDirection.normalize().multiply(3);

    player.setVelocity(pushbackApplied);

    PowerCooldown.startCooldownTimer(eliteMobEntity, cooldownList, 10 * 20);

}
 
Example 17
Source File: BlockPlace.java    From FunnyGuilds with Apache License 2.0 4 votes vote down vote up
@EventHandler(priority = EventPriority.LOWEST)
public void onPlace(BlockPlaceEvent event) {
    Player player = event.getPlayer();
    Block block = event.getBlock();
    Material type = block.getType();

    Location blockLocation = block.getLocation();
    if (!ProtectionSystem.build(player, blockLocation, true)) {
        return;
    }

    // always cancel to prevent breaking other protection
    // plugins or plugins using BlockPlaceEvent (eg. special ability blocks)
    event.setCancelled(true);

    // disabled bugged-blocks or blacklisted item
    if (!this.config.buggedBlocks || this.config.buggedBlocksExclude.contains(type)) {
        return;
    }

    // remove one item from the player
    ItemStack itemInHand = event.getItemInHand();
    if ((itemInHand.getAmount() - 1) == 0) {
        // wondering why? because bukkit and you probably don't want dupe glitches
        if (Reflections.USE_PRE_9_METHODS) {
            player.setItemInHand(null);
        } else {
            itemInHand.setAmount(0);
        }
    } else {
        itemInHand.setAmount(itemInHand.getAmount() - 1);
    }

    // if the player is standing on the placed block add some velocity to prevent glitching
    // side effect: velocity with +y0.4 is like equivalent to jumping while building, just hold right click, that's real fun!
    Location playerLocation = player.getLocation();
    boolean sameColumn = (playerLocation.getBlockX() == blockLocation.getBlockX()) && (playerLocation.getBlockZ() == blockLocation.getBlockZ());
    double distanceUp = (playerLocation.getY() - blockLocation.getBlockY());
    boolean upToTwoBlocks = (distanceUp > 0) && (distanceUp <= 2);
    if (sameColumn && upToTwoBlocks) {
        player.setVelocity(ANTI_GLITCH_VELOCITY);
    }

    // delay, because we cannot do {@link Block#setType(Material)} immediately
    Bukkit.getScheduler().runTask(FunnyGuilds.getInstance(), () -> {

        // fake place for bugged block
        block.setType(type);

        // start timer and return the item to the player if specified to do so
        ItemStack returnItem = event.getItemInHand().clone();
        returnItem.setAmount(1);

        Bukkit.getScheduler().runTaskLater(FunnyGuilds.getInstance(), () -> {
            event.getBlockReplacedState().update(true);
            if (!player.isOnline()) {
                return;
            }
            if (this.config.buggedBlockReturn) {
                player.getInventory().addItem(returnItem);
            }
        }, this.config.buggedBlocksTimer);

    });
}
 
Example 18
Source File: NoEntryExpansion.java    From CombatLogX with GNU General Public License v3.0 4 votes vote down vote up
private void knockbackPlayer(Player player, Location fromLoc, Location toLoc) {
    if(player == null) return;

    Vector vector = getVector(fromLoc, toLoc);
    player.setVelocity(vector);
}
 
Example 19
Source File: GadgetHandler.java    From StaffPlus with GNU General Public License v3.0 4 votes vote down vote up
public void onCompass(Player player)
{
	Vector vector = player.getLocation().getDirection();
	
	player.setVelocity(vector.multiply(options.modeCompassVelocity));
}
 
Example 20
Source File: AttackVacuum.java    From EliteMobs with GNU General Public License v3.0 3 votes vote down vote up
@EventHandler
public void onHit(EntityDamageByEntityEvent event) {

    EliteMobEntity eliteMobEntity = EventValidator.getEventEliteMob(this, event);
    if (eliteMobEntity == null) return;
    Player player = EntityFinder.findPlayer(event);
    if (PowerCooldown.isInCooldown(eliteMobEntity, cooldownList)) return;

    player.setVelocity(eliteMobEntity.getLivingEntity().getLocation().clone().subtract(player.getLocation()).toVector().normalize().multiply(3));

    PowerCooldown.startCooldownTimer(eliteMobEntity, cooldownList, 5 * 20);

}