aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin/moe/nea/ledger/Ledger.kt
blob: 521fd9d3c6b56c3f1790fed08d469a204c4bbbaf (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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package moe.nea.ledger

import io.github.notenoughupdates.moulconfig.common.IMinecraft
import io.github.notenoughupdates.moulconfig.managed.ManagedConfig
import moe.nea.ledger.config.LedgerConfig
import moe.nea.ledger.database.Database
import moe.nea.ledger.events.ChatReceived
import moe.nea.ledger.events.LateWorldLoadEvent
import moe.nea.ledger.modules.AuctionHouseDetection
import moe.nea.ledger.modules.BankDetection
import moe.nea.ledger.modules.BazaarDetection
import moe.nea.ledger.modules.BazaarOrderDetection
import moe.nea.ledger.modules.BitsDetection
import moe.nea.ledger.modules.BitsShopDetection
import moe.nea.ledger.modules.DungeonChestDetection
import moe.nea.ledger.modules.MinionDetection
import moe.nea.ledger.modules.NpcDetection
import moe.nea.ledger.utils.DI
import net.minecraft.client.Minecraft
import net.minecraft.command.CommandBase
import net.minecraft.command.ICommandSender
import net.minecraftforge.client.ClientCommandHandler
import net.minecraftforge.client.event.ClientChatReceivedEvent
import net.minecraftforge.common.MinecraftForge
import net.minecraftforge.event.entity.EntityJoinWorldEvent
import net.minecraftforge.fml.common.Mod
import net.minecraftforge.fml.common.event.FMLInitializationEvent
import net.minecraftforge.fml.common.eventhandler.EventPriority
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent
import net.minecraftforge.fml.common.gameevent.TickEvent
import net.minecraftforge.fml.common.gameevent.TickEvent.ClientTickEvent
import org.apache.logging.log4j.LogManager
import java.io.File
import java.util.concurrent.ConcurrentLinkedQueue

@Mod(modid = "ledger", useMetadata = true)
class Ledger {
	/*
	You have withdrawn 1M coins! You now have 518M coins in your account!
	You have deposited 519M coins! You now have 519M coins in your account!

	// ORDERS:

	[Bazaar] Buy Order Setup! 160x Wheat for 720.0 coins.
	[Bazaar] Claimed 160x Wheat worth 720.0 coins bought for 4.5 each!

	[Bazaar] Sell Offer Setup! 160x Wheat for 933.4 coins.
	[Bazaar] Claimed 34,236,799 coins from selling 176x Hyper Catalyst at 196,741 each!

	// INSTABUY:

	[Bazaar] Bought 64x Wheat for 377.6 coins!
	[Bazaar] Sold 64x Wheat for 268.8 coins!

	// AUCTION HOUSE:

	You collected 8,712,000 coins from selling Ultimate Carrot Candy Upgrade to [VIP] kodokush in an auction!
	You purchased 2x Walnut for 69 coins!
	You purchased ◆ Ice Rune I for 4,000 coins!

	// NPC

	// You bought Cactus x32 for 465.6 Coins!
	// You sold Cactus x1 for 3 Coins!
	// You bought back Potato x3 for 9 Coins!

	TODO: TRADING, FORGE, COOKIE_EATEN
	*/
	companion object {
		val dataFolder = File("money-ledger").apply { mkdirs() }
		val logger = LogManager.getLogger("MoneyLedger")
		val managedConfig = ManagedConfig.create(File("config/money-ledger/config.json"), LedgerConfig::class.java) {
			checkExpose = false
		}
		private val tickQueue = ConcurrentLinkedQueue<Runnable>()
		fun runLater(runnable: Runnable) {
			tickQueue.add(runnable)
		}
	}

	@Mod.EventHandler
	fun init(event: FMLInitializationEvent) {
		logger.info("Initializing ledger")
		Database.init()

		ClientCommandHandler.instance.registerCommand(object : CommandBase() {
			override fun canCommandSenderUseCommand(sender: ICommandSender?): Boolean {
				return true
			}

			override fun getCommandName(): String {
				return "ledger"
			}

			override fun getCommandUsage(sender: ICommandSender?): String {
				return ""
			}

			override fun processCommand(sender: ICommandSender?, args: Array<out String>) {
				val editor = managedConfig.getEditor()
				editor.search(args.joinToString(" "))
				runLater {
					IMinecraft.instance.openWrappedScreen(editor)
				}
			}

			override fun getCommandAliases(): List<String> {
				return listOf("moneyledger")
			}
		})
		val di = DI()
		di.registerSingleton(this)
		di.registerInjectableClasses(
			LedgerLogger::class.java,
			ItemIdProvider::class.java,
			BankDetection::class.java,
			BazaarDetection::class.java,
			DungeonChestDetection::class.java,
			BazaarOrderDetection::class.java,
			AuctionHouseDetection::class.java,
			BitsDetection::class.java,
			BitsShopDetection::class.java,
			MinionDetection::class.java,
			NpcDetection::class.java,
		)
		di.instantiateAll()
		di.getAllInstances().forEach(MinecraftForge.EVENT_BUS::register)
	}

	var lastJoin = -1L

	@SubscribeEvent
	fun worldSwitchEvent(event: EntityJoinWorldEvent) {
		if (event.entity == Minecraft.getMinecraft().thePlayer) {
			lastJoin = System.currentTimeMillis()
		}
	}

	@SubscribeEvent
	fun tickEvent(event: ClientTickEvent) {
		if (event.phase == TickEvent.Phase.END
			&& lastJoin > 0
			&& System.currentTimeMillis() - lastJoin > 10_000
			&& Minecraft.getMinecraft().thePlayer != null
		) {
			lastJoin = -1
			MinecraftForge.EVENT_BUS.post(LateWorldLoadEvent())
		}
		while (true) {
			val queued = tickQueue.poll() ?: break
			queued.run()
		}
	}

	@SubscribeEvent(receiveCanceled = true, priority = EventPriority.HIGHEST)
	fun onChat(event: ClientChatReceivedEvent) {
		if (event.type != 2.toByte())
			MinecraftForge.EVENT_BUS.post(ChatReceived(event))
	}
}