Compare commits

...

2 Commits

Author SHA1 Message Date
Zax71 6982d8e7ba Add null check for world 2024-03-08 18:02:15 +00:00
Zax71 1a508a5d14 Add logic for spawn and setspawn 2024-03-08 17:58:45 +00:00
4 changed files with 105 additions and 29 deletions

View File

@ -0,0 +1,71 @@
package org.mvplugins.multiverse.core.commands;
import co.aikar.commands.BukkitCommandIssuer;
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.Optional;
import co.aikar.commands.annotation.Subcommand;
import co.aikar.commands.annotation.Syntax;
import jakarta.inject.Inject;
import org.bukkit.Location;
import org.jetbrains.annotations.NotNull;
import org.jvnet.hk2.annotations.Service;
import org.mvplugins.multiverse.core.api.BlockSafety;
import org.mvplugins.multiverse.core.commandtools.MVCommandManager;
import org.mvplugins.multiverse.core.commandtools.MultiverseCommand;
import org.mvplugins.multiverse.core.world.LoadedMultiverseWorld;
import org.mvplugins.multiverse.core.world.WorldManager;
@Service
@CommandAlias("mv")
public class SetSpawnCommand extends MultiverseCommand {
@Inject
SetSpawnCommand(@NotNull MVCommandManager commandManager) {
super(commandManager);
}
@Subcommand("setspawn")
@CommandPermission("multiverse.core.setspawn")
@CommandCompletion("@nothing @mvworlds:scope=loaded ") // TODO: Use Brigadier to show <position> above in chat like the vanilla TP command
@Syntax("[location] [world]")
@Description("{@@mv-core.setspawn.description}")
void onSetSpawnCommand(
BukkitCommandIssuer issuer,
@Optional
@Flags("resolve=issuerAware")
@Syntax("<location>")
@Description("{@@mv-core.setspawn.location.description}")
Location location,
@Optional
@Flags("resolve=issuerAware")
@Syntax("<world>")
@Description("{@@mv-core.setspawn.world.description}")
LoadedMultiverseWorld world
) {
// TODO: Use a flag to do this, no clue how to edit an inbuilt ACF flag though
// Get the Location
if (location == null) {
if (issuer.isPlayer()) {
location = issuer.getPlayer().getLocation();
} else {
issuer.sendMessage("The console must specify a location");
return;
}
}
issuer.sendMessage("Setting spawn in " + world.getName() + " to " + prettyLocation(location));
world.setSpawnLocation(location);
}
private String prettyLocation(Location location) {
return location.getX() + ", " + location.getY() + ", " + location.getZ();
}
}

View File

@ -9,6 +9,7 @@ import co.aikar.commands.annotation.Description;
import co.aikar.commands.annotation.Flags;
import co.aikar.commands.annotation.Subcommand;
import co.aikar.commands.annotation.Syntax;
import com.dumptruckman.minecraft.util.Logging;
import jakarta.inject.Inject;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
@ -23,55 +24,59 @@ import org.mvplugins.multiverse.core.world.WorldManager;
@Service
@CommandAlias("mv")
class SpawnCommand extends MultiverseCommand {
private final WorldManager worldManager;
private final AsyncSafetyTeleporter safetyTeleporter;
@Inject
SpawnCommand(@NotNull MVCommandManager commandManager,
WorldManager worldManager,
@NotNull WorldManager worldManager,
@NotNull AsyncSafetyTeleporter safetyTeleporter) {
super(commandManager);
this.worldManager = worldManager;
this.safetyTeleporter = safetyTeleporter;
}
@Subcommand("spawn tp")
@Subcommand("spawn")
@CommandPermission("multiverse.core.spawn")
@CommandCompletion("@players")
@Syntax("[player]")
@Description("{@@mv-core.spawn.tp.description}")
@Description("{@@mv-core.spawn.description}")
void onSpawnTpCommand(
BukkitCommandIssuer issuer,
@Flags("resolve=issuerAware")
@Syntax("[player]")
@Description("{@@mv-core.spawn.tp.player.description}")
@Description("{@@mv-core.spawn.player.description}")
Player player
) {
// The player is in the world, so it must be loaded
LoadedMultiverseWorld world = worldManager.getLoadedWorld(player.getWorld().getName()).getOrNull();
if (world == null) {
issuer.sendMessage("The world the player you are trying to teleport is in, is not a multiverse world");
}
// TODO: Log when the player cannot be teleported there. No clue how to detect that
// Teleport the player
safetyTeleporter.teleportSafely(issuer.getIssuer(), player, world.getSpawnLocation());
// Make the conformation message make sense
String teleporterName;
if (issuer.getIssuer().getName().equals("CONSOLE")) {
teleporterName = commandManager.formatMessage(issuer, MessageType.INFO, MVCorei18n.SPAWN_TP_CONSOLENAME);
} else if (issuer.getIssuer().getName().equals(player.getName())) {
teleporterName = commandManager.formatMessage(issuer, MessageType.INFO, MVCorei18n.SPAWN_TP_YOU);
} else {
teleporterName = issuer.getIssuer().getName();
}
// Send the conformation message
player.sendMessage(commandManager.formatMessage(
issuer,
MessageType.INFO,
MVCorei18n.SPAWN_TP_MESSAGE,
MVCorei18n.SPAWN_MESSAGE,
"{teleporter}",
teleporterName
getTeleporterName(issuer, player)
));
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();
}
}

View File

@ -95,11 +95,11 @@ public enum MVCorei18n implements MessageKeyProvider {
ROOT_HELP,
// spawn tp command
SPAWN_TP_DESCRIPTION,
SPAWN_TP_PLAYER_DESCRIPTION,
SPAWN_TP_MESSAGE,
SPAWN_TP_CONSOLENAME,
SPAWN_TP_YOU,
SPAWN_DESCRIPTION,
SPAWN_PLAYER_DESCRIPTION,
SPAWN_MESSAGE,
SPAWN_CONSOLENAME,
SPAWN_YOU,
// teleport command
TELEPORT_SUCCESS,

View File

@ -124,12 +124,12 @@ mv-core.remove.success=&aWorld '{world}' removed!
mv-core.root.title=&a{name} version {version}
mv-core.root.help=&aSee &f/mv help&a for commands available.
# /mv spawn tp
mv-core.spawn.tp.description=Teleports the specified player to the spawn of the world they are in
mv-core.spawn.tp.player.description=The player
mv-core.spawn.tp.message={teleporter} just sent you to spawn!
mv-core.spawn.tp.consolename=The console
mv-core.spawn.tp.you=You
# /mv spawn
mv-core.spawn.description=Teleports the specified player to the spawn of the world they are in
mv-core.spawn.player.description=The player
mv-core.spawn.message={teleporter} just sent you to spawn!
mv-core.spawn.consolename=The console
mv-core.spawn.you=You
# /mv tp
mv-core.teleport.description=Allows you to teleport to a location on your server!