Multiverse-Core/src/main/java/org/mvplugins/multiverse/core/commands/SpawnCommand.java

83 lines
3.4 KiB
Java
Raw Normal View History

2024-02-24 18:38:40 +01:00
package org.mvplugins.multiverse.core.commands;
import co.aikar.commands.BukkitCommandIssuer;
2024-02-24 18:56:54 +01:00
import co.aikar.commands.MessageType;
2024-02-24 18:38:40 +01:00
import co.aikar.commands.annotation.CommandAlias;
import co.aikar.commands.annotation.CommandCompletion;
import co.aikar.commands.annotation.CommandPermission;
import co.aikar.commands.annotation.Description;
import co.aikar.commands.annotation.Flags;
import co.aikar.commands.annotation.Subcommand;
import co.aikar.commands.annotation.Syntax;
2024-03-08 18:58:45 +01:00
import com.dumptruckman.minecraft.util.Logging;
2024-02-24 18:38:40 +01:00
import jakarta.inject.Inject;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import org.jvnet.hk2.annotations.Service;
import org.mvplugins.multiverse.core.commandtools.MVCommandManager;
import org.mvplugins.multiverse.core.commandtools.MultiverseCommand;
2024-02-24 18:56:54 +01:00
import org.mvplugins.multiverse.core.teleportation.AsyncSafetyTeleporter;
import org.mvplugins.multiverse.core.utils.MVCorei18n;
2024-02-24 18:38:40 +01:00
import org.mvplugins.multiverse.core.world.LoadedMultiverseWorld;
import org.mvplugins.multiverse.core.world.WorldManager;
@Service
@CommandAlias("mv")
class SpawnCommand extends MultiverseCommand {
private final WorldManager worldManager;
2024-02-24 18:56:54 +01:00
private final AsyncSafetyTeleporter safetyTeleporter;
2024-02-24 18:38:40 +01:00
@Inject
2024-02-24 18:56:54 +01:00
SpawnCommand(@NotNull MVCommandManager commandManager,
2024-03-08 18:58:45 +01:00
@NotNull WorldManager worldManager,
2024-02-24 18:56:54 +01:00
@NotNull AsyncSafetyTeleporter safetyTeleporter) {
2024-02-24 18:38:40 +01:00
super(commandManager);
this.worldManager = worldManager;
2024-02-24 18:56:54 +01:00
this.safetyTeleporter = safetyTeleporter;
2024-02-24 18:38:40 +01:00
}
2024-03-08 18:58:45 +01:00
@Subcommand("spawn")
2024-02-24 18:38:40 +01:00
@CommandPermission("multiverse.core.spawn")
@CommandCompletion("@players")
@Syntax("[player]")
2024-03-08 18:58:45 +01:00
@Description("{@@mv-core.spawn.description}")
void onSpawnTpCommand(
2024-02-24 18:38:40 +01:00
BukkitCommandIssuer issuer,
@Flags("resolve=issuerAware")
@Syntax("[player]")
2024-03-08 18:58:45 +01:00
@Description("{@@mv-core.spawn.player.description}")
2024-02-24 18:38:40 +01:00
Player player
) {
// The player is in the world, so it must be loaded
LoadedMultiverseWorld world = worldManager.getLoadedWorld(player.getWorld().getName()).getOrNull();
2024-03-08 19:02:15 +01:00
if (world == null) {
issuer.sendMessage("The world the player you are trying to teleport is in, is not a multiverse world");
}
2024-02-24 18:38:40 +01:00
2024-03-08 18:58:45 +01:00
// TODO: Log when the player cannot be teleported there. No clue how to detect that
2024-02-24 18:56:54 +01:00
// Teleport the player
safetyTeleporter.teleportSafely(issuer.getIssuer(), player, world.getSpawnLocation());
2024-02-24 18:38:40 +01:00
2024-02-24 18:56:54 +01:00
player.sendMessage(commandManager.formatMessage(
issuer,
MessageType.INFO,
2024-03-08 18:58:45 +01:00
MVCorei18n.SPAWN_MESSAGE,
2024-02-24 18:56:54 +01:00
"{teleporter}",
2024-03-08 18:58:45 +01:00
getTeleporterName(issuer, player)
2024-02-24 18:56:54 +01:00
));
2024-03-08 18:58:45 +01:00
Logging.fine("Teleported " + player.getName() + " to " + world.getSpawnLocation().getX() + ", " + world.getSpawnLocation().getY() + ", " + world.getSpawnLocation().getZ());
}
private String getTeleporterName(BukkitCommandIssuer issuer, Player teleportTo) {
if (issuer.getIssuer().getName().equals("CONSOLE")) {
return commandManager.formatMessage(issuer, MessageType.INFO, MVCorei18n.SPAWN_CONSOLENAME);
}
if (issuer.getIssuer().getName().equals(teleportTo.getName())) {
return commandManager.formatMessage(issuer, MessageType.INFO, MVCorei18n.SPAWN_YOU);
}
return issuer.getIssuer().getName();
2024-02-24 18:38:40 +01:00
}
}