import{EntityComponentTypes as t,ItemStack as e,BlockComponentTypes as o}from"@minecraft/server";import{getBlockOffset as n,vec3 as r}from"../utils.js";import{DrawerType as i}from"../base/drawer_type.js";import{Hand as s}from"../render.js";import{ITEM_COUNT_KEY as a}from"../drawers.js";import{getType as m}from"../main.js";class p extends i{constructor(){super(...arguments),this.getEntityTypeId="ftb_sd:drawer_contoller",this.getBlockTag="ftb:drawer_controller",this.shouldSneakCheck=!1,this.tick=t=>{this.transferAllItems(t,this.getHopperMinecartOrHopperContainer(t.dimension,n(t.location,r(0,1,0)),0),!1),this.transferAllItems(t,this.getHopperMinecartOrHopperContainer(t.dimension,n(t.location,r(0,0,1)),2),!1),this.transferAllItems(t,this.getHopperMinecartOrHopperContainer(t.dimension,n(t.location,r(0,0,-1)),3),!1),this.transferAllItems(t,this.getHopperMinecartOrHopperContainer(t.dimension,n(t.location,r(1,0,0)),4),!1),this.transferAllItems(t,this.getHopperMinecartOrHopperContainer(t.dimension,n(t.location,r(-1,0,0)),5),!1)},this.onUse=(e,o)=>{const n=o.getComponent(t.Inventory).container;this.transferAllItems(e,n,!0)},this.canBreak=(t,e)=>!0,this.onHit=(t,e,o)=>{},this.onPlace=(t,e)=>{},this.onBreak=(t,e)=>{},this.getKeyForHand=t=>a,this.getSlotForHand=t=>0}transferAllItems(t,e,o){this.transferItems(t,e,s.MAIN_HAND,o),this.transferItems(t,e,s.OFF_HAND,o)}transferItems(t,o,n,r){if(void 0===o)return;const i=Array();for(const s of t.dimension.getEntities({families:["ftb_sd:has_items"],maxDistance:8,location:t.location}))for(let t=0;t<o.size;t++){const a=o.getItem(t),p=m(s);if(p.isSameStackAsDrawer(s,n,a)&&a.amount>=1){i.push(a.typeId);let m=r?a.amount:1;const c=p.addItem(new e(a.typeId,m),n,s,s.dimension.getBlock(s.location)),l=a.amount-(m+c);if(m===c)continue;if(0===l?o.setItem(t,void 0):o.setItem(t,new e(a.typeId,l)),!r)return}}this.transferItemsToHopper(o,t,i)}transferItemsToHopper(t,n,r){const i=n.below(1);if("minecraft:hopper"===i?.typeId){const n=i.getComponent(o.Inventory);if(void 0!==n)for(let o=0;o<t.size;o++){const i=t.getItem(o);if(void 0!==i){if(r.includes(i.typeId))continue;const s=i.clone();s.amount=1;if(void 0===n.container.addItem(s)){const n=i.amount-1;0===n?t.setItem(o,void 0):t.setItem(o,new e(i.typeId,n))}}}}}}export{p as DrawerController};
