Kradus
Wiek: 28 Na forum: 4169 dni Posty: 100
Nick w MP: Salldan
Piwa : 78
Witam. Posiadam skrypt na prywatne pojazdy kt?rego autorem jest Jacob. I mam zapytanie czy jest kto? kto by mi pom?g? to zautomatyzowa?? Chodzi o to aby tworzenie nie odbywa?o si? przy u?yciu admina tylko na zasadzie salonu lub fabryki. A opr?cz tego aby mo?na by?o no handlowa? nimi. O ile dobrze pami?tam ten skrypt jest w oop. Wklejam ca?o?? lecz je?li kto? chce z ?r?d?a to podaj? link: Klik!
config.lua
--[[
PRIVATE CARS
by WhoAmI ( aka Jacob )
You are able to modify the script , if you dont change author .
-- #config.lua
--]]--
--[[ change if you wish
availible values :
- MySQL
- SQL
--]]--
connectionType = "MySQL" ;
-- ONLY if you chose MySQL
MySQLdbhost = "" ; -- host
MySQLdbname = "" ; -- database name
MySQLdbuser = "" ; -- database user
MySQLdbpassword = "" ; -- user 's password
--end
-- ONLY if you chose SQL
--SQLfilename = "example.db"; -- choose name for databse file
--[[ WARNING! without ".db" at the and it won' t work .
-- REMEMBER ! you have to add following line to meta . xml :
--< file src = "YOUR_NAME.db" />
--AND create this file !
-- otherwise , SQL won 't work. REMEMBER!
--end ]]
--ACL groups allowed to create private car
aclGroups = {
"Admin",
"Moderator",
};
-- end
-- ACL groups allowed to see car info
aclGroupsInfo = {
"Everyone",
};
-- end
-- command name for car' s information
carInfoCommandName = "carinfo" ;
-- end
-- time after the vehicle will be fixed after explosion IN SECONDS
respawnTime = 10 ;
-- end
db.lua
--[[
PRIVATE CARS
by WhoAmI ( aka Jacob )
You are able to modify the script , if you don 't change author.
#db.lua
]]--
-- connection to database and creating tables if don' t exist
addEventHandler ( "onResourceStart" , resourceRoot ,
function ( )
if ( connectionType ~= "MySQL" and connectionType ~= "SQL" ) then -- if connectionType is bad
outputServerLog ( "[ERROR] PRIVATE CARS: Invalid connection type! [config.lua]" );
return;
end
if ( connectionType == "MySQL" ) then
connection = Connection ( "mysql" , "dbname=" .. MySQLdbname .. ";host=" .. MySQLdbhost , MySQLdbuser , MySQLdbpassword ); -- making connection if MySQL
tableFormula = "CREATE TABLE IF NOT EXISTS `vehicles` (`id` int(11) NOT NULL PRIMARY KEY AUTO_INCREMENT,`vehicle_model` int(11) NOT NULL,`x` float NOT NULL,`y` float NOT NULL,`z` float NOT NULL,`rotX` float NOT NULL,`rotY` float NOT NULL,`rotZ` float NOT NULL,`color` varchar(255) NOT NULL,`upgrades` varchar(255) NOT NULL,`acc_name` varchar(255) NOT NULL)" ; -- define formula to create tables in MySQL
elseif ( connectionType == "SQL" ) then
connection = Connection ( "sqlite" , SQLfilename ); -- making connection if SQL
tableFormula = "CREATE TABLE IF NOT EXISTS `vehicles` (`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `vehicle_model` INTEGER, `x` INTEGER, `y` INTEGER, `z` INTEGER, `rotX` INTEGER, `rotY` INTEGER, `rotZ` INTEGER, `color` VARCHAR, `upgrades` VARCHAR, `acc_name` VARCHAR )" ; -- define formula to create tables in SQL
end
if ( connection ) then -- checking if connection exist
outputServerLog ( "[INFO] PRIVATE CARS: Loaded connection! [" .. connectionType .. "]" );
local creatingTable = connection : exec ( tableFormula ); -- creating tables
if ( not creatingTable ) then -- if something went wrong
outputServerLog ( "[ERROR] PRIVATE CARS: Unable to create table! [" .. connectionType .. "]" );
end
else -- if connection doesn 't exist
outputServerLog ( "[ERROR] PRIVATE CARS: Unable to connect to database! [" .. connectionType .. "] [config.lua]" );
end
end
);
main_c.lua
gui = {
tab = {},
tabpanel = {},
edit = {},
button = {},
label = {}
};
screenX , screenY = guiGetScreenSize ( );
-- events
addEvent ( "car:showGUI" , true );
addEvent ( "car:message" , true );
-- events
addEventHandler ( "onClientResourceStart" , resourceRoot ,
function()
gui . tabpanel [ 1 ] = guiCreateTabPanel (( screenX - 332 )/ 2 , ( screenY - 238 )/ 2 , 332 , 238 , false );
guiSetVisible ( gui . tabpanel [ 1 ], false );
gui . tab [ 1 ] = guiCreateTab ( "Private cars" , gui . tabpanel [ 1 ]);
gui . label [ 1 ] = guiCreateLabel ( 5 , 1 , 316 , 35 , "Hi, mate! Here you are able to create private car. Nice, huh? Alright, type values you have to, in edit boxes below." , false , gui . tab [ 1 ]);
guiSetFont ( gui . label [ 1 ], "default-small" );
guiLabelSetHorizontalAlign ( gui . label [ 1 ], "center" , true );
guiLabelSetVerticalAlign ( gui . label [ 1 ], "center" );
gui . label [ 2 ] = guiCreateLabel ( 6 , 36 , 22 , 19 , "x:" , false , gui . tab [ 1 ]);
guiSetFont ( gui . label [ 2 ], "default-bold-small" );
guiLabelSetHorizontalAlign ( gui . label [ 2 ], "center" , true );
guiLabelSetVerticalAlign ( gui . label [ 2 ], "center" );
gui . label [ 3 ] = guiCreateLabel ( 6 , 60 , 22 , 19 , "y:" , false , gui . tab [ 1 ]);
guiSetFont ( gui . label [ 3 ], "default-bold-small" );
guiLabelSetHorizontalAlign ( gui . label [ 3 ], "center" , true );
guiLabelSetVerticalAlign ( gui . label [ 3 ], "center" );
gui . label [ 4 ] = guiCreateLabel ( 6 , 84 , 22 , 19 , "z:" , false , gui . tab [ 1 ]);
guiSetFont ( gui . label [ 4 ], "default-bold-small" );
guiLabelSetHorizontalAlign ( gui . label [ 4 ], "center" , true );
guiLabelSetVerticalAlign ( gui . label [ 4 ], "center" );
gui . edit [ 1 ] = guiCreateEdit ( 28 , 37 , 223 , 18 , "" , false , gui . tab [ 1 ]);
gui . edit [ 2 ] = guiCreateEdit ( 28 , 61 , 223 , 18 , "" , false , gui . tab [ 1 ]);
gui . edit [ 3 ] = guiCreateEdit ( 28 , 85 , 223 , 18 , "" , false , gui . tab [ 1 ]);
gui . label [ 5 ] = guiCreateLabel ( 6 , 113 , 99 , 21 , "Account name:" , false , gui . tab [ 1 ]);
guiSetFont ( gui . label [ 5 ], "default-bold-small" );
guiLabelSetHorizontalAlign ( gui . label [ 5 ], "center" , true );
guiLabelSetVerticalAlign ( gui . label [ 5 ], "center" );
gui . edit [ 4 ] = guiCreateEdit ( 107 , 115 , 215 , 19 , "" , false , gui . tab [ 1 ])
gui . button [ 1 ] = guiCreateButton ( 255 , 37 , 68 , 66 , "Get\ncurrent\nposition" , false , gui . tab [ 1 ]);
gui . button [ 2 ] = guiCreateButton ( 219 , 146 , 74 , 19 , "Color picker" , false , gui . tab [ 1 ]);
gui . button [ 3 ] = guiCreateButton ( 130 , 184 , 80 , 19 , "Cancel" , false , gui . tab [ 1 ]);
gui . button [ 4 ] = guiCreateButton ( 220 , 184 , 80 , 19 , "Create" , false , gui . tab [ 1 ]);
gui . label [ 6 ] = guiCreateLabel ( 6 , 144 , 69 , 21 , "Color:" , false , gui . tab [ 1 ]);
guiSetFont ( gui . label [ 6 ], "default-bold-small" );
guiLabelSetHorizontalAlign ( gui . label [ 6 ], "center" , false );
guiLabelSetVerticalAlign ( gui . label [ 6 ], "center" );
gui . edit [ 5 ] = guiCreateEdit ( 65 , 146 , 144 , 19 , "" , false , gui . tab [ 1 ]);
guiEditSetMaxLength ( gui . edit [ 5 ], 7 );
gui . label [ 7 ] = guiCreateLabel ( 299 , 144 , 24 , 21 , "C" , false , gui . tab [ 1 ]);
guiSetFont ( gui . label [ 7 ], "sa-gothic" );
gui . edit [ 6 ] = guiCreateEdit ( 65 , 184 , 55 , 18 , "" , false , gui . tab [ 1 ]);
guiEditSetMaxLength ( gui . edit [ 6 ], 3 );
gui . label [ 8 ] = guiCreateLabel ( 6 , 182 , 69 , 21 , "Model:" , false , gui . tab [ 1 ]);
guiSetFont ( gui . label [ 8 ], "default-bold-small" );
guiLabelSetHorizontalAlign ( gui . label [ 8 ], "center" , false );
guiLabelSetVerticalAlign ( gui . label [ 8 ], "center" );
bindKey ( "F7" , "down" ,
function ( )
triggerServerEvent ( "car:afterBind" , localPlayer );
end
);
addCommandHandler ( "privatecars" ,
function ( )
triggerServerEvent ( "car:afterBind" , localPlayer );
end
);
addEventHandler ( "onClientGUIClick" , gui . button [ 2 ],
function ( )
if ( type ( cp ) == "table" ) then return; end
cp = colorPicker . create ( 1 , "" , "" );
end ,
false );
addEventHandler ( "onClientGUIChanged" , resourceRoot ,
function ( )
if ( source : getType ( ) ~= "gui-edit" ) then return; end
guiSetText ( source , guiGetText ( source ): gsub ( " " , "" ) );
if ( source == gui . edit [ 5 ] ) then
local text = guiGetText ( source );
if ( text : find ( "#%x%x%x%x%x%x" ) ) then
local r , g , b = hex2rgb ( text );
guiLabelSetColor ( gui . label [ 7 ], r , g , b );
end
end
if ( source == gui . edit [ 1 ] or source == gui . edit [ 2 ] or source == gui . edit [ 3 ] or source == gui . edit [ 6 ] ) then
local text = guiGetText ( source );
if ( not tonumber ( text ) ) then
guiSetText ( source , text : sub ( 1 , #text - 1 ) );
end
end
end
);
addEventHandler ( "onClientGUIClick" , gui . button [ 1 ],
function ( )
local p = localPlayer . position ;
guiSetText ( gui . edit [ 1 ], p . x );
guiSetText ( gui . edit [ 2 ], p . y );
guiSetText ( gui . edit [ 3 ], p . z );
end ,
false );
addEventHandler ( "onClientGUIClick" , gui . button [ 3 ],
function ( )
showGUI ( );
end ,
false );
addEventHandler ( "onClientGUIClick" , gui . button [ 4 ],
function ( )
local x , y , z , accName , color , model = guiGetText ( gui . edit [ 1 ] ), guiGetText ( gui . edit [ 2 ] ), guiGetText ( gui . edit [ 3 ] ), guiGetText ( gui . edit [ 4 ] ), guiGetText ( gui . edit [ 5 ] ), guiGetText ( gui . edit [ 6 ] );
if ( x ~= "" and y ~= "" and z ~= "" and accName ~= "" and color ~= "" and model ~= "" ) then
local x , y , z , model = tonumber ( x ), tonumber ( y ), tonumber ( z ), tonumber ( model );
if ( x and y and z and model ) then
if ( getVehicleNameFromModel ( model ) == "" ) then
message ( "Invalid model ID." , "error" );
return;
end
if ( color : find ( "#%x%x%x%x%x%x" ) ) then
triggerServerEvent ( "car:createFromGUI" , localPlayer , x , y , z , accName , color , model );
else
message ( "Type correct HEX code or use color picker!" , "error" );
end
else
message ( "Position's xyz have to be numbers." , "error" );
end
else
message ( "None of the edit box can be left blank." , "error" );
end
end ,
false );
end
);
function message ( text , type )
local r , g , b = "" ;
if ( type == "info" ) then
r , g , b = 0 , 255 , 0 ;
elseif ( type == "error" ) then
r , g , b = 255 , 0 , 0 ;
else
return false ;
end
outputChatBox ( string . upper ( type ) .. ": #FFFFFF" .. text , r , g , b , true );
return true ;
end
addEventHandler ( "car:message" , root , message );
function hex2rgb ( hex )
hex = hex : gsub ( "#" , "" );
return tonumber ( "0x" .. hex : sub ( 1 , 2 ) ), tonumber ( "0x" .. hex : sub ( 3 , 4 ) ), tonumber ( "0x" .. hex : sub ( 5 , 6 ) );
end
function clearEdits ( )
for _ , v in pairs ( gui . edit ) do
guiSetText ( v , "" );
end
end
function renderIfVisible ( )
local text = guiGetText ( gui . edit [ 5 ] );
if ( #text == 0 ) then
guiSetText ( gui . edit [ 5 ], "#FF0000" );
end
end
function showGUI ( )
local bool = gui . tabpanel [ 1 ]: getVisible ( );
local render = bool and removeEventHandler or addEventHandler
gui . tabpanel [ 1 ]: setVisible ( not bool );
showCursor ( not bool );
render ( "onClientRender" , root , renderIfVisible )
if ( bool ) then
clearEdits ( );
if ( type ( cp ) == "table" ) then
cp . handlers . destroy ( );
cp = nil ;
end
end
end
addEventHandler ( "car:showGUI" , root , showGUI );
main_s.lua
--[[
PRIVATE CARS
by WhoAmI ( aka Jacob )
You are able to modify the script , if you dont change author .
#main_s.lua
--]]---
-- events
addEvent ( "car:afterBind" , true )
addEvent ( "car:createFromGUI" , true )
-- events
vehicles = { };
addEventHandler ( "onResourceStart" , resourceRoot ,
function ( )
if ( not connection ) then return; end
connection : query ( spawnVehiclesFromDatabase , { }, "SELECT * FROM `vehicles`" );
end
);
addEventHandler ( "onResourceStop" , resourceRoot ,
function ( )
if ( not connection ) then return; end
for _ , v in pairs ( vehicles ) do
local id = v : getID ( );
local p = v . position ;
local rot = v . rotation ;
local r , g , b , r1 , g1 , b1 , r2 , g2 , b2 , r3 , g3 , b3 , r4 , g4 , b4 = v : getColor ( true );
local color = { r , g , b , r1 , g1 , b1 , r2 , g2 , b2 , r3 , g3 , b3 , r4 , g4 , b4 };
local upgrades = v : getUpgrades ( );
connection : exec ( "UPDATE `vehicles` SET x=?, y=?, z=?, rotX=?, rotY=?, rotZ=?, color=?, upgrades=? WHERE id=?" , p . x , p . y , p . z , rot . x , rot . y , rot . z , toJSON ( color ), toJSON ( upgrades ), id )
end
end
);
function hex2rgb ( hex )
hex = hex : gsub ( "#" , "" );
return tonumber ( "0x" .. hex : sub ( 1 , 2 ) ), tonumber ( "0x" .. hex : sub ( 3 , 4 ) ), tonumber ( "0x" .. hex : sub ( 5 , 6 ) );
end
function afterBind ( )
if ( not isPlayerInGroup ( source , aclGroups ) ) then
return;
end
triggerClientEvent ( source , "car:showGUI" , source );
end
addEventHandler ( "car:afterBind" , root , afterBind )
function isPlayerInGroup ( thePlayer , groups )
local account = getPlayerAccount ( thePlayer );
local inGroup = false ;
for _ , group in ipairs ( groups ) do
if isObjectInACLGroup ( "user." .. getAccountName ( account ), aclGetGroup ( group ) ) then
inGroup = true ;
break;
end
end
return inGroup ;
end
last_id = 0
function spawnVehiclesFromDatabase ( q )
if ( q ) then
local p = q : poll ( 0 );
if ( #p > 0 ) then
for _ , d in pairs ( p ) do
last_id = d [ "id" ];
createCar ( d [ "vehicle_model" ], d [ "x" ], d [ "y" ], d [ "z" ], d [ "rotX" ], d [ "rotY" ], d [ "rotZ" ], d [ "color" ], d [ "upgrades" ], d [ "acc_name" ], d [ "id" ] );
end
end
end
end
function createCar ( model , x , y , z , rotX , rotY , rotZ , color , upgrades , acc_name , id )
local c = type ( color ) == "table" and color or ( fromJSON ( color ) or { 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 } );
local upgrades = type ( upgrades ) == "table" and upgrades or ( fromJSON ( upgrades ) or { } );
local rotX = type ( rotX ) == "number" and rotX or 0 ;
local rotY = type ( rotY ) == "number" and rotY or 0 ;
local rotZ = type ( rotZ ) == "number" and rotZ or 0 ;
local id = id or ( last_id + 1 );
local vehicle = Vehicle ( model , x , y , z , rotX , rotY , rotZ );
if ( isElement ( vehicle ) ) then
vehicle : setColor ( c [ 1 ], c [ 2 ], c [ 3 ], c [ 4 ], c [ 5 ], c [ 6 ], c [ 7 ], c [ 8 ], c [ 9 ], c [ 10 ], c [ 11 ], c [ 12 ] );
vehicle : setID ( id );
vehicle : setPlateText ( "LS" .. id )
vehicle : setData ( "car:owner" , acc_name );
if ( #upgrades > 0 ) then
for _ , upgrade in pairs ( upgrades ) do
vehicle : addUpgrade ( upgrade );
end
end
else
return false ;
end
vehicles [ #vehicles+1] = vehicle;
return vehicle ;
end
function createPrivateCar ( model , x , y , z , rotX , rotY , rotZ , color , upgrades , acc_name )
if ( not model or not x or not y or not z or not acc_name ) then
outputServerLog ( "[ERROR] PRIVATE CARS: Woops! Something went wrong. [createPrivateCar]" );
return false ;
end
local q = connection : exec ( "INSERT INTO `vehicles` (`vehicle_model`, `x`, `y`, `z`, `rotX`, `rotY`, `rotZ`, `color`, `upgrades`, `acc_name` ) VALUES (?,?,?,?,?,?,?,?,?,?)" , model , x , y , z , rotX , rotY , rotZ , toJSON ( color ), toJSON ( upgrades ), acc_name )
if ( not q ) then
return false ;
end
local vehicle = createCar ( model , x , y , z , rotX , rotY , rotZ , color , upgrades , acc_name )
if ( isElement ( vehicle ) ) then
return vehicle ;
else
return false ;
end
end
function isCarPrivate ( v )
if ( v : getData ( "car:owner" ) ) then
return true ;
else
return false ;
end
end
function getPrivateCarOwner ( v )
if ( v : getData ( "car:owner" ) ) then
return tostring ( v : getData ( "car:owner" ) );
else
return false ;
end
end
function message ( player , text , type )
triggerClientEvent ( player , "car:message" , player , text , type )
end
addEventHandler ( "car:createFromGUI" , root ,
function ( x , y , z , accName , color , model )
local account = getAccount ( accName );
if ( not account ) then
triggerClientEvent ( source , "car:message" , source , "Unable to find an account with this name." , "error" );
return;
end
local rot = source . rotation ;
local r , g , b = hex2rgb ( color );
source : setPosition ( x , y , z + 1 );
local veh = createPrivateCar ( model , x , y , z , rot . x , rot . y , rot . z , { r , g , b , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 }, { }, accName );
if ( isElement ( veh ) ) then
veh : setColor ( r , g , b );
message ( source , "Created " .. veh : getName ( ) .. " for account " .. accName .. "." , "info" );
else
message ( source , "Unknown error while creating the vehicle." , "error" );
end
end
);
addEventHandler ( "onVehicleStartEnter" , root ,
function ( player , seat )
if ( seat ~= 0 ) then
return;
end
local owner = source : getData ( "car:owner" );
if ( owner ) then
local account = player : getAccount ( );
if ( account ) then
if ( owner ~= account : getName ( ) ) then
message ( player , "Nie masz kluczy do tego pojazdu." , "info" );
cancelEvent ( );
end
end
end
end
);
function fixVehicleFromTimer ( v )
v : fix ( );
end
addEventHandler ( "onVehicleExplode" , root ,
function ( )
if ( source : getData ( "car:owner" ) ) then
setTimer ( fixVehicleFromTimer , respawnTime * 1000 , 1 , source )
end
end
);
addCommandHandler ( carInfoCommandName ,
function ( player )
local v = player : getOccupiedVehicle ( );
if ( isElement ( v ) ) then
local owner = v : getData ( "car:owner" );
if ( not owner ) then
return;
end
if ( isPlayerInGroup ( player , aclGroupsInfo ) ) then
message ( player , "W?a?cicielem tego pojazdu jest #00FF00" .. owner .. "#FFFFFF (ID " .. v : getID ( ) .. ")." , "info" );
end
end
end
);