Modern Apps git

← Modern-Apps.git

voxels: refuse crafts and trades that would drop a tool on the floor

Commit
e1faee2e404ecfc6c5ea1a2f4dddc1c14bfecfcf
Author
Vayun Mathur <[email protected]>
When
2026-08-05 19:13 UTC (4 months ago)
Parents
21a910a

Diff

games/voxels/src/main/rust/src/inventory.rs
diff --git a/games/voxels/src/main/rust/src/inventory.rs b/games/voxels/src/main/rust/src/inventory.rsindex 99c2fad5a3..c4033fa2aa 100644--- a/games/voxels/src/main/rust/src/inventory.rs+++ b/games/voxels/src/main/rust/src/inventory.rs@@ -335,7 +335,9 @@ impl Inventory {         let Some(&Recipe { in1, n1, in2, n2, out, out_n, .. }) = RECIPES.get(recipe) else { return false; };         if self.count_of(in1) < n1 { return false; }         if in2 != 0 && self.count_of(in2) < n2 { return false; }-        if !crate::item::has_durability(out) && !self.has_room_for(out, out_n) { return false; }+        // `has_room_for` already knows a tool needs a whole empty slot, so this covers both cases.+        // Skipping it for durable output would craft a tool into a full inventory and drop it.+        if !self.has_room_for(out, out_n) { return false; }         self.remove_count(in1, n1);         if in2 != 0 { self.remove_count(in2, n2); }         if crate::item::has_durability(out) { self.add_item_with_count(out, crate::item::max_durability(out)); }@@ -375,7 +377,7 @@ impl Inventory {         let give_n = crate::villager::give_count(o);         if self.count_of(o.cost) < o.cost_n { return false; }         if o.cost2 != 0 && self.count_of(o.cost2) < o.cost2_n { return false; }-        if !crate::item::has_durability(o.give) && !self.has_room_for(o.give, give_n) { return false; }+        if !self.has_room_for(o.give, give_n) { return false; }         self.remove_count(o.cost, o.cost_n);         if o.cost2 != 0 { self.remove_count(o.cost2, o.cost2_n); }         if crate::item::has_durability(o.give) { self.add_item_with_count(o.give, give_n); }@@ -612,6 +614,44 @@ mod tests {         assert_eq!(inv.count_of(Block::Wood as Id), STACK, "the logs must survive");     } +    // A tool needs a whole empty slot. Crafting one into a full inventory used to consume the+    // ingredients and drop the tool on the floor, because the room check skipped durable output.+    #[test]+    fn crafting_a_tool_with_no_empty_slot_is_refused() {+        let idx = RECIPES.iter().position(|r| r.out == 163).expect("wood pickaxe");+        let none = vec![false; RECIPES.len()];+        let mut inv = Inventory::default();+        // Every slot occupied, but the two ingredients are present in the stacks that fill it.+        for s in inv.slots.iter_mut() { *s = InvSlot { id: Block::Dirt as Id, count: STACK }; }+        inv.slots[0] = InvSlot { id: Block::Planks as Id, count: STACK };+        inv.slots[1] = InvSlot { id: 159, count: STACK };++        assert!(!inv.craft(idx, &none), "there is nowhere to put a pickaxe");+        assert_eq!(inv.count_of(Block::Planks as Id), STACK, "the planks must survive");+        assert_eq!(inv.count_of(159), STACK, "the sticks must survive");+        assert_eq!(inv.count_of(163), 0);++        // Free one slot and it goes through.+        inv.slots[5] = InvSlot::default();+        assert!(inv.craft(idx, &none));+        assert!(inv.count_of(163) > 0);+    }++    // Same hazard on the trade path: a forged tool with nowhere to go must not eat the payment.+    #[test]+    fn trading_for_a_tool_with_no_empty_slot_is_refused() {+        use crate::villager::{Offer, EMERALD};+        let forge = Offer { cost: EMERALD, cost_n: 4, cost2: 154, cost2_n: 2, give: 167, give_n: 1 };+        let mut inv = Inventory::default();+        for s in inv.slots.iter_mut() { *s = InvSlot { id: Block::Dirt as Id, count: STACK }; }+        inv.slots[0] = InvSlot { id: EMERALD, count: STACK };+        inv.slots[1] = InvSlot { id: 154, count: STACK };++        assert!(!inv.trade_offer(&forge), "nowhere to put the pickaxe");+        assert_eq!(inv.count_of(EMERALD), STACK, "the emeralds must survive");+        assert_eq!(inv.count_of(154), STACK);+    }+     #[test]     fn recipe_tables_are_well_formed() {         for (i, &Recipe { in1, n1, in2, n2, out, out_n, .. }) in RECIPES.iter().enumerate() {
games/voxels/src/main/rust/src/world/save.rs
diff --git a/games/voxels/src/main/rust/src/world/save.rs b/games/voxels/src/main/rust/src/world/save.rsindex 61626e970f..720d89caf4 100644--- a/games/voxels/src/main/rust/src/world/save.rs+++ b/games/voxels/src/main/rust/src/world/save.rs@@ -328,6 +328,35 @@ mod tests {         assert_eq!(partial.get_block(1, 1, 1), 5);     } +    // The whole point of keeping VOX1 and VOX2 readable: `ChunkMap::load_or_gen` treats a decode+    // failure as "never generated" and rebuilds the chunk from the seed, so a rejected file silently+    // erases whatever the player built. This drives the real load path against a real file to prove+    // an old-format world survives the upgrade.+    #[test]+    fn a_world_saved_by_the_old_build_still_loads_through_chunk_map() {+        use super::super::ChunkMap;+        let dir = std::env::temp_dir().join("voxels_vox2_world_test");+        let _ = fs::remove_dir_all(&dir);+        let base = dir.to_string_lossy().into_owned();++        // Hand-build a chunk the way the previous build wrote it, well above sea level so the water+        // pass can't touch it, and drop it exactly where the loader looks.+        let pos = ChunkPos(5, -3);+        let mut original = Chunk::new(pos);+        original.set_block(2, 200, 3, 42);+        original.set_block_meta(4, 200, 3, 103, 0b110);+        fs::create_dir_all(region_dir(&base)).unwrap();+        fs::write(chunk_file(&base, pos), encode_vox2(&original)).unwrap();++        let mut map = ChunkMap::new(0xB10CCA, base.clone());+        map.load_or_gen(pos);+        assert_eq!(map.get_block_world(5 * 16 + 2, 200, -3 * 16 + 3), 42, "the build was regenerated away");+        assert_eq!(map.get_block_world(5 * 16 + 4, 200, -3 * 16 + 3), 103);+        assert_eq!(map.get_meta_world(5 * 16 + 4, 200, -3 * 16 + 3), 0b110, "stair facing was lost");++        let _ = fs::remove_dir_all(&dir);+    }+     #[test]     fn slabs_and_stairs_survive_a_round_trip() {         let mut original = Chunk::new(ChunkPos(0, 0));