summaryrefslogtreecommitdiff
path: root/src/main/kotlin/io/dico/parcels2/defaultimpl/ParcelImpl.kt
blob: dce36279b7bc610f1716bcce5931d5e228a9fa74 (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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package io.dico.parcels2.defaultimpl

import io.dico.parcels2.*
import io.dico.parcels2.Privilege.*
import io.dico.parcels2.util.ext.alsoIfTrue
import io.dico.parcels2.util.isServerThread
import io.dico.parcels2.util.math.Vec2i
import org.bukkit.Material
import org.joda.time.DateTime
import java.lang.IllegalStateException

class ParcelImpl (
    override val world: ParcelWorld,
    override val x: Int,
    override val z: Int
) : Parcel, ParcelId {
    override val id: ParcelId get() = this
    override val pos get() = Vec2i(x, z)
    override var data = ParcelDataHolder(); private set
    override val worldId: ParcelWorldId get() = world.id

    override fun copyData(newData: ParcelDataHolder, callerIsDatabase: Boolean) {
        if (callerIsDatabase) {
            data = newData
            return
        }

        val ownerChanged = owner != newData.owner
        data = newData

        if (ownerChanged) {
            updateOwnerSign(true, false, false)
        }

        world.storage.setParcelData(this, data)
    }

    override var owner: PlayerProfile?
        get() = data.owner
        set(value) {
            if (data.owner != value) {
                world.storage.setParcelOwner(this, value)
                data.owner = value
                updateOwnerSign(true, false, true)
            }
        }

    override val lastClaimTime: DateTime?
        get() = data.lastClaimTime

    override var isOwnerSignOutdated: Boolean
        get() = data.isOwnerSignOutdated
        set(value) {
            if (data.isOwnerSignOutdated != value) {
                world.storage.setParcelOwnerSignOutdated(this, value)
                data.isOwnerSignOutdated = value
            }
        }

    override fun updateOwnerSign(force: Boolean) {
        updateOwnerSign(false, force, true)
    }

    private fun updateOwnerSign(ownerChanged: Boolean, force: Boolean, updateDatabase: Boolean) {
        if (!ownerChanged && !isOwnerSignOutdated && !force) return

        val update = force || world.blockManager.isParcelInfoSectionLoaded(this)
        if (update) world.blockManager.updateParcelInfo(this, owner)

        if (updateDatabase) isOwnerSignOutdated = !update
        else data.isOwnerSignOutdated = !update
    }


    override val privilegeMap: PrivilegeMap
        get() = data.privilegeMap

    override val globalPrivileges: GlobalPrivileges?
        get() = keyOfOwner?.let { world.globalPrivileges[it] }

    override fun getRawStoredPrivilege(key: PrivilegeKey) = data.getRawStoredPrivilege(key)

    override fun setRawStoredPrivilege(key: PrivilegeKey, privilege: Privilege) =
        data.setRawStoredPrivilege(key, privilege).alsoIfTrue {
            world.storage.setLocalPrivilege(this, key, privilege)
        }

    override fun getStoredPrivilege(key: PrivilegeKey): Privilege =
        super.getStoredPrivilege(key).takeIf { it != DEFAULT }
            ?: globalPrivileges?.getStoredPrivilege(key)
            ?: DEFAULT

    override fun hasAnyDeclaredPrivileges() = data.hasAnyDeclaredPrivileges()

    private var _interactableConfig: InteractableConfiguration? = null

    private fun updateInteractableConfigStorage() {
        world.storage.setParcelOptionsInteractConfig(this, data.interactableConfig)
    }

    override var interactableConfig: InteractableConfiguration
        get() {
            if (_interactableConfig == null) {
                _interactableConfig = object : InteractableConfiguration {
                    override fun isInteractable(material: Material): Boolean = data.interactableConfig.isInteractable(material)
                    override fun isInteractable(clazz: Interactables): Boolean = data.interactableConfig.isInteractable(clazz)
                    override fun isDefault(): Boolean = data.interactableConfig.isDefault()

                    override fun setInteractable(clazz: Interactables, interactable: Boolean): Boolean =
                        data.interactableConfig.setInteractable(clazz, interactable).alsoIfTrue { updateInteractableConfigStorage() }

                    override fun clear(): Boolean =
                        data.interactableConfig.clear().alsoIfTrue { updateInteractableConfigStorage() }
                }
            }
            return _interactableConfig!!
        }
        set(value) {
            if (data.interactableConfig.copyFrom(value)) {
                updateInteractableConfigStorage()
            }
        }

    override val hasBlockVisitors: Boolean
        get() = permit != null

    private var permit: Permit? = null

    fun acquireBlockVisitorPermit(with: Permit): Boolean {
        if (permit === with) return true
        if (permit != null) return false
        permit = with
        return true
    }

    fun releaseBlockVisitorPermit(with: Permit) {
        if (permit !== with) throw IllegalStateException()
        permit = null
    }

    /*
    private var blockVisitors = AtomicInteger(0)

    override suspend fun withBlockVisitorPermit(block: suspend () -> Unit) {
        try {
            blockVisitors.getAndIncrement()
            block()
        } finally {
            blockVisitors.getAndDecrement()
        }
    }*/

    override fun toString() = parcelIdToString()

    override val infoString: String
        get() = getInfoString()
}

private fun Parcel.getInfoString() = StringBuilder().apply {
    with(InfoBuilder) {
        appendField("ID") {
            append(x)
            append(',')
            append(z)
        }

        val owner = owner
        appendField("Owner") {
            if (owner == null) {
                append(infoStringColor1)
                append("none")
            } else {
                append(owner.notNullName)
            }
        }

        // plotme appends biome here

        append('\n')

        val local: RawPrivileges = data
        val global = globalPrivileges
        appendProfilesWithPrivilege("Allowed", local, global, CAN_BUILD) // includes CAN_MANAGE privilege
        append('\n')
        appendProfilesWithPrivilege("Banned", local, global, BANNED)

        if (!interactableConfig.isDefault()) {
            val interactables = interactableConfig.interactableClasses
            appendFieldWithCount("Interactables", interactables.size) {
                interactables.asSequence().map { it.name }.joinTo(this)
            }
        }
    }
}.toString()