Crafting table - #3590
Crafting table#3590Crepestrom wants to merge 17 commits into
Conversation
|
used this to quickly whip together a table saw (#1734) |
| .sourceAmounts = main.worldArena.alloc(u16, inputs.len), | ||
| .resultItem = output.item, | ||
| .resultAmount = output.amount, | ||
| .craftingTags = main.worldArena.alloc(Tag, craftingTags.len), |
There was a problem hiding this comment.
| .craftingTags = main.worldArena.alloc(Tag, craftingTags.len), | |
| .craftingTags = main.worldArena.dupe(Tag, craftingTags), |
Then you don't need to copy them later
There was a problem hiding this comment.
ah i see now
i copied the other type of code but this doesnt need that implementaiton
There was a problem hiding this comment.
swapped that over
| var defaultCraftingTags = arena.alloc(main.Tag, 1); | ||
| defaultCraftingTags[0] = main.Tag.handCraftable; | ||
| const foundCraftingTags = Tag.loadTagsFromZon(arena, zon.getChild("craftingTags")); | ||
| const craftingTags = if (foundCraftingTags.len != 0) foundCraftingTags else defaultCraftingTags; |
There was a problem hiding this comment.
If you decide 4 lines later if you even need your allocation, then only do it if you need it
There was a problem hiding this comment.
moved over so we dont unessicarily duplicate the work
|
|
||
| return getValidRecipe(.{.sourceItems = sourceItems.items, .sourceAmounts = sourceAmounts.items, .resultItem = resultItem, .resultAmount = resultAmount}); | ||
| const tagCount = try reader.readVarInt(usize); | ||
| var craftingTagTypes: main.List(Tag) = .initCapacity(main.stackAllocator, @min(256, tagCount)); |
There was a problem hiding this comment.
no magic numbers please, where does the 256 come from? where do you have this limitation? If its by a type, then use std.math.maxInt for example
There was a problem hiding this comment.
i dont actually know
| const craftingTags = if (foundCraftingTags.len != 0) foundCraftingTags else blk: { | ||
| var defaultCraftingTags = arena.alloc(main.Tag, 1); | ||
| defaultCraftingTags[0] = main.Tag.handCraftable; | ||
| break :blk defaultCraftingTags; | ||
| }; |
There was a problem hiding this comment.
I just remembered, because you don't use an arena, you should also just be able to do:
| const craftingTags = if (foundCraftingTags.len != 0) foundCraftingTags else blk: { | |
| var defaultCraftingTags = arena.alloc(main.Tag, 1); | |
| defaultCraftingTags[0] = main.Tag.handCraftable; | |
| break :blk defaultCraftingTags; | |
| }; | |
| const craftingTags = if (foundCraftingTags.len != 0) foundCraftingTags else &.{main.Tag.handCraftable} |
There was a problem hiding this comment.
oh yeah i did something like that
There was a problem hiding this comment.
It never leaves the stack, so please don't dupe it.

When you open the crafting menu it will check if it has the right tags to show a recipe to the player
It also allows blocks to call open the crafting menu with its own tags (allows for gating recipes behind certain crafting tables)