From 9947c94414fc0b54c1b7d024b65f64e408a238c9 Mon Sep 17 00:00:00 2001 From: DBotThePony Date: Wed, 19 Feb 2025 14:40:44 +0700 Subject: [PATCH] Fix matter entangler not working --- .../matter/MatterEntanglerBlockEntity.kt | 4 +- .../kotlin/ru/dbotthepony/mc/otm/core/Ext.kt | 11 ++- .../mc/otm/menu/matter/MatterEntanglerMenu.kt | 4 +- .../ru/dbotthepony/mc/otm/recipe/Ext.kt | 68 +++++++------------ .../mc/otm/recipe/MatterEntanglerRecipe.kt | 6 +- 5 files changed, 43 insertions(+), 50 deletions(-) diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/block/entity/matter/MatterEntanglerBlockEntity.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/block/entity/matter/MatterEntanglerBlockEntity.kt index 80953817a..a09006236 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/block/entity/matter/MatterEntanglerBlockEntity.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/block/entity/matter/MatterEntanglerBlockEntity.kt @@ -77,12 +77,12 @@ class MatterEntanglerBlockEntity(blockPos: BlockPos, blockState: BlockState) : M override fun canInsert(slot: Int, stack: ItemStack): Boolean { val list = inputs.toList() list[slot] = stack - val shadow = CraftingInput.of(3, 3, list) + val shadow = CraftingInput.ofPositioned(3, 3, list) return (level ?: return false) .recipeManager .byType(MRecipes.MATTER_ENTANGLER) - .any { it.value.preemptivelyMatches(shadow, level!!) } + .any { it.value.preemptivelyMatches(shadow, level!!, 3, 3) } } override fun canExtract(slot: Int, amount: Int, stack: ItemStack): Boolean { diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/core/Ext.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/core/Ext.kt index f0cea4a71..b66c2aeb7 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/core/Ext.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/core/Ext.kt @@ -72,7 +72,16 @@ import kotlin.reflect.KProperty operator fun RecipeInput.get(index: Int): ItemStack = getItem(index) operator fun CraftingInput.get(x: Int, y: Int): ItemStack = getItem(x, y) -operator fun CraftingInput.get(x: Int, y: Int, flop: Boolean): ItemStack = if (flop) getItem(x, y) else getItem(width() - x - 1, y) + +operator fun CraftingInput.Positioned.get(x: Int, y: Int): ItemStack { + val actualX = x - left + val actualY = y - top + + if (actualX !in 0 until input.width() || actualY !in 0 until input.height()) + return ItemStack.EMPTY + + return input.getItem(actualX, actualY) +} fun FriendlyByteBuf.writeBigInteger(value: BigInteger) { writeByteArray(value.toByteArray()) diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/menu/matter/MatterEntanglerMenu.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/menu/matter/MatterEntanglerMenu.kt index b4143c90a..561f3b872 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/menu/matter/MatterEntanglerMenu.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/menu/matter/MatterEntanglerMenu.kt @@ -48,13 +48,13 @@ class MatterEntanglerMenu( override fun mayPlace(itemStack: ItemStack): Boolean { val list = it.toList() list[i] = itemStack - val shadow = CraftingInput.of(3, 3, list) + val shadow = CraftingInput.ofPositioned(3, 3, list) val level = player.level() return super.mayPlace(itemStack) && (level ?: return false) .recipeManager .byType(MRecipes.MATTER_ENTANGLER) - .any { it.value.preemptivelyMatches(shadow, level) } + .any { it.value.preemptivelyMatches(shadow, level, 3, 3) } } } } diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/recipe/Ext.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/recipe/Ext.kt index 86ac3cdfa..39713b74c 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/recipe/Ext.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/recipe/Ext.kt @@ -16,63 +16,47 @@ operator fun ShapedRecipePattern.get(column: Int, row: Int): Ingredient { return ingredients()[column + row * width] } -operator fun ShapedRecipePattern.get(column: Int, row: Int, flop: Boolean): Ingredient { - return if (flop) - get(width - column - 1, row) - else - get(column, row) -} - -fun ShapedRecipePattern.preemptiveTest(t: CraftingInput, fromColumn: Int, fromRow: Int, flop: Boolean): Boolean { - if (t.width() - fromColumn < width || t.height() - fromRow < height) +fun ShapedRecipePattern.preemptiveTest(t: CraftingInput.Positioned, craftingWidth: Int, craftingHeight: Int): Boolean { + if ( + t.input.width() > width || + t.input.height() > height + ) { return false + } - for (column in 0 until width) { - for (row in 0 until height) { - val item = t[fromColumn + column, fromRow + row, flop] - val ingredient = this[column, row, flop] + for (columnPadding in 0 .. craftingWidth - width) { + for (rowPadding in 0 .. craftingHeight - height) { + var invalid = false - if (!ingredient.test(item) && item.isNotEmpty) { - return false + outer@for (column in 0 until width) { + for (row in 0 until height) { + val ingredient = this[column, row] + val item = t[column + columnPadding, row + rowPadding] + + if (!ingredient.test(item) && item.isNotEmpty) { + invalid = true + break@outer + } + } + } + + if (!invalid) { + return true } } } - return true -} - -fun ShapedRecipePattern.preemptiveTest(t: CraftingInput): Boolean { - if (t.width() < width || t.height() < height) - return false - - for (column in 0 .. t.width() - width) - for (row in 0 .. t.height() - height) - if (preemptiveTest(t, column, row, false) || preemptiveTest(t, column, row, true)) - return true - return false } -fun ShapedRecipePattern.test(t: CraftingInput, fromColumn: Int, fromRow: Int, flop: Boolean): Boolean { - if (t.width() - fromColumn < width || t.height() - fromRow < height) +fun ShapedRecipePattern.test(t: CraftingInput): Boolean { + if (t.width() != width || t.height() != height) return false for (column in 0 until width) for (row in 0 until height) - if (!this[column, row, flop].test(t[fromColumn + column, fromRow + row, flop])) + if (!this[column, row].test(t[column, row])) return false return true } - -fun ShapedRecipePattern.test(t: CraftingInput): Boolean { - if (t.width() < width || t.height() < height) - return false - - for (column in 0 .. t.width() - width) - for (row in 0 .. t.height() - height) - if (test(t, column, row, false) || test(t, column, row, true)) - return true - - return false -} diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/recipe/MatterEntanglerRecipe.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/recipe/MatterEntanglerRecipe.kt index 3ff5221fe..b5999ed44 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/recipe/MatterEntanglerRecipe.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/recipe/MatterEntanglerRecipe.kt @@ -39,7 +39,7 @@ interface IMatterEntanglerRecipe : IMatteryRecipe { val result: ItemStack val experience: Float - fun preemptivelyMatches(container: CraftingInput, level: Level): Boolean + fun preemptivelyMatches(container: CraftingInput.Positioned, level: Level, craftingWidth: Int, craftingHeight: Int): Boolean } open class MatterEntanglerRecipe( @@ -56,9 +56,9 @@ open class MatterEntanglerRecipe( return ingredients.test(container) } - override fun preemptivelyMatches(container: CraftingInput, level: Level): Boolean { + override fun preemptivelyMatches(container: CraftingInput.Positioned, level: Level, craftingWidth: Int, craftingHeight: Int): Boolean { if (isIncomplete) return false - return ingredients.preemptiveTest(container) + return ingredients.preemptiveTest(container, craftingWidth, craftingHeight) } override fun assemble(container: CraftingInput, registry: HolderLookup.Provider): ItemStack {