Skip to content

Commit

Permalink
fix: Warp problems (#113)
Browse files Browse the repository at this point in the history
Fixed a bug when loading warps of different (unloaded) worlds
  • Loading branch information
CoasterFreakDE committed May 6, 2024
1 parent b271000 commit e16d4ba
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 9 deletions.
7 changes: 6 additions & 1 deletion src/main/kotlin/net/blockventuremc/VentureLibs.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ import net.blockventuremc.database.DatabaseManager
import net.blockventuremc.modules.discord.DiscordBot
import net.blockventuremc.modules.i18n.TranslationCache
import net.blockventuremc.modules.placeholders.PlayerPlaceholderManager
import net.blockventuremc.modules.warps.WarpManager
import net.blockventuremc.utils.RegisterManager.registerMC
import net.blockventuremc.utils.mcasyncBlocking
import org.bukkit.Bukkit
import org.bukkit.plugin.java.JavaPlugin
import kotlin.math.log

class VentureLibs : JavaPlugin() {
companion object {
Expand Down Expand Up @@ -77,7 +79,10 @@ class VentureLibs : JavaPlugin() {
logger.warning("BOT_TOKEN is not set in .env file, Discord bot will not be started")
}

logger.info("Hello, Minecraft!")
logger.info("Preloading warps...")
WarpManager

logger.info("Plugin has been enabled.")
}

override fun onDisable() {
Expand Down
71 changes: 63 additions & 8 deletions src/main/kotlin/net/blockventuremc/modules/warps/WarpManager.kt
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
package net.blockventuremc.modules.warps

import dev.fruxz.stacked.extension.asPlainString
import io.sentry.Sentry
import net.blockventuremc.VentureLibs
import net.blockventuremc.extensions.getLogger
import net.blockventuremc.extensions.sendMessagePrefixed
import net.blockventuremc.extensions.translate
import net.blockventuremc.modules.general.events.custom.*
import net.blockventuremc.modules.general.model.Ranks
import net.blockventuremc.utils.FileConfig
import org.bukkit.Bukkit
import org.bukkit.WorldCreator

object WarpManager {

Expand Down Expand Up @@ -37,19 +44,67 @@ object WarpManager {
try {
val warpSection =
warpConfig.getConfigurationSection(warpKey) ?: error("Invalid warp section for $warpKey")
val warp = Warp(
warpKey,
warpSection.getLocation("location") ?: error("Invalid location for warp $warpKey"),
warpSection.getString("rankNeeded")?.let { Ranks.valueOf(it) } ?: Ranks.TEAM
)
warps[warpKey] = warp

if (warpSection.contains("old_location")) {
migrateLocation(warpConfig, warpKey)
}


val locationString = warpSection.getString("location") ?: error("Invalid location for warp $warpKey")
val ventureLocation = convertToBlockLocation(locationString)
val world = Bukkit.getWorld(ventureLocation.world)

if (world == null) {
Bukkit.createWorld(WorldCreator(ventureLocation.world))
getLogger().info("World ${ventureLocation.world} created successfully")
}

Bukkit.getScheduler().runTaskLater(VentureLibs.instance, Runnable {
val warp = Warp(
warpKey,
ventureLocation.toBukkitLocation(),
warpSection.getString("rankNeeded")?.let { Ranks.valueOf(it) } ?: Ranks.TEAM
)
warps[warpKey] = warp
}, 1L)
} catch (exception: Exception) {
// Log error
exception.printStackTrace()
Sentry.captureException(exception)
}
}
getLogger().info("Warps reloaded. ${warps.size} warps loaded.")

Bukkit.getScheduler().runTaskLater(VentureLibs.instance, Runnable {
getLogger().info("Warps reloaded. ${warps.size} warps loaded.")
}, 10L)
}

private fun migrateLocation(warpConfig: FileConfig, warpKey: String) {
val warpSection =
warpConfig.getConfigurationSection(warpKey) ?: error("Invalid warp section for $warpKey")

val oldWorld = warpSection.getString("old_location.world") ?: error("Invalid old location for warp $warpKey")
val oldX = warpSection.getDouble("old_location.x")
val oldY = warpSection.getDouble("old_location.y")
val oldZ = warpSection.getDouble("old_location.z")
val oldYaw = warpSection.getDouble("old_location.yaw").toFloat()
val oldPitch = warpSection.getDouble("old_location.pitch").toFloat()

val ventureLocation = VentureLocation(
x = oldX,
y = oldY,
z = oldZ,
yaw = oldYaw,
pitch = oldPitch,
world = oldWorld,
server = Bukkit.getServer().motd().asPlainString
)

warpConfig.set("${warpKey}.location", ventureLocation.toSimpleString())
warpConfig.set("${warpKey}.old_location", null)
warpConfig.saveConfig()

getLogger().info("Warp $warpKey migrated.")
}

/**
Expand Down Expand Up @@ -86,7 +141,7 @@ object WarpManager {
try {
val warpConfig = FileConfig(WARP_CONFIG_FILE)
warpConfig.set(warp.name, null)
warpConfig.set("${warp.name}.location", warp.location)
warpConfig.set("${warp.name}.location", warp.location.toVentureLocation().toSimpleString())
warpConfig.set("${warp.name}.rankNeeded", warp.rankNeeded.name)
warpConfig.saveConfig()
getLogger().info("Warp ${warp.name} added.")
Expand Down

0 comments on commit e16d4ba

Please sign in to comment.