summaryrefslogtreecommitdiff
path: root/src/main/kotlin/io/dico/parcels2/defaultimpl/ParcelWorldImpl.kt
blob: 531a25fc2b05772568525ad78f399ccf29f4bff0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
@file:Suppress("CanBePrimaryConstructorProperty", "UsePropertyAccessSyntax")

package io.dico.parcels2.defaultimpl

import io.dico.parcels2.*
import io.dico.parcels2.options.RuntimeWorldOptions
import io.dico.parcels2.storage.Storage
import kotlinx.coroutines.CoroutineScope
import org.bukkit.GameRule
import org.bukkit.World
import org.joda.time.DateTime
import java.util.UUID

class ParcelWorldImpl(
    val plugin: ParcelsPlugin,
    override val world: World,
    override val generator: ParcelGenerator,
    override var options: RuntimeWorldOptions,
    containerFactory: ParcelContainerFactory
) : ParcelWorld, ParcelWorldId, ParcelContainer, ParcelLocator {
    override val id: ParcelWorldId get() = this
    override val uid: UUID? get() = world.uid

    override val storage get() = plugin.storage
    override val globalPrivileges get() = plugin.globalPrivileges

    init {
        if (generator.world != world) {
            throw IllegalArgumentException()
        }
    }

    override val name: String = world.name!!
    override val container: ParcelContainer = containerFactory(this)
    override val locator: ParcelLocator
    override val blockManager: ParcelBlockManager

    init {
        val (locator, blockManager) = generator.makeParcelLocatorAndBlockManager(plugin.parcelProvider, container, plugin, plugin.jobDispatcher)
        this.locator = locator
        this.blockManager = blockManager
        enforceOptions()
    }

    fun enforceOptions() {
        if (options.dayTime) {
            world.setGameRule(GameRule.DO_DAYLIGHT_CYCLE, false)
            world.setTime(6000)
        }

        if (options.noWeather) {
            world.setStorm(false)
            world.setThundering(false)
            world.weatherDuration = Int.MAX_VALUE
        }

        world.setGameRule(GameRule.DO_TILE_DROPS, options.doTileDrops)
    }

    // Accessed by ParcelProviderImpl
    override var creationTime: DateTime? = null


    override fun getParcelAt(x: Int, z: Int): Parcel? = locator.getParcelAt(x, z)

    override fun getParcelIdAt(x: Int, z: Int): ParcelId? = locator.getParcelIdAt(x, z)

    override fun getParcelById(x: Int, z: Int): Parcel? = container.getParcelById(x, z)

    override fun getParcelById(id: ParcelId): Parcel? = container.getParcelById(id)

    override fun nextEmptyParcel(): Parcel? = container.nextEmptyParcel()

    override fun toString() = parcelWorldIdToString()
}