diff --git a/src/main/java/dan200/computercraft/shared/util/InventoryUtil.java b/src/main/java/dan200/computercraft/shared/util/InventoryUtil.java index faab94b04..5fad59eb2 100644 --- a/src/main/java/dan200/computercraft/shared/util/InventoryUtil.java +++ b/src/main/java/dan200/computercraft/shared/util/InventoryUtil.java @@ -5,9 +5,11 @@ */ package dan200.computercraft.shared.util; +import net.minecraft.block.BlockState; import net.minecraft.entity.Entity; import net.minecraft.inventory.IInventory; import net.minecraft.inventory.ISidedInventory; +import net.minecraft.inventory.ISidedInventoryProvider; import net.minecraft.item.ItemStack; import net.minecraft.tileentity.TileEntity; import net.minecraft.util.Direction; @@ -23,6 +25,7 @@ import net.minecraftforge.items.wrapper.SidedInvWrapper; import org.apache.commons.lang3.tuple.Pair; import javax.annotation.Nonnull; +import javax.annotation.Nullable; public final class InventoryUtil { @@ -41,7 +44,8 @@ public final class InventoryUtil // Methods for finding inventories: - public static IItemHandler getInventory( World world, BlockPos pos, Direction side ) + @Nullable + public static IItemHandler getInventory( @Nonnull World world, @Nonnull BlockPos pos, @Nonnull Direction side ) { // Look for tile with inventory TileEntity tileEntity = world.getBlockEntity( pos ); @@ -52,7 +56,7 @@ public final class InventoryUtil { return itemHandler.orElseThrow( NullPointerException::new ); } - else if( side != null && tileEntity instanceof ISidedInventory ) + else if( tileEntity instanceof ISidedInventory ) { return new SidedInvWrapper( (ISidedInventory) tileEntity, side ); } @@ -62,6 +66,13 @@ public final class InventoryUtil } } + BlockState block = world.getBlockState( pos ); + if( block.getBlock() instanceof ISidedInventoryProvider ) + { + ISidedInventory inventory = ((ISidedInventoryProvider) block.getBlock()).getContainer( block, world, pos ); + return new SidedInvWrapper( inventory, side ); + } + // Look for entity with inventory Vec3d vecStart = new Vec3d( pos.getX() + 0.5 + 0.6 * side.getStepX(), diff --git a/src/test/java/dan200/computercraft/ingame/TurtleTest.kt b/src/test/java/dan200/computercraft/ingame/TurtleTest.kt index 822c1d9fc..4c793a763 100644 --- a/src/test/java/dan200/computercraft/ingame/TurtleTest.kt +++ b/src/test/java/dan200/computercraft/ingame/TurtleTest.kt @@ -55,4 +55,11 @@ class TurtleTest { */ @GameTest suspend fun `Place monitor`(context: TestContext) = context.checkComputerOk(10) + + /** + * Checks computers can place into compostors. These are non-typical inventories, so + * worth ensuring. + */ + @GameTest + suspend fun `Use compostors`(context: TestContext) = context.checkComputerOk(11) } diff --git a/src/test/server-files/computers/computer/11/startup.lua b/src/test/server-files/computers/computer/11/startup.lua new file mode 100644 index 000000000..bcc6c8ee2 --- /dev/null +++ b/src/test/server-files/computers/computer/11/startup.lua @@ -0,0 +1,3 @@ +test.eq(true, turtle.dropDown(), "Drop items into compostor") + +test.ok() diff --git a/src/test/server-files/computers/ids.json b/src/test/server-files/computers/ids.json index 87dab4f96..a90f89af9 100644 --- a/src/test/server-files/computers/ids.json +++ b/src/test/server-files/computers/ids.json @@ -1,3 +1,3 @@ { - "computer": 9 + "computer": 11 } \ No newline at end of file diff --git a/src/test/server-files/structures/turtle_test.use_compostors.snbt b/src/test/server-files/structures/turtle_test.use_compostors.snbt new file mode 100644 index 000000000..6ad6889cc --- /dev/null +++ b/src/test/server-files/structures/turtle_test.use_compostors.snbt @@ -0,0 +1,548 @@ +{ + size: [5, 5, 5], + entities: [], + blocks: [ + { + pos: [0, 0, 0], + state: 0 + }, + { + pos: [1, 0, 0], + state: 0 + }, + { + pos: [2, 0, 0], + state: 0 + }, + { + pos: [3, 0, 0], + state: 0 + }, + { + pos: [4, 0, 0], + state: 0 + }, + { + pos: [0, 0, 1], + state: 0 + }, + { + pos: [1, 0, 1], + state: 0 + }, + { + pos: [2, 0, 1], + state: 0 + }, + { + pos: [3, 0, 1], + state: 0 + }, + { + pos: [4, 0, 1], + state: 0 + }, + { + pos: [0, 0, 2], + state: 0 + }, + { + pos: [1, 0, 2], + state: 0 + }, + { + pos: [2, 0, 2], + state: 0 + }, + { + pos: [3, 0, 2], + state: 0 + }, + { + pos: [4, 0, 2], + state: 0 + }, + { + pos: [0, 0, 3], + state: 0 + }, + { + pos: [1, 0, 3], + state: 0 + }, + { + pos: [2, 0, 3], + state: 0 + }, + { + pos: [3, 0, 3], + state: 0 + }, + { + pos: [4, 0, 3], + state: 0 + }, + { + pos: [0, 0, 4], + state: 0 + }, + { + pos: [1, 0, 4], + state: 0 + }, + { + pos: [2, 0, 4], + state: 0 + }, + { + pos: [3, 0, 4], + state: 0 + }, + { + pos: [4, 0, 4], + state: 0 + }, + { + nbt: { + Owner: { + UpperId: 4039158846114182220L, + LowerId: -6876936588741668278L, + Name: "Dev" + }, + Fuel: 0, + Label: "Uses Compostors", + Slot: 0, + Items: [ + { + Slot: 0b, + id: "minecraft:spruce_sapling", + Count: 64b + } + ], + id: "computercraft:turtle_normal", + ComputerId: 11, + On: 1b + }, + pos: [2, 2, 2], + state: 1 + }, + { + pos: [0, 1, 0], + state: 2 + }, + { + pos: [1, 1, 0], + state: 2 + }, + { + pos: [2, 1, 0], + state: 2 + }, + { + pos: [3, 1, 0], + state: 2 + }, + { + pos: [4, 1, 0], + state: 2 + }, + { + pos: [0, 2, 0], + state: 2 + }, + { + pos: [1, 2, 0], + state: 2 + }, + { + pos: [2, 2, 0], + state: 2 + }, + { + pos: [3, 2, 0], + state: 2 + }, + { + pos: [4, 2, 0], + state: 2 + }, + { + pos: [0, 3, 0], + state: 2 + }, + { + pos: [1, 3, 0], + state: 2 + }, + { + pos: [2, 3, 0], + state: 2 + }, + { + pos: [3, 3, 0], + state: 2 + }, + { + pos: [4, 3, 0], + state: 2 + }, + { + pos: [0, 4, 0], + state: 2 + }, + { + pos: [1, 4, 0], + state: 2 + }, + { + pos: [2, 4, 0], + state: 2 + }, + { + pos: [3, 4, 0], + state: 2 + }, + { + pos: [4, 4, 0], + state: 2 + }, + { + pos: [0, 1, 1], + state: 2 + }, + { + pos: [1, 1, 1], + state: 2 + }, + { + pos: [2, 1, 1], + state: 2 + }, + { + pos: [3, 1, 1], + state: 2 + }, + { + pos: [4, 1, 1], + state: 2 + }, + { + pos: [0, 2, 1], + state: 2 + }, + { + pos: [1, 2, 1], + state: 2 + }, + { + pos: [2, 2, 1], + state: 2 + }, + { + pos: [3, 2, 1], + state: 2 + }, + { + pos: [4, 2, 1], + state: 2 + }, + { + pos: [0, 3, 1], + state: 2 + }, + { + pos: [1, 3, 1], + state: 2 + }, + { + pos: [2, 3, 1], + state: 2 + }, + { + pos: [3, 3, 1], + state: 2 + }, + { + pos: [4, 3, 1], + state: 2 + }, + { + pos: [0, 4, 1], + state: 2 + }, + { + pos: [1, 4, 1], + state: 2 + }, + { + pos: [2, 4, 1], + state: 2 + }, + { + pos: [3, 4, 1], + state: 2 + }, + { + pos: [4, 4, 1], + state: 2 + }, + { + pos: [0, 1, 2], + state: 2 + }, + { + pos: [1, 1, 2], + state: 2 + }, + { + pos: [2, 1, 2], + state: 3 + }, + { + pos: [3, 1, 2], + state: 2 + }, + { + pos: [4, 1, 2], + state: 2 + }, + { + pos: [0, 2, 2], + state: 2 + }, + { + pos: [1, 2, 2], + state: 2 + }, + { + pos: [3, 2, 2], + state: 2 + }, + { + pos: [4, 2, 2], + state: 2 + }, + { + pos: [0, 3, 2], + state: 2 + }, + { + pos: [1, 3, 2], + state: 2 + }, + { + pos: [2, 3, 2], + state: 2 + }, + { + pos: [3, 3, 2], + state: 2 + }, + { + pos: [4, 3, 2], + state: 2 + }, + { + pos: [0, 4, 2], + state: 2 + }, + { + pos: [1, 4, 2], + state: 2 + }, + { + pos: [2, 4, 2], + state: 2 + }, + { + pos: [3, 4, 2], + state: 2 + }, + { + pos: [4, 4, 2], + state: 2 + }, + { + pos: [0, 1, 3], + state: 2 + }, + { + pos: [1, 1, 3], + state: 2 + }, + { + pos: [2, 1, 3], + state: 2 + }, + { + pos: [3, 1, 3], + state: 2 + }, + { + pos: [4, 1, 3], + state: 2 + }, + { + pos: [0, 2, 3], + state: 2 + }, + { + pos: [1, 2, 3], + state: 2 + }, + { + pos: [2, 2, 3], + state: 2 + }, + { + pos: [3, 2, 3], + state: 2 + }, + { + pos: [4, 2, 3], + state: 2 + }, + { + pos: [0, 3, 3], + state: 2 + }, + { + pos: [1, 3, 3], + state: 2 + }, + { + pos: [2, 3, 3], + state: 2 + }, + { + pos: [3, 3, 3], + state: 2 + }, + { + pos: [4, 3, 3], + state: 2 + }, + { + pos: [0, 4, 3], + state: 2 + }, + { + pos: [1, 4, 3], + state: 2 + }, + { + pos: [2, 4, 3], + state: 2 + }, + { + pos: [3, 4, 3], + state: 2 + }, + { + pos: [4, 4, 3], + state: 2 + }, + { + pos: [0, 1, 4], + state: 2 + }, + { + pos: [1, 1, 4], + state: 2 + }, + { + pos: [2, 1, 4], + state: 2 + }, + { + pos: [3, 1, 4], + state: 2 + }, + { + pos: [4, 1, 4], + state: 2 + }, + { + pos: [0, 2, 4], + state: 2 + }, + { + pos: [1, 2, 4], + state: 2 + }, + { + pos: [2, 2, 4], + state: 2 + }, + { + pos: [3, 2, 4], + state: 2 + }, + { + pos: [4, 2, 4], + state: 2 + }, + { + pos: [0, 3, 4], + state: 2 + }, + { + pos: [1, 3, 4], + state: 2 + }, + { + pos: [2, 3, 4], + state: 2 + }, + { + pos: [3, 3, 4], + state: 2 + }, + { + pos: [4, 3, 4], + state: 2 + }, + { + pos: [0, 4, 4], + state: 2 + }, + { + pos: [1, 4, 4], + state: 2 + }, + { + pos: [2, 4, 4], + state: 2 + }, + { + pos: [3, 4, 4], + state: 2 + }, + { + pos: [4, 4, 4], + state: 2 + } + ], + palette: [ + { + Name: "minecraft:polished_andesite" + }, + { + Properties: { + waterlogged: "false", + facing: "south" + }, + Name: "computercraft:turtle_normal" + }, + { + Name: "minecraft:air" + }, + { + Properties: { + level: "0" + }, + Name: "minecraft:composter" + } + ], + DataVersion: 2230 +}