summaryrefslogtreecommitdiff
path: root/src/main/kotlin/io/dico/parcels2/defaultimpl/ParcelWorldImpl.kt
blob: 4a168f5a36850e8e2501d66f5b992e5757becf82 (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
91
92
93
94
95
@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 org.bukkit.World
import java.util.UUID

class ParcelWorldImpl private
constructor(override val world: World,
            override val generator: ParcelGenerator,
            override var options: RuntimeWorldOptions,
            override val storage: Storage,
            override val globalAddedData: GlobalAddedDataManager,
            containerFactory: ParcelContainerFactory,
            blockManager: ParcelBlockManager)
    : ParcelWorld,
      ParcelWorldId,
      ParcelContainer, // missing delegation
      ParcelLocator, // missing delegation
      ParcelBlockManager by blockManager {
    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 = generator.makeParcelLocator(container)
    override val blockManager: ParcelBlockManager = blockManager

    init {
        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}")
    }

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

    companion object {
        // Use this to be able to delegate blockManager and assign it to a property too, at least.
        operator fun invoke(world: World,
                            generator: ParcelGenerator,
                            options: RuntimeWorldOptions,
                            storage: Storage,
                            globalAddedData: GlobalAddedDataManager,
                            containerFactory: ParcelContainerFactory,
                            worktimeLimiter: WorktimeLimiter): ParcelWorldImpl {
            val blockManager = generator.makeParcelBlockManager(worktimeLimiter)
            return ParcelWorldImpl(world, generator, options, storage, globalAddedData, containerFactory, blockManager)
        }
    }

    // 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()
    }


}