import { world, system, BlockPermutation } from "@minecraft/server";

export default function removejam(jamblock, jamtype) {
  let loottable, newPermutation;
  try {
    loottable = jamtype.substring(9).slice(0, jamtype.length - 15);
    if (jamblock.permutation.getState("float_fc:jars") < 2) {
      jamblock.dimension.runCommand(
        `setblock ${jamblock.location.x} ${jamblock.location.y} ${jamblock.location.z} air destroy`
      );
    } else {
      newPermutation = BlockPermutation.resolve(jamblock.typeId, {
        "float_fc:jars": jamblock.permutation.getState("float_fc:jars") - 1,
        "minecraft:cardinal_direction": jamblock.permutation.getState("minecraft:cardinal_direction"),
      });
      if (newPermutation) {
        jamblock.setPermutation(newPermutation);
      }
      newPermutation = undefined;
      jamblock.dimension.runCommand(
        `loot spawn ${jamblock.location.x} ${jamblock.location.y} ${jamblock.location.z} loot "float/f/jam/single/${loottable}"`
      );
    }
  } catch {}
}
