Skip to content

Commit

Permalink
Search for items in the whole gametest structure
Browse files Browse the repository at this point in the history
This fixes some flakiness where items get thrown outside a 1 block
radius.
  • Loading branch information
SquidDev committed Aug 18, 2024
1 parent b89e261 commit 3299d0e
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -123,11 +123,10 @@ class Computer_Test {
@GameTest
fun Drops_on_explosion(context: GameTestHelper) = context.sequence {
thenExecute {
val pos = BlockPos(2, 2, 2)
val explosionPos = Vec3.atCenterOf(context.absolutePos(pos))
val explosionPos = Vec3.atCenterOf(context.absolutePos(BlockPos(2, 2, 2)))
context.level.explode(null, explosionPos.x, explosionPos.y, explosionPos.z, 2.0f, Level.ExplosionInteraction.TNT)

context.assertItemEntityPresent(ModRegistry.Items.COMPUTER_NORMAL.get(), pos, 1.0)
context.assertItemEntityCountIs(ModRegistry.Items.COMPUTER_NORMAL.get(), 1)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -703,8 +703,8 @@ class Turtle_Test {
thenOnComputer { turtle.dig(Optional.empty()) }
thenIdle(2)
thenExecute {
context.assertItemEntityCountIs(ModRegistry.Items.TURTLE_NORMAL.get(), BlockPos(2, 2, 2), 1.0, 1)
context.assertItemEntityCountIs(Items.BONE_BLOCK, BlockPos(2, 2, 2), 1.0, 65)
context.assertItemEntityCountIs(ModRegistry.Items.TURTLE_NORMAL.get(), 1)
context.assertItemEntityCountIs(Items.BONE_BLOCK, 65)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import net.minecraft.world.Container
import net.minecraft.world.InteractionHand
import net.minecraft.world.entity.Entity
import net.minecraft.world.entity.EntityType
import net.minecraft.world.item.Item
import net.minecraft.world.item.ItemStack
import net.minecraft.world.item.context.UseOnContext
import net.minecraft.world.level.block.Blocks
Expand Down Expand Up @@ -252,6 +253,16 @@ fun GameTestHelper.assertExactlyItems(vararg expected: ItemStack, message: Strin
}
}

/**
* Similar to [GameTestHelper.assertItemEntityCountIs], but searching anywhere in the structure bounds.
*/
fun GameTestHelper.assertItemEntityCountIs(expected: Item, count: Int) {
val actualCount = getEntities(EntityType.ITEM).sumOf { if (it.item.`is`(expected)) it.item.count else 0 }
if (actualCount != count) {
throw GameTestAssertException("Expected $count ${expected.description.string} items to exist (found $actualCount)")
}
}

private fun getName(type: BlockEntityType<*>): ResourceLocation = RegistryWrappers.BLOCK_ENTITY_TYPES.getKey(type)!!

/**
Expand Down

0 comments on commit 3299d0e

Please sign in to comment.