Skip to content

Commit

Permalink
Reduce map lags
Browse files Browse the repository at this point in the history
  • Loading branch information
acrylic-style committed Nov 9, 2024
1 parent e71b577 commit 2e2698b
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 10 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ plugins {
}

group = "net.azisaba"
version = "6.18.0+1.15.2"
version = "6.18.1+1.15.2"

java {
toolchain.languageVersion.set(JavaLanguageVersion.of(8))
Expand Down
9 changes: 6 additions & 3 deletions src/main/java/com/github/mori01231/lifecore/LifeCore.kt
Original file line number Diff line number Diff line change
Expand Up @@ -200,9 +200,12 @@ class LifeCore : JavaPlugin() {
if (Bukkit.getOnlinePlayers().isEmpty()) return@Runnable
Bukkit.getWorlds().forEach world@ { world ->
world.getEntitiesByClass(ItemFrame::class.java).forEach { itemFrame ->
Bukkit.getOnlinePlayers().forEach { player ->
MapUtil.initializeMapRenderer(player, itemFrame.item)
}
Bukkit.getScheduler().runTaskAsynchronously(this, Runnable {
val item = itemFrame.item
Bukkit.getOnlinePlayers().forEach { player ->
MapUtil.initializeMapRenderer(player, item)
}
})
}
}
}, 200, 200)
Expand Down
25 changes: 19 additions & 6 deletions src/main/java/com/github/mori01231/lifecore/util/MapUtil.kt
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package com.github.mori01231.lifecore.util

import com.github.mori01231.lifecore.LifeCore
import com.github.mori01231.lifecore.map.SerializedMapCanvas
import com.github.mori01231.lifecore.map.SerializedMapDataRenderer
import kotlinx.serialization.encodeToString
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_15_R1.entity.CraftPlayer
import org.bukkit.craftbukkit.v1_15_R1.map.CraftMapCanvas
Expand All @@ -18,6 +20,7 @@ import org.bukkit.inventory.meta.MapMeta
import org.bukkit.map.MapCanvas
import org.bukkit.map.MapRenderer
import org.bukkit.map.MapView
import org.bukkit.plugin.java.JavaPlugin

object MapUtil {
private fun convertCanvasToSerializable(canvas: MapCanvas) =
Expand All @@ -42,13 +45,21 @@ object MapUtil {
fun MapView.getCanvases() =
CraftMapView::class.java.getDeclaredField("canvases").apply { isAccessible = true }[this] as Map<MapRenderer, Map<CraftPlayer, CraftMapCanvas>>

private val renderedMapViews = mutableListOf<Pair<Player, CraftMapView>>()

fun initializeMapRenderer(player: Player, item: ItemStack) {
if (item.type != Material.FILLED_MAP) return
val meta = item.itemMeta as? MapMeta? ?: return
val mapView = meta.mapView ?: return
val hasRenderer = mapView.renderers.isNotEmpty()
if (hasRenderer && mapView.renderers[0] !is CraftMapRenderer) {
if (mapView is CraftMapView) mapView.render(player as CraftPlayer)
if (mapView is CraftMapView) {
Bukkit.getScheduler().runTask(JavaPlugin.getPlugin(LifeCore::class.java), Runnable {
if (renderedMapViews.contains(player to mapView)) return@Runnable
mapView.render(player as CraftPlayer)
renderedMapViews.add(player to mapView)
})
}
return
}
val serializedMapData =
Expand All @@ -60,10 +71,12 @@ object MapUtil {
} catch (_: Exception) {
return
}
if (hasRenderer) {
mapView.removeRenderer(mapView.renderers[0])
}
mapView.addRenderer(SerializedMapDataRenderer(serializedMapData))
if (mapView is CraftMapView) mapView.render(player as CraftPlayer)
Bukkit.getScheduler().runTask(JavaPlugin.getPlugin(LifeCore::class.java), Runnable {
if (hasRenderer) {
mapView.removeRenderer(mapView.renderers[0])
}
mapView.addRenderer(SerializedMapDataRenderer(serializedMapData))
if (mapView is CraftMapView) mapView.render(player as CraftPlayer)
})
}
}

0 comments on commit 2e2698b

Please sign in to comment.