mirror of
https://github.com/Refactorio/RedMew.git
synced 2024-12-14 10:13:13 +02:00
761b2094e2
Converted builder usage to using any instead of combine Added item_to_chest that empties the players inventory to a chest if toggled on. The chest is deleted if emptied by a player
63 lines
2.0 KiB
Lua
63 lines
2.0 KiB
Lua
local RS = require 'map_gen.shared.redmew_surface'
|
|
local Game = require 'utils.game'
|
|
local Event = require 'utils.event'
|
|
|
|
local Public = {}
|
|
|
|
local function create_chest(player)
|
|
local surface = RS.get_surface()
|
|
local pos = surface.find_non_colliding_position('steel-chest', player.position, 0, 1)
|
|
local chest = surface.create_entity { name = 'steel-chest', position = pos, force = player.force }
|
|
chest.minable = false
|
|
return chest
|
|
end
|
|
|
|
function Public.transfer_inventory(player_index, inventories)
|
|
if inventories == nil or player_index == nil then
|
|
return 'You need to specify a player index and a table of define.inventory'
|
|
end
|
|
local player = Game.get_player_by_index(player_index)
|
|
local chest = create_chest(player)
|
|
for _, inventory in pairs(inventories) do
|
|
inventory = player.get_inventory(inventory)
|
|
for name, count in pairs(inventory.get_contents()) do
|
|
local ItemStack = { name = name, count = count }
|
|
inventory.remove(ItemStack)
|
|
while count > 0 do
|
|
if not chest.can_insert(ItemStack) then
|
|
chest = create_chest(player)
|
|
end
|
|
count = count - chest.insert(ItemStack)
|
|
ItemStack = { name = name, count = count }
|
|
end
|
|
end
|
|
end
|
|
return true
|
|
end
|
|
|
|
local function on_gui_closed(event)
|
|
local entity = event.entity
|
|
if entity == nil or not entity.valid then
|
|
return
|
|
end
|
|
if entity.name == 'steel-chest' and entity.minable == false and not entity.has_items_inside() then
|
|
entity.destroy()
|
|
end
|
|
end
|
|
|
|
local function ctrl_empty(event)
|
|
local entity = event.last_entity
|
|
if entity == nil or not entity.valid then
|
|
return
|
|
end
|
|
if entity.name == 'steel-chest' and not entity.minable then
|
|
event.entity = entity
|
|
on_gui_closed(event)
|
|
end
|
|
end
|
|
|
|
Event.add(defines.events.on_gui_closed, on_gui_closed)
|
|
Event.add(defines.events.on_selected_entity_changed, ctrl_empty)
|
|
|
|
return Public
|