From 280a8f343169f4d3336ead8d5ea1d4d0eceba670 Mon Sep 17 00:00:00 2001 From: DBotThePony Date: Thu, 16 Nov 2023 21:29:19 +0700 Subject: [PATCH] Merge painter recipe types --- .../mc/otm/compat/jei/JEIPlugin.kt | 8 +++++++- .../mc/otm/core/collect/StreamyIterators.kt | 2 ++ .../mc/otm/menu/decorative/PainterMenu.kt | 10 +++------- .../mc/otm/recipe/PainterRecipe.kt | 20 ++++++++++--------- .../dbotthepony/mc/otm/registry/MRecipes.kt | 3 +-- 5 files changed, 24 insertions(+), 19 deletions(-) diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/compat/jei/JEIPlugin.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/compat/jei/JEIPlugin.kt index b4a834410..354c4346f 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/compat/jei/JEIPlugin.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/compat/jei/JEIPlugin.kt @@ -20,11 +20,17 @@ import net.minecraft.world.item.ItemStack import ru.dbotthepony.mc.otm.OverdriveThatMatters import ru.dbotthepony.mc.otm.client.minecraft import ru.dbotthepony.mc.otm.client.screen.MatteryScreen +import ru.dbotthepony.mc.otm.core.collect.filter +import ru.dbotthepony.mc.otm.core.collect.filterIsInstance +import ru.dbotthepony.mc.otm.core.collect.filterNotNull +import ru.dbotthepony.mc.otm.core.collect.map +import ru.dbotthepony.mc.otm.core.collect.toList import ru.dbotthepony.mc.otm.menu.decorative.PainterMenu import ru.dbotthepony.mc.otm.menu.matter.MatterEntanglerMenu import ru.dbotthepony.mc.otm.menu.tech.PlatePressMenu import ru.dbotthepony.mc.otm.menu.tech.PoweredFurnaceMenu import ru.dbotthepony.mc.otm.menu.tech.TwinPlatePressMenu +import ru.dbotthepony.mc.otm.recipe.PainterRecipe import ru.dbotthepony.mc.otm.registry.MItems import ru.dbotthepony.mc.otm.registry.MRecipes import java.util.* @@ -91,7 +97,7 @@ class JEIPlugin : IModPlugin { val level = minecraft.level ?: throw NullPointerException("No ClientLevel. OLOLOLOLOLOLO") registration.addRecipes(PlatePressRecipeCategory.recipeType, level.recipeManager.getAllRecipesFor(MRecipes.PLATE_PRESS).filter { !it.value.isIncomplete }.map { it.value }) - registration.addRecipes(PainterRecipeCategory.recipeType, level.recipeManager.getAllRecipesFor(MRecipes.PAINTER).filter { !it.value.isIncomplete }.map { it.value }) + registration.addRecipes(PainterRecipeCategory.recipeType, level.recipeManager.getAllRecipesFor(MRecipes.PAINTER).iterator().filter { !it.value.isIncomplete }.map { it.value }.filterIsInstance().toList()) registration.addRecipes(MatterEntanglerRecipeCategory.recipeType, level.recipeManager.getAllRecipesFor(MRecipes.MATTER_ENTANGLER).filter { !it.value.isIncomplete }.map { it.value }) registration.addRecipes(MicrowaveRecipeCategory.recipeType, level.recipeManager.getAllRecipesFor(MRecipes.MICROWAVE).filter { !it.value.isIncomplete }.map { it.value }) } diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/core/collect/StreamyIterators.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/core/collect/StreamyIterators.kt index 572e0cb10..5d9d0e85c 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/core/collect/StreamyIterators.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/core/collect/StreamyIterators.kt @@ -230,6 +230,8 @@ inline fun Iterator.reduce(identity: T, reducer: (T, T) -> T): T { fun Iterator.filterNotNull(): MutableIterator = filter { it != null } as MutableIterator +inline fun Iterator<*>.filterIsInstance(): MutableIterator = filter { it is T } as MutableIterator + fun Iterator.any(predicate: Predicate): Boolean { while (hasNext()) if (predicate.test(next())) diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/menu/decorative/PainterMenu.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/menu/decorative/PainterMenu.kt index 02ce925e5..6e4f94e7c 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/menu/decorative/PainterMenu.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/menu/decorative/PainterMenu.kt @@ -57,7 +57,7 @@ class PainterMenu( return super.mayPlace(itemStack) } - return super.mayPlace(itemStack) && inventory.player.level().recipeManager.byType(MRecipes.PAINTER).values.any { it.value.input.test(itemStack) } + return super.mayPlace(itemStack) && inventory.player.level().recipeManager.byType(MRecipes.PAINTER).values.any { it.value.matches(itemStack) } } } @@ -128,11 +128,7 @@ class PainterMenu( private fun rescan() { possibleRecipes.clear() - possibleRecipes.addAll(inventory.player.level().recipeManager.byType(MRecipes.PAINTER).values.iterator().filter { it.value.input.test(inputContainer[0]) }) - - if (!inputContainer[0].isEmpty && inputContainer[0].item is DyeableArmorItem) { - possibleRecipes.addAll(inventory.player.level().recipeManager.byType(MRecipes.PAINTER_ARMOR_DYE).values) - } + possibleRecipes.addAll(inventory.player.level().recipeManager.byType(MRecipes.PAINTER).values.iterator().filter { it.value.matches(inputContainer[0]) }) listeners.accept(Unit) if (tile !is PainterBlockEntity) return @@ -140,7 +136,7 @@ class PainterMenu( if (inputContainer.isEmpty || selectedRecipe == null) { outputContainer.clearContent() } else { - val recipe = inventory.player.level().recipeManager.byKey(selectedRecipe).get() as RecipeHolder? + val recipe = inventory.player.level().recipeManager.byKey(selectedRecipe!!).get() as RecipeHolder? if (recipe == null || !recipe.value.canCraft(dyeStoredDirect) || !recipe.value.matches(inputContainer, inventory.player.level())) { outputContainer.clearContent() diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/recipe/PainterRecipe.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/recipe/PainterRecipe.kt index f8deb91d0..bc89e7f34 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/recipe/PainterRecipe.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/recipe/PainterRecipe.kt @@ -35,6 +35,12 @@ abstract class AbstractPainterRecipe( ) : IMatteryRecipe { val dyes: Object2IntMap = Object2IntMaps.unmodifiable(Object2IntArrayMap(dyes)) + abstract fun matches(value: ItemStack): Boolean + + override fun matches(contaier: Container, level: Level): Boolean { + return !isIncomplete && matches(contaier[0]) + } + override fun getToastSymbol(): ItemStack = ItemStack(MItems.PAINTER) fun canCraft(storedDyes: Map): Boolean { @@ -110,9 +116,8 @@ class PainterRecipe( dyes: Set ) : this(input, output, Object2IntArrayMap().also { map -> dyes.forEach { map[it] = 1 } }) - override fun matches(p_44002_: Container, p_44003_: Level): Boolean { - if (isIncomplete) return false - return input.test(p_44002_[0]) + override fun matches(value: ItemStack): Boolean { + return !isIncomplete && input.test(value) } override fun isIncomplete(): Boolean { @@ -176,11 +181,8 @@ class PainterArmorDyeRecipe( dyes: Set ) : this(Object2IntArrayMap().also { map -> dyes.forEach { map[it] = 1 } }) - override fun matches(contaier: Container, level: Level): Boolean { - if (this.isIncomplete) return false - val stack = contaier[0] - - return (!stack.isEmpty && stack.item is DyeableArmorItem) + override fun matches(value: ItemStack): Boolean { + return !isIncomplete && value.item is DyeableArmorItem } override fun assemble(container: Container, registry: RegistryAccess): ItemStack { @@ -201,7 +203,7 @@ class PainterArmorDyeRecipe( override fun getSerializer(): RecipeSerializer<*> = SERIALIZER - override fun getType(): RecipeType<*> = MRecipes.PAINTER_ARMOR_DYE + override fun getType(): RecipeType<*> = MRecipes.PAINTER override fun isIncomplete(): Boolean = dyes.isEmpty() diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/registry/MRecipes.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/registry/MRecipes.kt index d3cc8c922..db9109e31 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/registry/MRecipes.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/registry/MRecipes.kt @@ -33,8 +33,7 @@ object MRecipes { } val PLATE_PRESS by register("plate_press") - val PAINTER by register("painter") - val PAINTER_ARMOR_DYE by register("painter_armor_dye") + val PAINTER by register("painter") val MATTER_ENTANGLER by register("matter_entangler") val MICROWAVE by register("microwave")