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

system.runInterval(() => {
  let players = world.getAllPlayers();

  players.forEach((player) => {
    const container = player.getComponent("inventory")?.container;
    if (!container) return;

    for (let i = 0; i < container.size; i++) {
      const item = container.getItem(i);
      if (!item) continue;

      // Replace Tinker Table Top with Bottom
      if (item.typeId === "pb_gat:mb_tinker_table_top") {
        const newItem = new ItemStack("pb_gat:mb_tinker_table_bottom", item.amount);
        container.setItem(i, newItem);
      }

      // Replace Upgrade Puncher Top with Bottom
      if (item.typeId === "pb_gat:mb_upgrade_puncher_top") {
        const newItem = new ItemStack("pb_gat:mb_upgrade_puncher_bottom", item.amount);
        container.setItem(i, newItem);
      }
    }
  });
});
