Skip to content

Commit 69e7aa6

Browse files
committed
random stuff idk
1 parent a98c41a commit 69e7aa6

File tree

7 files changed

+171
-45
lines changed

7 files changed

+171
-45
lines changed

src/main/kotlin/com/github/subat0m1c/hatecheaters/HateCheaters.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import com.github.subat0m1c.hatecheaters.commands.CommandRegistry
44
import com.github.subat0m1c.hatecheaters.commands.impl.PVCommand
55
import com.github.subat0m1c.hatecheaters.modules.dungeons.BetterPartyFinder
66
import com.github.subat0m1c.hatecheaters.modules.dungeons.ClearSecrets
7+
import com.github.subat0m1c.hatecheaters.modules.dungeons.MobLogger
78
import com.github.subat0m1c.hatecheaters.modules.render.ProfileViewer
89
import com.github.subat0m1c.hatecheaters.modules.render.ProfileViewer.pvCommand
910
import com.github.subat0m1c.hatecheaters.modules.skyblock.HateCheatersModule
@@ -50,7 +51,7 @@ class HateCheaters {
5051
@Mod.EventHandler
5152
fun load(event: FMLLoadCompleteEvent) {
5253
ModuleManager.addModules(
53-
BetterPartyFinder, HateCheatersModule, ProfileViewer, ClearSecrets
54+
BetterPartyFinder, HateCheatersModule, ProfileViewer, ClearSecrets, MobLogger
5455
)
5556
}
5657

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
package com.github.subat0m1c.hatecheaters.modules.dungeons
2+
3+
import com.github.subat0m1c.hatecheaters.HateCheaters.Companion.launch
4+
import com.github.subat0m1c.hatecheaters.utils.LogHandler
5+
import kotlinx.serialization.Serializable
6+
import kotlinx.serialization.encodeToString
7+
import kotlinx.serialization.json.Json
8+
import me.odinmain.events.impl.RoomEnterEvent
9+
import me.odinmain.features.Module
10+
import me.odinmain.utils.skyblock.dungeon.DungeonUtils.getRelativeCoords
11+
import net.minecraft.entity.EntityLivingBase
12+
import net.minecraft.entity.item.EntityArmorStand
13+
import net.minecraft.network.play.server.S0CPacketSpawnPlayer
14+
import net.minecraft.network.play.server.S0FPacketSpawnMob
15+
import net.minecraft.util.BlockPos
16+
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent
17+
import java.io.File
18+
import java.text.SimpleDateFormat
19+
import java.util.*
20+
21+
object MobLogger : Module(
22+
"Mob Logger",
23+
description = "Logs dungeon mob spawns to a file.",
24+
) {
25+
val saved = mutableMapOf<String, HashMap<Int, ArrayList<MobData>>>()
26+
var instance = 0
27+
28+
val toCheck = hashSetOf<EntityLivingBase>()
29+
30+
private lateinit var file: File
31+
private fun newFile() {
32+
val date = SimpleDateFormat("yyyy-MM-dd_HH-mm-ss").format(Date())
33+
val dir = File("config/hatecheaters/mod_logs/dungeon_mobs")
34+
if (!dir.exists()) dir.mkdirs()
35+
file = File(dir, "dungeon_mobs_$date.json")
36+
file.createNewFile()
37+
}
38+
39+
init {
40+
newFile()
41+
}
42+
43+
@SubscribeEvent
44+
fun onRoomEnter(event: RoomEnterEvent) {
45+
toCheck.removeAll { entity ->
46+
val armorStand =
47+
mc.theWorld.getEntityByID(entity.entityId + 1) as? EntityArmorStand ?: return@removeAll true
48+
event.room?.getRelativeCoords(entity.position)
49+
event.room?.let { room ->
50+
room.roomComponents.forEach {
51+
val x = it.x - 16.0
52+
val endX = it.x + 16.0
53+
val y = it.z - 16.0
54+
val endY = it.x + 16.0
55+
56+
if (entity.posX in x..endX && entity.posZ in y..endY) {
57+
saved.getOrPut(room.data.name) { HashMap() }
58+
.getOrPut(instance) { ArrayList() }
59+
.add(MobData(armorStand.name, V3(BlockPos(entity.position))))
60+
return@removeAll true
61+
}
62+
}
63+
}
64+
false
65+
}
66+
}
67+
68+
init {
69+
onPacket<S0FPacketSpawnMob> {
70+
val entity = mc.theWorld.getEntityByID(it.entityID) as? EntityLivingBase ?: return@onPacket
71+
toCheck.add(entity)
72+
}
73+
onPacket<S0CPacketSpawnPlayer> {
74+
val entity = mc.theWorld.getEntityByID(it.entityID) as? EntityLivingBase ?: return@onPacket
75+
toCheck.add(entity)
76+
}
77+
onWorldLoad {
78+
if (saved.isNotEmpty()) {
79+
launch {
80+
try {
81+
file.bufferedWriter().use {
82+
it.write(json.encodeToString(saved))
83+
}
84+
} catch (e: Exception) {
85+
LogHandler.Logger.warning("Failed to save dungeon mob log: ${e.message}")
86+
} finally {
87+
saved.clear()
88+
newFile()
89+
}
90+
}
91+
}
92+
toCheck.clear()
93+
instance++
94+
}
95+
}
96+
97+
@Serializable
98+
data class MobData(
99+
val name: String,
100+
val coords: V3
101+
)
102+
103+
/// just to ensure its kotlinx serialization compatible
104+
@Serializable
105+
data class V3(
106+
val x: Double,
107+
val y: Double,
108+
val z: Double
109+
) {
110+
constructor(pos: BlockPos) : this(pos.x.toDouble() + 0.5, pos.y.toDouble(), pos.z.toDouble() + 0.5)
111+
}
112+
113+
val json = Json { prettyPrint = true }
114+
}

src/main/kotlin/com/github/subat0m1c/hatecheaters/pvgui/v2/utils/Utils.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,5 +65,6 @@ object Utils {
6565
drawEntityOnScreen(0, 0, scale, ((x*2)-mouseX).toFloat(), (mouseY-y).toFloat(), renderPlayer)
6666
GlStateManager.popMatrix()
6767
Shaders.startDraw()
68+
GlStateManager.popMatrix() // pop the matrix made by start draw probably?
6869
}
6970
}

src/main/kotlin/com/github/subat0m1c/hatecheaters/utils/LogHandler.kt

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,31 @@
11
package com.github.subat0m1c.hatecheaters.utils
22

33
import com.github.subat0m1c.hatecheaters.HateCheaters.Companion.launch
4-
import com.github.subat0m1c.hatecheaters.modules.skyblock.HateCheatersModule
54
import com.github.subat0m1c.hatecheaters.utils.ChatUtils.debug
6-
import com.github.subat0m1c.hatecheaters.utils.ChatUtils.getCurrentDateTimeString
75
import java.io.File
86
import java.io.FileInputStream
97
import java.io.FileOutputStream
108
import java.text.SimpleDateFormat
119
import java.util.*
1210
import java.util.logging.FileHandler
13-
import java.util.logging.Logger as javaLogger
1411
import java.util.logging.SimpleFormatter
1512
import java.util.zip.GZIPOutputStream
13+
import java.util.logging.Logger as javaLogger
1614

1715
/**
1816
* im pretty sure this is bad but i could not figure out making log4j actually log to the right folder
1917
*/
2018
object LogHandler {
19+
class Unique(name: String) {
20+
val log: javaLogger = javaLogger.getLogger(name)
21+
22+
fun log(message: Any?) = log.info(message.toString())
23+
24+
init {
25+
setupLogger(log, "mobs")
26+
}
27+
}
28+
2129
object Logger {
2230
val log: javaLogger = javaLogger.getLogger("HateCheatersLogger")
2331

@@ -36,7 +44,7 @@ object LogHandler {
3644

3745
init {
3846
compressOldLogs()
39-
setupLogger()
47+
setupLogger(Logger.log, "hc")
4048
}
4149

4250
private fun compressOldLogs() = launch {
@@ -64,13 +72,16 @@ object LogHandler {
6472
}
6573
}.onFailure { it.printStackTrace() }
6674

67-
private fun setupLogger() = runCatching {
75+
private fun setupLogger(logger: javaLogger, name: String) = runCatching {
6876
val logDir = File("config/hatecheaters/mod_logs")
6977
if (!logDir.exists()) logDir.mkdirs()
7078

71-
val fileHandler = FileHandler("${logDir.absolutePath}/${SimpleDateFormat("yyyy-MM-dd_HH-mm-ss").format(Date())}.log", true)
79+
val fileHandler = FileHandler(
80+
"${logDir.absolutePath}/${name}_${SimpleDateFormat("yyyy-MM-dd_HH-mm-ss").format(Date())}.log",
81+
true
82+
)
7283
fileHandler.formatter = SimpleFormatter()
73-
Logger.log.addHandler(fileHandler)
84+
logger.addHandler(fileHandler)
7485
Logger.log.useParentHandlers = false
7586
}.onFailure { it.printStackTrace() }
7687

src/main/kotlin/com/github/subat0m1c/hatecheaters/utils/OdinCheck.kt

Lines changed: 25 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,11 @@ object OdinCheck {
3030

3131
try {
3232
val currentOdin = Loader.instance().activeModList.find { it.modId == "od" || it.modId == "odclient" }?.version ?: ""
33-
if (compareVersions(currentOdin, "@REQUIREDODINVERSION@") == -1) odinWarning("Odin is outdated!", currentOdin)
33+
if (compareVersions(
34+
currentOdin,
35+
"@REQUIREDODINVERSION@"
36+
) == Comparison.OLDER
37+
) odinWarning("Odin is outdated!", currentOdin)
3438
} catch (e: Throwable) {
3539
odinWarning("An unknown error occurred trying to determine Odin version!")
3640
return
@@ -115,40 +119,33 @@ object OdinCheck {
115119

116120
private val versionRegex = Regex("(\\d+)\\.(\\d+)\\.(\\d+)(?:\\.(\\d+))?(?:\\.beta(\\d+))?")
117121

118-
/**
119-
* UPDATE: it now generally compares versions between the 2 args, might be good idk
120-
*
121-
* I think this code is bad but i cant be bothered fixing it. also can return lower than -1.
122-
*
123-
* @return -1 if version1 is older, 1 if version1 is newer, 0 if they're the same.
124-
*/
125-
fun compareVersions(version1: String, version2: String): Int {
126-
val v1 = versionRegex.find(version1)?.groupValues?.drop(1)?.map { it.toIntOrNull() } ?: return -2
127-
val v2 = versionRegex.find(version2)?.groupValues?.drop(1)?.map { it.toIntOrNull() } ?: return -2
128-
122+
fun compareVersions(current: String, compare: String): Comparison? {
123+
val cur = versionRegex.find(current)?.groupValues?.drop(1)?.map { it.toIntOrNull() } ?: return null
124+
val cmp = versionRegex.find(compare)?.groupValues?.drop(1)?.map { it.toIntOrNull() } ?: return null
129125

130126
for (i in 0..2) {
131-
val compared = compareNumbers(v1[i], v2[i])
132-
if (compared != 0) return compared
127+
val compared = compareNumbers(cur[i], cmp[i])
128+
if (compared != Comparison.EQUAL) return compared
133129
}
134130

135-
val comparedFourth = compareNumbers(v1.getOrNull(3), v2.getOrNull(3))
136-
if (comparedFourth != 0) return comparedFourth
131+
val comparedFourth = compareNumbers(cur.getOrNull(3), cmp.getOrNull(3))
132+
if (comparedFourth != Comparison.EQUAL) return comparedFourth
137133

138-
val v1Beta = v1.getOrNull(4)
139-
val v2Beta = v2.getOrNull(4)
140-
141-
if (v1Beta != null && v2Beta != null) return compareNumbers(v1Beta, v2Beta)
142-
else if (v1Beta != null) return -1
143-
else if (v2Beta != null) return 1
134+
return compareNumbers(cur.getOrNull(4), cmp.getOrNull(4))
135+
}
144136

145-
return 0
137+
private fun compareNumbers(current: Int?, compare: Int?): Comparison = when {
138+
current == null && compare == null -> Comparison.EQUAL
139+
current == null -> Comparison.OLDER
140+
compare == null -> Comparison.NEWER
141+
current > compare -> Comparison.NEWER
142+
current < compare -> Comparison.OLDER
143+
else -> Comparison.EQUAL
146144
}
147145

148-
private fun compareNumbers(n1: Int?, n2: Int?): Int {
149-
if (n1 == null && n2 == null) return 0
150-
if (n1 == null) return -1
151-
if (n2 == null) return 1
152-
return n1.compareTo(n2)
146+
enum class Comparison {
147+
EQUAL,
148+
OLDER,
149+
NEWER
153150
}
154151
}

src/main/kotlin/com/github/subat0m1c/hatecheaters/utils/networkutils/CheckUpdate.kt

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
11
package com.github.subat0m1c.hatecheaters.utils.networkutils
22

3-
import com.github.subat0m1c.hatecheaters.utils.LogHandler.Logger
4-
import me.odinmain.utils.skyblock.getChatBreak
5-
import com.github.subat0m1c.hatecheaters.utils.OdinCheck.compareVersions
63
import com.github.subat0m1c.hatecheaters.HateCheaters
74
import com.github.subat0m1c.hatecheaters.HateCheaters.Companion.launch
85
import com.github.subat0m1c.hatecheaters.utils.ChatUtils
96
import com.github.subat0m1c.hatecheaters.utils.ChatUtils.modMessage
7+
import com.github.subat0m1c.hatecheaters.utils.LogHandler.Logger
8+
import com.github.subat0m1c.hatecheaters.utils.OdinCheck
9+
import com.github.subat0m1c.hatecheaters.utils.OdinCheck.compareVersions
1010
import com.github.subat0m1c.hatecheaters.utils.networkutils.WebUtils.streamAndRead
11-
import net.minecraft.event.ClickEvent
12-
import kotlinx.serialization.*
13-
import kotlinx.serialization.json.*
11+
import kotlinx.serialization.SerialName
12+
import kotlinx.serialization.Serializable
13+
import kotlinx.serialization.json.Json
1414
import me.odinmain.utils.runIn
15+
import me.odinmain.utils.skyblock.getChatBreak
16+
import net.minecraft.event.ClickEvent
1517

1618
object CheckUpdate {
1719
private val json = Json {
@@ -32,12 +34,12 @@ object CheckUpdate {
3234

3335
val isNewVersionAvailable = compareVersions(latestVersion, currentVersion)
3436
when {
35-
isNewVersionAvailable < 0 -> {
37+
isNewVersionAvailable == OdinCheck.Comparison.OLDER -> {
3638
Logger.info("HC > Development Build Detected!")
3739
modMessage("You are on a development build! Hi!")
3840
}
3941

40-
isNewVersionAvailable > 0 -> {
42+
isNewVersionAvailable == OdinCheck.Comparison.NEWER -> {
4143
ChatUtils.chatConstructor {
4244
displayText(getChatBreak())
4345
displayText("§bHate§3Cheaters§r Update available! ($currentVersion$latestVersion)")

src/main/kotlin/com/github/subat0m1c/hatecheaters/utils/networkutils/WebUtils.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ object WebUtils {
2828
fun getFromCache(name: String): String? = cache[name.lowercase()]
2929
}
3030

31-
private const val USER_AGENT = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36"
31+
private const val USER_AGENT = "HateCheatersAPI"
3232

3333
suspend inline fun <reified T> streamAndRead(url: String, json: Json = HateCheaters.json): Result<T> = runCatching {
3434
return getInputStream(url).map {

0 commit comments

Comments
 (0)