aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin/repo/recipes/SBReforgeRecipeRenderer.kt
blob: c841dd995291fcd630b197f8cf18b347b8931bf0 (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
161
162
163
164
165
166
167
package moe.nea.firmament.repo.recipes

import io.github.moulberry.repo.NEURepository
import me.shedaniel.math.Point
import me.shedaniel.math.Rectangle
import net.minecraft.network.chat.Component
import net.minecraft.resources.ResourceLocation
import net.minecraft.world.entity.EntitySpawnReason
import net.minecraft.world.entity.EntityType
import net.minecraft.world.entity.npc.VillagerProfession
import net.minecraft.world.item.ItemStack
import moe.nea.firmament.Firmament
import moe.nea.firmament.gui.entity.EntityRenderer
import moe.nea.firmament.repo.ExpensiveItemCacheApi
import moe.nea.firmament.repo.Reforge
import moe.nea.firmament.repo.ReforgeStore
import moe.nea.firmament.repo.RepoItemTypeCache
import moe.nea.firmament.repo.SBItemStack
import moe.nea.firmament.util.FirmFormatters.formatCommas
import moe.nea.firmament.util.MC
import moe.nea.firmament.util.gold
import moe.nea.firmament.util.grey
import moe.nea.firmament.util.skyblock.Rarity
import moe.nea.firmament.util.skyblock.SkyBlockItems
import moe.nea.firmament.util.skyblockId
import moe.nea.firmament.util.tr

object SBReforgeRecipeRenderer : GenericRecipeRenderer<Reforge> {
	@OptIn(ExpensiveItemCacheApi::class)
	override fun render(
		recipe: Reforge,
		bounds: Rectangle,
		layouter: RecipeLayouter,
		mainItem: SBItemStack?
	) {
		val inputSlot = layouter.createCyclingItemSlot(
			bounds.minX + 10, bounds.centerY - 9,
			if (mainItem != null) listOf(mainItem)
			else generateAllItems(recipe),
			RecipeLayouter.SlotKind.SMALL_INPUT
		)
		val outputSlut = layouter.createItemSlot(
			bounds.minX + 10 + 24 + 24, bounds.centerY - 9,
			null,
			RecipeLayouter.SlotKind.SMALL_OUTPUT
		)
		val statLines = mutableListOf<Pair<String, RecipeLayouter.Updater<Component>>>()
		for ((i, statId) in recipe.statUniverse.withIndex()) {
			val label = layouter.createLabel(
				bounds.minX + 10 + 24 + 24 + 20, bounds.minY + 8 + i * 11,
				Component.empty()
			)
			statLines.add(statId to label)
		}

		fun updateOutput() {
			val currentBaseItem = inputSlot.current()
			outputSlut.update(currentBaseItem.copy(reforge = recipe.reforgeId))
			val stats = recipe.reforgeStats?.get(currentBaseItem.rarity) ?: mapOf()
			for ((stat, label) in statLines) {
				label.update(
					SBItemStack.Companion.StatLine(
						SBItemStack.statIdToName(stat), null,
						valueNum = stats[stat]
					).reconstitute(7)
				)
			}
		}

		if (recipe.reforgeStone != null) {
			layouter.createItemSlot(
				bounds.minX + 10 + 24, bounds.centerY - 9 - 10,
				SBItemStack(recipe.reforgeStone),
				RecipeLayouter.SlotKind.SMALL_INPUT
			)
			val d = Rectangle(
				bounds.minX + 10 + 24, bounds.centerY - 9 + 10,
				16, 16
			)
			layouter.createItemSlot(
				d.x, d.y,
				SBItemStack(SkyBlockItems.REFORGE_ANVIL),
				RecipeLayouter.SlotKind.DISPLAY
			)
			layouter.createTooltip(
				d,
				Rarity.entries.mapNotNull { rarity ->
					recipe.reforgeCosts?.get(rarity)?.let { rarity to it }
				}.map { (rarity, cost) ->
					Component.literal("")
						.append(rarity.text)
						.append(": ")
						.append(Component.literal("${formatCommas(cost, 0)} Coins").gold())
				}
			)
		} else {
			val entity = EntityType.VILLAGER.create(EntityRenderer.fakeWorld, EntitySpawnReason.COMMAND)
				?.also {
					it.villagerData =
						it.villagerData.withProfession(
							MC.currentOrDefaultRegistries,
							VillagerProfession.WEAPONSMITH
						)
				}
			val dim = EntityRenderer.defaultSize
			val d = Rectangle(
				Point(bounds.minX + 10 + 24 + 8 - dim.width / 2, bounds.centerY - dim.height / 2),
				dim
			)
			if (entity != null)
				layouter.createEntity(
					d,
					entity
				)
			layouter.createTooltip(
				d,
				tr(
					"firmament.recipecategory.reforge.basic",
					"This is a basic reforge, available at the Blacksmith."
				).grey()
			)
		}
	}

	private fun generateAllItems(recipe: Reforge): List<SBItemStack> {
		return recipe.eligibleItems.flatMap {
			when (it) {
				is Reforge.ReforgeEligibilityFilter.AllowsInternalName -> listOf(SBItemStack(it.internalName))
				is Reforge.ReforgeEligibilityFilter.AllowsItemType ->
					ReforgeStore.resolveItemType(it.itemType)
						.flatMapTo(mutableSetOf()) { itemType ->
							listOf(itemType, itemType.dungeonVariant)
						}
						.flatMapTo(mutableSetOf()) { itemType ->
							RepoItemTypeCache.byItemType[itemType] ?: listOf()
						}
						.map { SBItemStack(it.skyblockId) }

				is Reforge.ReforgeEligibilityFilter.AllowsVanillaItemType -> listOf()
			}
		}
	}

	override fun getInputs(recipe: Reforge): Collection<SBItemStack> {
		val reforgeStone = recipe.reforgeStone ?: return emptyList()
		return listOf(SBItemStack(reforgeStone))
	}

	override fun getOutputs(recipe: Reforge): Collection<SBItemStack> {
		return listOf()
	}

	@OptIn(ExpensiveItemCacheApi::class)
	override val icon: ItemStack
		get() = SBItemStack(SkyBlockItems.REFORGE_ANVIL).asImmutableItemStack()
	override val title: Component
		get() = tr("firmament.recipecategory.reforge", "Reforge")
	override val identifier: ResourceLocation
		get() = Firmament.identifier("reforge_recipe")

	override fun findAllRecipes(neuRepository: NEURepository): Iterable<Reforge> {
		return ReforgeStore.allReforges
	}

	override val typ: Class<Reforge>
		get() = Reforge::class.java
}