From 5fa74568316c76cac6e84d2ff9d4ecdbfafddc6d Mon Sep 17 00:00:00 2001 From: acrylic-style Date: Sat, 9 Nov 2024 20:58:37 +0900 Subject: [PATCH 1/2] fix potential memory leak --- build.gradle.kts | 2 +- .../com/github/mori01231/lifecore/LifeCore.kt | 1 + .../lifecore/listener/PlayerQuitListener.kt | 18 ++++++++++++++++++ .../github/mori01231/lifecore/util/MapUtil.kt | 7 ++++--- 4 files changed, 24 insertions(+), 4 deletions(-) create mode 100644 src/main/java/com/github/mori01231/lifecore/listener/PlayerQuitListener.kt diff --git a/build.gradle.kts b/build.gradle.kts index 39b77d1..729580f 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -9,7 +9,7 @@ plugins { } group = "net.azisaba" -version = "6.18.2+1.15.2" +version = "6.18.3+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 969f324..a2d92b6 100644 --- a/src/main/java/com/github/mori01231/lifecore/LifeCore.kt +++ b/src/main/java/com/github/mori01231/lifecore/LifeCore.kt @@ -332,6 +332,7 @@ class LifeCore : JavaPlugin() { pm.registerEvents(PicksawItemListener(dataLoader), this) pm.registerEvents(BlockListener, this) pm.registerEvents(MapListScreen.EventListener, this) + pm.registerEvents(PlayerQuitListener, this) // Items pm.registerEvents(OreOnlyItemListener(), this) diff --git a/src/main/java/com/github/mori01231/lifecore/listener/PlayerQuitListener.kt b/src/main/java/com/github/mori01231/lifecore/listener/PlayerQuitListener.kt new file mode 100644 index 0000000..6ff2c81 --- /dev/null +++ b/src/main/java/com/github/mori01231/lifecore/listener/PlayerQuitListener.kt @@ -0,0 +1,18 @@ +package com.github.mori01231.lifecore.listener + +import com.github.mori01231.lifecore.LifeCore +import com.github.mori01231.lifecore.util.MapUtil +import org.bukkit.Bukkit +import org.bukkit.event.EventHandler +import org.bukkit.event.Listener +import org.bukkit.event.player.PlayerQuitEvent +import org.bukkit.plugin.java.JavaPlugin + +object PlayerQuitListener : Listener { + @EventHandler + fun onPlayerQuit(e: PlayerQuitEvent) { + Bukkit.getScheduler().runTaskAsynchronously(JavaPlugin.getPlugin(LifeCore::class.java), Runnable { + MapUtil.renderedMapViews.removeIf { it.first == e.player.uniqueId } + }) + } +} 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 1ac8825..661b328 100644 --- a/src/main/java/com/github/mori01231/lifecore/util/MapUtil.kt +++ b/src/main/java/com/github/mori01231/lifecore/util/MapUtil.kt @@ -22,6 +22,7 @@ import org.bukkit.map.MapRenderer import org.bukkit.map.MapView import org.bukkit.plugin.java.JavaPlugin import java.util.Collections +import java.util.UUID object MapUtil { private fun convertCanvasToSerializable(canvas: MapCanvas) = @@ -46,7 +47,7 @@ object MapUtil { fun MapView.getCanvases() = CraftMapView::class.java.getDeclaredField("canvases").apply { isAccessible = true }[this] as Map> - private val renderedMapViews = Collections.synchronizedList(mutableListOf>()) + val renderedMapViews = Collections.synchronizedList(mutableListOf>()) fun initializeMapRenderer(player: Player, item: ItemStack) { if (item.type != Material.FILLED_MAP) return @@ -55,8 +56,8 @@ object MapUtil { val hasRenderer = mapView.renderers.isNotEmpty() if (hasRenderer && mapView.renderers[0] !is CraftMapRenderer) { if (mapView is CraftMapView) { - if (renderedMapViews.contains(player to mapView)) return - renderedMapViews.add(player to mapView) + if (renderedMapViews.contains(player.uniqueId to mapView.id)) return + renderedMapViews.add(player.uniqueId to mapView.id) Bukkit.getScheduler().runTask(JavaPlugin.getPlugin(LifeCore::class.java), Runnable { mapView.render(player as CraftPlayer) }) From 46a61b2bcec13174b02f0d201867f1bd3924cab7 Mon Sep 17 00:00:00 2001 From: acrylic-style Date: Sat, 9 Nov 2024 23:58:42 +0900 Subject: [PATCH 2/2] fix PromptSign map not removed on sign --- build.gradle.kts | 2 +- .../github/mori01231/lifecore/listener/PromptSignListener.kt | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index 729580f..a23f432 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -9,7 +9,7 @@ plugins { } group = "net.azisaba" -version = "6.18.3+1.15.2" +version = "6.18.4+1.15.2" java { toolchain.languageVersion.set(JavaLanguageVersion.of(8)) diff --git a/src/main/java/com/github/mori01231/lifecore/listener/PromptSignListener.kt b/src/main/java/com/github/mori01231/lifecore/listener/PromptSignListener.kt index fdc5cad..d64ee1a 100644 --- a/src/main/java/com/github/mori01231/lifecore/listener/PromptSignListener.kt +++ b/src/main/java/com/github/mori01231/lifecore/listener/PromptSignListener.kt @@ -9,6 +9,6 @@ import org.bukkit.event.Listener object PromptSignListener : Listener { @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) fun onSign(e: AsyncPreSignChangeEvent) { - PromptSign.awaitingSign[e.player.uniqueId]?.invoke(e.lines.toList()) + PromptSign.awaitingSign.remove(e.player.uniqueId)?.invoke(e.lines.toList()) } }