Skip to content

Commit

Permalink
Merge pull request #149 from SpigotBasics/dev
Browse files Browse the repository at this point in the history
Parsed /world command
  • Loading branch information
mfnalex authored Feb 18, 2024
2 parents 79a26c2 + ba8cf49 commit 5fd573f
Show file tree
Hide file tree
Showing 5 changed files with 185 additions and 168 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
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.logger.BasicsLoggerFactory
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> {
private val logger = BasicsLoggerFactory.getModuleLogger(module, BasicsWorldCommand::class)

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

logger.debug(100, "Teleporting player ${player.name} to world ${world.name}")

if (world == player.world) {
module.msgAlreadyInWorld(world.name).sendToSender(player)
logger.debug(100, "Abort: Player already is in target world")
return
}

val targetLocation = TeleportUtils.getScaledLocationInOtherWorld(origin, world)
logger.debug(100, "Scaled target location: $targetLocation")

if (force) {
logger.debug(100, "Force-Teleporting player ${player.name} to world ${world.name} now ...")
Spiper.teleportAsync(player, targetLocation)
return
}

module.msgStartingTeleport(world.name).sendToPlayerActionBar(player)

val future = TeleportUtils.findSafeLocationInSameChunkAsync(targetLocation, world.minHeight, world.maxHeight)

future.thenAccept { safeLocation ->
if (safeLocation == null) {
logger.debug(100, "No safe location found.")
module.coreMessages.noSafeLocationFound.sendToSender(player)
return@thenAccept
}

module.scheduler.runTask {
logger.debug(100, "Safe location found: $safeLocation")
Spiper.teleportAsync(player, safeLocation).whenComplete { success, throwable ->
if (throwable != null || !success) {
logger.debug(100, "Could not teleport player to world ${world.name} - success: $success, throwable: $throwable")
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) {
logger.debug(100, "Player ${player.name} teleported to world ${world.name} successfully")
module.msgSuccess(world.name).sendToSender(player)
}
}
}
}.exceptionally { throwable ->
logger.debug(100, "Exception while finding safe location for player: $throwable")
module.logger.log(Level.SEVERE, "Could not find safe location for player", throwable)
module.msgUnsuccessful(world.name).sendToSender(player)
null
}
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
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.World
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,14 +23,33 @@ 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("force", forceArg)
}
}
}.executor(BasicsWorldCommand(this)).register()

server.worlds.forEach {
getWorldPermission(it.name)
getWorldPermission(it)
}
}

Expand All @@ -36,7 +58,8 @@ class BasicsWorldModule(context: ModuleInstantiationContext) : AbstractBasicsMod
messages.reload()
}

fun getWorldPermission(name: String): Permission {
fun getWorldPermission(world: World): Permission {
val name = world.name
return worldPermissions.computeIfAbsent(name.lowercase()) {
permissionManager.createSimplePermission(
"basics.world.${name.lowercase()}",
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
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 com.github.spigotbasics.core.util.WorldUtils
import org.bukkit.Bukkit
import org.bukkit.World
import org.bukkit.command.CommandSender
import org.bukkit.permissions.Permissible

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

private fun getWorld(name: String): World? {
val worlds = Bukkit.getWorlds()
return when (name) {
"0" -> worlds[0]
"1" -> worlds.find { it.environment == World.Environment.NETHER }
"2" -> worlds.find { it.environment == World.Environment.THE_END }
else -> Bukkit.getWorld(name)
}
}

override fun errorMessage(
sender: CommandSender,
value: String,
): Message {
val world = getWorld(value)
if (world == null) {
return Basics.messages.worldNotFound(value)
}
val perm = module.getWorldPermission(world)
if (!sender.hasPermission(perm)) {
return Basics.messages.noPermission(perm)
}
throw IllegalStateException()
}

override fun tabComplete(
sender: CommandSender,
typing: String,
): List<String> {
return allWorldsAnd012(sender)
}

private fun allWorldsAnd012(sender: Permissible): MutableList<String> {
val list =
Bukkit.getWorlds().filter {
val hasPerm = sender.hasPermission(module.getWorldPermission(it))
return@filter hasPerm
}.map { it.name }.toMutableList()

if (list.contains(WorldUtils.defaultWorldName)) {
list.add("0")
}
if (list.contains(WorldUtils.netherWorldName)) {
list.add("1")
}
if (list.contains(WorldUtils.endWorldName)) {
list.add("2")
}
return list
}
}

This file was deleted.

0 comments on commit 5fd573f

Please sign in to comment.