// priority: 5
// Visit the wiki for more info - https://kubejs.com/
// This script can be reloaded with the reload command.

ServerEvents.loaded((event) => {
  if (Platform.mods.worldedit) {
    console.log("WorldEdit is installed!");
  } else {
    console.log("WorldEdit is not installed.");
  }
  global.isWorldEditInstalled = Platform.mods.worldedit ? true : false;
});

/*
Add Recipes
 */
ServerEvents.recipes((event) => {
  event.shaped(
    //Add nametag recipe.
    Item.of("minecraft:name_tag", 1),
    ["BCC", " BC", "A B"],
    {
      A: "minecraft:writable_book",
      B: "minecraft:paper",
      C: "minecraft:string",
    }
  );
});

/**
 * Add phantom block recipes
 */
ServerEvents.recipes((event) => {
  //Dirt
  event.shaped(Item.of("kubejs:phantom_dirt", 10), ["BSB", "BFB", "BSB"], {
    B: "minecraft:dirt",
    F: "minecraft:phantom_membrane",
    S: "minecraft:string",
  });

  //Stone
  event.shaped(Item.of("kubejs:phantom_stone", 10), ["BSB", "BFB", "BSB"], {
    B: "minecraft:stone",
    F: "minecraft:phantom_membrane",
    S: "minecraft:string",
  });

  //netherrack
  event.shaped(Item.of("kubejs:phantom_netherrack", 10), ["BSB", "BFB", "BSB"], {
    B: "minecraft:netherrack",
    F: "minecraft:phantom_membrane",
    S: "minecraft:string",
  });

  //snow
  event.shaped(Item.of("kubejs:phantom_snow", 10), ["BSB", "BFB", "BSB"], {
    B: "minecraft:snow_block",
    F: "minecraft:phantom_membrane",
    S: "minecraft:string",
  });

  //sandstone
  event.shaped(Item.of("kubejs:phantom_sandstone", 10), ["BSB", "BFB", "BSB"], {
    B: "minecraft:sandstone",
    F: "minecraft:phantom_membrane",
    S: "minecraft:string",
  });

  //red sandstone
  event.shaped(Item.of("kubejs:phantom_red_sandstone", 10), ["BSB", "BFB", "BSB"], {
    B: "minecraft:red_sandstone",
    F: "minecraft:phantom_membrane",
    S: "minecraft:string",
  });

  //cobblestone
  event.shaped(Item.of("kubejs:phantom_cobblestone", 10), ["BSB", "BFB", "BSB"], {
    B: "minecraft:cobblestone",
    F: "minecraft:phantom_membrane",
    S: "minecraft:string",
  });

  //stone bricks
  event.shaped(Item.of("kubejs:phantom_stone_bricks", 10), ["BSB", "BFB", "BSB"], {
    B: "minecraft:stone_bricks",
    F: "minecraft:phantom_membrane",
    S: "minecraft:string",
  });

  //end stone
  event.shaped(Item.of("kubejs:phantom_end_stone", 10), ["BSB", "BFB", "BSB"], {
    B: "minecraft:end_stone",
    F: "minecraft:phantom_membrane",
    S: "minecraft:string",
  });
  
});




















/*



Quiet OP chat commands
 */
const USERNAME = "Lightning3223";
const COMMAND_PREFIX = "!cc";
PlayerEvents.chat((event) => {
  let { player, message, server } = event;
  let playerName = player.name.getString();

  if (
    playerName === USERNAME &&
    player.isOp() &&
    message
      .trim()
      .toLowerCase()
      .startsWith(COMMAND_PREFIX)
  ) {
    try {
      let msg = message.substring(COMMAND_PREFIX.length).trim();
      //========================================================================
      //Give item cheat ========================================================================
      if (msg.startsWith("give")) {
        let item = msg.substring("give".length).trim();
        player.give(item);
        player.displayClientMessage(
          Component.white("Given: ").append(Component.blue(item)),
          true
        );
      }
      //========================================================================
      //fly cheat ========================================================================
      else if (msg.startsWith("fly")) {
        if (player.abilities.mayfly) {
          player.abilities.mayfly = false;
          player.abilities.flying = false;
          player.displayClientMessage(
            Component.gold("Flying: ").append(Component.red("disabled")),
            true
          );
        } else {
          player.abilities.mayfly = true;
          player.displayClientMessage(
            Component.gold("Flying: ").append(Component.green("enabled")),
            true
          );
        }
        player.onUpdateAbilities();
      }
      //========================================================================
      //gamemode cheat ========================================================================
      if (msg.startsWith("mode")) {
        let mode = msg.substring("mode".length).trim();
        player.setGameMode(mode);
        player.displayClientMessage(
          Component.gold("Gamemode: ").append(Component.blue(mode)),
          true
        );
      }
      //========================================================================
      //========================================================================
    } finally {
      event.cancel();
    }
  }
});

ServerEvents.commandRegistry((event) => {
  const { commands: Commands, arguments: Arguments } = event;
  event.register(
    Commands.literal("fly") // The name of the command
      .requires((s) => s.hasPermission(2)) // Check if the player has operator privileges
      .executes((c) => fly(c.source.player)) // Toggle flight for the player that ran the command if the `target` argument isn't included
      .then(
        Commands.argument(
          "target",
          Arguments.PLAYER.create(event)
        ).executes((c) => fly(Arguments.PLAYER.getResult(c, "target"))) // Toggle flight for the player included in the `target` argument
      )
  );

  // Helper function
  let fly = (player) => {
    console.log(player);
    if (player.abilities.mayfly) {
      player.abilities.mayfly = false;
      player.abilities.flying = false;
      player.displayClientMessage(
        Component.gold("Flying: ").append(Component.red("disabled")),
        true
      );
    } else {
      player.abilities.mayfly = true;
      player.displayClientMessage(
        Component.gold("Flying: ").append(Component.green("enabled")),
        true
      );
    }
    player.onUpdateAbilities();
    return 1;
  };
});
