Armor equipment is lost when using the armor as an ingredient in crafting

Things that we don't consider worth fixing at this moment.
Post Reply
troonie
Burner Inserter
Burner Inserter
Posts: 14
Joined: Thu Apr 21, 2016 4:13 pm
Contact:

Armor equipment is lost when using the armor as an ingredient in crafting

Post by troonie »

With Space Exploration mod installed, armor recipes are tweaked so a next tier armor uses a previous tier armor as an ingredient. E.g, Power Armor MK1 is crafted from Modular Armor. After such craft, equipment previously installed in the Modular Armor grid is lost. It would be better to have it put into inventory instead. Not sure what's better to do if the crafting takes place in an assembly machine.

Alternatively, the equipment can be automatically put into the newly crafted armor, but it may not always be possible in more general case.

User avatar
ickputzdirwech
Filter Inserter
Filter Inserter
Posts: 638
Joined: Sun May 07, 2017 10:16 am
Contact:

Re: Armor equipment is lost when using the armor as an ingredient in crafting

Post by ickputzdirwech »

I solved this issue some time ago for my own mod. Feel free to send this code to other mod authors.

Code: Select all

local function initialize()
	if global.ick_queued_armor == nil then
		global.ick_queued_armor = {}
	end
end

script.on_init(initialize)
script.on_configuration_changed(initialize)

local function fill_armor_grid(event, stack)
  	local index = event.player_index
  	local recipe = event.recipe.name
	local storage = global.ick_queued_armor[index]
	if stack.name == "modular-armor" or stack.name == "power-armor" then
		if storage and storage[string.sub(recipe, 5)] then
		local saved_grid = storage[string.sub(recipe, 5)]
		for _, equipment in pairs(saved_grid[#saved_grid]) do
			stack.grid.put{name = equipment.name, position = equipment.position}
		end
		table.remove(saved_grid)
		end
	end
end

script.on_event(defines.events.on_pre_player_crafted_item, function(event)
  local index = event.player_index
  local recipe = event.recipe.name
  if recipe == "ick-power-armor" or recipe == "ick-power-armor-mk2" then
    local armor_name = string.sub(recipe, 5)
    if global.ick_queued_armor[index] == nil then
      global.ick_queued_armor[index] = {}
    end
    if global.ick_queued_armor[index][armor_name] == nil then
      global.ick_queued_armor[index][armor_name] = {}
    end
    for i = 1, (#event.items) do
			local stack = event.items[i]
      if (stack.name == "modular-armor" or stack.name == "power-armor") and stack.grid.equipment then
        local grid = {}
        for i, equipment in pairs(stack.grid.equipment) do
          table.insert(grid, {name = equipment.name, position = equipment.position})
        end
        table.insert(global.ick_queued_armor[index][armor_name], grid)
      end
    end
  end
end)

script.on_event(defines.events.on_player_cancelled_crafting, function(event)
	local recipe = event.recipe.name
	if recipe == "ick-power-armor" or recipe == "ick-power-armor-mk2" then
		for i = 1, (#event.items) do
			fill_armor_grid(event, event.items[i])
		end
	end
end)

script.on_event(defines.events.on_player_crafted_item, function(event)
	local recipe = event.recipe.name
	if recipe == "ick-power-armor" or recipe == "ick-power-armor-mk2" then
		fill_armor_grid(event, event.item_stack)
	end
end)
Mods: Shortcuts for 1.1, ick's Sea Block, ick's vanilla tweaks
Tools: Atom language pack
Text quickly seems cold and unfriendly. Be careful how you write and interpret what others have written.
- A reminder for me and all who read what I write

Post Reply

Return to “Won't fix.”