This commit is contained in:
TONY_All 2022-08-16 16:57:12 +08:00
parent 1c47e01048
commit 9b3ef2ea13
7 changed files with 37 additions and 18 deletions

View File

@ -2,7 +2,7 @@ plugins {
`java-library`
`maven-publish`
id("io.izzel.taboolib") version "1.42"
id("org.jetbrains.kotlin.jvm") version "1.5.10"
id("org.jetbrains.kotlin.jvm") version "1.7.10"
}
taboolib {

View File

@ -8,18 +8,18 @@ import org.bukkit.entity.Player
import org.bukkit.inventory.Inventory
import org.bukkit.inventory.ItemStack
import taboolib.library.xseries.XMaterial
import taboolib.module.ui.ClickType.*
import taboolib.module.ui.ClickType.CLICK
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) {
private var menu: Inventory = buildInventory()
fun open() = player.openInventory(menu)
private fun buildInventory() = buildMenu<Linked<Chunk>>(player.asLangText("chunk_gui_claim_title")) {
rows(6)
for (slot in 9..53) {
val chunkCoordinate = slot2ChunkCoordinate(slot)
val chunk = centerChunk.world.getChunkAt(chunkCoordinate.first, chunkCoordinate.second)
@ -51,7 +51,7 @@ class ChunkInfoGUI(private val player: Player, private val centerChunk: Chunk) {
private fun slot2ChunkCoordinate(slot: Int): Pair<Int, Int> {
val offset = slot - 31
if (offset == 0) return 3 to 3
if (offset == 0) return centerChunk.x to centerChunk.z
return centerChunk.x + (slot / 9) to centerChunk.z + (slot % 9)
}

View File

@ -6,17 +6,19 @@ import org.bukkit.Location
import org.bukkit.OfflinePlayer
import org.bukkit.configuration.serialization.ConfigurationSerializable
import org.bukkit.configuration.serialization.SerializableAs
import taboolib.module.configuration.ConfigSection
import taboolib.module.configuration.util.getLocation
import taboolib.platform.util.toBukkitLocation
import java.util.*
@SerializableAs("ChunkLoader")
data class ChunkLoader(val owner: OfflinePlayer, val chunk: Chunk) : ConfigurationSerializable {
data class ChunkLoader(val owner: OfflinePlayer, val chunk: Chunk) {
@Suppress("unused")
constructor(map: Map<String, Any>) : this(
Bukkit.getOfflinePlayer(UUID.fromString(map["owner"] as String)), (map["chunk"] as Location).chunk
constructor(map: ConfigSection) : this(
Bukkit.getOfflinePlayer(UUID.fromString(map["owner"] as String)), map.getLocation("chunk")!!.toBukkitLocation().chunk
)
override fun serialize(): MutableMap<String, Any> = hashMapOf(
fun serialize(): MutableMap<String, Any> = hashMapOf(
"owner" to owner.uniqueId, "chunk" to chunk.getBlock(0, 0, 0).location
)

View File

@ -3,14 +3,25 @@ package cc.maxmc.chunkloader.misc
import cc.maxmc.chunkloader.provider.ClaimProviderManager
import org.bukkit.Chunk
import org.bukkit.entity.Player
import taboolib.common.LifeCycle
import taboolib.common.platform.SkipTo
import taboolib.common.platform.function.getDataFolder
import taboolib.module.configuration.ConfigSection
import taboolib.module.configuration.Configuration
import taboolib.platform.util.sendLang
import java.io.File
@SkipTo(LifeCycle.ACTIVE)
object ChunkLoaderManager {
val loaderMap = HashMap<Chunk, ChunkLoader>()
private val config = Configuration.loadFromFile(File(getDataFolder(), "loaders.yaml"))
private val storage by lazy {
val loaderFile = File(getDataFolder(), "loaders.yaml")
if (!loaderFile.exists()) {
loaderFile.parentFile.mkdirs()
loaderFile.createNewFile()
}
Configuration.loadFromFile(loaderFile)
}
init {
loadChunkLoaders()
@ -18,8 +29,8 @@ object ChunkLoaderManager {
private fun loadChunkLoaders() {
@Suppress("UNCHECKED_CAST") val loaders =
config.getList("loaders", emptyList<ChunkLoader>()) as List<ChunkLoader>
loaders.forEach {
storage.getList("loaders", emptyList<ConfigSection>()) as List<ConfigSection>
loaders.map { ChunkLoader(it) }.forEach {
loaderMap[it.chunk] = it
it.enable()
}
@ -27,14 +38,18 @@ object ChunkLoaderManager {
private fun registerChunkLoader(chunkLoader: ChunkLoader) {
loaderMap[chunkLoader.chunk] = chunkLoader
config["loaders"] = loaderMap.values.toList()
config.saveToFile()
storage["loaders"] = loaderMap.values.toList().map {
it.serialize()
}
storage.saveToFile()
}
private fun unregisterChunkLoader(chunkLoader: ChunkLoader) {
loaderMap.remove(chunkLoader.chunk)
config["loaders"] = loaderMap.values.toList()
config.saveToFile()
storage["loaders"] = loaderMap.values.toList().map {
it.serialize()
}
storage.saveToFile()
}
fun tryClaimChunk(player: Player, chunk: Chunk) {
@ -70,6 +85,7 @@ object ChunkLoaderManager {
if (loader.owner != player && !player.isOp && !player.hasPermission("chunk_loader.disclaim_force")) {
return player.sendLang("chunk_disclaim_others")
}
loader.disable()
unregisterChunkLoader(loader)
}

View File

@ -4,4 +4,4 @@ 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} 所加载, 您无法取消加载"
chunk_gui_claim_title: "§a| §7查看区块信息"
chunk_gui_claim_title: "§a| §f查看区块信息"

View File

@ -0,0 +1 @@
loaders: