Update game states & world management

This commit is contained in:
Laurin
2025-12-06 05:41:42 +01:00
parent 2c10e3e7fd
commit 590318772f
14 changed files with 1105 additions and 362 deletions

View File

@@ -0,0 +1,157 @@
package club.mcscrims.speedhg.world
import club.mcscrims.speedhg.SpeedHG
import org.bukkit.Bukkit
import org.bukkit.GameRule
import org.bukkit.Location
import org.bukkit.World
import org.popcraft.chunky.api.ChunkyAPI
import java.io.File
class WorldManager(
private val plugin: SpeedHG
) {
private lateinit var worldName: String
private lateinit var world: World
fun highestLocationWithRadius(
center: Location,
radius: Int
): Location
{
getWorld()
val minX = center.blockX - radius
val minZ = center.blockZ - radius
val maxX = center.blockX + radius
val maxZ = center.blockZ + radius
var highestY = center.blockY
var highestX = minX
var highestZ = minZ
for ( x in minX..maxX )
for ( z in minZ..maxZ )
{
val y = world.getHighestBlockYAt( x, z )
if ( y > highestY )
{
highestY = y
highestX = x
highestZ = z
}
}
val highest = Location( world, highestX.toDouble(), highestY.toDouble(), highestZ.toDouble() )
return highest
}
/*
* DELETION >>
*/
fun deleteWorld()
{
getWorld()
Bukkit.unloadWorld( worldName, false )
val folder = File( worldName )
deleteFolder( folder )
}
private fun deleteFolder(
folder: File
) {
val files = folder.listFiles()
if ( files != null )
for ( f in files )
{
if ( f.isDirectory )
deleteFolder( f )
else
f.delete()
}
folder.delete()
}
/*
* WORLD >>
*/
lateinit var spawnLocation: Location
var borderDecrease: Double = 100.0
fun setupWorld()
{
val world = getWorld() ?: return
plugin.logger.info("Setting up world...")
// BORDER >>
plugin.logger.info("Setting up world... [STAGE [1]: WORLDBORDER]")
spawnLocation = Location( world, 0.0, world.getHighestBlockYAt( 0, 0).toDouble(), 0.0 )
world.worldBorder.center = spawnLocation
world.worldBorder.size = plugin.pluginConfig.data.world.border["size"]!!
world.worldBorder.warningDistance = plugin.pluginConfig.data.world.border["warning_distance"]!!.toInt()
world.worldBorder.damageAmount = plugin.pluginConfig.data.world.border["damage"]!!
borderDecrease = plugin.pluginConfig.data.world.border["decrease"]!!
// GAMERULES >>
plugin.logger.info("Setting up world... [Stage [2]: GAMERULES]")
world.setGameRule( GameRule.ANNOUNCE_ADVANCEMENTS, false )
world.setGameRule( GameRule.DO_INSOMNIA, false )
world.setGameRule( GameRule.DISABLE_RAIDS, true )
world.setGameRule( GameRule.DO_PATROL_SPAWNING, false )
world.setGameRule( GameRule.DO_TRADER_SPAWNING, false )
// CHUNKY >>
plugin.logger.info("Setting up world... [Stage [3]: CHUNKY]")
val chunky = Bukkit.getServicesManager().load( ChunkyAPI::class.java )
if ( chunky == null || chunky.version() != 0 )
{
plugin.isReady = true
return
}
val radius = world.worldBorder.size / 2
chunky.startTask( worldName, "square", 0.0, 0.0, radius, radius, "concentric" )
chunky.onGenerationComplete { plugin.isReady = true }
plugin.server.dispatchCommand( Bukkit.getConsoleSender(), "chunky silent" )
// FINISH >>
plugin.logger.info("World has been set up!")
}
private fun setWorld(
worldName: String
): World?
{
this.worldName = worldName
val world = Bukkit.getWorld( worldName )
if ( world != null )
this.world = world
return world
}
fun getWorld(): World?
{
return if ( !::world.isInitialized )
setWorld( plugin.pluginConfig.data.world.name )
else this.world
}
}