Multiverse-Core/src/main/java/com/onarandombox/MultiverseCore/commands/TeleportCommand.java

196 lines
9.9 KiB
Java
Raw Normal View History

/******************************************************************************
* Multiverse 2 Copyright (c) the Multiverse Team 2011. *
* Multiverse 2 is licensed under the BSD License. *
* For more information please check the README.md file included *
* with this project. *
******************************************************************************/
2011-07-20 16:48:46 +02:00
package com.onarandombox.MultiverseCore.commands;
2020-12-12 03:49:49 +01:00
import com.dumptruckman.minecraft.util.Logging;
2011-07-20 16:48:46 +02:00
import com.onarandombox.MultiverseCore.MultiverseCore;
2012-12-26 20:09:09 +01:00
import com.onarandombox.MultiverseCore.api.Teleporter;
2011-09-25 18:40:23 +02:00
import com.onarandombox.MultiverseCore.api.MVDestination;
2012-12-26 20:09:09 +01:00
import com.onarandombox.MultiverseCore.destination.CustomTeleporterDestination;
2011-09-25 18:46:07 +02:00
import com.onarandombox.MultiverseCore.destination.DestinationFactory;
import com.onarandombox.MultiverseCore.destination.InvalidDestination;
import com.onarandombox.MultiverseCore.destination.WorldDestination;
import com.onarandombox.MultiverseCore.enums.TeleportResult;
2011-08-24 00:48:11 +02:00
import com.onarandombox.MultiverseCore.event.MVTeleportEvent;
import com.onarandombox.MultiverseCore.api.SafeTTeleporter;
import com.onarandombox.MultiverseCore.utils.PlayerFinder;
2011-09-17 17:59:38 +02:00
import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.World;
2011-09-17 17:59:38 +02:00
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.permissions.Permission;
import org.bukkit.permissions.PermissionDefault;
import java.util.ArrayList;
import java.util.List;
2011-07-20 16:48:46 +02:00
/**
* Used to teleport players.
*/
2011-07-20 16:48:46 +02:00
public class TeleportCommand extends MultiverseCommand {
2011-10-08 02:47:39 +02:00
private SafeTTeleporter playerTeleporter;
2011-07-20 16:48:46 +02:00
public TeleportCommand(MultiverseCore plugin) {
super(plugin);
Permission menu = new Permission("multiverse.teleport.*", "Allows you to display the teleport menu.", PermissionDefault.OP);
2011-07-27 02:47:33 +02:00
2011-07-20 16:48:46 +02:00
this.setName("Teleport");
this.setCommandUsage("/mv tp " + ChatColor.GOLD + "[PLAYER]" + ChatColor.GREEN + " {DESTINATION}");
2011-07-20 16:48:46 +02:00
this.setArgRange(1, 2);
this.addKey("mvtp");
this.addKey("mv tp");
this.playerTeleporter = this.plugin.getSafeTTeleporter();
2011-09-17 17:59:38 +02:00
this.setPermission(menu);
2011-07-20 16:48:46 +02:00
}
private static final int UNSAFE_TELEPORT_EXPIRE_DELAY = 15;
2011-07-20 16:48:46 +02:00
@Override
public void runCommand(CommandSender sender, List<String> args) {
CommandSender teleporter = sender;
2011-07-20 16:48:46 +02:00
Player teleportee = null;
2011-08-07 17:40:22 +02:00
String destinationName;
2011-07-20 16:48:46 +02:00
if (args.size() == 2) {
teleportee = PlayerFinder.get(args.get(0), sender);
2011-07-20 16:48:46 +02:00
if (teleportee == null) {
this.messaging.sendMessage(sender, String.format("Sorry, I couldn't find player: %s%s",
ChatColor.GOLD, args.get(0)), false);
2011-07-20 16:48:46 +02:00
return;
}
2011-08-07 17:40:22 +02:00
destinationName = args.get(1);
2011-07-20 16:48:46 +02:00
} else {
2011-08-07 17:40:22 +02:00
destinationName = args.get(0);
2011-07-20 16:48:46 +02:00
if (!(sender instanceof Player)) {
this.messaging.sendMessage(sender, String.format("From the console, you must specify a player to teleport"), false);
2011-07-20 16:48:46 +02:00
return;
}
teleportee = (Player) sender;
}
2011-10-08 18:57:48 +02:00
DestinationFactory df = this.plugin.getDestFactory();
MVDestination d = df.getPlayerAwareDestination(teleportee, destinationName);
2011-09-17 17:59:38 +02:00
MVTeleportEvent teleportEvent = new MVTeleportEvent(d, teleportee, teleporter, true);
2011-08-24 00:48:11 +02:00
this.plugin.getServer().getPluginManager().callEvent(teleportEvent);
if (teleportEvent.isCancelled()) {
2020-12-12 03:49:49 +01:00
Logging.fine("Someone else cancelled the MVTeleport Event!!!");
2011-08-24 00:48:11 +02:00
return;
}
if (d != null && d instanceof InvalidDestination) {
this.messaging.sendMessage(sender, String.format("Multiverse does not know how to take you to %s%s",
ChatColor.RED, destinationName), false);
2011-07-20 16:48:46 +02:00
return;
}
2011-09-25 17:14:17 +02:00
if (!this.checkSendPermissions(teleporter, teleportee, d)) {
2011-09-17 17:59:38 +02:00
return;
}
2012-02-04 14:22:55 +01:00
if (plugin.getMVConfig().getEnforceAccess() && teleporter != null && !this.plugin.getMVPerms().canEnterDestination(teleporter, d)) {
2011-07-20 16:48:46 +02:00
if (teleportee.equals(teleporter)) {
teleporter.sendMessage("Doesn't look like you're allowed to go " + ChatColor.RED + "there...");
} else {
teleporter.sendMessage("Doesn't look like you're allowed to send " + ChatColor.GOLD
+ teleportee.getName() + ChatColor.WHITE + " to " + ChatColor.RED + "there...");
2011-07-20 16:48:46 +02:00
}
return;
2011-10-08 18:14:52 +02:00
} else if (teleporter != null && !this.plugin.getMVPerms().canTravelFromLocation(teleporter, d.getLocation(teleportee))) {
2011-07-20 16:48:46 +02:00
if (teleportee.equals(teleporter)) {
this.messaging.sendMessage(teleporter, String.format("DOH! Doesn't look like you can get to %s%s %sfrom where you are...",
ChatColor.GREEN, d.toString(), ChatColor.WHITE), false);
2011-07-20 16:48:46 +02:00
} else {
this.messaging.sendMessage(teleporter, String.format("DOH! Doesn't look like %s%s %scan get to %sTHERE from where they are...",
ChatColor.GREEN, ((Player) teleporter).getWorld().getName(), ChatColor.WHITE, ChatColor.RED), false);
2011-07-20 16:48:46 +02:00
}
return;
}
// Special check to verify if players are tryint to teleport to the same
// WORLDDestination as the world they're in, that they ALSO have multiverse.core.spawn.self
2011-09-25 17:14:17 +02:00
if (d instanceof WorldDestination) {
World w = d.getLocation(teleportee).getWorld();
2011-09-25 17:14:17 +02:00
if (teleportee.getWorld().equals(w)) {
if (teleporter.equals(teleportee)) {
2011-10-08 18:14:52 +02:00
if (!this.plugin.getMVPerms().hasPermission(teleporter, "multiverse.core.spawn.self", true)) {
this.messaging.sendMessages(teleporter, new String[]{
String.format("Sorry you don't have permission to go to the world spawn!"),
String.format("%s (multiverse.core.spawn.self)",
ChatColor.RED) }, false);
return;
}
} else {
2011-10-08 18:14:52 +02:00
if (!this.plugin.getMVPerms().hasPermission(teleporter, "multiverse.core.spawn.other", true)) {
this.messaging.sendMessages(teleporter, new String[]{
String.format("Sorry you don't have permission to send %s to the world spawn!",
teleportee.getDisplayName()),
String.format("%s (multiverse.core.spawn.other)",
ChatColor.RED) }, false);
return;
}
}
}
}
if (d.getLocation(teleportee) == null) {
this.messaging.sendMessage(teleporter, "Sorry Boss, I tried everything, but just couldn't teleport ya there!", false);
2011-07-20 16:48:46 +02:00
return;
}
2012-12-26 20:09:09 +01:00
Teleporter teleportObject = (d instanceof CustomTeleporterDestination) ?
((CustomTeleporterDestination)d).getTeleporter() : this.playerTeleporter;
TeleportResult result = teleportObject.teleport(teleporter, teleportee, d);
if (result == TeleportResult.FAIL_UNSAFE) {
2020-12-12 03:49:49 +01:00
Logging.fine("Could not teleport " + teleportee.getName()
+ " to " + plugin.getLocationManipulation().strCoordsRaw(d.getLocation(teleportee)));
2020-12-12 03:49:49 +01:00
Logging.fine("Queueing Command");
Class<?>[] paramTypes = { CommandSender.class, Player.class, Location.class };
2011-07-30 17:17:23 +02:00
List<Object> items = new ArrayList<Object>();
items.add(teleporter);
2011-07-30 17:17:23 +02:00
items.add(teleportee);
items.add(d.getLocation(teleportee));
String player = "you";
if (!teleportee.equals(teleporter)) {
player = teleportee.getName();
}
String message = String.format("%sMultiverse %sdid not teleport %s%s %sto %s%s %sbecause it was unsafe.",
ChatColor.GREEN, ChatColor.WHITE, ChatColor.AQUA, player, ChatColor.WHITE, ChatColor.DARK_AQUA, d.getName(), ChatColor.WHITE);
this.plugin.getCommandHandler().queueCommand(sender, "mvteleport", "teleportPlayer", items,
paramTypes, message, "Would you like to try anyway?", "", "", UNSAFE_TELEPORT_EXPIRE_DELAY);
2011-07-30 08:53:55 +02:00
}
// else: Player was teleported successfully (or the tp event was fired I should say)
2011-07-20 16:48:46 +02:00
}
2011-09-17 17:59:38 +02:00
private boolean checkSendPermissions(CommandSender teleporter, Player teleportee, MVDestination destination) {
2011-09-18 17:48:23 +02:00
if (teleporter.equals(teleportee)) {
2011-10-08 18:14:52 +02:00
if (!this.plugin.getMVPerms().hasPermission(teleporter, "multiverse.teleport.self." + destination.getIdentifier(), true)) {
this.messaging.sendMessages(teleporter, new String[]{
String.format("%sYou don't have permission to teleport %syourself %sto a %s%s %sDestination",
ChatColor.WHITE, ChatColor.AQUA, ChatColor.WHITE, ChatColor.RED, destination.getType(), ChatColor.WHITE),
String.format("%s (multiverse.teleport.self.%s)",
ChatColor.RED, destination.getIdentifier()) }, false);
2011-09-17 17:59:38 +02:00
return false;
}
} else {
2011-10-08 18:14:52 +02:00
if (!this.plugin.getMVPerms().hasPermission(teleporter, "multiverse.teleport.other." + destination.getIdentifier(), true)) {
this.messaging.sendMessages(teleporter, new String[]{
String.format("You don't have permission to teleport another player to a %s%s Destination.",
ChatColor.GREEN, destination.getType()),
String.format("%s(multiverse.teleport.other.%s)",
ChatColor.RED, destination.getIdentifier()) }, false);
2011-09-17 17:59:38 +02:00
return false;
}
}
return true;
}
2011-07-20 16:48:46 +02:00
}