import { BlockPermutation } from "@minecraft/server";

export default function slicepie(pieblock, pietype, source) {
  let loottable, newPermutation;
  try {
    loottable = pietype.substring(9).slice(0, pietype.length - 19);
    if (pieblock.permutation.getState("float_fc:slices") == 4) {
      pieblock.dimension.runCommand(
        `setblock ${pieblock.location.x} ${pieblock.location.y} ${pieblock.location.z} air replace`
      );

      source.playSound(`float_fc.slice_pie`);
      pieblock.dimension.runCommand(
        `loot spawn ${pieblock.location.x} ${pieblock.location.y} ${pieblock.location.z} loot "float/f/pie/${loottable}"`
      );
    } else {
      newPermutation = BlockPermutation.resolve(pietype, {
        "float_fc:slices": pieblock.permutation.getState("float_fc:slices") + 1,
        "minecraft:cardinal_direction": pieblock.permutation.getState("minecraft:cardinal_direction"),
      });
      if (newPermutation) {
        pieblock.setPermutation(newPermutation);
      }
      newPermutation = undefined;
      source.playSound(`float_fc.slice_pie`, pieblock.location);
      pieblock.dimension.runCommand(
        `loot spawn ${pieblock.location.x} ${pieblock.location.y} ${pieblock.location.z} loot "float/f/pie/${loottable}"`
      );
    }
  } catch {}
}
