-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLANBroadcaster.lua
More file actions
54 lines (39 loc) · 1.47 KB
/
LANBroadcaster.lua
File metadata and controls
54 lines (39 loc) · 1.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
-- LANBroadcaster.lua
-- Implements the entire plugin in a single file
function Initialize(a_Plugin)
-- Open the UDP endpoint:
-- Not interested in any callbacks, we're just sending data
local Endpoint = cNetwork:CreateUDPEndpoint(0, {})
if not(Endpoint:IsOpen()) then
LOGINFO("LANBroadcaster: Cannot open UDP port for broadcasting: " .. a_ErrorCode .. " (" .. a_ErrorMsg .. ")")
return
end
Endpoint:EnableBroadcasts()
-- We don't have a generic task scheduler yet (#1754), so piggyback on the first available world's scheduler:
local IsScheduled = false
local OnWorldInitialized = function (a_World)
-- Check if scheduler already active:
if (IsScheduled) then
return
end
IsScheduled = true
-- Schedule the broadcast
local Server = cRoot:Get():GetServer()
local DatagramData = "[MOTD]" .. Server:GetDescription() .. "[/MOTD][AD]25565[/AD]"
local Task -- Must be defined before assigning, because the function refers to itself
Task = function ()
-- Send the datagram:
Endpoint:Send(DatagramData, "224.0.2.60", 4445)
-- Re-schedule the task:
a_World:ScheduleTask(30, Task)
end
a_World:ScheduleTask(30, Task)
end
-- Initialize the scheduler in the first started world, or (if reloading the server) on the default world:
cPluginManager.AddHook(cPluginManager.HOOK_WORLD_STARTED, OnWorldInitialized)
local DefaultWorld = cRoot:Get():GetDefaultWorld()
if (DefaultWorld ~= nil) then
OnWorldInitialized(DefaultWorld)
end
return true
end