From e53255acc85ad70c67c011b4a4898e1a6442ccc4 Mon Sep 17 00:00:00 2001 From: ForestOfLight Date: Thu, 10 Jul 2025 15:57:12 -0700 Subject: [PATCH] pull supply from smallest stack --- packs/BP/scripts/options/fastEasyPlace.js | 17 ++--------------- packs/BP/scripts/utils.js | 8 ++++++-- 2 files changed, 8 insertions(+), 17 deletions(-) diff --git a/packs/BP/scripts/options/fastEasyPlace.js b/packs/BP/scripts/options/fastEasyPlace.js index a66d8ae..8302fb8 100644 --- a/packs/BP/scripts/options/fastEasyPlace.js +++ b/packs/BP/scripts/options/fastEasyPlace.js @@ -1,8 +1,8 @@ import { BuilderOption } from '../classes/Builder/BuilderOption'; -import { BlockPermutation, EntityComponentTypes, EquipmentSlot, GameMode, InputMode, ItemStack, system, world } from '@minecraft/server'; +import { BlockPermutation, EntityComponentTypes, EquipmentSlot, GameMode, ItemStack, system, world } from '@minecraft/server'; import { bannedBlocks, bannedToValidBlockMap, whitelistedBlockStates, resetToBlockStates, bannedDimensionBlocks, blockIdToItemStackMap } from '../data'; -import { placeBlock } from '../utils'; +import { placeBlock, fetchMatchingItemSlot } from '../utils'; import { Raycaster } from '../classes/Raycaster'; import { Builders } from '../classes/Builder/Builders'; @@ -147,17 +147,4 @@ function getPlaceableItemStack(structureBlock) { const blockId = structureBlock.type.id.replace('minecraft:', ''); const newItemId = blockIdToItemStackMap[blockId]; return newItemId ? new ItemStack(newItemId) : structureBlock.getItemStack(); -} - -function fetchMatchingItemSlot(player, itemToMatchId) { - if (!itemToMatchId) - return void 0; - const inventory = player.getComponent(EntityComponentTypes.Inventory)?.container; - if (!inventory) - return void 0; - for (let index = 0; index < inventory.size; index++) { - const itemSlot = inventory.getSlot(index); - if (itemSlot.hasItem() && itemSlot?.typeId === itemToMatchId) - return itemSlot; - } } \ No newline at end of file diff --git a/packs/BP/scripts/utils.js b/packs/BP/scripts/utils.js index 47336b1..7cabf70 100644 --- a/packs/BP/scripts/utils.js +++ b/packs/BP/scripts/utils.js @@ -21,11 +21,15 @@ export function fetchMatchingItemSlot(entity, itemToMatchId) { const inventory = entity.getComponent(EntityComponentTypes.Inventory)?.container; if (!inventory) return void 0; + let smallestItemSlot; for (let index = 0; index < inventory.size; index++) { const itemSlot = inventory.getSlot(index); - if (itemSlot.hasItem() && itemSlot?.typeId === itemToMatchId) - return itemSlot; + if (itemSlot.hasItem() && itemSlot?.typeId === itemToMatchId) { + if (!smallestItemSlot || itemSlot.amount < smallestItemSlot.amount) + smallestItemSlot = itemSlot; + } } + return smallestItemSlot; } export function placeBlock(player, placedBlock, blockToPlace, itemSlotToConsume = void 0) {