Skip to content

Commit

Permalink
fix potential memory leak
Browse files Browse the repository at this point in the history
  • Loading branch information
acrylic-style committed Nov 9, 2024
1 parent 6e3e052 commit 5fa7456
Show file tree
Hide file tree
Showing 4 changed files with 24 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 @@ -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))
Expand Down
1 change: 1 addition & 0 deletions src/main/java/com/github/mori01231/lifecore/LifeCore.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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 }
})
}
}
7 changes: 4 additions & 3 deletions src/main/java/com/github/mori01231/lifecore/util/MapUtil.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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) =
Expand All @@ -46,7 +47,7 @@ object MapUtil {
fun MapView.getCanvases() =
CraftMapView::class.java.getDeclaredField("canvases").apply { isAccessible = true }[this] as Map<MapRenderer, Map<CraftPlayer, CraftMapCanvas>>

private val renderedMapViews = Collections.synchronizedList(mutableListOf<Pair<Player, CraftMapView>>())
val renderedMapViews = Collections.synchronizedList(mutableListOf<Pair<UUID, Int>>())

fun initializeMapRenderer(player: Player, item: ItemStack) {
if (item.type != Material.FILLED_MAP) return
Expand All @@ -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)
})
Expand Down

0 comments on commit 5fa7456

Please sign in to comment.