Building a custom CraftingInventory?

Is it possible to build a CraftingInventory object with an Inventory.Builder?

I’m trying to make a CraftingInventory with a crafting grid of whatever size I choose, e.g. 3x4. And ultimately, I would like to be able to pass the custom inventory to CraftingRecipe::isValid.

I’m currently attempting to query for a CraftingInventory, but a ClassCastException is being thrown.

val craftingInv = Inventory.builder()
        .of(InventoryArchetypes.WORKBENCH)
        .property(
                InventoryDimension.PROPERTY_NAME,
                InventoryDimension(3, 4)
        )
        .build(this)
        .query<CraftingInventory>(CraftingInventory::class.java)
java.lang.ClassCastException: org.spongepowered.common.item.inventory.EmptyInventoryImpl cannot be cast to org.spongepowered.api.item.inventory.crafting.CraftingInventory
	at net.benwoodworth.fastcraft.impl.sponge.SpongeFastCraft.onPreInit(SpongeFastCraft.kt:48) ~[SpongeFastCraft.class:?]
	at org.spongepowered.common.event.listener.GamePreInitializationEventListener_SpongeFastCraft_onPreInit3.handle(Unknown Source) ~[?:?]
	at org.spongepowered.common.event.RegisteredListener.handle(RegisteredListener.java:95) ~[RegisteredListener.class:1.12-7.0.0-BETA-300]
	at org.spongepowered.common.event.SpongeEventManager.post(SpongeEventManager.java:349) [SpongeEventManager.class:1.12-7.0.0-BETA-300]
	at org.spongepowered.common.event.SpongeEventManager.post(SpongeEventManager.java:366) [SpongeEventManager.class:1.12-7.0.0-BETA-300]
	at org.spongepowered.common.event.SpongeEventManager.post(SpongeEventManager.java:370) [SpongeEventManager.class:1.12-7.0.0-BETA-300]
	at org.spongepowered.common.SpongeImpl.postState(SpongeImpl.java:205) [SpongeImpl.class:1.12-7.0.0-BETA-300]
	at org.spongepowered.server.SpongeVanilla.preInitialize(SpongeVanilla.java:125) [SpongeVanilla.class:1.12-7.0.0-BETA-300]
	at net.minecraft.server.dedicated.DedicatedServer.handler$onServerLoad$zog000(SourceFile:1228) [nx.class:?]
	at net.minecraft.server.dedicated.DedicatedServer.func_71197_b(SourceFile:114) [nx.class:?]
	at net.minecraft.server.MinecraftServer.run(SourceFile:434) [MinecraftServer.class:?]
	at java.lang.Thread.run(Thread.java:748) [?:1.8.0_131]

That’s not how inventories work. Only chest inventories can be resized (and even then only in rows of 9).

The getResult documentation says the grid parameter is “The crafting input, typically 3x3 or 2x2”, which led me to believe the that dimensions could be anything. I’ve also found that you can create chest inventories with row sizes not equal to 9. You’re just unable to show it to the player, which isn’t something I intend on doing.

Ignoring nonstandard crafting grid dimensions, is there a way to build a CraftingInventory?

val craftingInv = Inventory.builder()
        .of(InventoryArchetypes.WORKBENCH)
        .build(this)
        .query<CraftingInventory>(CraftingInventory::class.java)

No you cannot build CraftingInventories yourself.

getResult is on the CraftinRecipe Interface. Minecraft will give the Recipe a CraftingInventory when you’re crafting something. (Internally the CraftingInventory only exists when you actually have an open container (Workbench or Player))

All Inventories you build using this are CustomInventory.
While you can change the size of the inventory, if you change it to something else you might not be able to show the inventory to a player.

With this PR you will be able to open working workbench containers (also anvils etc.).

1 Like

That answers my question. Thanks for the info!