From 86efe1443bd5735605aeaa86e8c7fc550457203a Mon Sep 17 00:00:00 2001 From: acrylic-style Date: Wed, 21 Aug 2024 15:42:56 +0900 Subject: [PATCH] fix: map --- build.gradle.kts | 2 +- .../java/com/github/mori01231/lifecore/LifeCore.kt | 5 +++-- .../lifecore/command/LifeCoreUtilCommand.kt | 5 ++++- .../com/github/mori01231/lifecore/util/MapUtil.kt | 12 ++++++++++++ 4 files changed, 20 insertions(+), 4 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index d51425f..f7d2503 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -11,7 +11,7 @@ plugins { } group = "net.azisaba" -version = "1.20.2+6.16.8" +version = "1.20.2+6.16.8a" java { toolchain.languageVersion.set(JavaLanguageVersion.of(17)) diff --git a/src/main/java/com/github/mori01231/lifecore/LifeCore.kt b/src/main/java/com/github/mori01231/lifecore/LifeCore.kt index c4dc503..b6f66b8 100644 --- a/src/main/java/com/github/mori01231/lifecore/LifeCore.kt +++ b/src/main/java/com/github/mori01231/lifecore/LifeCore.kt @@ -187,9 +187,9 @@ class LifeCore : JavaPlugin() { // check for maps Bukkit.getScheduler().runTaskTimer(this, Runnable { Bukkit.getOnlinePlayers().forEach player@ { player -> - player.inventory.contents.forEach { item -> - @Suppress("SENSELESS_COMPARISON") + player.inventory.contents.forEachIndexed { index, item -> if (item != null) { + MapUtil.checkMapView(item)?.let { player.inventory.setItem(index, it) } MapUtil.initializeMapRenderer(player, item) } } @@ -198,6 +198,7 @@ class LifeCore : JavaPlugin() { Bukkit.getWorlds().forEach world@ { world -> world.getEntitiesByClass(ItemFrame::class.java).forEach { itemFrame -> Bukkit.getOnlinePlayers().forEach { player -> + MapUtil.checkMapView(itemFrame.item)?.let { itemFrame.setItem(it, false) } MapUtil.initializeMapRenderer(player, itemFrame.item) } } diff --git a/src/main/java/com/github/mori01231/lifecore/command/LifeCoreUtilCommand.kt b/src/main/java/com/github/mori01231/lifecore/command/LifeCoreUtilCommand.kt index 5a83e4f..2ab0da6 100644 --- a/src/main/java/com/github/mori01231/lifecore/command/LifeCoreUtilCommand.kt +++ b/src/main/java/com/github/mori01231/lifecore/command/LifeCoreUtilCommand.kt @@ -437,7 +437,10 @@ class LifeCoreUtilCommand(val plugin: LifeCore) : TabExecutor { }, LoadMapData("地図を読み込みます") { override fun execute(plugin: LifeCore, player: CommandSender, args: Array) { - MapUtil.initializeMapRenderer((player as Player), player.inventory.itemInMainHand) + MapUtil.checkMapView((player as Player).inventory.itemInMainHand)?.let { + player.inventory.setItemInMainHand(it) + } + MapUtil.initializeMapRenderer(player, player.inventory.itemInMainHand) } }, FixItem("displayタグを修正します") { diff --git a/src/main/java/com/github/mori01231/lifecore/util/MapUtil.kt b/src/main/java/com/github/mori01231/lifecore/util/MapUtil.kt index 5ebdf0c..044d0f5 100644 --- a/src/main/java/com/github/mori01231/lifecore/util/MapUtil.kt +++ b/src/main/java/com/github/mori01231/lifecore/util/MapUtil.kt @@ -7,6 +7,7 @@ import kotlinx.serialization.json.Json import kotlinx.serialization.json.JsonElement import kotlinx.serialization.json.decodeFromJsonElement import kotlinx.serialization.json.encodeToJsonElement +import org.bukkit.Bukkit import org.bukkit.Material import org.bukkit.craftbukkit.v1_20_R2.entity.CraftPlayer import org.bukkit.craftbukkit.v1_20_R2.map.CraftMapCanvas @@ -42,6 +43,17 @@ object MapUtil { fun MapView.getCanvases() = CraftMapView::class.java.getDeclaredField("canvases").apply { isAccessible = true }[this] as Map> + fun checkMapView(item: ItemStack): ItemStack? { + if (item.type != Material.FILLED_MAP) return null + val meta = item.itemMeta as? MapMeta ?: return null + if (meta.mapView == null) { + meta.mapView = Bukkit.createMap(Bukkit.getWorlds()[0]) + item.itemMeta = meta + return item + } + return null + } + fun initializeMapRenderer(player: Player, item: ItemStack) { if (item.type != Material.FILLED_MAP) return val meta = item.itemMeta as? MapMeta? ?: return