summaryrefslogtreecommitdiff
path: root/src/main/kotlin/tech/thatgravyboat/rewardclaim/RewardClaim.kt
blob: 56566928935cd5dd5f01a2304a47ba1952c1cc31 (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
package tech.thatgravyboat.rewardclaim

import gg.essential.api.EssentialAPI
import net.minecraft.client.gui.GuiScreenBook
import net.minecraftforge.client.event.ClientChatReceivedEvent
import net.minecraftforge.client.event.GuiOpenEvent
import net.minecraftforge.common.MinecraftForge
import net.minecraftforge.fml.common.Mod
import net.minecraftforge.fml.common.event.FMLInitializationEvent
import net.minecraftforge.fml.common.event.FMLPreInitializationEvent
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent
import tech.thatgravyboat.rewardclaim.ui.RewardClaimGui

@Mod(
    name = "RewardClaim",
    modid = "gravyrewardclaim",
    version = "1.0.0",
    modLanguageAdapter = "gg.essential.api.utils.KotlinAdapter"
)
object ForgeTemplate {

    private var rewardClaimTime: Long = 0

    @Mod.EventHandler
    fun onFMLInitialization(event: FMLInitializationEvent?) {
        MinecraftForge.EVENT_BUS.register(this)
    }

    @Mod.EventHandler
    fun onPreInit(event: FMLPreInitializationEvent?) {
        RewardConfiguration.loadData()
    }

    @SubscribeEvent
    fun onChatMessage(event: ClientChatReceivedEvent) {
        RewardConfiguration.rewardMessageRegex.matchEntire(event.message.unformattedText.trim())?.apply {
            EssentialAPI.getGuiUtil().openScreen(RewardClaimGui(groups["id"]!!.value))
            rewardClaimTime = System.currentTimeMillis()
        }

        RewardConfiguration.rewardMissedMessageRegex.matchEntire(event.message.unformattedText.trim())?.apply {
            EssentialAPI.getNotifications().push(
                "Reward Claim Missed!",
                "You missed a reward claim, click on this to open the reward claim gui to claim your reward."
            ) { EssentialAPI.getGuiUtil().openScreen(RewardClaimGui(groups["id"]!!.value)) }
            event.isCanceled = true
        }
    }

    @SubscribeEvent
    fun onScreen(event: GuiOpenEvent) {
        if (EssentialAPI.getGuiUtil()
                .openedScreen() is RewardClaimGui && event.gui is GuiScreenBook && System.currentTimeMillis() - rewardClaimTime <= 3000
        ) {
            event.isCanceled = true
            rewardClaimTime = 0
        }
    }

}