1
0
mirror of https://github.com/Refactorio/RedMew.git synced 2025-01-18 03:21:47 +02:00

Merge pull request #634 from grilledham/event-fixes

Event fixes
This commit is contained in:
Matthew 2019-01-10 13:07:00 -05:00 committed by GitHub
commit a65bfc33de
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 54 additions and 24 deletions

View File

@ -126,8 +126,9 @@ local function boost_player_running_speed(player)
boost_lvl = 0,
}
end
global.player_speed_boost_records[player.index].boost_lvl =
1 + global.player_speed_boost_records[player.index].boost_lvl
global.player_speed_boost_records[player.index].boost_lvl = 1 + global.player_speed_boost_records[player.index].boost_lvl
player.character_running_speed_modifier = 1 + player.character_running_speed_modifier
if global.player_speed_boost_records[player.index].boost_lvl >= 4 then
@ -157,8 +158,8 @@ local function boost_player_mining_speed(player)
boost_lvl = 0,
}
end
global.player_mining_boost_records[player.index].boost_lvl = 1 + global.player_mining_boost_records[player.index].boost_lvl
player.character_mining_speed_modifier = 1 + player.character_mining_speed_modifier
global.player_mining_boost_records[player.index].boost_lvl =
1 + global.player_mining_boost_records[player.index].boost_lvl
if global.player_mining_boost_records[player.index].boost_lvl >= 4 then
game.print(format(mining_speed_boost_messages[global.player_mining_boost_records[player.index].boost_lvl], player.name))
@ -170,7 +171,7 @@ local function boost_player_mining_speed(player)
player.print(format(mining_speed_boost_messages[global.player_mining_boost_records[player.index].boost_lvl], player.name))
end
local function on_market_purchase(event)
local function market_item_purchased(event)
local item_name = event.item.name
if item_name == 'temporary-running-speed-bonus' then
boost_player_running_speed(event.player)
@ -224,17 +225,21 @@ local function player_created(event)
end
local count = global.config.player_rewards.info_player_reward and 1 or 10
player.insert({name = 'coin', count = count})
player.insert {name = 'coin', count = count}
end
Command.add('market', {
description = 'Places a market near you.',
admin_only = true,
}, spawn_market)
Command.add(
'market',
{
description = 'Places a market near you.',
admin_only = true
},
spawn_market
)
Event.on_nth_tick(180, on_180_ticks)
Event.add(defines.events.on_pre_player_mined_item, pre_player_mined_item)
Event.add(defines.events.on_entity_died, fish_drop_entity_died)
Event.add(Retailer.events.on_market_purchase, on_market_purchase)
Event.add(defines.events.on_market_item_purchased, market_item_purchased)
Event.add(defines.events.on_player_crafted_item, fish_player_crafted_item)
Event.add(defines.events.on_player_created, player_created)

View File

@ -19,7 +19,11 @@
-- ** Event.add_removable(event_name, token) **
--
-- For conditional event handlers. Event.add_removable can be safely called at runtime without desync risk.
-- Only use this if you need to add the handler at runtime or need to remove the handler, other wise use Event.add
-- Only use this if you need to add the handler at runtime or need to remove the handler, otherwise use Event.add
--
-- Event.add_removable can be safely used at the control stage or in Event.on_init. If used in on_init you don't
-- need to also add in on_load (unlike Event.add).
-- Event.add_removable cannot be called in on_load, doing so will crash the game on loading.
-- Token is used because it's a desync risk to store closures inside the global table.
--
-- @usage
@ -46,6 +50,7 @@
-- ** Event.add_removable_function(event_name, func) **
--
-- Only use this function if you can't use Event.add_removable. i.e you are registering the handler at the console.
-- The same restrictions that apply to Event.add_removable also apply to Event.add_removable_function.
-- func cannot be a closure in this case, as there is no safe way to store closures in the global table.
-- A closure is a function that uses a local variable not defined in the function.
--
@ -102,6 +107,8 @@ local core_on_nth_tick = EventCore.on_nth_tick
local Event = {}
local handlers_added = false -- set to true after the removeable event handlers have been added.
local event_handlers = EventCore.get_event_handlers()
local on_nth_tick_event_handlers = EventCore.get_on_nth_tick_event_handlers()
@ -126,6 +133,10 @@ Global.register(
)
local function remove(tbl, handler)
if tbl == nil then
return
end
-- the handler we are looking for is more likly to be at the back of the array.
for i = #tbl, 1, -1 do
if tbl[i] == handler then
@ -186,6 +197,7 @@ function Event.on_nth_tick(tick, handler)
end
--- Register a token handler that can be safely added and removed at runtime.
-- Do NOT call this method during on_load.
-- See documentation at top of file for details on using events.
-- @param event_name<number>
-- @param token<number>
@ -201,15 +213,14 @@ function Event.add_removable(event_name, token)
tokens[#tokens + 1] = token
end
-- If this is called before runtime, we don't need to add the handlers
-- as they will be added later either in on_init or on_load.
if EventCore.runtime then
if handlers_added then
local handler = Token.get(token)
core_add(event_name, handler)
end
end
--- Removes a token handler for the given event_name.
-- Do NOT call this method during on_load.
-- See documentation at top of file for details on using events.
-- @param event_name<number>
-- @param token<number>
@ -233,6 +244,7 @@ end
--- Register a handler that can be safely added and removed at runtime.
-- The handler must not be a closure, as that is a desync risk.
-- Do NOT call this method during on_load.
-- See documentation at top of file for details on using events.
-- @param event_name<number>
-- @param func<function>
@ -255,14 +267,13 @@ function Event.add_removable_function(event_name, func)
funcs[#funcs + 1] = func
end
-- If this is called before runtime, we don't need to add the handlers
-- as they will be added later either in on_init or on_load.
if EventCore.runtime then
if handlers_added then
core_add(event_name, func)
end
end
--- Removes a handler for the given event_name.
-- Do NOT call this method during on_load.
-- See documentation at top of file for details on using events.
-- @param event_name<number>
-- @param func<function>
@ -284,6 +295,7 @@ function Event.remove_removable_function(event_name, func)
end
--- Register a token handler for the nth tick that can be safely added and removed at runtime.
-- Do NOT call this method during on_load.
-- See documentation at top of file for details on using events.
-- @param event_name<number>
-- @param token<number>
@ -299,15 +311,14 @@ function Event.add_removable_nth_tick(tick, token)
tokens[#tokens + 1] = token
end
-- If this is called before runtime, we don't need to add the handlers
-- as they will be added later either in on_init or on_load.
if EventCore.runtime then
if handlers_added then
local handler = Token.get(token)
core_on_nth_tick(tick, handler)
end
end
--- Removes a token handler for the nth tick.
-- Do NOT call this method during on_load.
-- See documentation at top of file for details on using events.
-- @param event_name<number>
-- @param token<number>
@ -331,6 +342,7 @@ end
--- Register a handler for the nth tick that can be safely added and removed at runtime.
-- The handler must not be a closure, as that is a desync risk.
-- Do NOT call this method during on_load.
-- See documentation at top of file for details on using events.
-- @param event_name<number>
-- @param func<function>
@ -353,14 +365,13 @@ function Event.add_removable_nth_tick_function(tick, func)
funcs[#funcs + 1] = func
end
-- If this is called before runtime, we don't need to add the handlers
-- as they will be added later either in on_init or on_load.
if EventCore.runtime then
if handlers_added then
core_on_nth_tick(tick, func)
end
end
--- Removes a handler for the nth tick.
-- Do NOT call this method during on_load.
-- See documentation at top of file for details on using events.
-- @param event_name<number>
-- @param func<function>
@ -409,6 +420,8 @@ local function add_handlers()
core_on_nth_tick(tick, handler)
end
end
handlers_added = true
end
core_on_init(add_handlers)

View File

@ -6,7 +6,7 @@ local Public = {}
local init_event_name = -1
local load_event_name = -2
Public.runtime = false
Public.runtime = false -- Set to true after on_init or on_load has finished.
-- map of event_name to handlers[]
local event_handlers = {}
@ -60,6 +60,9 @@ function Public.add(event_name, handler)
script.on_event(event_name, on_event)
else
table.insert(handlers, handler)
if #handlers == 1 then
script.on_event(event_name, on_event)
end
end
end
@ -71,6 +74,9 @@ function Public.on_init(handler)
script.on_init(on_init)
else
table.insert(handlers, handler)
if #handlers == 1 then
script.on_init(on_init)
end
end
end
@ -82,6 +88,9 @@ function Public.on_load(handler)
script.on_load(on_load)
else
table.insert(handlers, handler)
if #handlers == 1 then
script.on_load(on_load)
end
end
end
@ -93,6 +102,9 @@ function Public.on_nth_tick(tick, handler)
script.on_nth_tick(tick, on_nth_tick_event)
else
table.insert(handlers, handler)
if #handlers == 1 then
script.on_nth_tick(tick, on_nth_tick_event)
end
end
end