1
0
mirror of https://github.com/Refactorio/RedMew.git synced 2025-01-03 22:52:13 +02:00

Add generate_event_name to event module

This commit is contained in:
plague006 2019-03-03 11:49:16 -05:00
parent 7e7fcf908d
commit 535c675028
8 changed files with 27 additions and 18 deletions

View File

@ -12,7 +12,7 @@ local ForceControl = {}
ForceControl.events = {
--- triggered when the force levels up
--- uses event = {level_reached = number, force = LuaForce}
on_level_up = script.generate_event_name()
on_level_up = Event.generate_event_name('on_level_up')
}
-- the builder, can only be accessed through ForceControl.register() and should be avoided used run-time

View File

@ -74,7 +74,7 @@ Retailer.events = {
-- player = player,
-- group_name = group_name,
-- }
on_market_purchase = script.generate_event_name(),
on_market_purchase = Event.generate_event_name('on_market_purchase'),
}
Retailer.item_types = {

View File

@ -66,7 +66,7 @@ local data_set_handlers = {}
-- function()
-- Server.try_get_all_data('regulars', callback)
-- end)
Public.events = {on_server_started = script.generate_event_name()}
Public.events = {on_server_started = Event.generate_event_name('on_server_started')}
--- Sends a message to the linked discord channel. The message is sanitized of markdown server side.
-- @param message<string> message to send.

View File

@ -91,14 +91,14 @@ DiggyCaveCollapse.events = {
- surface LuaSurface
- player_index Number (index of player that caused the collapse)
]]
on_collapse_triggered = script.generate_event_name(),
on_collapse_triggered = Event.generate_event_name('on_collapse_triggered'),
--[[--
After a collapse
- position LuaPosition
- surface LuaSurface
- player_index Number (index of player that caused the collapse)
]]
on_collapse = script.generate_event_name()
on_collapse = Event.generate_event_name('on_collapse')
}
local function create_collapse_template(positions, surface)

View File

@ -1,6 +1,7 @@
-- dependencies
local Task = require 'utils.task'
local Token = require 'utils.token'
local Event = require 'utils.event'
local min = math.min
local ceil = math.ceil
local raise_event = script.raise_event
@ -19,14 +20,14 @@ Template.events = {
When an entity is placed via the template function.
- event.entity LuaEntity
]]
on_placed_entity = script.generate_event_name(),
on_placed_entity = Event.generate_event_name('on_placed_entity'),
--[[--
Triggers when an 'out-of-map' tile is replaced by something else.
{surface, old_tile={name, position={x, y}}}
]]
on_void_removed = script.generate_event_name(),
on_void_removed = Event.generate_event_name('on_void_removed'),
}
local on_void_removed = Template.events.on_void_removed

View File

@ -26,7 +26,7 @@ local Public = {
surface :: LuaSurface: The surface the chunk is on
chunk_index :: the index of the chunk in Chunklist's table
]]
on_chunk_registered = script.generate_event_name()
on_chunk_registered = Event.generate_event_name('on_chunk_registered')
}
}

View File

@ -58,7 +58,7 @@ local Public = {
ghost :: boolean indicating if the entity was a ghost
stack :: LuaItemStack
]]
on_pre_restricted_entity_destroyed = script.generate_event_name(),
on_pre_restricted_entity_destroyed = Event.generate_event_name('on_pre_restricted_entity_destroyed'),
--[[
on_restricted_entity_destroyed
Called when an entity is destroyed by this script
@ -70,7 +70,7 @@ local Public = {
ghost :: boolean indicating if the entity was a ghost
item_returned :: boolean indicating if a refund of the item was attempted
]]
on_restricted_entity_destroyed = script.generate_event_name()
on_restricted_entity_destroyed = Event.generate_event_name('on_restricted_entity_destroyed')
}
}

View File

@ -107,6 +107,7 @@ local core_on_nth_tick = EventCore.on_nth_tick
local stage_load = _STAGE.load
local script_on_event = script.on_event
local script_on_nth_tick = script.on_nth_tick
local generate_event_name = script.generate_event_name
local Event = {}
@ -266,10 +267,7 @@ function Event.add_removable_function(event_name, func)
end
if Debug.is_closure(func) then
error(
'func cannot be a closure as that is a desync risk. Consider using Event.add_removable(event_name, token) instead.',
2
)
error('func cannot be a closure as that is a desync risk. Consider using Event.add_removable(event_name, token) instead.', 2)
end
local funcs = function_handlers[event_name]
@ -376,10 +374,7 @@ function Event.add_removable_nth_tick_function(tick, func)
end
if Debug.is_closure(func) then
error(
'func cannot be a closure as that is a desync risk. Consider using Event.add_removable_nth_tick(tick, token) instead.',
2
)
error('func cannot be a closure as that is a desync risk. Consider using Event.add_removable_nth_tick(tick, token) instead.', 2)
end
local funcs = function_nth_tick_handlers[tick]
@ -419,6 +414,19 @@ function Event.remove_removable_nth_tick_function(tick, func)
end
end
--- Generate a new, unique event ID.
-- @param <string> name of the event/variable that is exposed
function Event.generate_event_name(name)
local event_id = generate_event_name()
-- If we're in debug, we add the event to defines.events in order to list it via the debugger.
if _DEBUG then
defines.events[name] = event_id -- luacheck: ignore 122
end
return event_id
end
local function add_handlers()
for event_name, tokens in pairs(token_handlers) do
for i = 1, #tokens do