Advertisement
shaiof

Offedit Map Editor - Shaio Fencski

Feb 15th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 16.29 KB | None | 0 0
  1. ----- Configuration -----
  2. commands = {
  3.     objectCreate = 'mcreate',
  4.     objectDestroy = 'mdestroy',
  5.     objectScale = 'mscale',
  6.     objectCollision = 'mcol',
  7.     objectSave = 'msave',
  8.     objectSelect = 'msel',
  9.     objectColor = 'mcolor',
  10.     moveX = 'ox',
  11.     moveY = 'oy',
  12.     moveZ = 'oz',
  13.     rotateX = 'rx',
  14.     rotateY = 'ry',
  15.     rotateZ = 'rz',
  16.     saveMap = 'savemap',
  17.     loadMap = 'loadmap',
  18.     userPanel = 'maps',
  19.     adminPanel = 'madmin',
  20.     deleteMap = 'mdelete',
  21.     copyMap = 'mcopy'
  22. }
  23.  
  24. jsonMapDirectory = '/maps/'-- location for saving and loading json maps
  25. toggleMouseKey = 'mouse3'-- this only works if axisMovement is enabled.
  26. axisMovement = true-- Allow people to move objects with dx axis bars.
  27.  
  28. -- MySQL Configuration --
  29. sqlEnabled = true -- if set to false it will only use json saving method.
  30. db = {
  31.     host = 'fencski.ga',
  32.     username = 'JohnFlower',
  33.     password = 'flowerserver',
  34.     database = 'JohnFlower_Maps'
  35. }
  36. -- Note that MySQL is no longer used and MariaDB is used as an update to MySQL for security purposes.
  37. -- Syntax must be changed if using MySQL instead of MariaDB.
  38. -------------------------
  39. -- Error Codes ----------
  40. debugMessagesEnabled = true-- Set to false if you do not want debug messages.
  41. ec = {
  42.     [111] = '[Offedit] Message:111 | MySQL Successfully Connected, Host: %r1% Database: %r2%',
  43.     [112] = '[Offedit] Message:112 | Map Loaded (JSON): %r1%',
  44.     [113] = '[Offedit] Message:113 | Map Contains No Data: %r1%',
  45.     [114] = '[Offedit] Message:114 | Map Doesnt Exist: %r1%',
  46.     [115] = '[Offedit] Message:115 | Map Saved (JSON): %r1%',
  47.     [116] = '[Offedit] Message:116 | Map Saved (SQL): %r1%',
  48.     [117] = '[Offedit] Message:117 | Map Conversion (JSON to SQL): %r1%',
  49.     [118] = '[Offedit] Message:118 | Map Conversion (SQL to JSON): %r1%',
  50.     [119] = '[Offedit] Message:119 | Map Loaded (SQL): %r1%',
  51.     [120] = '[Offedit] Message:120 | Map Deleted (SQL): %r1%',
  52.     [121] = '[Offedit] Message:121 | Map Deleted (JSON): %r1%',
  53.     [122] = '[Offedit] Message:122 | Map Copied (JSON): %r1% NEW: %r2%',
  54.     [123] = '[Offedit] Message:123 | Map Copied (SQL): %r1% NEW: %r2%'
  55. }
  56. -------------------------
  57. local objects = {}
  58. local selected = {}
  59.  
  60. function debug(...)
  61.     if debugMessagesEnabled then
  62.         local textTable = {}
  63.         local main = textTable[1]
  64.         if string.find(main,'%r1%',1,true) then
  65.             main = string.gsub(main,'%r1%',textTable[2])
  66.         end
  67.         if string.find(main,'%r2%',1,true) then
  68.             main = string.gsub(main,'%r2%',textTable[3])
  69.         end
  70.         outputDebugString(main)
  71.     end
  72. end
  73.  
  74. if sqlEnabled then
  75.     sql = dbConnect('mysql','dbname='..db.database..';host='..db.host,db.username,db.password,'share=1')
  76.     if sql then
  77.         debug(ec[111],db.host,db.database)
  78.     end
  79. end
  80.  
  81. local JSON = {}
  82. JSON.load = function(mapName,fileOnly)
  83.     if fileExists(jsonMapDirectory..string.lower(mapName)..'.json') then
  84.         local file = fileOpen(jsonMapDirectory..string.lower(mapName)..'.json')
  85.         local size = fileGetSize(file)
  86.         local buffer = fileRead(file,size)
  87.         local data = fromJSON(buffer)
  88.         fileClose(file)
  89.         if data then
  90.             if not fileOnly then
  91.                 objects = data
  92.                 for i=1,#data do
  93.                     createObjects(data[i])
  94.                 end
  95.             end
  96.             debug(ec[112],mapName)
  97.             return data
  98.         else
  99.             debug(ec[113],mapName)
  100.             return false
  101.         end
  102.     else
  103.         debug(ec[114],mapName)
  104.     end
  105. end
  106.  
  107. JSON.save = function(fileName,data,deleteOld)
  108.     if fileName and data and data[1] then
  109.         if deleteOld then
  110.             if fileExists(jsonMapDirectory..string.lower(fileName)..'.json') then fileDelete(jsonMapDirectory..string.lower(fileName)..'.json') end
  111.         end
  112.         local file = fileCreate(jsonMapDirectory..string.lower(fileName)..'.json')
  113.         local content = toJSON(data)
  114.         fileWrite(file,content)
  115.         debug(ec[115],fileName)
  116.         fileClose(file)
  117.     end
  118. end
  119.  
  120. function sqlExec(mapName,data)
  121.     if mapName and data and data[1] then
  122.         dbExec(sql,'CREATE TABLE IF NOT EXISTS `'..string.lower(mapName)..'` (`index` INT,`objectID` INT,`type` VARCHAR(255),`x` INT,`y` INT,`z` INT,`rx` INT,`ry` INT,`rz` INT,`scale` INT,`col` VARCHAR(255),`owner` VARCHAR(255))')
  123.         dbExec(sql,'TRUNCATE `'..string.lower(mapName)..'`')
  124.         for i=1,#data do
  125.             local o = data[i]
  126.             dbExec(sql,'INSERT INTO `'..string.lower(mapName)..'` VALUES ('..o.index..','..o.object..',"'..o.type..'",'..o.x..','..o.y..','..o.z..','..o.rx..','..o.ry..','..o.rz..','..o.scale..',"'..o.col..'","'..o.owner..'")')
  127.         end
  128.         debug(ec[116],mapName)
  129.     end
  130. end
  131.  
  132. function createObjects(data)
  133.     if data.type == 'vehicle' then
  134.         data.object = createVehicle(data.objectID,data.x,data.y,data.z,data.rx,data.ry,data.rz)
  135.     elseif data.type == 'ped' then
  136.         data.object = createPed(data.objectID,data.x,data.y,data.z,data.rz)
  137.     elseif data.type == 'object' then
  138.         data.object = createObject(data.objectID,data.x,data.y,data.z,data.rx,data.ry,data.rz)
  139.     end
  140.     triggerClientEvent('updateGridlines',root,data.object,player)
  141. end
  142.  
  143. function createElement(player,...)
  144.     local data = {...}
  145.     local self = {}
  146.     if data[1] and selected[player] < 1 then
  147.         if tonumber(data[2]) and tonumber(data[3]) and tonumber(data[4]) and tonumber(data[5]) and tonumber(data[6]) and tonumber(data[7]) then
  148.             self.objectID = data[1]
  149.             local px,py,pz = getElementPosition(player)
  150.             local prx,pry,prz = getElementRotation(player)
  151.             self.x = data[2] or px+2
  152.             self.y = data[3] or py
  153.             self.z = data[4] or pz
  154.             self.rx = data[5] or 0
  155.             self.ry = data[6] or 0
  156.             self.rz = data[7] or prz
  157.             self.color = {255,255,255,255}
  158.             self.col = data[8] or true
  159.             self.scale = data[9] or 1
  160.             self.owner = getPlayerSerial(player) or 'Console'
  161.            
  162.             for i,veh in pairs(getValidVehicleModels()) do
  163.                 if self.objectID == veh then
  164.                     self.type = 'vehicle'
  165.                 end
  166.             end
  167.             for k,ped in pairs(getValidPedModels()) do
  168.                 if self.objectID == ped then
  169.                     self.type = 'ped'
  170.                 end
  171.             end
  172.             if not self.type then self.type = 'object' end
  173.            
  174.             startObjectCreation(player,self)
  175.             table.insert(objects,self)
  176.             return self
  177.         end
  178.     end
  179. end
  180.  
  181. function startObjectCreation(player,data)
  182.     if data.objectID then
  183.         for i=1,#objects do
  184.             if data == objects[i] then
  185.                 selected[player] = i
  186.             end
  187.         end
  188.         createObjects(data)
  189.     end
  190. end
  191.  
  192. function JSONtoSQL(mapName)
  193.     if sql then
  194.         local data = JSON.load(mapName,true)
  195.         if data then
  196.             sqlExec(mapName,data)
  197.             debug(ec[117],mapName)
  198.         end
  199.     end
  200. end
  201.  
  202. function SQLtoJSON(mapName)
  203.     if sql and mapName end
  204.         local query = dbQuery(sql,'SELECT * FROM `'..mapName..'`')
  205.         local result = dbPoll(query,-1)
  206.         if result then
  207.             JSON.save(mapName,result,true)
  208.             debug(ec[118],mapName)
  209.         end
  210.     end
  211. end
  212.  
  213. setTimer(function()
  214.     for i,plr in pairs(getElementsByType('player')) do
  215.         if not selected[plr] then selected[plr] = 0 end
  216.         if selected[plr] and selected[plr] == 0 then
  217.             triggerClientEvent('updateGridlines',root,root,plr)
  218.         end
  219.     end
  220. end,1000,0)
  221.  
  222. function saveLoad(player,name,direction)
  223.     if player and name and direction then
  224.         if direction == 'save' then
  225.             if sql then
  226.                 sqlExec(string.lower(name),objects)
  227.             else
  228.                 JSON.save(string.lower(name),objects,true)
  229.             end
  230.         elseif direction == 'load' then
  231.             if sql then
  232.                 local query = dbQuery(sql,'SELECT * FROM `'..string.lower(name)..'`')
  233.                 local result = dbPoll(query,-1)
  234.                 if result then
  235.                     objects = result
  236.                     for i=1,#result do
  237.                         createObjects(result[i])
  238.                     end
  239.                     debug(ec[119],name)
  240.                 else
  241.                     JSON.load(string.lower(name),false)
  242.                 end
  243.             else
  244.                 JSON.load(string.lower(name),false)
  245.             end
  246.         end
  247.     end
  248. end
  249.  
  250. local c = commands
  251. function runCommand(player,cmd,...)
  252.     local data = {...}
  253.     if cmd == c.objectCreate then
  254.         createElement(player,...)
  255.     elseif cmd == c.objectDestroy then
  256.         if selected[player] > 0 and objects[selected[player]] then
  257.             if isElement(objects[selected[player]].object) then
  258.                 destroyElement(objects[selected[player]].object)
  259.             end
  260.             table.remove(objects,selected[player])
  261.             selected[player] = 0
  262.         end
  263.     elseif cmd == c.objectScale then
  264.         if selected[player] > 0 and data[1] and objects[selected[player]] then
  265.             if objects[selected[player]].type == 'object' then
  266.                 objects[selected[player]].scale = data[1]
  267.                 setObjectScale(objects[selected[player]].object,data[1])
  268.             end
  269.         end
  270.     elseif cmd == c.objectCollision then
  271.         if selected[player] > 0 and data[1] and objects[selected[player]] then
  272.             local value
  273.             if data[1] == '0' then value = false elseif data[1] == '1' then value = true end
  274.             value = value or false
  275.             setElementCollisionsEnabled(objects[selected[player]].object,value)
  276.             objects[selected[player]].col = value
  277.         end
  278.     elseif cmd == c.objectSave then
  279.         if selected[player] > 0 and objects[selected[player]] then
  280.             if isElementFrozen(objects[selected[player]].object) then
  281.                 setElementFrozen(objects[selected[player]].object,false)
  282.             end
  283.             selected[player] = 0
  284.         end
  285.     elseif cmd == c.objectSelect then
  286.         if data[1] then
  287.             if objects[tonumber(data[1])] and objects[tonumber(data[1])].object or data[1] == '0' then
  288.                 selected[player] = tonumber(data[1])
  289.                 if selected[player] ~= 0 then
  290.                     setElementFrozen(objects[selected[player]].object,true)
  291.                     triggerClientEvent('updateGridlines',root,objects[selected[player]].object,player)
  292.                 end
  293.             end
  294.         end
  295.     elseif cmd == c.objectColor then
  296.         if selected[player] > 0 and data[1] and data[2] and data[3] and objects[selected[player]] then
  297.             if objects[selected[player]].type == 'vehicle' then
  298.                 if data[4] and data[5] and data[6] then
  299.                     setVehicleColor(objects[selected[player]].object,data[1],data[2],data[3],data[4],data[5],data[6])
  300.                 else
  301.                     setVehicleColor(objects[selected[player]].object,data[1],data[2],data[3])
  302.                 end
  303.             elseif objects[selected[player]].type == 'object' then
  304.                 local alpha
  305.                 if data[4] then alpha = tonumber(data[4]) else alpha = 255 end
  306.                 local data[1],data[2],data[3] = tonumber(data[1]),tonumber(data[2]),tonumber(data[3])
  307.                 if data[1] >= 255 then data[1] == 255 elseif data[1] <= 0 then data[1] = 0 end
  308.                 if data[2] >= 255 then data[2] == 255 elseif data[2] <= 0 then data[2] = 0 end
  309.                 if data[3] >= 255 then data[3] == 255 elseif data[3] <= 0 then data[3] = 0 end
  310.                 if data[4] >= 255 then data[4] == 255 elseif data[4] <= 0 then data[4] = 0 end
  311.                 triggerClientEvent('setColor',root,objects[selected[player]].object,data[1],data[2],data[3],alpha)
  312.                 objects[selected[player]].color = {data[1],data[2],data[3],alpha}
  313.             end
  314.         end
  315.     elseif cmd == c.moveX then
  316.         if selected[player] > 0 and data[1] and objects[selected[player]] then
  317.             local px,py,pz = getElementPosition(objects[selected[player]].object)
  318.             setElementPosition(objects[selected[player]].object,px+data[1],py,pz)
  319.             objects[selected[player]].x = px+data[1]
  320.         end
  321.     elseif cmd == c.moveY then
  322.         if selected[player] > 0 and data[1] and objects[selected[player]] then
  323.             local px,py,pz = getElementPosition(objects[selected[player]].object)
  324.             setElementPosition(objects[selected[player]].object,px,py+data[1],pz)
  325.             objects[selected[player]].y = py+data[1]
  326.         end
  327.     elseif cmd == c.moveZ then
  328.         if selected[player] > 0 and data[1] and objects[selected[player]] then
  329.             local px,py,pz = getElementPosition(objects[selected[player]].object)
  330.             setElementPosition(objects[selected[player]].object,px,py,pz+data[1])
  331.             objects[selected[player]].z = pz+data[1]
  332.         end
  333.     elseif cmd == c.rotateX then
  334.         if selected[player] > 0 and data[1] and objects[selected[player]] then
  335.             local px,py,pz = getElementRotation(objects[selected[player]].object)
  336.             setElementRotation(objects[selected[player]].object,px+data[1],py,pz)
  337.             objects[selected[player]].rx = px+data[1]
  338.         end
  339.     elseif cmd == c.rotateY then
  340.         if selected[player] > 0 and data[1] and objects[selected[player]] then
  341.             local px,py,pz = getElementRotation(objects[selected[player]].object)
  342.             setElementRotation(objects[selected[player]].object,px,py+data[1],pz)
  343.             objects[selected[player]].ry = py+data[1]
  344.         end
  345.     elseif cmd == c.rotateZ then
  346.         if selected[player] > 0 and data[1] and objects[selected[player]] then
  347.             local px,py,pz = getElementRotation(objects[selected[player]].object)
  348.             setElementRotation(objects[selected[player]].object,px,py,pz+data[1])
  349.             objects[selected[player]].rz = pz+data[1]
  350.         end
  351.     elseif cmd == c.saveMap then
  352.         if selected[player] == 0 and data[1] and objects[1] then
  353.             saveLoad(player,data[1],'save')
  354.         end
  355.     elseif cmd == c.loadMap then
  356.         if selected[player] == 0 and data[1] then
  357.             saveLoad(player,data[1],'load')
  358.         end
  359.     elseif cmd == c.userPanel then
  360.        
  361.     elseif cmd == c.adminPanel then
  362.        
  363.     elseif cmd == c.deleteMap then
  364.         if data[1] then
  365.             if sql then
  366.                 local query = dbQuery(sql,'SELECT * FROM `'..string.lower(data[1])..'`')
  367.                 local result = dbPoll(query,-1)
  368.                 if result then
  369.                     dbExec(sql,'DROP TABLE `'..string.lower(data[1])..'`')
  370.                     debug(ec[120],data[1])
  371.                 end
  372.             else
  373.                 if fileExists(jsonMapDirectory..string.lower(data[1])..'.json') then
  374.                     fileDelete(jsonMapDirectory..string.lower(data[1])..'.json')
  375.                     debug(ec[121],data[1])
  376.                 end
  377.             end
  378.         end
  379.     elseif cmd == c.copyMap then
  380.         if data[1] and data[2] then
  381.             if sql then
  382.                 local query = dbQuery(sql,'SELECT * FROM `'..string.lower(data[1])..'`')
  383.                 local result = dbPoll(query,-1)
  384.                 if result then
  385.                     sqlExec(data[2],result)
  386.                     debug(ec[123],data[1],data[2])
  387.                 end
  388.             else
  389.                 if fileExists(jsonMapDirectory..string.lower(data[1])..'.json') then
  390.                     local file = fileOpen(jsonMapDirectory..string.lower(data[1])..'.json')
  391.                     local size = fileGetSize(file)
  392.                     local buffer = fileRead(file,size)
  393.                     fileClose(file)
  394.                     if fileExists(jsonMapDirectory..string.lower(data[2])..'.json') then fileDelete(jsonMapDirectory..string.lower(data[2])..'.json') end
  395.                     local newFile = fileCreate(jsonMapDirectory..string.lower(data[2])..'.json')
  396.                     fileWrite(newFile,buffer)
  397.                     debug(ec[122],data[1],data[2])
  398.                     fileClose(newFile)
  399.                 end
  400.             end
  401.         end
  402.     end
  403. end
  404.  
  405. for i=1,#c do
  406.     addCommandHandler(c[i],runCommand)
  407. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement