import{world as e,system as t,Player as o,ItemStack as n,EntityComponentTypes as r}from"@minecraft/server";import"./book.js";import"./player_controller.js";import{SingleDrawer as i,HalfSingleDrawer as s}from"./drawers/single.js";import{DrawerController as c}from"./drawers/controller.js";import{SingleDoubleDrawer as a,HalfDoubleSingleDrawer as l,AbstractDoubleDrawer as f}from"./drawers/double.js";import{ClickableBlockComponent as m,DrawerViewer as d}from"./component/block/clickable.js";import{getOpsiteDirection as u,camelCaseNamespacedID as k,vec3 as b}from"./utils.js";import{ALL_MODIFIERS as p}from"./modifer/modifers.js";import{Hand as y}from"./render.js";const g=new i,w=new s,E=new a,B=!1,_=[g,w,new l,E,new c];function v(e){for(const t of _)if(t.isDrawerEntity(e))return t}e.beforeEvents.worldInitialize.subscribe((e=>{e.blockComponentRegistry.registerCustomComponent("ftb_sd:clickable",new m),e.blockComponentRegistry.registerCustomComponent("ftb_sd:viewer",new d)})),e.afterEvents.playerSpawn.subscribe((e=>{const t=e.player;e.initialSpawn&&!t.hasTag("ftb_sd_joined")&&(t.sendMessage({translate:"ftb_sd.welcome"}),t.sendMessage({translate:"ftb_sd.book"}),t.runCommand('loot spawn ^ ^ ^1 loot "ftb/sd/drawers_book"'),t.runCommand("gamerule recipesunlock false"),t.runCommand("scoreboard objectives add ftb_sd:jig_computer.addon_stats dummy"),t.addTag("ftb_sd_joined"))})),t.runInterval((()=>{e.getAllPlayers().forEach((e=>{const t=e.dimension.getBlockFromRay(e.getHeadLocation(),e.getViewDirection(),{maxDistance:5,includeTags:["ftb_sd:storage_drawer"]});if(!t)return;const o=t.block.permutation.getState("minecraft:facing_direction");if(u(t.face)!==o)return;const n=e.dimension.getEntitiesAtBlockLocation(t.block)[0];if(!n)return;const r=v(n);if(!r)return;let i;if(r instanceof f){const t=r.getHandFromBlock(r.rayCastBlock(e));i=r.getStackInDrawer(n,t)}else i=r.getStackInDrawer(n,y.MAIN_HAND);i&&e.onScreenDisplay.setActionBar({text:k(i.typeId)})}))}),5),e.beforeEvents.playerBreakBlock.subscribe((e=>{for(const t of _)if(t.isBlockDrawer(e.block)){if(!t.canBreak(e.player,e.block))return e.cancel=!0,void e.player.sendMessage({translate:"ftb_sd.drawer_not_empty"});t.onBreak(e.player,t.getEntityFromBlock(e.block))}})),t.afterEvents.scriptEventReceive.subscribe((e=>{if("ftb:storage_drawer_tick"!==e.id)return;if(!e.sourceEntity)return;if("ftb_sd:drawer_contoller"===e.sourceEntity?.typeId){const t=e.sourceEntity.dimension.getBlock(e.sourceEntity.location);return t&&(e.sourceEntity.dimension.setBlockType(t.location,t.typeId),e.sourceEntity.dimension.setBlockPermutation(t.location,t.permutation)),void e.sourceEntity.remove()}const t=v(e.sourceEntity);t&&t.onEntityTick(e.sourceEntity)})),e.beforeEvents.itemUse.subscribe((e=>{for(const t of p)if(t.isItemValid(e.itemStack))return void t.onUseItem(e.source,e.itemStack)}));const I=new Map;function D(e){let t=Math.sqrt(e.x*e.x+e.y*e.y+e.z*e.z);return b(e.x/t,e.y/t,e.z/t)}function h(e,t){return b(e.x-t.x,e.y-t.y,e.z-t.z)}function j(e){const t=e.getComponent(r.Inventory);return t?.container}e.beforeEvents.itemUseOn.subscribe((e=>{if(I.has(e.source)){if(I.get(e.source)+5>=t.currentTick)return}I.set(e.source,t.currentTick);const o=e.blockFace,n=e.block.permutation.getState("minecraft:cardinal_direction");e.source.isSneaking&&"minecraft:hopper"===e.itemStack?.typeId||_.forEach((r=>{if(r.isBlockDrawer(e.block)&&u(o)===n)return e.cancel=!0,void t.run((()=>{r.onUse(e.block,e.source)}))}))})),e.afterEvents.entityHitBlock.subscribe((e=>{if(!(e.damagingEntity instanceof o))return;const t=e.damagingEntity;for(const o of _)if(o.isBlockDrawer(e.hitBlock)){if(e.hitBlock.permutation.getState("minecraft:cardinal_direction")!==u(e.blockFace))return;o.onHit(t,e.blockFace,e.hitBlock);break}})),e.afterEvents.pistonActivate.subscribe((e=>{const o=e.piston.getAttachedBlocks();if(0===o.length)return;const r=e.isExpanding?D(h(o[0],e.block.location)):D(h(e.piston.block.location,o[0]));o.forEach((o=>{const i=(c=r,b((s=o).x+c.x,s.y+c.y,s.z+c.z));var s,c;t.runTimeout((()=>{const t=e.piston.block.dimension.getBlock(i);_.forEach((e=>{const o=t.dimension.getEntities({location:t.location,maxDistance:1.5});e.isBlockDrawer(t)&&(t.dimension.spawnItem(new n(t.typeId),t.location),t.setType("minecraft:air"));for(const t of o)if(e.isDrawerEntity(t)){e.onBreak(void 0,t);const o=t.dimension.getBlock(t.location);e.isBlockDrawer(o)&&(o.dimension.spawnItem(new n(o.typeId),o.location),o.setType("minecraft:air"))}}))}),2)}))}));export{_ as ALL_DRAWER_TYPES,B as DEBUG,j as getEntityContainer,v as getType};
