aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin/dulkirmod/features/MemoryLeakFix.kt
blob: 5edde33ea913bbbbbfde3ae872951f0ceaec9d39 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package dulkirmod.features

import dulkirmod.DulkirMod.Companion.mc
import dulkirmod.config.DulkirConfig
import dulkirmod.utils.WorldRenderUtils
import net.minecraft.entity.Entity
import net.minecraft.entity.item.EntityArmorStand
import net.minecraftforge.client.event.RenderLivingEvent
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent
import net.minecraftforge.fml.common.gameevent.TickEvent
import java.awt.Color

object MemoryLeakFix {
	private var lastClear = System.currentTimeMillis()

	@SubscribeEvent
	fun onTick(event: TickEvent.ClientTickEvent) {
		if (!DulkirConfig.crimsonIslesMemoryLeakPatch) return

		if (System.currentTimeMillis() - lastClear >= 30000L) {
			val world = mc.theWorld ?: return
			val currentEnts = world.playerEntities.toMutableList()
			currentEnts.forEach {
				if (it.isDead) {
					world.playerEntities.remove(it)
				}
				if (isNullVec(it)) {
					world.removeEntityFromWorld(it.entityId)
				}
			}
			lastClear = System.currentTimeMillis()
		}
	}

	private fun isNullVec(entity: Entity): Boolean {
		return entity.posX == 0.0 && entity.posY == 0.0 && entity.posZ == 0.0
	}

	fun clearBlankStands() {
		if (!DulkirConfig.blankStandRemoval) return
		val world = mc.theWorld ?: return
		val currentEnts = world.loadedEntityList
		currentEnts.forEach {
			if (it !is EntityArmorStand) return@forEach
			if (it.hasCustomName()) return@forEach
			if (it.inventory.any{slot -> slot != null}) return@forEach
			if (it.ticksExisted < 1200) return@forEach
			world.removeEntityFromWorld(it.entityId)
		}
	}

	@SubscribeEvent
	fun displayBlankStands(event: RenderLivingEvent.Post<*>) {
		if (!DulkirConfig.debugStandRemoval) return
		if (event.entity !is EntityArmorStand) return
		if (event.entity.hasCustomName()) return
		if (event.entity.inventory.any{slot -> slot != null}) return
		val pos = WorldRenderUtils.fixRenderPos(event.x, event.y, event.z)
		val x = pos[0]
		val y = pos[1]
		val z = pos[2]

		WorldRenderUtils.drawCustomBox(
			x - .5,
			1.0,
			y - .5,
			1.0,
			z - .5,
			1.0,
			Color(15, 247, 236, 255),
			3f,
			phase = true
		)
	}
}