summaryrefslogtreecommitdiff
path: root/src/main/kotlin/io/dico/parcels2/defaultimpl/ParcelWorldImpl.kt
blob: c143ff63a20121d485a8d0269346ab2fae6cbf2a (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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
@file:Suppress("CanBePrimaryConstructorProperty", "UsePropertyAccessSyntax")

package io.dico.parcels2.defaultimpl

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

class ParcelWorldImpl(override val world: World,
                      override val generator: ParcelGenerator,
                      override var options: RuntimeWorldOptions,
                      override val storage: Storage,
                      override val globalPrivileges: GlobalPrivilegesManager,
                      containerFactory: ParcelContainerFactory,
                      coroutineScope: CoroutineScope,
                      worktimeLimiter: WorktimeLimiter)
    : ParcelWorld,
      ParcelWorldId,
      ParcelContainer, /* missing delegation */
      ParcelLocator /* missing delegation */ {

    override val id: ParcelWorldId get() = this
    override val uid: UUID? get() = world.uid

    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 pair = generator.makeParcelLocatorAndBlockManager(id, container, coroutineScope, worktimeLimiter)
        locator = pair.first
        blockManager = pair.second

        enforceOptions()
    }

    fun enforceOptions() {
        if (options.dayTime) {
            world.setGameRuleValue("doDaylightCycle", "false")
            world.setTime(6000)
        }

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

        world.setGameRuleValue("doTileDrops", "${options.doTileDrops}")
    }

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

    /*
    Interface delegation needs to be implemented manually because JetBrains has yet to fix it.
     */

    // ParcelLocator interface
    override fun getParcelAt(x: Int, z: Int): Parcel? {
        return locator.getParcelAt(x, z)
    }

    override fun getParcelIdAt(x: Int, z: Int): ParcelId? {
        return locator.getParcelIdAt(x, z)
    }

    // ParcelContainer interface
    override fun getParcelById(x: Int, z: Int): Parcel? {
        return container.getParcelById(x, z)
    }

    override fun nextEmptyParcel(): Parcel? {
        return container.nextEmptyParcel()
    }

    override fun toString() = toStringExt()
}