下面列出了怎么用org.bukkit.event.player.PlayerTeleportEvent.TeleportCause的API类实例代码及写法,或者点击链接到github查看源代码。
public boolean teleport(Location location, TeleportCause cause) {
Preconditions.checkArgument(location != null, "location");
location.checkFinite();
if (entity.isBeingRidden() || entity.isDead) {
return false;
}
// If this entity is riding another entity, we must dismount before teleporting.
entity.dismountRidingEntity();
entity.world = ((CraftWorld) location.getWorld()).getHandle();
// entity.setLocation() throws no event, and so cannot be cancelled
entity.setPositionAndRotation(location.getX(), location.getY(), location.getZ(), location.getYaw(), location.getPitch());
// SPIGOT-619: Force sync head rotation also
entity.setRotationYawHead(location.getYaw());
return true;
}
@Override
public void execute(CommandSender sender, String label, String[] args) throws CommandException {
Player player = CommandValidator.getPlayerSender(sender);
NamedHologram hologram = CommandValidator.getNamedHologram(args[0]);
hologram.teleport(player.getWorld(), player.getLocation().getX(), player.getLocation().getY(), player.getLocation().getZ());
hologram.despawnEntities();
hologram.refreshAll();
HologramDatabase.saveHologram(hologram);
HologramDatabase.trySaveToDisk();
Location to = player.getLocation();
to.setPitch(90);
player.teleport(to, TeleportCause.PLUGIN);
player.sendMessage(Colors.PRIMARY + "You moved the hologram '" + hologram.getName() + "' near to you.");
}
private void deathListener(Player p, int index) {
RedProtect.get().logger.debug(LogLevel.PLAYER, "Added index " + index);
HashMap<Integer, Location> loc1 = new HashMap<>();
if (!deathLocs.containsKey(p.getName())) {
loc1.put(index, p.getLocation());
deathLocs.put(p.getName(), loc1);
} else {
loc1 = deathLocs.get(p.getName());
loc1.put(index, p.getLocation());
deathLocs.put(p.getName(), loc1);
if (loc1.size() == 2) {
Location from = deathLocs.get(p.getName()).get(0);
Location to = deathLocs.get(p.getName()).get(1);
deathLocs.remove(p.getName());
PlayerTeleportEvent televent = new PlayerTeleportEvent(p, from, to, TeleportCause.PLUGIN);
Bukkit.getPluginManager().callEvent(televent);
}
}
}
private static void prepareForEditor(Player player, GameMap gMap, String worldName) {
World editWorld = SkyWarsReloaded.get().getServer().getWorld(worldName);
editWorld.setAutoSave(true);
for (TeamCard tCard: gMap.getTeamCards()) {
if (tCard.getSpawn() != null) {
editWorld.getBlockAt(tCard.getSpawn().getX(), tCard.getSpawn().getY(), tCard.getSpawn().getZ()).setType(Material.DIAMOND_BLOCK);
}
}
for (CoordLoc cl: gMap.getDeathMatchSpawns()) {
editWorld.getBlockAt(cl.getX(), cl.getY(), cl.getZ()).setType(Material.EMERALD_BLOCK);
}
SkyWarsReloaded.get().getServer().getScheduler().scheduleSyncDelayedTask(SkyWarsReloaded.get(), () -> {
player.teleport(new Location(editWorld, 0, 95, 0), TeleportCause.PLUGIN);
player.setGameMode(GameMode.CREATIVE);
player.setAllowFlight(true);
player.setFlying(true);
}, 20);
}
/**
* Fire a CoarsePlayerMoveEvent that wraps the given event, only if it crosses
* a block boundary, or the PoseFlags change.
* @param event The movement event to potentially wrap
* @return True if the original event was not cancelled, and a coarse event was fired,
* and that coarse event was cancelled. In this case, the wrapped event won't
* actually be cancelled, but callers should treat it like it is.
*/
private boolean callCoarsePlayerMove(final PlayerMoveEvent event) {
// Don't fire coarse events for teleports that are not "in-game"
// e.g. /jumpto command
if(event instanceof PlayerTeleportEvent) {
PlayerTeleportEvent teleportEvent = (PlayerTeleportEvent) event;
if(teleportEvent.getCause() != TeleportCause.ENDER_PEARL &&
teleportEvent.getCause() != TeleportCause.UNKNOWN) {
return false;
}
}
// If the movement does not cross a block boundary, and no PoseFlags changed, we don't care about it
final EntityLocation from = event.getEntityFrom();
final EntityLocation to = event.getEntityTo();
if(from.position().coarseEquals(to.position()) && from.poseFlags().equals(to.poseFlags())) {
return false;
}
// Remember whether the original event was already cancelled
boolean wasCancelled = event.isCancelled();
CoarsePlayerMoveEvent generalEvent = new CoarsePlayerMoveEvent(event, event.getPlayer(), from, to);
this.eventBus.callEvent(generalEvent);
if(!wasCancelled && generalEvent.isCancelled()) {
// When a coarse event is cancelled, we have our own logic for resetting the
// player's position, so we un-cancel the event and instead modify its
// to location to put the player where we want them.
resetPosition(event);
return true;
} else {
return false;
}
}
public void checkSpectators() {
if (!spectators.isEmpty()) {
for (UUID uuid: spectators) {
Player spec = SkyWarsReloaded.get().getServer().getPlayer(uuid);
if (isOutsideBorder(spec)) {
CoordLoc ss = getSpectateSpawn();
Location spectateSpawn = new Location(getCurrentWorld(), ss.getX(), ss.getY(), ss.getZ());
spec.teleport(spectateSpawn, TeleportCause.END_PORTAL);
}
}
}
}
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void onPlayerTeleport(PlayerTeleportEvent e) {
final Player player = e.getPlayer();
final GameMap gameMap = MatchManager.get().getSpectatorMap(player);
if (gameMap == null || player.hasPermission("sw.opteleport")) {
return;
}
if (e.getCause() != TeleportCause.END_PORTAL) {
e.setCancelled(true);
}
}
@EventHandler(priority = EventPriority.NORMAL)
public void onInventoryClick(InventoryClickEvent e) {
final Player player = (Player) e.getWhoClicked();
final GameMap gameMap = MatchManager.get().getSpectatorMap(player);
if (gameMap == null) {
return;
}
int slot = e.getSlot();
if (slot == 8) {
player.closeInventory();
gameMap.getSpectators().remove(player.getUniqueId());
MatchManager.get().removeSpectator(player);
} else if (slot >= 9 && slot <= 35) {
player.closeInventory();
ItemStack item = e.getCurrentItem();
if (item != null && !item.getType().equals(Material.AIR)) {
String name = ChatColor.stripColor(item.getItemMeta().getDisplayName());
Player toSpec = SkyWarsReloaded.get().getServer().getPlayer(name);
if (toSpec != null) {
if (!gameMap.mapContainsDead(toSpec.getUniqueId()) && player != null) {
player.teleport(toSpec.getLocation(), TeleportCause.END_PORTAL);
}
}
}
}
}
@EventHandler(priority = EventPriority.MONITOR)
public void onPlayerTeleportEvent(PlayerTeleportEvent event) {
if (event.getCause().equals(TeleportCause.COMMAND) ||
event.getCause().equals(TeleportCause.PLUGIN)) {
CivLog.info("[TELEPORT] "+event.getPlayer().getName()+" to:"+event.getTo().getBlockX()+","+event.getTo().getBlockY()+","+event.getTo().getBlockZ()+
" from:"+event.getFrom().getBlockX()+","+event.getFrom().getBlockY()+","+event.getFrom().getBlockZ());
}
}
@EventHandler(priority = EventPriority.HIGHEST)
public void onPlayerPortalEvent(PlayerPortalEvent event) {
if(event.getCause().equals(TeleportCause.END_PORTAL)) {
event.setCancelled(true);
CivMessage.sendErrorNoRepeat(event.getPlayer(), "The End portal is disabled on this server.");
return;
}
if (event.getCause().equals(TeleportCause.NETHER_PORTAL)) {
event.setCancelled(true);
CivMessage.sendErrorNoRepeat(event.getPlayer(), "The Nether is disabled on this server.");
return;
}
}
@Override
public CompletableFuture<Boolean> teleportAsync(Entity entity, Location location, TeleportCause cause) {
return CompletableFuture.completedFuture(entity.teleport(location, cause));
}
@Override
public CompletableFuture<Boolean> teleportAsync(Entity entity, Location location, TeleportCause cause) {
int x = location.getBlockX() >> 4;
int z = location.getBlockZ() >> 4;
return PaperLib.getChunkAtAsyncUrgently(entity.getWorld(), x, z, true).thenApply(chunk -> entity.teleport(location, cause));
}
public CompletableFuture<Boolean> teleport(Entity entity, Location location, TeleportCause cause) {
return asyncTeleportHandler.teleportAsync(entity, location, cause);
}
public boolean teleport(Location location) {
return teleport(location, TeleportCause.PLUGIN);
}
public boolean teleport(org.bukkit.entity.Entity destination, TeleportCause cause) {
return teleport(destination.getLocation(), cause);
}
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void checkLaneMovement(final CoarsePlayerMoveEvent event) {
MatchPlayer player = this.match.getPlayer(event.getPlayer());
if(player == null ||
!player.canInteract() ||
!(player.getParty() instanceof Team) ||
player.getBukkit().getGameMode() == GameMode.CREATIVE ||
event.getTo().getY() <= 0) return;
Region laneRegion = this.lanes.get(player.getParty());
if(laneRegion == null) return;
boolean containsFrom = laneRegion.contains(event.getBlockFrom().toVector());
boolean containsTo = laneRegion.contains(event.getBlockTo().toVector());
// prevent ender pearling to the other lane
if(!containsTo && event.getCause() instanceof PlayerTeleportEvent) {
if(((PlayerTeleportEvent) event.getCause()).getCause() == TeleportCause.ENDER_PEARL) {
event.setCancelled(true, new TranslatableComponent("match.lane.enderPearl.disabled"));
return;
}
}
if(this.voidPlayers.contains(player.getPlayerId())) {
event.getPlayer().setFallDistance(0);
// they have been marked as "out of lane"
if(containsTo && !containsFrom) {
// prevent the player from re-entering the lane
event.setCancelled(true, new TranslatableComponent("match.lane.reEntry.disabled"));
} else {
// if they are going to land on something, teleport them underneith it
Block under = event.getTo().clone().add(new Vector(0, -1, 0)).getBlock();
if(under != null && under.getType() != Material.AIR) {
// teleport them to the lowest block
Vector safe = getSafeLocationUnder(under);
EntityLocation safeLocation = event.getPlayer().getEntityLocation();
safeLocation.setPosition(safe);
event.setTo(safeLocation);
}
}
} else {
if(!containsFrom && !containsTo) {
// they are outside of the lane
if(isIllegallyOutsideLane(laneRegion, event.getTo())) {
this.voidPlayers.add(player.getPlayerId());
event.getPlayer().sendMessage(ChatColor.RED + PGMTranslations.t("match.lane.exit", player));
}
}
}
}
@Override
public void run(GoalSelector goal) {
if (target == null) {
if (searchCooldown <= 0) {
if (npc.isSpawned()) {
double closestDis = cuttoffDistance;
for (Entity e : npc.getEntity().getNearbyEntities(cuttoffDistance, cuttoffDistance,
cuttoffDistance)) {
if (e instanceof LivingEntity) {
if (!CitizensAPI.getNPCRegistry().isNPC(e)) {
double k = e.getLocation().distanceSquared(npc.getEntity().getLocation());
if (target == null || closestDis > k) {
if (inLineOfSight(e, false)) {
target = e;
closestDis = k;
}
}
}
}
}
}
searchCooldown = searchCooldownMax;
} else {
searchCooldown--;
}
} else {
if (target.isDead()
|| target.getLocation().distanceSquared(npc.getEntity().getLocation()) >= cuttoffDistance) {
target = null;
} else {
// Util.faceEntity(npc.getEntity(), target);
Location tempc = npc.getEntity().getLocation();
tempc.setDirection(new Vector(target.getLocation().getX() - tempc.getX(),
target.getLocation().getY() - tempc.getY(), target.getLocation().getZ() - tempc.getZ()));
npc.teleport(tempc, TeleportCause.PLUGIN);
npc.faceLocation(target.getLocation());
if (shootcooldown > 0) {
shootcooldown--;
return;
} else if (reloadcooldown > 0) {
reloadcooldown--;
internalAmmoCount = g.getMaxBullets();
return;
}
if (internalAmmoCount <= 0) {
reloadcooldown = maxReloadCooldown;
return;
}
if (!inLineOfSight(target, true)) {
target=null;
return;
}
internalAmmoCount--;
shootcooldown = maxShootCooldown;
GunUtil.shootHandler(g, (Player) npc.getEntity());
GunUtil.playShoot(g, (Player) npc.getEntity());
if (target == null) {
Bukkit.broadcastMessage("Shooting no target");
}
// Bukkit.broadcastMessage("run1");
// new BukkitRunnable() {
// public void run() {
// GunUtil.shoot(g, (Player) npc.getEntity(), g.getSway(), g.getDamage(), 1,
// g.getMaxDistance());
// GunUtil.playShoot(g, null, (Player) npc.getEntity());
// Bukkit.broadcastMessage("run");
// }
// }.runTaskTimer(Main.getInstance(), 10 / g.getBulletsPerShot(), 10 /
// g.getBulletsPerShot());
}
}
}
@Override
public boolean teleport(Location location, PlayerTeleportEvent.TeleportCause cause) {
net.minecraft.entity.player.EntityPlayerMP entity = getHandle();
if (getHealth() == 0 || entity.isDead || entity instanceof net.minecraftforge.common.util.FakePlayer) {
return false;
}
if (entity.playerNetServerHandler == null || entity.playerNetServerHandler.isDisconnected()) {
return false;
}
// Spigot Start
// if (entity.vehicle != null || entity.passenger != null) {
// return false;
// }
// Spigot End
// From = Players current Location
Location from = this.getLocation();
// To = Players new Location if Teleport is Successful
Location to = location;
// Create & Call the Teleport Event.
PlayerTeleportEvent event = new PlayerTeleportEvent(this, from, to, cause);
server.getPluginManager().callEvent(event);
// Return False to inform the Plugin that the Teleport was unsuccessful/cancelled.
if (event.isCancelled()) {
return false;
}
// Spigot Start
eject();
leaveVehicle();
// Spigot End
// Update the From Location
from = event.getFrom();
// Grab the new To Location dependent on whether the event was cancelled.
to = event.getTo();
// Grab the To and From World Handles.
net.minecraft.world.WorldServer fromWorld = ((CraftWorld) from.getWorld()).getHandle();
net.minecraft.world.WorldServer toWorld = ((CraftWorld) to.getWorld()).getHandle();
// Close any foreign inventory
if (getHandle().openContainer != getHandle().inventoryContainer) {
getHandle().closeScreen();
}
// Check if the fromWorld and toWorld are the same.
if (fromWorld == toWorld) {
entity.playerNetServerHandler.teleport(to);
} else {
//Thermos....transfer them correctly?!
this.getHandle().mountEntity(null);
thermos.thermite.ThermiteTeleportationHandler.transferPlayerToDimension(this.getHandle(), toWorld.dimension, this.getHandle().mcServer.getConfigurationManager(), to.getWorld().getEnvironment());
//this.getHandle().playerNetServerHandler.teleport(to);
this.getHandle().playerNetServerHandler.teleport(to);
//this.getHandle().playerNetServerHandler.setPlayerLocation(to.getX(), to.getY(), to.getZ(), this.getHandle().rotationYaw, this.getHandle().rotationPitch);
//server.getHandle().respawnPlayer(entity, toWorld.dimension, false, to, false); // Cauldron
}
return true;
}
public boolean teleport(Location location) {
return teleport(location, TeleportCause.PLUGIN);
}
@EventHandler(priority = EventPriority.HIGHEST)
public void onPlayerTeleport(final PlayerTeleportEvent a1) {
Player player = a1.getPlayer();
final GameMap gameMap = MatchManager.get().getPlayerMap(player);
if (gameMap == null) {
if (SkyWarsReloaded.getCfg().getSpawn() != null) {
if (!a1.getFrom().getWorld().equals(SkyWarsReloaded.getCfg().getSpawn().getWorld()) && a1.getTo().getWorld().equals(SkyWarsReloaded.getCfg().getSpawn().getWorld())) {
PlayerStat.updatePlayer(a1.getPlayer().getUniqueId().toString());
return;
}
if (a1.getFrom().getWorld().equals(SkyWarsReloaded.getCfg().getSpawn().getWorld()) && !a1.getTo().getWorld().equals(SkyWarsReloaded.getCfg().getSpawn().getWorld())) {
if (SkyWarsReloaded.getCfg().lobbyBoardEnabled()) {
player.setScoreboard(Bukkit.getScoreboardManager().getNewScoreboard());
}
if (SkyWarsReloaded.getCfg().optionsMenuEnabled()) {
if (player.getInventory().getItem(SkyWarsReloaded.getCfg().getOptionsSlot()) != null) {
if (player.getInventory().getItem(SkyWarsReloaded.getCfg().getOptionsSlot()).equals(SkyWarsReloaded.getIM().getItem("optionselect"))) {
player.getInventory().setItem(SkyWarsReloaded.getCfg().getOptionsSlot(), new ItemStack(Material.AIR, 1));
}
}
}
if (SkyWarsReloaded.getCfg().joinMenuEnabled() && player.hasPermission("sw.join")) {
if (player.getInventory().getItem(SkyWarsReloaded.getCfg().getJoinSlot()) != null) {
if (player.getInventory().getItem(SkyWarsReloaded.getCfg().getJoinSlot()).equals(SkyWarsReloaded.getIM().getItem("joinselect"))) {
player.getInventory().setItem(SkyWarsReloaded.getCfg().getJoinSlot(), new ItemStack(Material.AIR, 1));
}
}
}
if (SkyWarsReloaded.getCfg().spectateMenuEnabled() && player.hasPermission("sw.spectate")) {
if (player.getInventory().getItem(SkyWarsReloaded.getCfg().getSpectateSlot()) != null) {
if (player.getInventory().getItem(SkyWarsReloaded.getCfg().getSpectateSlot()).equals(SkyWarsReloaded.getIM().getItem("spectateselect"))) {
player.getInventory().setItem(SkyWarsReloaded.getCfg().getSpectateSlot(), new ItemStack(Material.AIR, 1));
}
}
}
}
}
} else {
if (a1.getCause().equals(TeleportCause.END_PORTAL) || player.hasPermission("sw.opteleport") || a1.getTo().getWorld().equals(a1.getFrom().getWorld())) {
a1.setCancelled(false);
} else {
if (a1.getCause().equals(TeleportCause.ENDER_PEARL) && gameMap.getMatchState() != MatchState.ENDING && gameMap.getMatchState() != MatchState.WAITINGSTART) {
a1.setCancelled(false);
} else {
a1.setCancelled(true);
}
}
}
}
@Override
public Entity a(WorldServer worldserver, TeleportCause cause) {
return this;
}
@Override
public Entity a(DimensionManager dimensionmanager, TeleportCause cause) {
return this;
}
@Override
public Entity a(DimensionManager dimensionmanager, TeleportCause cause) {
return this;
}
/**
* Teleports an Entity to the target location, loading the chunk asynchronously first if needed.
* @param entity The Entity to teleport
* @param location The Location to Teleport to
* @return Future that completes with the result of the teleport
*/
@Nonnull
public static CompletableFuture<Boolean> teleportAsync(@Nonnull Entity entity, @Nonnull Location location) {
return ENVIRONMENT.teleport(entity, location, TeleportCause.PLUGIN);
}
/**
* Teleports this entity to the given location. If this entity is riding a
* vehicle, it will be dismounted prior to teleportation.
*
* @param location New location to teleport this entity to
* @param cause The cause of this teleportation
* @return <code>true</code> if the teleport was successful
*/
public boolean teleport(Location location, TeleportCause cause);
/**
* Teleports this entity to the target Entity. If this entity is riding a
* vehicle, it will be dismounted prior to teleportation.
*
* @param destination Entity to teleport this entity to
* @param cause The cause of this teleportation
* @return <code>true</code> if the teleport was successful
*/
public boolean teleport(Entity destination, TeleportCause cause);
@Override public boolean teleport(Entity entity, TeleportCause cause) { return false; }
@Override public boolean teleport(Location loc, TeleportCause cause) { return false; }
CompletableFuture<Boolean> teleportAsync(Entity entity, Location location, TeleportCause cause);
@Override public boolean teleport(Location loc, TeleportCause cause) { return false; }