-
Notifications
You must be signed in to change notification settings - Fork 5
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 #149 from SpigotBasics/dev
Parsed /world command
- Loading branch information
Showing
5 changed files
with
185 additions
and
168 deletions.
There are no files selected for viewing
80 changes: 80 additions & 0 deletions
80
...s-world/src/main/kotlin/com/github/spigotbasics/modules/basicsworld/BasicsWorldCommand.kt
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,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 | ||
} | ||
} | ||
} |
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
26 changes: 0 additions & 26 deletions
26
.../basics-world/src/main/kotlin/com/github/spigotbasics/modules/basicsworld/LocationList.kt
This file was deleted.
Oops, something went wrong.
74 changes: 74 additions & 0 deletions
74
modules/basics-world/src/main/kotlin/com/github/spigotbasics/modules/basicsworld/WorldArg.kt
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,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 | ||
} | ||
} |
134 changes: 0 additions & 134 deletions
134
.../basics-world/src/main/kotlin/com/github/spigotbasics/modules/basicsworld/WorldCommand.kt
This file was deleted.
Oops, something went wrong.