aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin/repo/MiningRepoData.kt
blob: e40292d64be4ffbcdc53977956e58292ad553520 (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
package moe.nea.firmament.repo

import io.github.moulberry.repo.IReloadable
import io.github.moulberry.repo.NEURepository
import io.github.moulberry.repo.data.NEUItem
import java.util.Collections
import java.util.NavigableMap
import java.util.TreeMap
import kotlinx.serialization.Serializable
import kotlinx.serialization.Transient
import kotlinx.serialization.serializer
import kotlin.jvm.optionals.getOrNull
import kotlin.streams.asSequence
import net.minecraft.block.Block
import net.minecraft.item.BlockItem
import net.minecraft.item.ItemStack
import net.minecraft.nbt.NbtCompound
import net.minecraft.text.Text
import moe.nea.firmament.repo.ReforgeStore.kJson
import moe.nea.firmament.util.MC
import moe.nea.firmament.util.SBData
import moe.nea.firmament.util.SkyBlockIsland
import moe.nea.firmament.util.SkyblockId
import moe.nea.firmament.util.mc.FirmamentDataComponentTypes
import moe.nea.firmament.util.mc.displayNameAccordingToNbt
import moe.nea.firmament.util.skyblockId

class MiningRepoData : IReloadable {
	var customMiningAreas: Map<SkyBlockIsland, CustomMiningArea> = mapOf()
		private set
	var customMiningBlocks: List<CustomMiningBlock> = listOf()
		private set
	var toolsByBreakingPower: NavigableMap<BreakingPowerKey, SBItemStack> = Collections.emptyNavigableMap()
		private set


	data class BreakingPowerKey(
		val breakingPower: Int,
		val itemId: SkyblockId? = null
	) {
		companion object {
			val COMPARATOR: Comparator<BreakingPowerKey> =
				Comparator
					.comparingInt<BreakingPowerKey> { it.breakingPower }
					.thenComparing(Comparator.comparing(
						{ it.itemId },
						nullsFirst(Comparator.comparing<SkyblockId, Boolean> { "PICK" in it.neuItem || "BING" in it.neuItem }.thenComparing(Comparator.naturalOrder<SkyblockId>()))))
		}
	}

	override fun reload(repo: NEURepository) {
		customMiningAreas = repo.file("mining/custom_mining_areas.json")
			?.kJson(serializer()) ?: mapOf()
		customMiningBlocks = repo.tree("mining/blocks")
			.asSequence()
			.filter { it.path.endsWith(".json") }
			.map { it.kJson(serializer<CustomMiningBlock>()) }
			.toList()
		toolsByBreakingPower = Collections.unmodifiableNavigableMap(
			repo.items.items
				.values
				.asSequence()
				.map { SBItemStack(it.skyblockId) }
				.filter { it.breakingPower > 0 }
				.associateTo(TreeMap<BreakingPowerKey, SBItemStack>(BreakingPowerKey.COMPARATOR)) {
					BreakingPowerKey(it.breakingPower, it.skyblockId) to it
				})
	}

	fun getToolsThatCanBreak(breakingPower: Int): Collection<SBItemStack> {
		return toolsByBreakingPower.tailMap(BreakingPowerKey(breakingPower, null), true).values
	}

	@Serializable
	data class CustomMiningBlock(
		val breakingPower: Int = 0,
		val blockStrength: Int = 0,
		val name: String? = null,
		val baseDrop: SkyblockId? = null,
		val blocks189: List<Block189> = emptyList()
	) {
		@Transient
		val dropItem = baseDrop?.let(::SBItemStack)
		private val labeledStack by lazy {
			dropItem?.asCopiedItemStack()?.also(::markItemStack)
		}

		private fun markItemStack(itemStack: ItemStack) {
			itemStack.set(FirmamentDataComponentTypes.CUSTOM_MINING_BLOCK_DATA, this)
			if (name != null)
				itemStack.displayNameAccordingToNbt = Text.literal(name)
		}

		fun getDisplayItem(block: Block): ItemStack {
			return labeledStack ?: ItemStack(block).also(::markItemStack)
		}
	}

	@Serializable
	data class Block189(
		val itemId: String,
		val damage: Short = 0,
		val onlyIn: List<SkyBlockIsland>? = null,
	) {
		@Transient
		val block = convertToModernBlock()

		val isCurrentlyActive: Boolean
			get() = isActiveIn(SBData.skyblockLocation ?: SkyBlockIsland.NIL)

		fun isActiveIn(location: SkyBlockIsland) = onlyIn == null || location in onlyIn

		private fun convertToModernBlock(): Block? {
			// TODO: this should be in a shared util, really
			val newCompound = ItemCache.convert189ToModern(NbtCompound().apply {
				putString("id", itemId)
				putShort("Damage", damage)
			}) ?: return null
			val itemStack = ItemStack.fromNbt(MC.defaultRegistries, newCompound).getOrNull() ?: return null
			val blockItem = itemStack.item as? BlockItem ?: return null
			return blockItem.block
		}
	}

	@Serializable
	data class CustomMiningArea(
		val isSpecialMining: Boolean = true
	)


}