import*as mc from"@minecraft/server";import*as vector from"../utils/vector";import{toStructureRot}from"../structure/structureManager";const rotToDir=new Map([[0,{x:0,y:0,z:-1}],[360,{x:0,y:0,z:-1}],[90,{x:1,y:0,z:0}],[-270,{x:1,y:0,z:0}],[180,{x:0,y:0,z:1}],[-180,{x:0,y:0,z:1}],[-90,{x:-1,y:0,z:0}],[270,{x:-1,y:0,z:0}]]);export function registerComponent(o){o.blockComponentRegistry.registerCustomComponent("hf_mzs:gong",{onPlayerInteract(o){const t=o.block.dimension.getEntities({maxDistance:2,families:["hf_mzs_gong"],location:o.block.location})[0];if(void 0!==t){if(t.getProperty("hf_mzs:ringing"))return;t.triggerEvent("ring"),t.runCommand("playsound hf_mzs:misc.gong @a[r=16] ~~~"),mc.system.runTimeout((()=>{t.isValid()&&t.setProperty("hf_mzs:ringing",!1)}),1)}}})}export function onUseOn(o){const{source:t,block:r,faceLocation:e}=o,c=vector.add(r.location,e),i=90*Math.round(t.getRotation().y/90)+180;t.dimension.getBlock(c).isAir&&t.dimension.getBlock(vector.add(c,vector.up)).isAir&&t.dimension.getBlock(vector.add(c,vector.multiply(vector.up,2))).isAir&&t.dimension.getBlock(vector.add(c,vector.cross(rotToDir.get(i),vector.up))).isAir&&t.dimension.getBlock(vector.add(vector.add(c,vector.up),vector.cross(rotToDir.get(i),vector.up))).isAir&&t.dimension.getBlock(vector.add(vector.add(c,vector.multiply(vector.up,2)),vector.cross(rotToDir.get(i),vector.up))).isAir&&t.dimension.getBlock(vector.add(c,vector.cross(rotToDir.get(i),vector.down))).isAir&&t.dimension.getBlock(vector.add(vector.add(c,vector.up),vector.cross(rotToDir.get(i),vector.down))).isAir&&t.dimension.getBlock(vector.add(vector.add(c,vector.multiply(vector.up,2)),vector.cross(rotToDir.get(i),vector.down))).isAir?mc.system.run((()=>{let r;r=270===i||-90===i||-270===i||-180===i||180===i?vector.add(c,vector.cross(rotToDir.get(i),vector.up)):vector.add(c,vector.cross(rotToDir.get(i),vector.down)),mc.world.structureManager.place("hf_mzs:gong",t.dimension,r,{rotation:toStructureRot.get(i)}),o.source.runCommand(`/playsound random.anvil_land @a[r=16] ${vector.string(r)} 1 0.5`)})):o.cancel=!0}