diff --git a/src/main/kotlin/cc/maxmc/chunkloader/SimpleChunkLoader.kt b/src/main/kotlin/cc/maxmc/chunkloader/SimpleChunkLoader.kt index ee0d8b8..c29ac1b 100644 --- a/src/main/kotlin/cc/maxmc/chunkloader/SimpleChunkLoader.kt +++ b/src/main/kotlin/cc/maxmc/chunkloader/SimpleChunkLoader.kt @@ -1,5 +1,6 @@ package cc.maxmc.chunkloader +import cc.maxmc.chunkloader.command.CommandOpenGUI import cc.maxmc.chunkloader.misc.ChunkLoaderManager import taboolib.common.platform.Plugin import taboolib.common.platform.function.info @@ -7,7 +8,9 @@ import taboolib.common.platform.function.info object SimpleChunkLoader : Plugin() { override fun onEnable() { - info("") + info("§a| §7Loading simple chunk loader.") ChunkLoaderManager + CommandOpenGUI + info("§a| §7Simple chunk loader loaded successfully.") } } \ No newline at end of file diff --git a/src/main/kotlin/cc/maxmc/chunkloader/command/CommandOpenGUI.kt b/src/main/kotlin/cc/maxmc/chunkloader/command/CommandOpenGUI.kt new file mode 100644 index 0000000..747a088 --- /dev/null +++ b/src/main/kotlin/cc/maxmc/chunkloader/command/CommandOpenGUI.kt @@ -0,0 +1,17 @@ +package cc.maxmc.chunkloader.command + +import cc.maxmc.chunkloader.gui.ChunkInfoGUI +import org.bukkit.entity.Player +import taboolib.common.platform.command.command + +object CommandOpenGUI { + init { + commandGUI() + } + + private fun commandGUI() = command("chunkloader") { + execute { sender, _, _ -> + ChunkInfoGUI(sender, sender.location.chunk).open() + } + } +} \ No newline at end of file diff --git a/src/main/kotlin/cc/maxmc/chunkloader/gui/ChunkInfoGUI.kt b/src/main/kotlin/cc/maxmc/chunkloader/gui/ChunkInfoGUI.kt index 99a964f..864e119 100644 --- a/src/main/kotlin/cc/maxmc/chunkloader/gui/ChunkInfoGUI.kt +++ b/src/main/kotlin/cc/maxmc/chunkloader/gui/ChunkInfoGUI.kt @@ -11,10 +11,15 @@ import taboolib.library.xseries.XMaterial import taboolib.module.ui.ClickType.* import taboolib.module.ui.buildMenu import taboolib.module.ui.type.Linked +import taboolib.platform.util.asLangText import taboolib.platform.util.buildItem +import taboolib.platform.util.sendLang class ChunkInfoGUI(private val player: Player, private val centerChunk: Chunk) { - val menu: Inventory = buildMenu>("§a| §7查看区块信息") { + private var menu: Inventory = buildInventory() + fun open() = player.openInventory(menu) + + private fun buildInventory() = buildMenu>(player.asLangText("chunk_gui_claim_title")) { for (slot in 9..53) { val chunkCoordinate = slot2ChunkCoordinate(slot) val chunk = centerChunk.world.getChunkAt(chunkCoordinate.first, chunkCoordinate.second) @@ -23,10 +28,12 @@ class ChunkInfoGUI(private val player: Player, private val centerChunk: Chunk) { assert(clickType == CLICK) if (clickEvent().click.isLeftClick) { ChunkLoaderManager.tryClaimChunk(player, chunk) + updateInventory() return@set } if (clickEvent().click.isRightClick) { - + ChunkLoaderManager.tryDisclaimChunk(player, chunk) + updateInventory() return@set } player.sendMessage("喵喵喵?") @@ -34,6 +41,11 @@ class ChunkInfoGUI(private val player: Player, private val centerChunk: Chunk) { } } + private fun updateInventory() { + menu = buildInventory() + player.openInventory(menu) + } + private fun chunkCoordinate2Slot(coordinate: Pair) = 31 + (coordinate.second - centerChunk.z) * 9 + (coordinate.first - centerChunk.x) diff --git a/src/main/kotlin/cc/maxmc/chunkloader/misc/ChunkLoaderManager.kt b/src/main/kotlin/cc/maxmc/chunkloader/misc/ChunkLoaderManager.kt index e6d2386..f29208e 100644 --- a/src/main/kotlin/cc/maxmc/chunkloader/misc/ChunkLoaderManager.kt +++ b/src/main/kotlin/cc/maxmc/chunkloader/misc/ChunkLoaderManager.kt @@ -31,6 +31,12 @@ object ChunkLoaderManager { config.saveToFile() } + private fun unregisterChunkLoader(chunkLoader: ChunkLoader) { + loaderMap.remove(chunkLoader.chunk) + config["loaders"] = loaderMap.values.toList() + config.saveToFile() + } + fun tryClaimChunk(player: Player, chunk: Chunk) { val type = chunk.type() if (type == ChunkType.SYSTEM) { @@ -61,12 +67,13 @@ object ChunkLoaderManager { if (chunk.type() != ChunkType.CLAIMED) return player.sendLang("chunk_disclaim_system") val loader = loaderMap[chunk]!! - if (loader.owner != player) { - if (player.isOp || player.hasPermission("chunk_loader.disclaim_force")) return player.sendLang("chunk_disclaim_others") + if (loader.owner != player && !player.isOp && !player.hasPermission("chunk_loader.disclaim_force")) { + return player.sendLang("chunk_disclaim_others") } + unregisterChunkLoader(loader) } - fun calculateRemainClaims(player: Player): Int { + private fun calculateRemainClaims(player: Player): Int { val maxPermCount = ClaimProviderManager.calculateTotalClaims(player) val used = calculateUsedClaims(player) @@ -75,7 +82,7 @@ object ChunkLoaderManager { return maxPermCount - used } - fun calculateUsedClaims(player: Player) = + private fun calculateUsedClaims(player: Player) = loaderMap.filter { (_, loader) -> loader.owner.uniqueId == player.uniqueId }.size fun Chunk.type(): ChunkType { diff --git a/src/main/resources/lang/zh_CN.yml b/src/main/resources/lang/zh_CN.yml index 15a0ca8..47b18ca 100644 --- a/src/main/resources/lang/zh_CN.yml +++ b/src/main/resources/lang/zh_CN.yml @@ -3,4 +3,5 @@ chunk_already_claimed: "§a| §7该区块已被玩家 §a{0} §7认领." chunk_claim_success: "§a| §7成功加载位于 §a{0} 的区块 (§6{1}/{2}§7)." chunk_claim_reach_max: "§e| §7您加载的区块达到上限." chunk_disclaim_system: "§c| §7该区块未被认领或为默认强加载区块,您无法取消加载." -chunk_disclaim_others: "§c| §7该区块为 §c{0} 所加载, 您无法取消加载" \ No newline at end of file +chunk_disclaim_others: "§c| §7该区块为 §c{0} 所加载, 您无法取消加载" +chunk_gui_claim_title: "§a| §7查看区块信息" \ No newline at end of file