aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin/features/items/recipes/RecipeRegistry.kt
blob: c2df46f79103adfb2774466b079113ef34ff9cfa (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
package moe.nea.firmament.features.items.recipes

import com.mojang.blaze3d.platform.InputConstants
import io.github.moulberry.repo.IReloadable
import io.github.moulberry.repo.NEURepository
import moe.nea.firmament.annotations.Subscribe
import moe.nea.firmament.events.HandledScreenKeyPressedEvent
import moe.nea.firmament.events.ReloadRegistrationEvent
import moe.nea.firmament.keybindings.SavedKeyBinding
import moe.nea.firmament.repo.RepoManager
import moe.nea.firmament.repo.SBItemStack
import moe.nea.firmament.repo.recipes.GenericRecipeRenderer
import moe.nea.firmament.repo.recipes.SBCraftingRecipeRenderer
import moe.nea.firmament.repo.recipes.SBEssenceUpgradeRecipeRenderer
import moe.nea.firmament.repo.recipes.SBForgeRecipeRenderer
import moe.nea.firmament.repo.recipes.SBReforgeRecipeRenderer
import moe.nea.firmament.util.MC
import moe.nea.firmament.util.SkyblockId
import moe.nea.firmament.util.focusedItemStack

object RecipeRegistry {
	val recipeTypes: List<GenericRecipeRenderer<*>> = listOf(
		SBCraftingRecipeRenderer,
		SBForgeRecipeRenderer,
		SBReforgeRecipeRenderer,
		SBEssenceUpgradeRecipeRenderer,
	)


	@Subscribe
	fun showUsages(event: HandledScreenKeyPressedEvent) {
		val provider =
			if (event.matches(SavedKeyBinding.keyWithoutMods(InputConstants.KEY_R))) {
				::getRecipesFor
			} else if (event.matches(SavedKeyBinding.keyWithoutMods(InputConstants.KEY_U))) {
				::getUsagesFor
			} else {
				return
			}
		val stack = event.screen.focusedItemStack ?: return
		val recipes = provider(SBItemStack(stack))
		if (recipes.isEmpty()) return
		MC.screen = RecipeScreen(recipes.toList())
	}


	object RecipeIndexes : IReloadable {

		private fun <T : Any> createIndexFor(
			neuRepository: NEURepository,
			recipeRenderer: GenericRecipeRenderer<T>,
			outputs: Boolean,
		): List<Pair<SkyblockId, RenderableRecipe<T>>> {
			val indexer: (T) -> Collection<SBItemStack> =
				if (outputs) recipeRenderer::getOutputs
				else recipeRenderer::getInputs
			return recipeRenderer.findAllRecipes(neuRepository)
				.flatMap {
					val wrappedRecipe = RenderableRecipe(it, recipeRenderer, null)
					indexer(it).map { it.skyblockId to wrappedRecipe }
				}
		}

		fun createIndex(outputs: Boolean): MutableMap<SkyblockId, List<RenderableRecipe<*>>> {
			val m: MutableMap<SkyblockId, List<RenderableRecipe<*>>> = mutableMapOf()
			recipeTypes.forEach { renderer ->
				createIndexFor(RepoManager.neuRepo, renderer, outputs)
					.forEach { (stack, recipe) ->
						m.merge(stack, listOf(recipe)) { a, b -> a + b }
					}
			}
			return m
		}

		lateinit var recipesForIndex: Map<SkyblockId, List<RenderableRecipe<*>>>
		lateinit var usagesForIndex: Map<SkyblockId, List<RenderableRecipe<*>>>
		override fun reload(recipe: NEURepository) {
			recipesForIndex = createIndex(true)
			usagesForIndex = createIndex(false)
		}
	}

	@Subscribe
	fun onRepoBuild(event: ReloadRegistrationEvent) {
		event.repo.registerReloadListener(RecipeIndexes)
	}


	fun getRecipesFor(itemStack: SBItemStack): Set<RenderableRecipe<*>> {
		val recipes = LinkedHashSet<RenderableRecipe<*>>()
		recipeTypes.forEach { injectRecipesFor(it, recipes, itemStack, true) }
		recipes.addAll(RecipeIndexes.recipesForIndex[itemStack.skyblockId] ?: emptyList())
		return recipes
	}

	fun getUsagesFor(itemStack: SBItemStack): Set<RenderableRecipe<*>> {
		val recipes = LinkedHashSet<RenderableRecipe<*>>()
		recipeTypes.forEach { injectRecipesFor(it, recipes, itemStack, false) }
		recipes.addAll(RecipeIndexes.usagesForIndex[itemStack.skyblockId] ?: emptyList())
		return recipes
	}

	private fun <T : Any> injectRecipesFor(
		recipeRenderer: GenericRecipeRenderer<T>,
		collector: MutableCollection<RenderableRecipe<*>>,
		relevantItem: SBItemStack,
		mustBeInOutputs: Boolean
	) {
		collector.addAll(
			recipeRenderer.discoverExtraRecipes(RepoManager.neuRepo, relevantItem, mustBeInOutputs)
				.map { RenderableRecipe(it, recipeRenderer, relevantItem) }
		)
	}


}