Add 2.0 support to module template

This commit is contained in:
Cooldude2606 committed 2024-11-23 14:26:12 +00:00
1 parent 2ba33d58ed
commit 8b551deda8
3 files changed
+42 -18

No files matched your search

@@ -1,19 +1,20 @@
local clusterio_api = require("modules/clusterio/api")
local compat = require("modules/clusterio/compat")
--- Top level module table, contains event handlers and public methods
--- @class __plugin_name__
local MyModule = {
events = {},
on_nth_tick = {},
}
--- global is 'synced' between players, you should use your plugin name to avoid name conflicts
--- script_data is 'synced' between players, you should use your plugin name to avoid name conflicts
-- The following helper function is desync safe and avoids name conflicts after being called during on_server_startup
-- There are edge cases in the following events: pre player left, player left, console command, and config changed
-- If you use any of these events and access globalData then make sure to call this function first
local globalData = {}
local function setupGlobalData()
if global["__plugin_name__"] == nil then
global["__plugin_name__"] = {
local script_data = {}
local function setup_script_data()
local _script_data = compat.script_data()
if _script_data["__plugin_name__"] == nil then
_script_data["__plugin_name__"] = {
-- starting values go here
}
end
@@ -23,7 +24,8 @@ end
--- The on_load function is called independently for each client when they first load the map
-- It should be used to restore global aliases and metatables not registered with script.register_metatable
function MyModule.on_load()
globalData = global["__plugin_name__"]
local _script_data = compat.script_data()
script_data = _script_data["__plugin_name__"]
end
--- Public methods should be available though your top level module table
@@ -37,26 +39,43 @@ local function bar()
end
--- Clusterio provides a few custom events, on_server_startup is the most useful and should be used in place of on_init
MyModule.events[clusterio_api.events.on_server_startup] = function(event)
setupGlobalData()
game.print(game.table_to_json(event))
--- @param event EventData.on_server_startup
local function on_server_startup(event)
setup_script_data()
game.print(compat.table_to_json(event))
end
--- Factorio events are accessible through defines.events, you can have one handler per event per module
MyModule.events[defines.events.on_player_crafted_item] = function(event)
game.print(game.table_to_json(event))
--- Triggered every time a player crafts an item, see below for how handlers are registered
--- @param event EventData.on_player_crafted_item
local function on_player_crafted_item(event)
game.print(compat.table_to_json(event))
//%if instance
local player = assert(game.get_player(event.player_index))
clusterio_api.send_json("__plugin_name__-plugin_example_ipc", {
tick = game.tick, player_name = game.get_player(event.player_index).name
tick = game.tick, player_name = player.name
})
//%endif
end
--- Nth tick is a special case that requires its own table, the index represents the time period between calls in ticks
MyModule.on_nth_tick[300] = function()
--- Run bar every 5 seconds, see below for how handlers are registered
local function on_nth_tick_300()
game.print(game.tick)
bar()
end
--- Factorio events are accessible through defines.events, you can have one handler per event per module
local e = defines.events
local events = {
[clusterio_api.events.on_server_startup] = on_server_startup,
[e.on_player_crafted_item] = on_player_crafted_item,
}
--- Nth tick is a special case that requires its own table, the index represents the time period between calls in ticks
local on_nth_tick = {
[300] = on_nth_tick_300,
}
--- Always return the top level module table from control, this is how clusterio will access your event handlers
MyModule.events = events --- @package
MyModule.on_nth_tick = on_nth_tick --- @package
return MyModule
@@ -1,3 +1,4 @@
--- Can contain anything you want to allow other plugins to have access to, this example exposes the control file
--- If you only export a single file then it may be better to rename that file to module_exports and save the redirection
-- Access the exports from other modules using require("modules/__plugin_name__")
return require("modules/__plugin_name__/control")
+5 -1
View File
@@ -1,11 +1,15 @@
local api = require('modules/clusterio/api')
local compat = require("modules/clusterio/compat")
--- @class (exact) EventData.on_server_startup:EventData
local function check_patch()
local script_data = compat.script_data()
if script_data.clusterio_patch_number ~= clusterio_patch_number then
script_data.clusterio_patch_number = clusterio_patch_number
script.raise_event(api.events.on_server_startup, {})
script.raise_event(api.events.on_server_startup, {
name = api.events.on_server_startup, tick = game.tick
})
end
end