Use ParcelContainerFactory on world initialization
This commit is contained in:
@@ -2,7 +2,9 @@ package io.dico.parcels2
|
|||||||
|
|
||||||
import io.dico.parcels2.math.Vec2i
|
import io.dico.parcels2.math.Vec2i
|
||||||
import io.dico.parcels2.math.floor
|
import io.dico.parcels2.math.floor
|
||||||
|
import io.dico.parcels2.storage.Storage
|
||||||
import io.dico.parcels2.util.doAwait
|
import io.dico.parcels2.util.doAwait
|
||||||
|
import kotlinx.coroutines.experimental.launch
|
||||||
import org.bukkit.Bukkit
|
import org.bukkit.Bukkit
|
||||||
import org.bukkit.Location
|
import org.bukkit.Location
|
||||||
import org.bukkit.World
|
import org.bukkit.World
|
||||||
@@ -10,15 +12,14 @@ import org.bukkit.WorldCreator
|
|||||||
import org.bukkit.block.Block
|
import org.bukkit.block.Block
|
||||||
import org.bukkit.entity.Entity
|
import org.bukkit.entity.Entity
|
||||||
import org.bukkit.entity.Player
|
import org.bukkit.entity.Player
|
||||||
import org.bukkit.plugin.Plugin
|
|
||||||
import java.util.*
|
import java.util.*
|
||||||
import kotlin.coroutines.experimental.buildSequence
|
import kotlin.coroutines.experimental.buildSequence
|
||||||
|
|
||||||
class Worlds(private val plugin: Plugin) {
|
class Worlds(private val plugin: ParcelsPlugin) {
|
||||||
val worlds: Map<String, ParcelWorld> get() = _worlds
|
val worlds: Map<String, ParcelWorld> get() = _worlds
|
||||||
private val _worlds: MutableMap<String, ParcelWorld> = HashMap()
|
private val _worlds: MutableMap<String, ParcelWorld> = HashMap()
|
||||||
|
|
||||||
fun getWorld(name: String): ParcelWorld? = _worlds.get(name)
|
fun getWorld(name: String): ParcelWorld? = _worlds[name]
|
||||||
|
|
||||||
fun getWorld(world: World): ParcelWorld? = getWorld(world.name)
|
fun getWorld(world: World): ParcelWorld? = getWorld(world.name)
|
||||||
|
|
||||||
@@ -40,7 +41,16 @@ class Worlds(private val plugin: Plugin) {
|
|||||||
for ((worldName, worldOptions) in options.worlds.entries) {
|
for ((worldName, worldOptions) in options.worlds.entries) {
|
||||||
val world: ParcelWorld
|
val world: ParcelWorld
|
||||||
try {
|
try {
|
||||||
world = ParcelWorld(worldName, worldOptions, worldOptions.generator.getGenerator(this, worldName))
|
val containerFactory: ParcelContainerFactory = { parcelWorld ->
|
||||||
|
DefaultParcelContainer(parcelWorld, plugin.storage)
|
||||||
|
}
|
||||||
|
|
||||||
|
world = ParcelWorld(
|
||||||
|
worldName,
|
||||||
|
worldOptions,
|
||||||
|
worldOptions.generator.getGenerator(this, worldName),
|
||||||
|
containerFactory)
|
||||||
|
|
||||||
} catch (ex: Exception) {
|
} catch (ex: Exception) {
|
||||||
ex.printStackTrace()
|
ex.printStackTrace()
|
||||||
continue
|
continue
|
||||||
@@ -89,18 +99,12 @@ interface ParcelProvider {
|
|||||||
fun parcelAt(block: Block): Parcel? = parcelAt(block.x, block.z)
|
fun parcelAt(block: Block): Parcel? = parcelAt(block.x, block.z)
|
||||||
}
|
}
|
||||||
|
|
||||||
class ParcelWorld(val name: String,
|
class ParcelWorld constructor(val name: String,
|
||||||
val options: WorldOptions,
|
val options: WorldOptions,
|
||||||
val generator: ParcelGenerator) : ParcelProvider by generator {
|
val generator: ParcelGenerator,
|
||||||
val world: World by lazy {
|
containerFactory: ParcelContainerFactory) : ParcelProvider by generator {
|
||||||
val tmp = Bukkit.getWorld(name)
|
val world: World by lazy { Bukkit.getWorld(name) ?: throw NullPointerException("World $name does not appear to be loaded") }
|
||||||
if (tmp == null) {
|
val container: ParcelContainer = containerFactory(this)
|
||||||
throw NullPointerException("World $name does not appear to be loaded")
|
|
||||||
}
|
|
||||||
tmp
|
|
||||||
}
|
|
||||||
|
|
||||||
val container: ParcelContainer = DefaultParcelContainer(this)
|
|
||||||
|
|
||||||
fun parcelByID(x: Int, z: Int): Parcel? {
|
fun parcelByID(x: Int, z: Int): Parcel? {
|
||||||
TODO("not implemented")
|
TODO("not implemented")
|
||||||
@@ -135,7 +139,10 @@ abstract class ParcelContainer {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
class DefaultParcelContainer(private val world: ParcelWorld) : ParcelContainer() {
|
typealias ParcelContainerFactory = (ParcelWorld) -> ParcelContainer
|
||||||
|
|
||||||
|
class DefaultParcelContainer(private val world: ParcelWorld,
|
||||||
|
private val storage: Storage) : ParcelContainer() {
|
||||||
private var parcels: Array<Array<Parcel>>
|
private var parcels: Array<Array<Parcel>>
|
||||||
|
|
||||||
init {
|
init {
|
||||||
@@ -154,13 +161,13 @@ class DefaultParcelContainer(private val world: ParcelWorld) : ParcelContainer()
|
|||||||
|
|
||||||
fun initArray(axisLimit: Int, world: ParcelWorld, cur: DefaultParcelContainer? = null): Array<Array<Parcel>> {
|
fun initArray(axisLimit: Int, world: ParcelWorld, cur: DefaultParcelContainer? = null): Array<Array<Parcel>> {
|
||||||
val arraySize = 2 * axisLimit + 1
|
val arraySize = 2 * axisLimit + 1
|
||||||
return Array(arraySize, {
|
return Array(arraySize) {
|
||||||
val x = it - axisLimit
|
val x = it - axisLimit
|
||||||
Array(arraySize, {
|
Array(arraySize) {
|
||||||
val z = it - axisLimit
|
val z = it - axisLimit
|
||||||
cur?.ployByID(x, z) ?: Parcel(world, Vec2i(x, z))
|
cur?.ployByID(x, z) ?: Parcel(world, Vec2i(x, z))
|
||||||
})
|
}
|
||||||
})
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun ployByID(x: Int, z: Int): Parcel? {
|
override fun ployByID(x: Int, z: Int): Parcel? {
|
||||||
@@ -178,17 +185,12 @@ class DefaultParcelContainer(private val world: ParcelWorld) : ParcelContainer()
|
|||||||
}
|
}
|
||||||
|
|
||||||
fun loadAllData() {
|
fun loadAllData() {
|
||||||
/*
|
val channel = storage.readParcelData(allParcels(), 100)
|
||||||
val channel = Main.instance.storage.readParcelData(allParcels(), 100).channel
|
launch(storage.asyncDispatcher) {
|
||||||
launch(Main.instance.storage.asyncDispatcher) {
|
|
||||||
for ((parcel, data) in channel) {
|
for ((parcel, data) in channel) {
|
||||||
if (data != null) {
|
data?.let { parcel.data = it }
|
||||||
parcel.data = data
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
*/
|
|
||||||
TODO()
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user