aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin/gg/skytils/skytilsmod/features/impl/misc/ScamCheck.kt
blob: c083311b17a1213b595dd313babc49ab4b243909 (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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/*
 * Skytils - Hypixel Skyblock Quality of Life Mod
 * Copyright (C) 2022 Skytils
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published
 * by the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

package gg.skytils.skytilsmod.features.impl.misc

import gg.essential.universal.UChat
import gg.essential.universal.wrappers.message.UMessage
import gg.skytils.skytilsmod.Skytils
import gg.skytils.skytilsmod.Skytils.Companion.client
import gg.skytils.skytilsmod.Skytils.Companion.mc
import gg.skytils.skytilsmod.core.TickTask
import gg.skytils.skytilsmod.events.impl.MainReceivePacketEvent
import gg.skytils.skytilsmod.utils.ItemUtil
import gg.skytils.skytilsmod.utils.Utils
import io.ktor.client.call.*
import io.ktor.client.request.*
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import kotlinx.serialization.Serializable
import net.minecraft.item.ItemStack
import net.minecraft.network.play.server.S2DPacketOpenWindow
import net.minecraft.network.play.server.S2FPacketSetSlot
import net.minecraft.network.play.server.S30PacketWindowItems
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent
import java.util.*

object ScamCheck {
    // it caps out at 10 characters for otherParty
    private val tradingRegex = Regex("You {18}(?<otherParty>\\w{1,16})")
    private val tradingWithRegex = Regex("§7Trading with.*? (?<otherParty>\\w{1,16})§f§7\\.")
    private var tradingWindowId = -1
    private var scamChecked = false

    @SubscribeEvent
    fun onPacket(event: MainReceivePacketEvent<*, *>) {
        if (!Utils.inSkyblock || !Skytils.config.scamCheck) return
        when (val packet = event.packet) {
            is S2DPacketOpenWindow -> {
                if (tradingRegex.matches(packet.windowTitle.unformattedText)) {
                    tradingWindowId = packet.windowId
                    scamChecked = false
                }
            }

            is S2FPacketSetSlot -> {
                if (!scamChecked && packet.func_149175_c() == tradingWindowId && packet.func_149173_d() == 41 && packet.func_149174_e() != null) {
                    checkScam(packet.func_149174_e())
                    scamChecked = true
                }
            }

            is S30PacketWindowItems -> {
                if (!scamChecked && packet.func_148911_c() == tradingWindowId && packet.itemStacks.size == 45) {
                    val tradingWith = packet.itemStacks[41] ?: return
                    checkScam(tradingWith)
                    scamChecked = true
                }
            }
        }
    }

    private fun checkScam(tradingWith: ItemStack) {
        val firstLore = ItemUtil.getItemLore(tradingWith).find { it.matches(tradingWithRegex) } ?: return
        val otherParty = firstLore.replace(tradingWithRegex, "$1")
        val worldUUID = mc.theWorld?.playerEntities?.find {
            it.uniqueID.version() == 4 && it.name == otherParty
        }?.uniqueID
        Skytils.IO.launch {
            val uuid = worldUUID ?: runCatching { Skytils.hylinAPI.getUUIDSync(otherParty) }.getOrNull()
            ?: return@launch UChat.chat("${Skytils.failPrefix} §cUnable to get the UUID for ${otherParty}! Could they be nicked?")
            val result = checkScammer(uuid, "tradewindow")
            if (result.isScammer) {
                TickTask(1) {
                    mc.thePlayer?.closeScreen()
                }
            }
            result.printResult(otherParty)
        }.invokeOnCompletion {
            if (it != null) UChat.chat("${Skytils.failPrefix} §cSomething went wrong while checking the scammer status for ${otherParty}!")
        }
    }


    suspend fun checkScammer(uuid: UUID, source: String = "unknown") = withContext(Skytils.IO.coroutineContext) {
        client.get("https://${Skytils.domain}/api/scams/check?uuid=$uuid&utm_source=${source}")
            .body<ScamCheckResponse>()
    }
}

@Serializable
data class ScamCheckResponse(
    val isScammer: Boolean,
    val reasons: Map<String, String>,
    val isAlt: Boolean
) {
    fun printResult(username: String) {
        if (!isScammer) {
            UChat.chat("${Skytils.prefix} §a$username is not a known scammer!")
        } else {
            UMessage("${Skytils.prefix} §c$username is a known scammer!\n",
                "§bDatabases:\n",
                reasons.entries.joinToString("\n") { (db, reason) ->
                    "§b${db}: $reason"
                }
            ).chat()
        }
    }
}