XDjackieXD

gamemode.lua

Jul 2nd, 2016
34
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local shell=require("shell")
  2.  
  3. local component = require("component")
  4. local debug = component.debug
  5.  
  6. local args, options = shell.parse(...)
  7.  
  8.  
  9. -------------- Functions --------------
  10.  
  11. local function setGamemode(player, gamemode)
  12.   if player ~= nil and gamemode ~= nil then
  13.     if gamemode == "survival" or gamemode == "creative" or gamemode == "adventure" or gamemode == "spectator" then
  14.       local p1 = debug.getPlayer(player)
  15.       local players = debug.getPlayers()
  16.       local f1 = false, false
  17.  
  18.       for i, v in ipairs(players) do
  19.         if v == player then f1 = true end
  20.       end
  21.  
  22.       if f1 then
  23.         p1.setGameType(gamemode)
  24.       else
  25.         print("Player not found!")
  26.       end
  27.     else
  28.       print("Invalid gamemode!")
  29.     end
  30.   else
  31.     print("Player or gamemode can't be nil!")
  32.   end
  33. end
  34.  
  35. local function getGamemode(player)
  36.   if player ~= nil then
  37.     local p1 = debug.getPlayer(player)
  38.     local players = debug.getPlayers()
  39.     local f1 = false, false
  40.  
  41.     for i, v in ipairs(players) do
  42.       if v == player then f1 = true end
  43.     end
  44.  
  45.     if f1 then
  46.       return p1.getGameType()
  47.     else
  48.       print("Player not found!")
  49.       return nil
  50.     end
  51.   else
  52.     print("Player can't be nil!")
  53.     return nil
  54.   end
  55. end
  56.  
  57. -------------- Main Code --------------
  58.  
  59. if #args == 1 then
  60.   local gm = getGamemode(args[1])
  61.   if gm ~= nil then
  62.     print(gm)
  63.   end
  64. elseif #args == 2 then
  65.   if args[2] == "0" then
  66.     args[2] = "survival"
  67.   elseif args[2] == "1" then
  68.     args[2] = "creative"
  69.   elseif args[2] == "2" then
  70.     args[2] = "adventure"
  71.   elseif args[2] == "3" then
  72.     args[2] = "spectator"
  73.   end
  74.   setGamemode(args[1], args[2])
  75. else
  76.   print("Usage:")
  77.   print("gamemode <username> [gamemode]")
  78.   return 1
  79. end
Add Comment
Please, Sign In to add comment