From 2e2698bf4d4e8ef2fd1b215dc03091ce613acec1 Mon Sep 17 00:00:00 2001 From: acrylic-style Date: Sat, 9 Nov 2024 17:47:43 +0900 Subject: [PATCH] Reduce map lags --- build.gradle.kts | 2 +- .../com/github/mori01231/lifecore/LifeCore.kt | 9 ++++--- .../github/mori01231/lifecore/util/MapUtil.kt | 25 ++++++++++++++----- 3 files changed, 26 insertions(+), 10 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index 80adf08..0045786 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -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)) diff --git a/src/main/java/com/github/mori01231/lifecore/LifeCore.kt b/src/main/java/com/github/mori01231/lifecore/LifeCore.kt index 79ffafa..969f324 100644 --- a/src/main/java/com/github/mori01231/lifecore/LifeCore.kt +++ b/src/main/java/com/github/mori01231/lifecore/LifeCore.kt @@ -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) 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 8ccb24b..0965f99 100644 --- a/src/main/java/com/github/mori01231/lifecore/util/MapUtil.kt +++ b/src/main/java/com/github/mori01231/lifecore/util/MapUtil.kt @@ -1,5 +1,6 @@ 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 @@ -7,6 +8,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_15_R1.entity.CraftPlayer import org.bukkit.craftbukkit.v1_15_R1.map.CraftMapCanvas @@ -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) = @@ -42,13 +45,21 @@ object MapUtil { fun MapView.getCanvases() = CraftMapView::class.java.getDeclaredField("canvases").apply { isAccessible = true }[this] as Map> + private val renderedMapViews = mutableListOf>() + 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 = @@ -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) + }) } }