Advertisement
Bolodefchoco_LUAXML

[Tutorial] Submodes

May 30th, 2018
363
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.49 KB | None | 0 0
  1. local module = {
  2.     _NAME = "first"
  3. }
  4.  
  5. local mode = {}
  6.  
  7. do
  8.     local addImage = tfm.exec.addImage
  9.     tfm.exec.addImage = function(...)
  10.         local id = addImage(...)
  11.        
  12.         if id then
  13.             system.objects.image[id] = true
  14.         end
  15.  
  16.         return id
  17.     end
  18.  
  19.     local removeImage = tfm.exec.removeImage
  20.     tfm.exec.removeImage = function(id)
  21.         removeImage(id)
  22.  
  23.         system.objetos.imagem[id] = nil
  24.     end
  25.  
  26.     local addTextArea = ui.addTextArea
  27.     ui.addTextArea = function(id, ...)
  28.         addTextArea(id, ...)
  29.  
  30.         system.objetos.textarea[id] = true
  31.     end
  32.    
  33.     local addPopup = ui.addPopup
  34.     ui.addPopup = function(id, ...)
  35.         addPopup(id, ...)
  36.  
  37.         system.objetos.popup[id] = true
  38.     end
  39. end
  40.  
  41. eventOnModeChange = function()
  42.     for id in next, system.objects.image do
  43.         tfm.exec.removeImage(id)
  44.     end
  45.  
  46.     for id in next, system.objects.textarea do
  47.         ui.removeTextArea(id)
  48.     end
  49.    
  50.     for id in next, system.objects.popup do
  51.         ui.addPopup(id, "", nil, -1500, -1500)
  52.     end
  53.  
  54.     system.objetos = {
  55.         textarea = {},
  56.         popup = {},
  57.         imagem = {}
  58.     }  
  59.  
  60. end
  61.  
  62. local getModule = function(moduleName, triggerEvent)
  63.     local exists = not not mode[moduleName]
  64.  
  65.     if exists then
  66.         module._NAME = moduleName
  67.  
  68.         if triggerEvent then
  69.             eventOnModeChange()
  70.         end
  71.     end
  72.  
  73.     return exists
  74. end
  75.  
  76. system.admin = {}
  77.  
  78. local normalizePlayerName = function(playerName)
  79.     return playerName:lower():gsub("%a", string.upper, 1)
  80. end
  81.  
  82. local roomSettings= {}
  83. roomSettings[1] = {
  84.     char = "&",
  85.     execute = function(moduleName)
  86.         if moduleName then
  87.             getModule(moduleName, false)
  88.         end
  89.     end
  90. }
  91. roomSettings[2] = {
  92.     char = "@",
  93.     execute = function(playerName)
  94.         if playerName then
  95.             if #playerName > 2 then
  96.                 system.admin[normalizePlayerName(playerName)] = true
  97.             end
  98.         end
  99.     end
  100. }
  101.  
  102. local isRoom = string.byte(tfm.get.room.name, 2) ~= 3
  103. local roomAttributes = isRoom and string.match(tfm.get.room.name, "%*?#" .. module._NAME .. "%d+(.*)")
  104.  
  105. local setRoomSettings = function()
  106.     if roomAttributes then
  107.         for playerName in string.gmatch(roomAttributes, roomSettings[2].char .. "(%+?[a-zA-Z0-9_]+#%d%d%d%d)") do
  108.             roomSettings[2].execute(playerName)
  109.         end
  110.  
  111.         roomSettings[1].execute(string.match(roomAttributes, roomSettings[1].char .. "([%a_]+)"))
  112.    
  113.         if #roomSettings > 2 then
  114.             local characters = ""
  115.             for id, setting in next, roomSettings do
  116.                 if id > 2 then
  117.                     characters = characters .. setting.char
  118.                 end
  119.             end
  120.            
  121.             for char, value in string.gmatch(roomAttributes, "([" .. characters .. "])([^" .. characters .. "]+)") do
  122.                 for id, setting in next, roomSettings do
  123.                     if setting.char == char then
  124.                         setting.execute(string.gmatch(value, "%S+"))
  125.            
  126.                         break
  127.                     end
  128.                 end
  129.             end
  130.         end
  131.     end
  132. end
  133.  
  134. mode.first = {
  135.     time = 0,
  136.  
  137.     printf = function(where, who, message)
  138.         print(string.format("[#%s] [%s] %s", where, who, message))
  139.     end,
  140.  
  141.     reset = function()
  142.  
  143.         mode.first.time = 10000
  144.     end,
  145.  
  146.     init = function()
  147.  
  148.         tfm.exec.disableAutoShaman()
  149.        
  150.         mode.first.printf("chat", "game", "this sheeet started")
  151.  
  152.         mode.first.time = 10000
  153.     end,
  154.  
  155.     eventLoop = function(time)
  156.         mode.first.time = time
  157.     end,
  158.     eventNewGame = function()
  159.         mode.first.printf("tribe", "game", "yaaaay come to play guys")
  160.     end
  161. }
  162.  
  163. local officialEvents = {}
  164.  
  165. officialEvents.eventChatCommand = function(name, command)
  166.     if command == "modules" then
  167.         local modules = ""
  168.         for moduleName in next, mode do
  169.             modules = modules .. moduleName .. "\n"
  170.         end
  171.  
  172.         tfm.exec.chatMessage("The modules in this room are:\n" .. modules, name)
  173.     end
  174. end
  175.  
  176. local eventNames = {"eventLoop", "eventNewGame", "eventPlayerDied", "eventPlayerGetCheese", "eventPlayerVampire", "eventPlayerWon", "eventPlayerLeft", "eventEmotePlayed", "eventKeyboard", "eventMouse", "eventPopupAnswer", "eventTextAreaCallback", "eventChatCommand", "eventChatMessage", "eventSummoningStart", "eventSummoningEnd", "eventSummoningCancel", "eventNewPlayer", "eventPlayerRespawn", "eventColorPicked"}
  177.  
  178. local events = {}
  179.  
  180. foo = function() end
  181.  
  182. initialize = function(refresh)
  183.     events = {}
  184.  
  185.     for _, event in next, eventNames do
  186.  
  187.         events[event] = mode[module._NAME][event] or foo
  188.     end
  189.  
  190.     if refresh then
  191.         if mode[module._NAME].reset then
  192.             mode[module._NAME].reset()
  193.         end
  194.     end
  195.  
  196.     mode[module._NAME].init()
  197.  
  198.     table.foreach(tfm.get.room.playerList, eventNewPlayer)
  199. end
  200.  
  201. for _, event in next, eventNames do
  202.     _G[event] = function(...)
  203.         if officialEvents[event] then
  204.             officialEvents[event](...)
  205.         end
  206.  
  207.         events[event](...)
  208.     end
  209. end
  210.  
  211. setRoomSettings()
  212.  
  213. initialize(false)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement