import*as mc from"@minecraft/server";import*as vector from"../utils/vector";import*as blockEvents from"../blockEvents";const toBlockRot={0:"south",90:"west","-270":"west",180:"north","-180":"north","-90":"east",270:"east"},SandBlockStates=[{"minecraft:cardinal_direction":"north","hf_mzs:corner":!1,"hf_mzs:north_east":!1,"hf_mzs:north_west":!1,"hf_mzs:south_east":!1,"hf_mzs:south_west":!1},{"minecraft:cardinal_direction":"east","hf_mzs:corner":!1,"hf_mzs:north_east":!1,"hf_mzs:north_west":!1,"hf_mzs:south_east":!1,"hf_mzs:south_west":!1},{"minecraft:cardinal_direction":"north","hf_mzs:corner":!0,"hf_mzs:north_east":!0,"hf_mzs:north_west":!1,"hf_mzs:south_east":!1,"hf_mzs:south_west":!1},{"minecraft:cardinal_direction":"north","hf_mzs:corner":!0,"hf_mzs:north_east":!1,"hf_mzs:north_west":!0,"hf_mzs:south_east":!1,"hf_mzs:south_west":!1},{"minecraft:cardinal_direction":"north","hf_mzs:corner":!0,"hf_mzs:north_east":!1,"hf_mzs:north_west":!1,"hf_mzs:south_east":!0,"hf_mzs:south_west":!1},{"minecraft:cardinal_direction":"north","hf_mzs:corner":!0,"hf_mzs:north_east":!1,"hf_mzs:north_west":!1,"hf_mzs:south_east":!1,"hf_mzs:south_west":!0}];function cycleSandState(t){let e=0;if(t.permutation.getState("hf_mzs:corner")){t.permutation.getState("hf_mzs:north_east")&&(e=2);t.permutation.getState("hf_mzs:north_west")&&(e=3);t.permutation.getState("hf_mzs:south_east")&&(e=4);t.permutation.getState("hf_mzs:south_west")&&(e=5)}else{const a=t.permutation.getState("minecraft:cardinal_direction");"north"===a||"south"===a?e=0:"east"!==a&&"west"!==a||(e=1)}return SandBlockStates[(e+1)%SandBlockStates.length]}export function useSandRake(t){const e=t.itemStack,a=t.source,n=t.block;if("hf_mzs:sand_rake"===e.typeId&&("minecraft:sand"===n.typeId||"minecraft:red_sand"===n.typeId||"hf_mzs:raked_sand"===n.typeId||"hf_mzs:red_raked_sand"===n.typeId)){if(a.lastTick||(a.lastTick=0),mc.system.currentTick-a.lastTick<=5)return;a.lastTick=mc.system.currentTick,mc.system.run((()=>{const t=90*Math.round(a.getRotation().y/90);if(a.isSneaking)switch(n.typeId){case"minecraft:sand":n.setPermutation(mc.BlockPermutation.resolve("hf_mzs:raked_sand",{"minecraft:cardinal_direction":toBlockRot[t]})),n.dimension.playSound("use.gravel",n.location,{volume:1,pitch:1}),blockEvents.rakedSandUpdate(n,a);break;case"minecraft:red_sand":n.setPermutation(mc.BlockPermutation.resolve("hf_mzs:red_raked_sand",{"minecraft:cardinal_direction":toBlockRot[t]})),n.dimension.playSound("use.gravel",n.location,{volume:1,pitch:1}),blockEvents.rakedSandUpdate(n,a);break;case"hf_mzs:raked_sand":n.setPermutation(mc.BlockPermutation.resolve("hf_mzs:raked_sand",cycleSandState(n))),n.dimension.playSound("use.gravel",n.location,{volume:1,pitch:1});break;case"hf_mzs:red_raked_sand":n.setPermutation(mc.BlockPermutation.resolve("hf_mzs:red_raked_sand",cycleSandState(n))),n.dimension.playSound("use.gravel",n.location,{volume:1,pitch:1})}else switch(n.typeId){case"minecraft:sand":case"hf_mzs:raked_sand":n.setPermutation(mc.BlockPermutation.resolve("hf_mzs:raked_sand",{"minecraft:cardinal_direction":toBlockRot[t]})),n.dimension.playSound("use.gravel",n.location,{volume:1,pitch:1}),blockEvents.rakedSandUpdate(n,a);break;case"minecraft:red_sand":case"hf_mzs:red_raked_sand":n.setPermutation(mc.BlockPermutation.resolve("hf_mzs:red_raked_sand",{"minecraft:cardinal_direction":toBlockRot[t]})),n.dimension.playSound("use.gravel",n.location,{volume:1,pitch:1}),blockEvents.rakedSandUpdate(n,a)}if(a.getGameMode()!=mc.GameMode.creative)if(e.getComponent("minecraft:durability").damage<e.getComponent("minecraft:durability").maxDurability-1){const t=e.clone();t.getComponent("minecraft:durability").damage+=1,a.getComponent("minecraft:equippable").setEquipment(mc.EquipmentSlot.Mainhand,t)}else a.playSound("random.break",{volume:1,pitch:1}),a.getComponent("minecraft:equippable").setEquipment(mc.EquipmentSlot.Mainhand,null)}))}}