Fix matter entangler not working

This commit is contained in:
DBotThePony 2025-02-19 14:40:44 +07:00
parent 5166bf20ad
commit 9947c94414
Signed by: DBot
GPG Key ID: DCC23B5715498507
5 changed files with 43 additions and 50 deletions

View File

@ -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 {

View File

@ -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())

View File

@ -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) }
}
}
}

View File

@ -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
}

View File

@ -39,7 +39,7 @@ interface IMatterEntanglerRecipe : IMatteryRecipe<CraftingInput> {
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 {