1
0
mirror of https://github.com/ComfyFactory/ComfyFactorio.git synced 2025-01-08 00:39:30 +02:00
ComfyFactorio/modules/autohotbar.lua

83 lines
2.2 KiB
Lua
Raw Normal View History

2021-03-24 21:14:55 +02:00
--luacheck: ignore
2021-03-24 17:46:00 +02:00
local event = require 'utils.event'
2019-08-16 23:19:19 +02:00
local function get_empty_hotbar_slot(player)
2021-03-24 17:46:00 +02:00
for i = 1, 20, 1 do
local item = player.get_quick_bar_slot(i)
if not item then
return i
end
end
return false
2019-08-16 23:19:19 +02:00
end
local function is_item_already_present_in_hotbar(player, item)
2021-03-24 17:46:00 +02:00
for i = 1, 20, 1 do
local prototype = player.get_quick_bar_slot(i)
if prototype then
if item == prototype.name then
return true
end
end
end
return false
2019-08-16 23:19:19 +02:00
end
local function set_hotbar(player, item)
2021-03-24 17:46:00 +02:00
if not game.entity_prototypes[item] then
return
end
if not game.recipe_prototypes[item] then
return
end
local slot_index = get_empty_hotbar_slot(player)
if not slot_index then
return
end
if is_item_already_present_in_hotbar(player, item) then
return
end
player.set_quick_bar_slot(slot_index, item)
2019-08-16 23:19:19 +02:00
end
local function on_player_fast_transferred(event)
2021-03-24 17:46:00 +02:00
if not global.auto_hotbar_enabled[event.player_index] then
return
end
local player = game.players[event.player_index]
for name, count in pairs(player.get_main_inventory().get_contents()) do
set_hotbar(player, name)
end
2019-08-16 23:19:19 +02:00
end
local function on_player_crafted_item(event)
2021-03-24 17:46:00 +02:00
if not global.auto_hotbar_enabled[event.player_index] then
return
end
set_hotbar(game.players[event.player_index], event.item_stack.name)
2019-08-16 23:19:19 +02:00
end
local function on_picked_up_item(event)
2021-03-24 17:46:00 +02:00
if not global.auto_hotbar_enabled[event.player_index] then
return
end
set_hotbar(game.players[event.player_index], event.item_stack.name)
2019-08-16 23:19:19 +02:00
end
2019-08-21 01:17:06 +02:00
local function on_player_mined_entity(event)
2021-03-24 17:46:00 +02:00
if not global.auto_hotbar_enabled[event.player_index] then
return
end
set_hotbar(game.players[event.player_index], event.entity.name)
2019-08-21 01:17:06 +02:00
end
2019-10-31 11:13:47 +02:00
local function on_init()
2021-03-24 17:46:00 +02:00
global.auto_hotbar_enabled = {}
2019-10-31 11:13:47 +02:00
end
event.on_init(on_init)
2019-08-16 23:19:19 +02:00
event.add(defines.events.on_player_fast_transferred, on_player_fast_transferred)
event.add(defines.events.on_player_crafted_item, on_player_crafted_item)
2019-08-21 01:17:06 +02:00
event.add(defines.events.on_picked_up_item, on_picked_up_item)
2021-03-24 17:46:00 +02:00
event.add(defines.events.on_player_mined_entity, on_player_mined_entity)