From 472e700e0422d1829aa26e04b74e2077807e75f0 Mon Sep 17 00:00:00 2001 From: Dico Karssiens Date: Wed, 1 Aug 2018 16:45:27 +0100 Subject: Improve database abstractions, add GlobalAddedData, some other things --- src/main/kotlin/io/dico/parcels2/Parcel.kt | 93 ++---------------------------- 1 file changed, 5 insertions(+), 88 deletions(-) (limited to 'src/main/kotlin/io/dico/parcels2/Parcel.kt') diff --git a/src/main/kotlin/io/dico/parcels2/Parcel.kt b/src/main/kotlin/io/dico/parcels2/Parcel.kt index 1d323dd..c8e7713 100644 --- a/src/main/kotlin/io/dico/parcels2/Parcel.kt +++ b/src/main/kotlin/io/dico/parcels2/Parcel.kt @@ -1,42 +1,16 @@ package io.dico.parcels2 import io.dico.parcels2.util.Vec2i -import io.dico.parcels2.util.getPlayerName import io.dico.parcels2.util.hasBuildAnywhere -import io.dico.parcels2.util.isValid -import io.dico.parcels2.util.uuid import org.bukkit.Bukkit import org.bukkit.OfflinePlayer import org.bukkit.entity.Player import org.joda.time.DateTime import java.util.* -interface AddedData { - val added: Map - - fun getAddedStatus(uuid: UUID): AddedStatus - fun setAddedStatus(uuid: UUID, status: AddedStatus): Boolean - - fun compareAndSetAddedStatus(uuid: UUID, expect: AddedStatus, status: AddedStatus): Boolean = - (getAddedStatus(uuid) == expect).also { if (it) setAddedStatus(uuid, status) } - - fun isAllowed(uuid: UUID) = getAddedStatus(uuid) == AddedStatus.ALLOWED - fun allow(uuid: UUID) = setAddedStatus(uuid, AddedStatus.ALLOWED) - fun disallow(uuid: UUID) = compareAndSetAddedStatus(uuid, AddedStatus.ALLOWED, AddedStatus.DEFAULT) - fun isBanned(uuid: UUID) = getAddedStatus(uuid) == AddedStatus.BANNED - fun ban(uuid: UUID) = setAddedStatus(uuid, AddedStatus.BANNED) - fun unban(uuid: UUID) = compareAndSetAddedStatus(uuid, AddedStatus.BANNED, AddedStatus.DEFAULT) - - fun isAllowed(player: OfflinePlayer) = isAllowed(player.uuid) - fun allow(player: OfflinePlayer) = allow(player.uuid) - fun disallow(player: OfflinePlayer) = disallow(player.uuid) - fun isBanned(player: OfflinePlayer) = isBanned(player.uuid) - fun ban(player: OfflinePlayer) = ban(player.uuid) - fun unban(player: OfflinePlayer) = unban(player.uuid) -} - interface ParcelData : AddedData { var owner: ParcelOwner? + val since: DateTime? fun canBuild(player: OfflinePlayer, checkAdmin: Boolean = true, checkGlobal: Boolean = true): Boolean @@ -83,6 +57,8 @@ class Parcel(val world: ParcelWorld, val pos: Vec2i) : ParcelData { override fun isAllowed(uuid: UUID) = data.isAllowed(uuid) override fun canBuild(player: OfflinePlayer, checkAdmin: Boolean, checkGlobal: Boolean) = data.canBuild(player) + override val since: DateTime? get() = data.since + override var owner: ParcelOwner? get() = data.owner set(value) { @@ -94,7 +70,7 @@ class Parcel(val world: ParcelWorld, val pos: Vec2i) : ParcelData { override fun setAddedStatus(uuid: UUID, status: AddedStatus): Boolean { return data.setAddedStatus(uuid, status).also { - if (it) world.storage.setParcelPlayerState(this, uuid, status.asBoolean) + if (it) world.storage.setParcelPlayerStatus(this, uuid, status) } } @@ -117,16 +93,9 @@ class Parcel(val world: ParcelWorld, val pos: Vec2i) : ParcelData { var hasBlockVisitors: Boolean = false; private set } -open class AddedDataHolder(override var added: MutableMap - = mutableMapOf()) : AddedData { - override fun getAddedStatus(uuid: UUID): AddedStatus = added.getOrDefault(uuid, AddedStatus.DEFAULT) - override fun setAddedStatus(uuid: UUID, status: AddedStatus): Boolean = status.takeIf { it != AddedStatus.DEFAULT } - ?.let { added.put(uuid, it) != it } - ?: added.remove(uuid) != null -} - class ParcelDataHolder : AddedDataHolder(), ParcelData { override var owner: ParcelOwner? = null + override var since: DateTime? = null override fun canBuild(player: OfflinePlayer, checkAdmin: Boolean, checkGlobal: Boolean) = isAllowed(player.uniqueId) || owner.let { it != null && it.matches(player, allowNameMatch = false) } || (checkAdmin && player is Player && player.hasBuildAnywhere) @@ -135,55 +104,3 @@ class ParcelDataHolder : AddedDataHolder(), ParcelData { override var allowInteractInventory = true } -enum class AddedStatus { - DEFAULT, - ALLOWED, - BANNED; - - val asBoolean - get() = when (this) { - DEFAULT -> null - ALLOWED -> true - BANNED -> false - } -} - -@Suppress("UsePropertyAccessSyntax") -class ParcelOwner(val uuid: UUID? = null, - name: String? = null, - val since: DateTime? = null) { - - companion object { - fun create(uuid: UUID?, name: String?, time: DateTime? = null): ParcelOwner? { - return uuid?.let { ParcelOwner(uuid, name, time) } - ?: name?.let { ParcelOwner(uuid, name, time) } - } - } - - val name: String? - - init { - uuid ?: name ?: throw IllegalArgumentException("uuid and/or name must be present") - - if (name != null) this.name = name - else { - val offlinePlayer = Bukkit.getOfflinePlayer(uuid).takeIf { it.isValid } - this.name = offlinePlayer?.name - } - } - - val playerName get() = getPlayerName(uuid, name) - - fun matches(player: OfflinePlayer, allowNameMatch: Boolean = false): Boolean { - return uuid?.let { it == player.uniqueId } ?: false - || (allowNameMatch && name?.let { it == player.name } ?: false) - } - - val onlinePlayer: Player? get() = uuid?.let { Bukkit.getPlayer(uuid) } - val onlinePlayerAllowingNameMatch: Player? get() = onlinePlayer ?: name?.let { Bukkit.getPlayer(name) } - - @Suppress("DEPRECATION") - val offlinePlayer - get() = (uuid?.let { Bukkit.getOfflinePlayer(it) } ?: Bukkit.getOfflinePlayer(name)) - ?.takeIf { it.isValid } -} -- cgit v1.2.3