Improve setspawn command location param

This commit is contained in:
Ben Woo 2024-08-30 13:55:39 +08:00
parent 330d4fbe39
commit 1ec4e4b8f0
2 changed files with 30 additions and 34 deletions

View File

@ -2,64 +2,60 @@ package org.mvplugins.multiverse.core.commands;
import co.aikar.commands.BukkitCommandIssuer; import co.aikar.commands.BukkitCommandIssuer;
import co.aikar.commands.annotation.CommandAlias; import co.aikar.commands.annotation.CommandAlias;
import co.aikar.commands.annotation.CommandCompletion;
import co.aikar.commands.annotation.CommandPermission; import co.aikar.commands.annotation.CommandPermission;
import co.aikar.commands.annotation.Description; import co.aikar.commands.annotation.Description;
import co.aikar.commands.annotation.Flags;
import co.aikar.commands.annotation.Optional; import co.aikar.commands.annotation.Optional;
import co.aikar.commands.annotation.Subcommand; import co.aikar.commands.annotation.Subcommand;
import co.aikar.commands.annotation.Syntax; import co.aikar.commands.annotation.Syntax;
import io.vavr.control.Option;
import jakarta.inject.Inject; import jakarta.inject.Inject;
import org.bukkit.Location; import org.bukkit.Location;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jvnet.hk2.annotations.Service; import org.jvnet.hk2.annotations.Service;
import org.mvplugins.multiverse.core.commandtools.MVCommandManager; import org.mvplugins.multiverse.core.commandtools.MVCommandManager;
import org.mvplugins.multiverse.core.commandtools.MultiverseCommand; import org.mvplugins.multiverse.core.commandtools.MultiverseCommand;
import org.mvplugins.multiverse.core.world.LoadedMultiverseWorld; import org.mvplugins.multiverse.core.world.WorldManager;
@Service @Service
@CommandAlias("mv") @CommandAlias("mv")
public class SetSpawnCommand extends MultiverseCommand { public class SetSpawnCommand extends MultiverseCommand {
private final WorldManager worldManager;
@Inject @Inject
SetSpawnCommand(@NotNull MVCommandManager commandManager) { SetSpawnCommand(
@NotNull MVCommandManager commandManager,
@NotNull WorldManager worldManager) {
super(commandManager); super(commandManager);
this.worldManager = worldManager;
} }
@CommandAlias("mvsetspawn") @CommandAlias("mvsetspawn")
@Subcommand("setspawn") @Subcommand("setspawn")
@CommandPermission("multiverse.core.setspawn") @CommandPermission("multiverse.core.spawn.set")
@CommandCompletion("@nothing @mvworlds:scope=loaded ") // TODO: Use Brigadier to show <position> above in chat like the vanilla TP command // @CommandCompletion("@location") // TODO: Use Brigadier to show <position> above in chat like the vanilla TP command
@Syntax("[location] [world]") @Syntax("[location]")
@Description("{@@mv-core.setspawn.description}") @Description("{@@mv-core.setspawn.description}")
void onSetSpawnCommand( void onSetSpawnCommand(
BukkitCommandIssuer issuer, BukkitCommandIssuer issuer,
@Optional @Optional
@Flags("resolve=issuerAware")
@Syntax("<location>") @Syntax("<location>")
@Description("{@@mv-core.setspawn.location.description}") @Description("{@@mv-core.setspawn.location.description}")
Location location, Location location) {
Option.of(location).orElse(() -> {
@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()) { if (issuer.isPlayer()) {
location = issuer.getPlayer().getLocation(); return Option.of(issuer.getPlayer().getLocation());
} else {
issuer.sendMessage("The console must specify a location");
return;
} }
} return Option.none();
}).peek(finalLocation ->
issuer.sendMessage("Setting spawn in " + world.getName() + " to " + prettyLocation(location)); worldManager.getLoadedWorld(finalLocation.getWorld())
.peek(mvWorld -> mvWorld.setSpawnLocation(finalLocation)
world.setSpawnLocation(location); .onSuccess(ignore -> issuer.sendMessage(
"Successfully set spawn in " + mvWorld.getName() + " to " + prettyLocation(mvWorld.getSpawnLocation())))
.onFailure(e -> issuer.sendMessage(e.getLocalizedMessage())))
.onEmpty(() -> issuer.sendMessage("That world is not loaded or does not exist!"))
).onEmpty(() -> issuer.sendMessage("You must specify a location in the format: worldname:x,y,z"));
} }
private String prettyLocation(Location location) { private String prettyLocation(Location location) {

View File

@ -124,18 +124,18 @@ mv-core.remove.success=&aWorld '{world}' removed!
mv-core.root.title=&a{name} version {version} mv-core.root.title=&a{name} version {version}
mv-core.root.help=&aSee &f/mv help&a for commands available. mv-core.root.help=&aSee &f/mv help&a for commands available.
# /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 setspawn # /mv setspawn
mv-core.setspawn.description=Sets the spawn location of the specified world mv-core.setspawn.description=Sets the spawn location of the specified world
mv-core.setspawn.location.description=Location of the new spawn mv-core.setspawn.location.description=Location of the new spawn
mv-core.setspawn.world.description=Target world to set spawn of (defaults to player's current world) mv-core.setspawn.world.description=Target world to set spawn of (defaults to player's current world)
# /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.success={teleporter} just sent you to spawn!
mv-core.spawn.consolename=The console
mv-core.spawn.you=You
# /mv tp # /mv tp
mv-core.teleport.description=Allows you to teleport to a location on your server! mv-core.teleport.description=Allows you to teleport to a location on your server!
mv-core.teleport.player.description=Target player to teleport. mv-core.teleport.player.description=Target player to teleport.