aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin/moe/nea/firmament/rei/SkyblockCraftingRecipeDynamicGenerator.kt
blob: 51369027e0e08fd217b62c39cf893f72c5168ed5 (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
package moe.nea.firmament.rei

import io.github.moulberry.repo.data.NEUCraftingRecipe
import io.github.moulberry.repo.data.NEUForgeRecipe
import io.github.moulberry.repo.data.NEUKatUpgradeRecipe
import io.github.moulberry.repo.data.NEUMobDropRecipe
import io.github.moulberry.repo.data.NEURecipe
import java.util.Optional
import me.shedaniel.rei.api.client.registry.display.DynamicDisplayGenerator
import me.shedaniel.rei.api.client.view.ViewSearchBuilder
import me.shedaniel.rei.api.common.display.Display
import me.shedaniel.rei.api.common.entry.EntryStack
import moe.nea.firmament.rei.recipes.SBCraftingRecipe
import moe.nea.firmament.rei.recipes.SBEssenceUpgradeRecipe
import moe.nea.firmament.rei.recipes.SBForgeRecipe
import moe.nea.firmament.rei.recipes.SBKatRecipe
import moe.nea.firmament.rei.recipes.SBMobDropRecipe
import moe.nea.firmament.repo.EssenceRecipeProvider
import moe.nea.firmament.repo.RepoManager


val SkyblockCraftingRecipeDynamicGenerator =
    neuDisplayGenerator<SBCraftingRecipe, NEUCraftingRecipe> { SBCraftingRecipe(it) }

val SkyblockForgeRecipeDynamicGenerator =
    neuDisplayGenerator<SBForgeRecipe, NEUForgeRecipe> { SBForgeRecipe(it) }

val SkyblockMobDropRecipeDynamicGenerator =
    neuDisplayGenerator<SBMobDropRecipe, NEUMobDropRecipe> { SBMobDropRecipe(it) }

val SkyblockKatRecipeDynamicGenerator =
    neuDisplayGenerator<SBKatRecipe, NEUKatUpgradeRecipe> { SBKatRecipe(it) }
val SkyblockEssenceRecipeDynamicGenerator =
    neuDisplayGenerator<SBEssenceUpgradeRecipe, EssenceRecipeProvider.EssenceUpgradeRecipe> { SBEssenceUpgradeRecipe(it) }

inline fun <D : Display, reified T : NEURecipe> neuDisplayGenerator(crossinline mapper: (T) -> D) =
    object : DynamicDisplayGenerator<D> {
        override fun getRecipeFor(entry: EntryStack<*>): Optional<List<D>> {
            if (entry.type != SBItemEntryDefinition.type) return Optional.empty()
            val item = entry.castValue<SBItemStack>()
            val recipes = RepoManager.getRecipesFor(item.skyblockId)
            val craftingRecipes = recipes.filterIsInstance<T>()
            return Optional.of(craftingRecipes.map(mapper))
        }

        override fun generate(builder: ViewSearchBuilder): Optional<List<D>> {
            if (SBCraftingRecipe.Category.catIdentifier !in builder.categories) return Optional.empty()
            return Optional.of(
                RepoManager.getAllRecipes().filterIsInstance<T>().map { mapper(it) }
                    .toList()
            )
        }

        override fun getUsageFor(entry: EntryStack<*>): Optional<List<D>> {
            if (entry.type != SBItemEntryDefinition.type) return Optional.empty()
            val item = entry.castValue<SBItemStack>()
            val recipes = RepoManager.getUsagesFor(item.skyblockId)
            val craftingRecipes = recipes.filterIsInstance<T>()
            return Optional.of(craftingRecipes.map(mapper))

        }
    }