-
Notifications
You must be signed in to change notification settings - Fork 307
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3057 from Multiverse/zax71/MV5/spawnCommand
Add `/mv spawn` and `/mv setspawn`
- Loading branch information
Showing
4 changed files
with
181 additions
and
0 deletions.
There are no files selected for viewing
64 changes: 64 additions & 0 deletions
64
src/main/java/org/mvplugins/multiverse/core/commands/SetSpawnCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package org.mvplugins.multiverse.core.commands; | ||
|
||
import co.aikar.commands.BukkitCommandIssuer; | ||
import co.aikar.commands.annotation.CommandAlias; | ||
import co.aikar.commands.annotation.CommandPermission; | ||
import co.aikar.commands.annotation.Description; | ||
import co.aikar.commands.annotation.Optional; | ||
import co.aikar.commands.annotation.Subcommand; | ||
import co.aikar.commands.annotation.Syntax; | ||
import io.vavr.control.Option; | ||
import jakarta.inject.Inject; | ||
import org.bukkit.Location; | ||
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; | ||
import org.mvplugins.multiverse.core.world.WorldManager; | ||
|
||
@Service | ||
@CommandAlias("mv") | ||
public class SetSpawnCommand extends MultiverseCommand { | ||
|
||
private final WorldManager worldManager; | ||
|
||
@Inject | ||
SetSpawnCommand( | ||
@NotNull MVCommandManager commandManager, | ||
@NotNull WorldManager worldManager) { | ||
super(commandManager); | ||
this.worldManager = worldManager; | ||
} | ||
|
||
@CommandAlias("mvsetspawn") | ||
@Subcommand("setspawn") | ||
@CommandPermission("multiverse.core.spawn.set") | ||
// @CommandCompletion("@location") // TODO: Use Brigadier to show <position> above in chat like the vanilla TP command | ||
@Syntax("[location]") | ||
@Description("{@@mv-core.setspawn.description}") | ||
void onSetSpawnCommand( | ||
BukkitCommandIssuer issuer, | ||
|
||
@Optional | ||
@Syntax("<location>") | ||
@Description("{@@mv-core.setspawn.location.description}") | ||
Location location) { | ||
Option.of(location).orElse(() -> { | ||
if (issuer.isPlayer()) { | ||
return Option.of(issuer.getPlayer().getLocation()); | ||
} | ||
return Option.none(); | ||
}).peek(finalLocation -> | ||
worldManager.getLoadedWorld(finalLocation.getWorld()) | ||
.peek(mvWorld -> mvWorld.setSpawnLocation(finalLocation) | ||
.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) { | ||
return location.getX() + ", " + location.getY() + ", " + location.getZ(); | ||
} | ||
} |
97 changes: 97 additions & 0 deletions
97
src/main/java/org/mvplugins/multiverse/core/commands/SpawnCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
package org.mvplugins.multiverse.core.commands; | ||
|
||
import co.aikar.commands.BukkitCommandIssuer; | ||
import co.aikar.commands.CommandIssuer; | ||
import co.aikar.commands.MessageType; | ||
import co.aikar.commands.annotation.*; | ||
import com.dumptruckman.minecraft.util.Logging; | ||
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.MVCommandIssuer; | ||
import org.mvplugins.multiverse.core.commandtools.MVCommandManager; | ||
import org.mvplugins.multiverse.core.commandtools.MultiverseCommand; | ||
import org.mvplugins.multiverse.core.teleportation.AsyncSafetyTeleporter; | ||
import org.mvplugins.multiverse.core.utils.MVCorei18n; | ||
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; | ||
private final AsyncSafetyTeleporter safetyTeleporter; | ||
|
||
@Inject | ||
SpawnCommand(@NotNull MVCommandManager commandManager, | ||
@NotNull WorldManager worldManager, | ||
@NotNull AsyncSafetyTeleporter safetyTeleporter) { | ||
super(commandManager); | ||
this.worldManager = worldManager; | ||
this.safetyTeleporter = safetyTeleporter; | ||
} | ||
|
||
@CommandAlias("mvspawn") | ||
@Subcommand("spawn") | ||
@CommandCompletion("@players") | ||
@Syntax("[player]") | ||
@Description("{@@mv-core.spawn.description}") | ||
void onSpawnTpCommand( | ||
MVCommandIssuer issuer, | ||
|
||
@Flags("resolve=issuerAware") | ||
@Syntax("[player]") | ||
@Description("{@@mv-core.spawn.player.description}") | ||
Player player) { | ||
// TODO: Better handling of permission checking with CorePermissionsChecker | ||
String permission = player.equals(issuer.getPlayer()) ? "multiverse.core.spawn.self" : "multiverse.core.spawn.other"; | ||
if (!issuer.hasPermission(permission)) { | ||
issuer.sendMessage("You do not have permission to use this command!"); | ||
return; | ||
} | ||
|
||
LoadedMultiverseWorld world = worldManager.getLoadedWorld(player.getWorld()).getOrNull(); | ||
if (world == null) { | ||
issuer.sendMessage("The world the player you are trying to teleport is in, is not a multiverse world"); | ||
return; | ||
} | ||
|
||
// Teleport the player | ||
// TODO: Different message for teleporting self vs others | ||
safetyTeleporter.teleportSafely(issuer.getIssuer(), player, world.getSpawnLocation()) | ||
.onSuccess(() -> player.sendMessage(commandManager.formatMessage( | ||
issuer, | ||
MessageType.INFO, | ||
MVCorei18n.SPAWN_SUCCESS, | ||
"{teleporter}", | ||
getTeleporterName(issuer, player) | ||
))) | ||
.onFailure(failure -> { | ||
issuer.sendError( | ||
MVCorei18n.SPAWN_FAILED, | ||
"{teleporter}", | ||
getTeleporterName(issuer, player) | ||
); | ||
issuer.sendError(failure.getFailureMessage()); | ||
}); | ||
|
||
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(); | ||
} | ||
|
||
@Override | ||
public boolean hasPermission(CommandIssuer issuer) { | ||
// TODO: Fix autocomplete showing even if the player doesn't have permission | ||
return issuer.hasPermission("multiverse.core.spawn.self") || issuer.hasPermission("multiverse.core.spawn.other"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters