Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Converts World Teleport To ParsedCommands #147

Merged
merged 3 commits into from
Feb 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package com.github.spigotbasics.modules.basicsworld

import com.github.spigotbasics.core.Spiper
import com.github.spigotbasics.core.command.parsed.CommandContextExecutor
import com.github.spigotbasics.core.command.parsed.context.MapContext
import com.github.spigotbasics.core.util.TeleportUtils
import org.bukkit.World
import org.bukkit.command.CommandSender
import org.bukkit.entity.Player
import java.util.logging.Level

class BasicsWorldCommand(private val module: BasicsWorldModule) : CommandContextExecutor<MapContext> {
override fun execute(
sender: CommandSender,
context: MapContext,
) {
val player = sender as Player
val force: Boolean = context["force"] != null
val world: World = context["world"] as World

val origin = player.location

if (world == player.world) {
module.msgAlreadyInWorld(world.name).sendToSender(player)
return
}

val targetLocation = TeleportUtils.getScaledLocationInOtherWorld(origin, world)

if (force) {
Spiper.teleportAsync(player, targetLocation)
}

val future = TeleportUtils.findSafeLocationInSameChunkAsync(targetLocation, world.minHeight, world.maxHeight)
future.thenAccept { safeLocation ->
if (safeLocation == null) {
module.coreMessages.noSafeLocationFound.sendToSender(player)
return@thenAccept
}

module.scheduler.runTask {
Spiper.teleportAsync(player, safeLocation).whenComplete { success, throwable ->
module.msgUnsuccessful(world.name).sendToSender(player)
if (throwable != null || !success) {
module.msgUnsuccessful(world.name).sendToSender(player)
throwable?.let {
module.logger.log(
Level.SEVERE,
"Could not teleport player to world ${world.name}",
it,
)
}
} else if (success) {
module.msgSuccess(world.name).sendToSender(player)
}
}
}
}.exceptionally { throwable ->
module.logger.log(Level.SEVERE, "Could not find safe location for player", throwable)
module.msgUnsuccessful(world.name).sendToSender(player)
null
}

module.msgStartingTeleport(world.name).sendToPlayerActionBar(player)
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package com.github.spigotbasics.modules.basicsworld

import com.github.spigotbasics.core.command.parsed.arguments.StringOptionArg
import com.github.spigotbasics.core.module.AbstractBasicsModule
import com.github.spigotbasics.core.module.loader.ModuleInstantiationContext
import org.bukkit.permissions.Permission

class BasicsWorldModule(context: ModuleInstantiationContext) : AbstractBasicsModule(context) {
private val permission = permissionManager.createSimplePermission("basics.world", "Allows to switch worlds using /world")
private val permission =
permissionManager.createSimplePermission("basics.world", "Allows to switch worlds using /world")
val worldPermissions = mutableMapOf<String, Permission>()

fun msgAlreadyInWorld(world: String) = messages.getMessage("already-in-world").tagUnparsed("world", world)
Expand All @@ -20,11 +22,30 @@ class BasicsWorldModule(context: ModuleInstantiationContext) : AbstractBasicsMod
).tagUnparsed("world", world)

override fun onEnable() {
commandFactory.rawCommandBuilder("world", permission)
.usage("<world>")
.description("Teleport to another world")
.executor(WorldCommand(this))
.register()
val worldArg = WorldArg(this, "World")
val forceArg = StringOptionArg("Force", listOf("--force", "-f"))
commandFactory.parsedCommandBuilder("world", permission)
.mapContext {
usage = "[world]"
aliases(listOf("worldtp", "tpworld"))

description("Teleport to another world")

path {
playerOnly()
arguments {
named("world", worldArg)
}
}

path {
playerOnly()
arguments {
named("world", worldArg)
named("forced", forceArg)
}
}
}.executor(BasicsWorldCommand(this)).register()

server.worlds.forEach {
getWorldPermission(it.name)
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.github.spigotbasics.modules.basicsworld

import com.github.spigotbasics.core.Basics
import com.github.spigotbasics.core.command.parsed.arguments.CommandArgument
import com.github.spigotbasics.core.messages.Message
import org.bukkit.Bukkit
import org.bukkit.World
import org.bukkit.command.CommandSender
import java.util.stream.Collectors

class WorldArg(private val module: BasicsWorldModule, name: String) : CommandArgument<World>(name) {
override fun parse(
sender: CommandSender,
value: String,
): World? {
return if (sender.hasPermission("basics.world.$value")) Bukkit.getWorld(value) else null
}

override fun errorMessage(
sender: CommandSender,
value: String,
): Message {
return Basics.messages.getMessage("world-not-found").tagUnparsed("argument", value)
}

override fun tabComplete(
sender: CommandSender,
typing: String,
): List<String> {
return Bukkit.getWorlds().stream().map { it.name }.filter { sender.hasPermission("basics.world.$it") }
.collect(Collectors.toList())
}
}

This file was deleted.