Skip to content

Commit 7ddf7e3

Browse files
committed
Update v2.0
* Callback changes * Added 'me' argument for MSK.RegisterCommand * Added MSK.GetConfig * Added AntiCombatLog
1 parent 3ab9a79 commit 7ddf7e3

10 files changed

+314
-105
lines changed

VERSION

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1.9.8
1+
1.9.9

client/cl_anticombatlog.lua

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
local drawDisplay = function(coords, text, size, color)
2+
if not color then color = {r = 255, g = 255, b = 255, a = 255} end
3+
local coords = type(coords) == "vector3" and coords or vec(coords.x, coords.y, coords.z)
4+
local camCoords = GetGameplayCamCoords()
5+
local distance = #(coords - camCoords)
6+
7+
if not size then size = 1 end
8+
9+
local scale = (size / distance) * 2
10+
local fov = (1 / GetGameplayCamFov()) * 100
11+
scale = scale * fov
12+
13+
SetTextScale(0.0, scale * 0.5)
14+
SetTextFont(0)
15+
SetTextProportional(1)
16+
SetTextColour(color.r, color.g, color.b, color.a)
17+
SetTextDropshadow(3, 0, 0, 0, 55)
18+
BeginTextCommandDisplayText('STRING')
19+
SetTextCentre(true)
20+
AddTextComponentSubstringPlayerName(text)
21+
SetDrawOrigin(coords.xyz, 0)
22+
EndTextCommandDisplayText(0.0, 0.0)
23+
ClearDrawOrigin()
24+
end
25+
26+
local display = function(data)
27+
-- data.playerId, data.playerName, data.coords, data.reason
28+
29+
local showDisplay = true
30+
local timeout = MSK.SetTimeout(60000, function()
31+
showDisplay = false
32+
MSK.DelTimeout(timeout)
33+
end)
34+
local text = ('%s (ID: %s)\n%s'):format(data.playerName, data.playerId, data.reason)
35+
36+
CreateThread(function()
37+
while showDisplay do
38+
local sleep = 500
39+
40+
local playerPed = PlayerPedId()
41+
local playerCoords = GetEntityCoords(playerPed)
42+
local dist = #(data.coords - playerCoords)
43+
44+
local coordsUsed = vec3(data.coords.x, data.coords.y, data.coords.z + 0.2)
45+
46+
if dist <= 20.0 then
47+
sleep = 0
48+
DrawMarker(32, data.coords.x, data.coords.y, data.coords.z + 0.5, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.8, 0.8, 0.8, 94, 177, 49, 1, false, true, 2, false, nil, nil, false)
49+
drawDisplay(data.coords, text, 0.8, {r = 94, g = 177, b = 49, a = 1})
50+
end
51+
52+
Wait(sleep)
53+
end
54+
end)
55+
end
56+
57+
RegisterNetEvent('msk_core:anticombatlog')
58+
AddEventHandler('msk_core:anticombatlog', function(data)
59+
display(data)
60+
end)

client/cl_callback.lua

+66-42
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,90 @@
1-
local callbackRequest = {}
1+
local Callbacks = {}
22
local CallbackHandler = {}
33

4-
AddEventHandler('onResourceStop', function(resource)
5-
if CallbackHandler[resource] then
6-
for k, handler in pairs(CallbackHandler[resource]) do
7-
RemoveEventHandler(handler)
8-
end
4+
local GenerateCallbackHandlerKey = function()
5+
local requestId = math.random(1, 999999999)
6+
7+
if not CallbackHandler[requestId] then
8+
return tostring(requestId)
9+
else
10+
GenerateCallbackHandlerKey()
911
end
10-
end)
12+
end
1113

14+
----------------------------------------------------------------
1215
-- NEW Method for Client Callbacks
13-
MSK.RegisterClientCallback = function(eventName, cb)
14-
local handler = RegisterNetEvent(('events-request-%s'):format(eventName), function (ticket, ...)
15-
TriggerServerEvent(('events-resolve-%s-%s'):format(eventName, ticket), cb(...))
16-
end)
17-
18-
local script = GetInvokingResource()
19-
if script then
20-
if CallbackHandler[script] then
21-
table.insert(CallbackHandler[script], handler)
22-
else
23-
CallbackHandler[script] = {}
24-
table.insert(CallbackHandler[script], handler)
25-
end
26-
end
16+
----------------------------------------------------------------
17+
MSK.Register = function(eventName, cb)
18+
Callbacks[eventName] = cb
2719
end
28-
MSK.Register = MSK.RegisterClientCallback
29-
exports('RegisterClientCallback', MSK.RegisterClientCallback)
20+
MSK.RegisterCallback = MSK.Register
21+
MSK.RegisterClientCallback = MSK.Register
22+
exports('Register', MSK.Register)
23+
exports('RegisterCallback', MSK.Register)
24+
exports('RegisterServerCallback', MSK.Register)
25+
26+
RegisterNetEvent('msk_core:client:triggerClientCallback', function(playerId, eventName, requestId, ...)
27+
if not Callbacks[eventName] then
28+
TriggerServerEvent('msk_core:server:callbackNotFound', requestId)
29+
return
30+
end
31+
32+
TriggerServerEvent("msk_core:server:callbackResponse", requestId, Callbacks[eventName](playerId, ...))
33+
end)
3034

35+
----------------------------------------------------------------
3136
-- NEW Method for Server Callbacks
37+
----------------------------------------------------------------
3238
MSK.Trigger = function(eventName, ...)
39+
local requestId = GenerateCallbackHandlerKey()
3340
local p = promise.new()
34-
local ticket = tostring(GetGameTimer() .. GetPlayerServerId(PlayerId()))
41+
CallbackHandler[requestId] = 'request'
3542

3643
SetTimeout(5000, function()
37-
p:reject("Request Timed Out (408)")
44+
CallbackHandler[requestId] = nil
45+
p:reject(('Request Timed Out: [%s] [%s]'):format(eventName, requestId))
3846
end)
3947

40-
local handler = RegisterNetEvent(('events-resolve-%s-%s'):format(eventName, ticket), function(...)
41-
p:resolve({...})
42-
end)
48+
TriggerServerEvent('msk_core:server:triggerServerCallback', eventName, requestId, ...)
4349

44-
TriggerServerEvent(('events-request-%s'):format(eventName), ticket, ...)
50+
while CallbackHandler[requestId] == 'request' do Wait(0) end
51+
if not CallbackHandler[requestId] then return end
52+
53+
p:resolve(CallbackHandler[requestId])
54+
CallbackHandler[requestId] = nil
4555

4656
local result = Citizen.Await(p)
47-
RemoveEventHandler(handler)
4857
return table.unpack(result)
4958
end
59+
exports('Trigger', MSK.Trigger)
60+
61+
RegisterNetEvent("msk_core:client:callbackResponse", function(requestId, ...)
62+
if not CallbackHandler[requestId] then return end
63+
CallbackHandler[requestId] = {...}
64+
end)
65+
66+
RegisterNetEvent("msk_core:client:callbackNotFound", function(requestId)
67+
if not CallbackHandler[requestId] then return end
68+
CallbackHandler[requestId] = nil
69+
end)
70+
71+
----------------------------------------------------------------
72+
-- OLD Method for Server Callbacks [OUTDATED - Do not use this!]
73+
----------------------------------------------------------------
74+
local callbackRequest = {}
75+
76+
local GenerateRequestKey = function()
77+
local requestId = math.random(1, 999999999)
78+
79+
if not callbackRequest[requestId] then
80+
return tostring(requestId)
81+
else
82+
GenerateRequestKey()
83+
end
84+
end
5085

51-
-- OLD Method for Server Callbacks
5286
MSK.TriggerServerCallback = function(name, ...)
53-
local requestId = GenerateRequestKey(callbackRequest)
87+
local requestId = GenerateRequestKey()
5488
local response
5589

5690
callbackRequest[requestId] = function(...)
@@ -66,16 +100,6 @@ end
66100
MSK.TriggerCallback = MSK.TriggerServerCallback
67101
exports('TriggerServerCallback', MSK.TriggerServerCallback)
68102

69-
GenerateRequestKey = function(tbl)
70-
local id = string.upper(MSK.GetRandomString(3)) .. math.random(000, 999) .. string.upper(MSK.GetRandomString(2)) .. math.random(00, 99)
71-
72-
if not tbl[id] then
73-
return tostring(id)
74-
else
75-
GenerateRequestKey(tbl)
76-
end
77-
end
78-
79103
RegisterNetEvent("msk_core:responseCallback")
80104
AddEventHandler("msk_core:responseCallback", function(requestId, ...)
81105
if callbackRequest[requestId] then

client/main.lua

+26-3
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,48 @@ MSK = {}
22

33
if Config.Framework:match('esx') then
44
ESX = exports["es_extended"]:getSharedObject()
5+
6+
AddEventHandler('esx:setPlayerData', function(key, val, last)
7+
if GetInvokingResource() == 'es_extended' then
8+
ESX.PlayerData[key] = val
9+
if OnPlayerData then
10+
OnPlayerData(key, val, last)
11+
end
12+
end
13+
end)
14+
15+
RegisterNetEvent('esx:playerLoaded', function(xPlayer)
16+
ESX.PlayerData = xPlayer
17+
ESX.PlayerLoaded = true
18+
end)
19+
20+
RegisterNetEvent('esx:onPlayerLogout', function()
21+
ESX.PlayerLoaded = false
22+
ESX.PlayerData = {}
23+
end)
524
elseif Config.Framework:match('qbcore') then
625
QBCore = exports['qb-core']:GetCoreObject()
26+
27+
RegisterNetEvent('QBCore:Player:SetPlayerData', function(PlayerData)
28+
QBCore.PlayerData = PlayerData
29+
end)
730
end
831

932
MSK.Notification = function(title, message, info, time)
1033
if Config.Notification == 'native' then
1134
SetNotificationTextEntry('STRING')
1235
AddTextComponentString(message)
1336
DrawNotification(false, true)
14-
elseif Config.Notification == 'nui' or Config.Notification == 'msk' then
37+
elseif Config.Notification == 'okok' then
38+
exports['okokNotify']:Alert(title, message, time or 5000, info or 'info')
39+
else
1540
SendNUIMessage({
1641
action = 'notify',
1742
title = title,
1843
message = message,
1944
info = info or 'general',
2045
time = time or 5000
2146
})
22-
elseif Config.Notification == 'okok' then
23-
exports['okokNotify']:Alert(title, message, time or 5000, info or 'info')
2447
end
2548
end
2649
exports('Notification', MSK.Notification)

common/common.lua

+12-13
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,18 @@
1-
local Timeouts, Letters = {}, {}
1+
local Timeouts, Charset = {}, {}
22

3-
for i = 48, 57 do table.insert(Letters, string.char(i)) end
4-
for i = 65, 90 do table.insert(Letters, string.char(i)) end
5-
for i = 97, 122 do table.insert(Letters, string.char(i)) end
3+
for i = 48, 57 do table.insert(Charset, string.char(i)) end
4+
for i = 65, 90 do table.insert(Charset, string.char(i)) end
5+
for i = 97, 122 do table.insert(Charset, string.char(i)) end
66

77
MSK.GetRandomString = function(length)
8-
Wait(0)
9-
if length > 0 then
10-
return MSK.GetRandomString(length - 1) .. Letters[math.random(1, #Letters)]
11-
else
12-
return ''
13-
end
8+
math.randomseed(GetGameTimer())
9+
10+
return length > 0 and ESX.GetRandomString(length - 1) .. Charset[math.random(1, #Charset)] or ''
1411
end
1512
MSK.GetRandomLetter = MSK.GetRandomString
1613
exports('GetRandomString', MSK.GetRandomString)
1714

18-
MSK.Round = function(num, decimal)
15+
MSK.Round = function(num, decimal)
1916
return tonumber(string.format("%." .. (decimal or 0) .. "f", num))
2017
end
2118
exports('Round', MSK.Round)
@@ -131,9 +128,11 @@ end
131128
MSK.logging = MSK.Logging
132129
exports('Logging', MSK.Logging)
133130

134-
exports('getConfig', function()
131+
MSK.GetConfig = function()
135132
return Config
136-
end)
133+
end
134+
exports('GetConfig', MSK.GetConfig)
135+
exports('getConfig', MSK.GetConfig)
137136

138137
logging = function(code, ...)
139138
if not Config.Debug then return end

config.lua

+18-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Config = {}
22
----------------------------------------------------------------
3-
Config.Debug = true
3+
Config.Debug = false
44
Config.VersionChecker = true
55
----------------------------------------------------------------
66
-- Only Required for MSK.RegisterCommand // View Wiki for more Information about that!
@@ -23,4 +23,21 @@ Config.LoggingTypes = {
2323
['info'] = '[^4Info^0]',
2424
['debug'] = '[^3DEBUG^0]',
2525
['error'] = '[^1ERROR^0]',
26+
}
27+
----------------------------------------------------------------
28+
Config.AntiCombatlog = {
29+
enable = false, -- Set to true if you want to use this Feature
30+
console = {
31+
enable = true,
32+
text = "Der Spieler ^3%s^0 mit der ^3ID %s^0 hat den Server verlassen.\n^4Uhrzeit:^0 %s\n^4Grund:^0 %s\n^4Identifier:^0\n %s\n %s\n %s\n^4Koordinaten:^0 %s"
33+
},
34+
discord = {
35+
-- Webhook in sv_anticombatlog.lua
36+
enable = true,
37+
color = "6205745", -- https://www.mathsisfun.com/hexadecimal-decimal-colors.html
38+
botName = "MSK Scripts",
39+
botAvatar = "https://i.imgur.com/PizJGsh.png",
40+
title = "Player Disconnected",
41+
text = "Der Spieler **%s** mit der **ID %s** hat den Server verlassen."
42+
}
2643
}

fxmanifest.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ games { 'gta5' }
44
author 'Musiker15 - MSK Scripts'
55
name 'msk_core'
66
description 'Core functions for MSK Scripts'
7-
version '1.9.8'
7+
version '2.0'
88

99
lua54 'yes'
1010

server/main.lua

+6-3
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,13 @@ MSK.RegisterCommand = function(name, group, cb, console, framework, suggestion)
5555
end
5656
elseif v.action == 'playerId' then
5757
if args[k] then
58-
if tonumber(args[k]) > 0 and doesPlayerIdExist(args[k]) then
59-
newArgs[v.name] = args[k]
58+
local targetId = args[k]
59+
if targetId == 'me' then targetId = source end
60+
61+
if tonumber(targetId) > 0 and doesPlayerIdExist(targetId) then
62+
newArgs[v.name] = targetId
6063
else
61-
error = ('PlayerId %s does not exist!'):format(args[k])
64+
error = ('PlayerId %s does not exist!'):format(targetId)
6265
end
6366
end
6467
else

0 commit comments

Comments
 (0)