import{Block as e,Dimension as t,Direction as n,Entity as r,EntityComponentTypes as i,ItemStack as o,Player as c,system as a}from"@minecraft/server";import{displayRegistry as s}from"./display/registry";import{Entities as d,Properties as l,ReplaceableBlocks as u,SneakToPlaceItems as f,SneakToPlaceTags as m}from"./consts";export function getSideOfBlock(e,t,r=1){switch(t){case n.Down:return e.below(r);case n.Up:return e.above(r);case n.North:return e.north(r);case n.East:return e.east(r);case n.South:return e.south(r);case n.West:return e.west(r)}}export function calculateRotation(e,t,n){e=(e+360)%360;let r=t?.calculateRotation(e,n);return void 0!==r?r:Math.floor(e/45)}export function getPlayerHeldItem(e){let t=e.getComponent(i.Inventory)?.container;return t?.getItem(e.selectedSlotIndex)}export function consumeHeldItem(e,t=1){if(t<1)return;let n=e.getComponent(i.Inventory)?.container,r=n?.getItem(e.selectedSlotIndex),o=r?.clone();return!r||r.amount<t?void 0:(r.amount==t?n.setItem(e.selectedSlotIndex,void 0):(o.amount=t,r.amount-=t,n.setItem(e.selectedSlotIndex,r)),o)}export function convertTypeId(e){return e.startsWith("minecraft:")?e.replace("minecraft:","bc_ds:item_"):e.replace("bc_ds:item_","minecraft:")}export function isDisplayBlock(e){return void 0!==s.fromTypeId(e)}export function isEntityItem(e){return e.startsWith("bc_ds:item_")}export function getBlockEntity(e,t){return getBlockEntities(e,t)[0]}export function getBlockEntities(e,t,n=!1){let r=e.getEntitiesAtBlockLocation(t.location).filter(e=>n?e.typeId===d.ArmorEnchantLocator:isEntityItem(e.typeId));return n?r:r.sort((e,t)=>t.getProperty(l.Layers)-e.getProperty(l.Layers))}export function getEntityItemStack(e){let t=e.getComponent(i.Inventory)?.container;return t?.getItem(0)}export function setEntityItemStack(e,t){let n=e.getComponent(i.Inventory)?.container;n?.setItem(0,t)}export function canReplaceBlock(e){return e.isAir||u.includes(e.typeId)}export function canPlaceOrInteractWith(e,t,n){if(!t||!n||!n.typeId.startsWith("minecraft:"))return!0;let r=s.fromTypeId(t.typeId);if(void 0!==r)return!0;let i=n.getTags();if(t.typeId.includes("item_frame")||"minecraft:decorated_pot"===t.typeId)return!1;if("minecraft:cauldron"===t.typeId)return!n.typeId.endsWith("dye");if("minecraft:lectern"===t.typeId||"minecraft:chiseled_bookshelf"===t.typeId)return!i.includes("minecraft:bookshelf_books");if("minecraft:end_portal_frame"===t.typeId)return"minecraft:eye_of_ender"!==n.typeId;if(t.typeId.includes("suspicious"))return"minecraft:brush"!==n.typeId;else if("minecraft:campfire"===t.typeId)return"minecraft:kelp"!==n.typeId&&!i.includes("minecraft:is_food");else if("minecraft:vault"===t.typeId)return"minecraft:trial_key"!==n.typeId&&"minecraft:ominous_trial_key"!==n.typeId;else if(t.typeId.endsWith("fence"))return"minecraft:lead"!==n.typeId;else if("minecraft:jukebox"===t.typeId)return!i.includes("minecraft:music_disc");else if(t.typeId.includes("sign"))return"minecraft:glow_ink_sac"!==n.typeId&&"minecraft:honeycomb"!==n.typeId;return!(!e.isSneaking&&(i.includes("minecraft:is_armor")||i.includes("minecraft:is_tool")||f.includes(n.typeId)||i.filter(e=>m.includes(e)).length>0))||(sendSneakHint(e,n.typeId),!1)}let sneakHintHistory={};function sendSneakHint(e,t){let n=sneakHintHistory[e.id]??{},r=Date.now();(!n[t]||!(n[t]>=r))&&(n[t]=r+5e3,sneakHintHistory[e.id]=n,a.run(()=>{e.onScreenDisplay.setActionBar({translate:"display.sneak_hint"})}))}