1+ package org.valkyrienskies.clockwork.compat.jei
2+
3+ import com.simibubi.create.AllBlocks
4+ import com.simibubi.create.AllFluids
5+ import com.simibubi.create.compat.jei.CreateJEI.consumeTypedRecipes
6+ import com.simibubi.create.compat.jei.DoubleItemIcon
7+ import com.simibubi.create.compat.jei.EmptyBackground
8+ import com.simibubi.create.compat.jei.ItemIcon
9+ import com.simibubi.create.compat.jei.ToolboxColoringRecipeMaker
10+ import com.simibubi.create.compat.jei.category.CreateRecipeCategory
11+ import com.simibubi.create.foundation.recipe.IRecipeTypeInfo
12+ import com.simibubi.create.infrastructure.config.AllConfigs
13+ import com.simibubi.create.infrastructure.config.CRecipes
14+ import mezz.jei.api.IModPlugin
15+ import mezz.jei.api.JeiPlugin
16+ import mezz.jei.api.constants.RecipeTypes
17+ import mezz.jei.api.gui.drawable.IDrawable
18+ import mezz.jei.api.recipe.RecipeType
19+ import mezz.jei.api.registration.IRecipeCatalystRegistration
20+ import mezz.jei.api.registration.IRecipeCategoryRegistration
21+ import mezz.jei.api.registration.IRecipeRegistration
22+ import mezz.jei.api.runtime.IIngredientManager
23+ import mezz.jei.api.runtime.IJeiRuntime
24+ import net.createmod.catnip.config.ConfigBase
25+ import net.minecraft.client.Minecraft
26+ import net.minecraft.resources.ResourceLocation
27+ import net.minecraft.world.item.ItemStack
28+ import net.minecraft.world.item.crafting.CraftingRecipe
29+ import net.minecraft.world.item.crafting.Recipe
30+ import net.minecraft.world.level.ItemLike
31+ import org.valkyrienskies.clockwork.ClockworkBlocks
32+ import org.valkyrienskies.clockwork.ClockworkLang
33+ import org.valkyrienskies.clockwork.ClockworkMod
34+ import org.valkyrienskies.clockwork.ClockworkRecipes
35+ import org.valkyrienskies.clockwork.content.logistics.gas.crafter.GasCraftingRecipe
36+ import java.util.List
37+ import java.util.function.Consumer
38+ import java.util.function.Function
39+ import java.util.function.Predicate
40+ import java.util.function.Supplier
41+ import javax.annotation.Nonnull
42+
43+ @JeiPlugin
44+ @Suppress(" unused" , " unchecked_cast" )
45+ class ClockworkJEI () : IModPlugin {
46+ private var ingredientManager: IIngredientManager ? = null
47+ private val ID : ResourceLocation = ClockworkMod .asResource(" jei_plugin" )
48+ var runtime: IJeiRuntime ? = null
49+
50+ private fun loadCategories () {
51+ allCategories.clear()
52+
53+ builder(GasCraftingRecipe ::class .java)
54+ .addTypedRecipes(ClockworkRecipes .ClockworkRecipeTypes .GAS_CRAFTING )
55+ .catalyst { ClockworkBlocks .GAS_CRAFTER .get() }
56+ .catalyst { AllBlocks .BASIN .get() }
57+ .itemIcon(ClockworkBlocks .GAS_CRAFTER .get())
58+ .emptyBackground(177 , 103 )
59+ .build(
60+ " gas_crafting" ,
61+ CreateRecipeCategory .Factory { info: CreateRecipeCategory .Info <GasCraftingRecipe > ->
62+ GasCrafterCategory (info)
63+ })
64+ }
65+
66+
67+ private fun <T : Recipe <* >? > builder (recipeClass : Class <out T ?>):CategoryBuilder <T ?> {
68+ return CategoryBuilder <T ?>(recipeClass)
69+ }
70+
71+ @Nonnull
72+ override fun getPluginUid (): ResourceLocation {
73+ return ID
74+ }
75+
76+ override fun registerCategories (registration : IRecipeCategoryRegistration ) {
77+ loadCategories()
78+ registration.addRecipeCategories(* allCategories.toTypedArray())
79+ }
80+
81+ override fun registerRecipeCatalysts (registration : IRecipeCatalystRegistration ) {
82+ allCategories.forEach(Consumer { c: CreateRecipeCategory <* >? -> c!! .registerCatalysts(registration) })
83+ }
84+
85+ override fun registerRecipes (registration : IRecipeRegistration ) {
86+ allCategories.forEach(Consumer { c: CreateRecipeCategory <* >? -> c!! .registerRecipes(registration) })
87+
88+ }
89+
90+
91+ class CategoryBuilder <T : Recipe <* >? >(private val recipeClass : Class <out T ?>) {
92+ private var predicate = Predicate { cRecipes: CRecipes ? -> true }
93+
94+ private var background: IDrawable ? = null
95+ private var icon: IDrawable ? = null
96+
97+ private val recipeListConsumers: MutableList <Consumer <MutableList <T ?>? >> =
98+ ArrayList <Consumer <MutableList <T ?>? >> ()
99+ private val catalysts: MutableList <Supplier <out ItemStack ?>? > = ArrayList <Supplier <out ItemStack ?>? > ()
100+
101+ fun enableIf (predicate : Predicate <CRecipes ?>): CategoryBuilder <T ?> {
102+ this .predicate = predicate
103+ return this as CategoryBuilder <T ?>
104+ }
105+
106+ fun enableWhen (configValue : Function <CRecipes ?, ConfigBase .ConfigBool ?>): CategoryBuilder <T ?> {
107+ predicate = Predicate { c: CRecipes ? -> configValue.apply (c)!! .get() }
108+ return this as CategoryBuilder <T ?>
109+ }
110+
111+ fun addRecipeListConsumer (consumer : Consumer <MutableList <T ?>? >): CategoryBuilder <T ?> {
112+ recipeListConsumers.add(consumer)
113+ return this as CategoryBuilder <T ?>
114+ }
115+
116+ fun addRecipes (collection : Supplier <MutableCollection <out T ?>? >): CategoryBuilder <T ?> {
117+ return addRecipeListConsumer(Consumer { recipes: MutableList <T ?>? -> recipes!! .addAll(collection.get()!! ) })
118+ }
119+
120+ fun addAllRecipesIf (pred : Predicate <Recipe <* >? >): CategoryBuilder <T ?> {
121+ return addRecipeListConsumer(Consumer { recipes: MutableList <T ?>? ->
122+ consumeAllRecipes(
123+ Consumer { recipe: Recipe <* >? ->
124+ if (pred.test(recipe)) recipes!! .add(recipe as T ? )
125+ })
126+ })
127+ }
128+
129+ fun addAllRecipesIf (
130+ pred : Predicate <Recipe <* >? >,
131+ converter : Function <Recipe <* >? , T ? >
132+ ): CategoryBuilder <T ?> {
133+ return addRecipeListConsumer(Consumer { recipes: MutableList <T ?>? ->
134+ consumeAllRecipes(
135+ Consumer { recipe: Recipe <* >? ->
136+ if (pred.test(recipe)) {
137+ recipes!! .add(converter.apply (recipe))
138+ }
139+ })
140+ })
141+ }
142+
143+ fun addTypedRecipes (recipeTypeEntry : IRecipeTypeInfo ): CategoryBuilder <T ?> {
144+ return addTypedRecipes(Supplier { recipeTypeEntry.getType() })
145+ }
146+
147+ fun addTypedRecipes (recipeType : Supplier <net.minecraft.world.item.crafting.RecipeType <out T ?>? >): CategoryBuilder <T ?> {
148+ return addRecipeListConsumer({ recipes: MutableList <T ?>? ->
149+ consumeTypedRecipes(
150+ { e: T ? -> recipes!! .add(e) },
151+ recipeType.get()
152+ )
153+ })
154+ }
155+
156+ fun addTypedRecipes (
157+ recipeType : Supplier <net.minecraft.world.item.crafting.RecipeType <out T ?>? >,
158+ converter : Function <Recipe <* >? , T ? >
159+ ): CategoryBuilder <T ?> {
160+ return addRecipeListConsumer(Consumer { recipes: MutableList <T ?>? ->
161+ consumeTypedRecipes(
162+ { recipe: T ? -> recipes!! .add(converter.apply (recipe)) },
163+ recipeType.get()
164+ )
165+ })
166+ }
167+
168+
169+
170+ fun catalystStack (supplier : Supplier <ItemStack ?>): CategoryBuilder <T ?> {
171+ catalysts.add(supplier)
172+ return this as CategoryBuilder <T ?>
173+ }
174+
175+ fun catalyst (supplier : Supplier <ItemLike ?>): CategoryBuilder <T ?> {
176+ return catalystStack(Supplier {
177+ ItemStack (
178+ supplier.get()!!
179+ .asItem()
180+ )
181+ })
182+ }
183+
184+ fun icon (icon : IDrawable ): CategoryBuilder <T ?> {
185+ this .icon = icon
186+ return this as CategoryBuilder <T ?>
187+ }
188+
189+ fun itemIcon (item : ItemLike ): CategoryBuilder <T ?> {
190+ icon(ItemIcon (Supplier { ItemStack (item) }))
191+ return this as CategoryBuilder <T ?>
192+ }
193+
194+ fun doubleItemIcon (item1 : ItemLike , item2 : ItemLike ): CategoryBuilder <T ?> {
195+ icon(DoubleItemIcon ({ ItemStack (item1) }, { ItemStack (item2) }))
196+ return this as CategoryBuilder <T ?>
197+ }
198+
199+ fun background (background : IDrawable ): CategoryBuilder <T ?> {
200+ this .background = background
201+ return this as CategoryBuilder <T ?>
202+ }
203+
204+ fun emptyBackground (width : Int , height : Int ): CategoryBuilder <T ?> {
205+ background(EmptyBackground (width, height))
206+ return this as CategoryBuilder <T ?>
207+ }
208+
209+ fun build (name : String , factory : CreateRecipeCategory .Factory <T ?>): CreateRecipeCategory <T ?> {
210+ val recipesSupplier: Supplier <MutableList <T ?>? > ?
211+ if (predicate.test(AllConfigs .server().recipes)) {
212+ recipesSupplier = Supplier {
213+ val recipes: MutableList <T ?> = ArrayList <T ?>()
214+ for (consumer in recipeListConsumers) consumer.accept(recipes)
215+ recipes
216+ }
217+ } else {
218+ recipesSupplier = Supplier { mutableListOf<T ?>() }
219+ }
220+
221+ val info = CreateRecipeCategory .Info <T ?>(
222+ RecipeType <T ?>(ClockworkMod .asResource(name), recipeClass),
223+ ClockworkLang .translateDirect(" recipe." + name), background, icon, recipesSupplier, catalysts
224+ )
225+ val category = factory.create(info)
226+ allCategories.add(category)
227+ return category
228+ }
229+ }
230+
231+ override fun onRuntimeAvailable (runtime : IJeiRuntime ) {
232+ this .runtime = runtime
233+ }
234+
235+
236+ companion object {
237+ fun consumeAllRecipes (consumer : Consumer <Recipe <* >? >) {
238+ Minecraft .getInstance()
239+ .getConnection()!!
240+ .getRecipeManager()
241+ .getRecipes()
242+ .forEach(consumer)
243+ }
244+
245+ private val allCategories: MutableList <CreateRecipeCategory <* >? > = ArrayList <CreateRecipeCategory <* >? > ()
246+ }
247+
248+ }
0 commit comments