HubThat/src/main/java/wtf/beatrice/hubthat/commands/WorldTpCommand.java

88 lines
4.1 KiB
Java
Raw Normal View History

2022-05-31 20:09:36 +02:00
package wtf.beatrice.hubthat.commands;
2020-04-09 16:31:14 +02:00
2022-05-31 20:09:36 +02:00
import wtf.beatrice.hubthat.HubThat;
2020-04-09 16:31:14 +02:00
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.command.ConsoleCommandSender;
import org.bukkit.entity.Player;
2022-05-31 20:09:36 +02:00
import wtf.beatrice.hubthat.utils.*;
2020-04-09 16:31:14 +02:00
import java.util.logging.Level;
2022-05-31 20:09:36 +02:00
import static wtf.beatrice.hubthat.utils.TeleportUtils.fixInvisibilityAfter;
import static wtf.beatrice.hubthat.utils.TeleportUtils.fixInvisibilityBefore;
2020-04-09 16:31:14 +02:00
public class WorldTpCommand implements CommandExecutor
{
// Initialize the debugger so I can debug the plugin.
2020-05-27 00:41:02 +02:00
private final Debugger debugger = new Debugger(getClass().getName());
2020-04-09 16:31:14 +02:00
// Initialize the plugin variable so we can access all of the plugin's data.
2020-05-27 00:41:02 +02:00
private final HubThat plugin;
2020-04-09 16:31:14 +02:00
// Constructor to actually give "plugin" a value.
public WorldTpCommand(HubThat givenPlugin) { plugin = givenPlugin; }
@Override
public boolean onCommand(CommandSender commandSender, Command command, String s, String[] args)
{
// Log who is using the command.
debugger.sendDebugMessage(Level.INFO, "Sender is instance of: " + commandSender.getClass().getName());
// If the command comes from Console, stop it and give a warning.
boolean senderIsConsole = (commandSender instanceof ConsoleCommandSender);
if(senderIsConsole)
{
2023-02-11 20:57:11 +01:00
MessageUtils.sendLocalizedMessage(commandSender, LocalizedMessage.ERROR_CONSOLE_ACCESS_BLOCKED);
2020-04-09 16:31:14 +02:00
return true;
}
// Check if the player has permission to teleport to any world.
if(PermissionUtils.playerHasPermission(commandSender, Permissions.TELEPORT_TO_WORLD))
{
// Check if there is the correct number of args.
if(args.length != 1)
{
// Warn the player in case it's wrong.
2023-02-11 20:57:11 +01:00
String wrongUsageMessage = MessageUtils.getLocalizedMessage(LocalizedMessage.ERROR_WRONG_USAGE, false).replace("%usage%", "/worldtp <world>");
2020-04-09 16:31:14 +02:00
MessageUtils.sendColorizedMessage(commandSender, wrongUsageMessage);
return true;
}
// Load the world's name from args and then the world.
String destinationWorldName = args[0];
World destinationWorld = plugin.getServer().getWorld(destinationWorldName);
// If the world does not exist, warn the player.
if(destinationWorld == null)
{
2023-02-11 20:57:11 +01:00
String worldDoesNotExistMessage = MessageUtils.getLocalizedMessage(LocalizedMessage.ERROR_WORLD_NOT_EXISTING, false);
2020-04-09 16:31:14 +02:00
worldDoesNotExistMessage = worldDoesNotExistMessage.replace("%w%", destinationWorldName);
MessageUtils.sendColorizedMessage(commandSender, worldDoesNotExistMessage);
return true;
}
// Load the spawnpoint. This is going to be different from HubThat's spawnpoint because it could be in another world!
Location destinationLocation = new Location(destinationWorld, destinationWorld.getSpawnLocation().getX(), destinationWorld.getSpawnLocation().getY(), destinationWorld.getSpawnLocation().getZ(), destinationWorld.getSpawnLocation().getYaw(), destinationWorld.getSpawnLocation().getPitch());
// Cast Player to commandSender so we can teleport it.
Player player = (Player)commandSender;
// Teleport the Player.
fixInvisibilityBefore(destinationLocation);
plugin.getServer().getScheduler().runTaskLater(plugin, () -> player.teleport(destinationLocation), 1);
fixInvisibilityAfter(player);
2020-04-09 16:31:14 +02:00
// Tell the player he has been teleported.
2023-02-11 20:57:11 +01:00
String teleportedMessage = MessageUtils.getLocalizedMessage(LocalizedMessage.INFO_WORLDTP_TELEPORTED, false);
2020-04-09 16:31:14 +02:00
teleportedMessage = teleportedMessage.replace("%world%", destinationWorldName).replace("%w%", destinationWorldName);
MessageUtils.sendColorizedMessage(commandSender, teleportedMessage);
}
return true;
}
}