import { World, Commands } from "mojang-minecraft"

//Prefix for all custom commands
const commandPrefix = "!!"

//The function that will handle all custom command inputs and outputs
function customCommand(command, args, msg, player) {
  
  let dimension = World.getDimension('overworld')
  
   switch(command) {
     case "spawn":
      Commands.run(`execute "${player.nameTag}" ~~~ tp @s 60 100 60`, dimension)
      break;
    case "info":
      Commands.run(`execute "${player.nameTag}" ~~~ function info`, dimension)
      break;
     case "gmc":
      Commands.run(`gamemode 1 "${player.nameTag}"`, dimension)
      break;
    case "gms":
      Commands.run(`gamemode 0 "${player.nameTag}"`, dimension)
      break;
    case "discord":
      Commands.run(`execute "${player.nameTag}" ~~~ tellraw @s {\"rawtext\":[{\"text\":\"§9§lJoin My Discord!\n§r§7discord.gg/9S6K935\"}]}`, dimension)
      break;
     default:
     Commands.run(`say error! ${command} is not a valid command!`, dimension)
   }
}

//Checks if a command was run (checks for the prefix)
World.events.beforeChat.subscribe(msg => {
  if(!msg.message.startsWith(commandPrefix)) return
   const args = msg.message.slice(commandPrefix.length).trim().split(/\s+/);
  msg.cancel = true
  const player = msg.sender
  const command = args.shift()
  customCommand(command, args, args.join(" "), player)
})