Skip to content

Commit

Permalink
fix: map
Browse files Browse the repository at this point in the history
  • Loading branch information
acrylic-style committed Aug 21, 2024
1 parent 9866f9a commit 86efe14
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 4 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/com/github/mori01231/lifecore/LifeCore.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand All @@ -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)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,10 @@ class LifeCoreUtilCommand(val plugin: LifeCore) : TabExecutor {
},
LoadMapData("地図を読み込みます") {
override fun execute(plugin: LifeCore, player: CommandSender, args: Array<String>) {
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タグを修正します") {
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/com/github/mori01231/lifecore/util/MapUtil.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -42,6 +43,17 @@ object MapUtil {
fun MapView.getCanvases() =
CraftMapView::class.java.getDeclaredField("canvases").apply { isAccessible = true }[this] as Map<MapRenderer, Map<CraftPlayer, CraftMapCanvas>>

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
Expand Down

0 comments on commit 86efe14

Please sign in to comment.