mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-22 08:13:12 +09:00
Add 2.0 support to module template
This commit is contained in:
3 files changed
+42
-18
No files matched your search
@@ -1,19 +1,20 @@
|
|||||||
local clusterio_api = require("modules/clusterio/api")
|
local clusterio_api = require("modules/clusterio/api")
|
||||||
|
local compat = require("modules/clusterio/compat")
|
||||||
|
|
||||||
--- Top level module table, contains event handlers and public methods
|
--- Top level module table, contains event handlers and public methods
|
||||||
|
--- @class __plugin_name__
|
||||||
local MyModule = {
|
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
|
-- 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
|
-- 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
|
-- If you use any of these events and access globalData then make sure to call this function first
|
||||||
local globalData = {}
|
local script_data = {}
|
||||||
local function setupGlobalData()
|
local function setup_script_data()
|
||||||
if global["__plugin_name__"] == nil then
|
local _script_data = compat.script_data()
|
||||||
global["__plugin_name__"] = {
|
if _script_data["__plugin_name__"] == nil then
|
||||||
|
_script_data["__plugin_name__"] = {
|
||||||
-- starting values go here
|
-- starting values go here
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
@@ -23,7 +24,8 @@ end
|
|||||||
--- The on_load function is called independently for each client when they first load the map
|
--- 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
|
-- It should be used to restore global aliases and metatables not registered with script.register_metatable
|
||||||
function MyModule.on_load()
|
function MyModule.on_load()
|
||||||
globalData = global["__plugin_name__"]
|
local _script_data = compat.script_data()
|
||||||
|
script_data = _script_data["__plugin_name__"]
|
||||||
end
|
end
|
||||||
|
|
||||||
--- Public methods should be available though your top level module table
|
--- Public methods should be available though your top level module table
|
||||||
@@ -37,26 +39,43 @@ local function bar()
|
|||||||
end
|
end
|
||||||
|
|
||||||
--- Clusterio provides a few custom events, on_server_startup is the most useful and should be used in place of on_init
|
--- 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)
|
--- @param event EventData.on_server_startup
|
||||||
setupGlobalData()
|
local function on_server_startup(event)
|
||||||
game.print(game.table_to_json(event))
|
setup_script_data()
|
||||||
|
game.print(compat.table_to_json(event))
|
||||||
end
|
end
|
||||||
|
|
||||||
--- Factorio events are accessible through defines.events, you can have one handler per event per module
|
--- Triggered every time a player crafts an item, see below for how handlers are registered
|
||||||
MyModule.events[defines.events.on_player_crafted_item] = function(event)
|
--- @param event EventData.on_player_crafted_item
|
||||||
game.print(game.table_to_json(event))
|
local function on_player_crafted_item(event)
|
||||||
|
game.print(compat.table_to_json(event))
|
||||||
//%if instance
|
//%if instance
|
||||||
|
local player = assert(game.get_player(event.player_index))
|
||||||
clusterio_api.send_json("__plugin_name__-plugin_example_ipc", {
|
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
|
//%endif
|
||||||
end
|
end
|
||||||
|
|
||||||
--- Nth tick is a special case that requires its own table, the index represents the time period between calls in ticks
|
--- Run bar every 5 seconds, see below for how handlers are registered
|
||||||
MyModule.on_nth_tick[300] = function()
|
local function on_nth_tick_300()
|
||||||
game.print(game.tick)
|
game.print(game.tick)
|
||||||
bar()
|
bar()
|
||||||
end
|
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
|
--- 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
|
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
|
--- 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__")
|
-- Access the exports from other modules using require("modules/__plugin_name__")
|
||||||
return require("modules/__plugin_name__/control")
|
return require("modules/__plugin_name__/control")
|
||||||
@@ -1,11 +1,15 @@
|
|||||||
local api = require('modules/clusterio/api')
|
local api = require('modules/clusterio/api')
|
||||||
local compat = require("modules/clusterio/compat")
|
local compat = require("modules/clusterio/compat")
|
||||||
|
|
||||||
|
--- @class (exact) EventData.on_server_startup:EventData
|
||||||
|
|
||||||
local function check_patch()
|
local function check_patch()
|
||||||
local script_data = compat.script_data()
|
local script_data = compat.script_data()
|
||||||
if script_data.clusterio_patch_number ~= clusterio_patch_number then
|
if script_data.clusterio_patch_number ~= clusterio_patch_number then
|
||||||
script_data.clusterio_patch_number = clusterio_patch_number
|
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
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user