From cb13a148572f3dc434534a0d20010992603f7ae1 Mon Sep 17 00:00:00 2001 From: Micalhl Date: Wed, 14 Aug 2024 12:06:12 +0800 Subject: [PATCH] Update --- .../kotlin/com/mcstarrysky/land/data/Land.kt | 29 +++++++++-- .../com/mcstarrysky/land/flag/PermAdmin.kt | 51 +++++++++++++++++++ .../land/listener/LandEnterLeaveListener.kt | 28 ---------- 3 files changed, 76 insertions(+), 32 deletions(-) create mode 100644 src/main/kotlin/com/mcstarrysky/land/flag/PermAdmin.kt delete mode 100644 src/main/kotlin/com/mcstarrysky/land/listener/LandEnterLeaveListener.kt diff --git a/src/main/kotlin/com/mcstarrysky/land/data/Land.kt b/src/main/kotlin/com/mcstarrysky/land/data/Land.kt index f9887fc..10537a2 100644 --- a/src/main/kotlin/com/mcstarrysky/land/data/Land.kt +++ b/src/main/kotlin/com/mcstarrysky/land/data/Land.kt @@ -1,6 +1,10 @@ +@file:Suppress("DEPRECATION") + package com.mcstarrysky.land.data +import com.mcstarrysky.land.flag.PermAdmin import com.mcstarrysky.land.flag.PermTeleport +import com.mcstarrysky.land.flag.Permission import com.mcstarrysky.land.manager.LandManager import com.mcstarrysky.land.serializers.ChunkSerializer import com.mcstarrysky.land.serializers.LocationSerializer @@ -18,6 +22,7 @@ import org.bukkit.OfflinePlayer import org.bukkit.entity.Player import taboolib.common5.cbool import java.util.Date +import java.util.HashMap import java.util.UUID /** @@ -41,10 +46,22 @@ data class Land( var leaveMessage: String? = "你离开了 &{#8abcd1}$name", @Serializable(with = LocationSerializer::class) var tpLocation: Location, + @Deprecated(message = "协作者功能已废除") val cooperators: MutableList<@Serializable(with = UUIDSerializer::class) UUID>, - val flags: MutableMap = mutableMapOf() + val flags: MutableMap = mutableMapOf(), + // 玩家 UUID 对一个 Map, Map 是 权限节点对应的值 + val users: MutableMap<@Serializable(with = UUIDSerializer::class) UUID, MutableMap>, ) { + init { + // 迁移协作者 + if (cooperators.isNotEmpty()) { + cooperators.forEach { uuid -> + (users.computeIfAbsent(uuid) { HashMap() }) += PermAdmin.id to true + } + } + } + @Transient val date = DATE_FORMAT.format(Date(timestamp)) @@ -56,15 +73,19 @@ data class Land( PermTeleport.teleport(player, this) } - fun hasPermission(player: Player): Boolean { - return player.isOp || player.uniqueId == owner || player.uniqueId in cooperators + fun hasPermission(player: Player, perm: Permission? = null): Boolean { + return if (perm == null) { + player.isOp || player.uniqueId == owner || users[player.uniqueId]?.get(PermAdmin.id) == true + } else { + users[player.uniqueId]?.get(perm.id) == true + } } fun saveToString(): String { return json.encodeToString(this) } - fun getOwner(): OfflinePlayer? { + private fun getOwner(): OfflinePlayer? { if (owner != ZERO_UUID) { return Bukkit.getOfflinePlayer(owner) } diff --git a/src/main/kotlin/com/mcstarrysky/land/flag/PermAdmin.kt b/src/main/kotlin/com/mcstarrysky/land/flag/PermAdmin.kt new file mode 100644 index 0000000..568d13a --- /dev/null +++ b/src/main/kotlin/com/mcstarrysky/land/flag/PermAdmin.kt @@ -0,0 +1,51 @@ +package com.mcstarrysky.land.flag + +import com.mcstarrysky.land.data.Land +import com.mcstarrysky.land.util.display +import com.mcstarrysky.land.util.registerPermission +import org.bukkit.inventory.ItemStack +import taboolib.common.LifeCycle +import taboolib.common.platform.Awake +import taboolib.library.xseries.XMaterial +import taboolib.platform.util.buildItem + +/** + * Land + * com.mcstarrysky.land.flag.PermAdmin + * + * @author mical + * @since 2024/8/14 11:58 + */ +object PermAdmin : Permission { + + @Awake(LifeCycle.ENABLE) + private fun init() { + registerPermission() + } + + override val id: String + get() = "admin" + + override val default: Boolean + get() = false + + override val worldSide: Boolean + get() = true + + override val playerSide: Boolean + get() = true + + override fun generateMenuItem(land: Land): ItemStack { + return buildItem(XMaterial.COMMAND_BLOCK) { + name = "&f管理权力 ${land.getFlagOrNull(id).display}" + lore += listOf( + "&7允许行为:", + "&8除管理领地与扩展领地外的所有权力", + "", + "&e左键修改值, 右键取消设置" + ) + if (land.getFlagOrNull(id) == true) shiny() + colored() + } + } +} \ No newline at end of file diff --git a/src/main/kotlin/com/mcstarrysky/land/listener/LandEnterLeaveListener.kt b/src/main/kotlin/com/mcstarrysky/land/listener/LandEnterLeaveListener.kt deleted file mode 100644 index 4a75471..0000000 --- a/src/main/kotlin/com/mcstarrysky/land/listener/LandEnterLeaveListener.kt +++ /dev/null @@ -1,28 +0,0 @@ -package com.mcstarrysky.land.listener - -import com.mcstarrysky.land.manager.LandManager -import com.mcstarrysky.land.util.prettyInfo -import org.bukkit.event.player.PlayerMoveEvent -import taboolib.common.platform.event.SubscribeEvent - -/** - * Land - * com.mcstarrysky.land.listener.KandEnterLeaveListener - * - * @author mical - * @since 2024/8/3 14:48 - */ -object LandEnterLeaveListener { - - // @SubscribeEvent - fun e(e: PlayerMoveEvent) { - if (e.from.x != e.to.x || e.from.y != e.to.y || e.from.z != e.to.z) { - val from = LandManager.getLand(e.from) - val to = LandManager.getLand(e.to) - if (from != to) { - from?.leaveMessage?.let { e.player.prettyInfo(it) } - to?.enterMessage?.let { e.player.prettyInfo(it) } - } - } - } -} \ No newline at end of file