aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin/moe/nea/ledger/ItemChange.kt
blob: 834cd2b28f27b5754be5ea1334b1a5e75c262420 (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
package moe.nea.ledger

import moe.nea.ledger.database.DBItemEntry
import moe.nea.ledger.database.ResultRow
import net.minecraft.event.HoverEvent
import net.minecraft.util.ChatComponentText
import net.minecraft.util.ChatStyle
import net.minecraft.util.EnumChatFormatting
import net.minecraft.util.IChatComponent

data class ItemChange(
	val itemId: ItemId,
	val count: Double,
	val direction: ChangeDirection,
) {
	fun formatChat(): IChatComponent {
		return ChatComponentText(" ")
			.appendSibling(direction.chatFormat)
			.appendText(" ")
			.appendSibling(ChatComponentText("$count").setChatStyle(ChatStyle().setColor(EnumChatFormatting.WHITE)))
			.appendSibling(ChatComponentText("x").setChatStyle(ChatStyle().setColor(EnumChatFormatting.DARK_GRAY)))
			.appendText(" ")
			.appendSibling(ChatComponentText(itemId.string).setChatStyle(ChatStyle().setParentStyle(ChatStyle().setColor(
				EnumChatFormatting.WHITE))))
	}

	enum class ChangeDirection {
		GAINED,
		TRANSFORM,
		SYNC,
		CATALYST,
		LOST;

		val chatFormat by lazy { formatChat0() }
		private fun formatChat0(): IChatComponent {
			val (text, color) = when (this) {
				GAINED -> "+" to EnumChatFormatting.GREEN
				TRANSFORM -> "~" to EnumChatFormatting.YELLOW
				SYNC -> "=" to EnumChatFormatting.BLUE
				CATALYST -> "*" to EnumChatFormatting.DARK_PURPLE
				LOST -> "-" to EnumChatFormatting.RED
			}
			return ChatComponentText(text)
				.setChatStyle(
					ChatStyle()
						.setColor(color)
						.setChatHoverEvent(HoverEvent(HoverEvent.Action.SHOW_TEXT,
						                              ChatComponentText(name).setChatStyle(ChatStyle().setColor(color)))))
		}
	}

	companion object {
		fun gainCoins(number: Double): ItemChange {
			return gain(ItemId.COINS, number)
		}

		fun gain(itemId: ItemId, amount: Number): ItemChange {
			return ItemChange(itemId, amount.toDouble(), ChangeDirection.GAINED)
		}

		fun lose(itemId: ItemId, amount: Number): ItemChange {
			return ItemChange(itemId, amount.toDouble(), ChangeDirection.LOST)
		}

		fun loseCoins(number: Double): ItemChange {
			return lose(ItemId.COINS, number)
		}

		fun from(result: ResultRow): ItemChange {
			return ItemChange(
				result[DBItemEntry.itemId],
				result[DBItemEntry.size],
				result[DBItemEntry.mode],
			)
		}
	}
}