mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-25 08:23:13 +09:00
Support synchronizing crafting queue in 2.0 with new storage api
This commit is contained in:
1 parent
4248e57dcb
commit
2fb33c9648
1 file changed
+48
-17
@@ -131,11 +131,11 @@ function serialize.serialize_crafting_queue(player)
|
|||||||
-- Save current items
|
-- Save current items
|
||||||
local inventory = player.get_main_inventory()
|
local inventory = player.get_main_inventory()
|
||||||
local old_items = inventory.get_contents()
|
local old_items = inventory.get_contents()
|
||||||
|
local crafting_queue_progress = player.crafting_queue_progress
|
||||||
|
|
||||||
-- Cancel old crafts to get the items back
|
-- Cancel old crafts to get the items back
|
||||||
while player.crafting_queue_size > 0 do
|
while player.crafting_queue_size > 0 do
|
||||||
local old_queue = player.crafting_queue
|
local old_queue = player.crafting_queue
|
||||||
local queueItem = player.crafting_queue[1]
|
|
||||||
|
|
||||||
-- Cancel craft
|
-- Cancel craft
|
||||||
player.cancel_crafting {
|
player.cancel_crafting {
|
||||||
@@ -180,22 +180,50 @@ function serialize.serialize_crafting_queue(player)
|
|||||||
-- game.print("Saved craft "..oldItem.recipe)
|
-- game.print("Saved craft "..oldItem.recipe)
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Find amount of items added and remove from inventory
|
|
||||||
local new_items = inventory.get_contents()
|
|
||||||
local difference = {}
|
local difference = {}
|
||||||
for k,v in pairs(new_items) do
|
if v2_storage_api then
|
||||||
local old_count = old_items[k]
|
-- Find amount of items added and remove from inventory
|
||||||
local diff = v
|
local new_items = inventory.get_contents()
|
||||||
if old_count ~= nil then
|
-- Build map of old counts by item name and quality
|
||||||
diff = diff - old_count
|
local old_counts = {}
|
||||||
|
for _, item in ipairs(old_items) do
|
||||||
|
local key = item.name .. ":" .. item.quality
|
||||||
|
old_counts[key] = (old_counts[key] or 0) + item.count
|
||||||
end
|
end
|
||||||
if diff > 0 then
|
|
||||||
local ingredient = {
|
-- Compare with new items to find differences
|
||||||
name = k,
|
for _, item in ipairs(new_items) do
|
||||||
count = diff,
|
local key = item.name .. ":" .. item.quality
|
||||||
}
|
local old_count = old_counts[key] or 0
|
||||||
inventory.remove(ingredient)
|
local diff = item.count - old_count
|
||||||
table.insert(difference, ingredient)
|
|
||||||
|
if diff > 0 then
|
||||||
|
-- We don't have to worry about quality because quality can't be handcrafted
|
||||||
|
local ingredient = {
|
||||||
|
name = item.name,
|
||||||
|
count = diff
|
||||||
|
}
|
||||||
|
inventory.remove(ingredient)
|
||||||
|
table.insert(difference, ingredient)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
else
|
||||||
|
-- Find amount of items added and remove from inventory
|
||||||
|
local new_items = inventory.get_contents()
|
||||||
|
for k,v in pairs(new_items) do
|
||||||
|
local old_count = old_items[k]
|
||||||
|
local diff = v
|
||||||
|
if old_count ~= nil then
|
||||||
|
diff = diff - old_count
|
||||||
|
end
|
||||||
|
if diff > 0 then
|
||||||
|
local ingredient = {
|
||||||
|
name = k,
|
||||||
|
count = diff,
|
||||||
|
}
|
||||||
|
inventory.remove(ingredient)
|
||||||
|
table.insert(difference, ingredient)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -204,6 +232,7 @@ function serialize.serialize_crafting_queue(player)
|
|||||||
|
|
||||||
local serialized = {
|
local serialized = {
|
||||||
crafting_queue = crafting_queue,
|
crafting_queue = crafting_queue,
|
||||||
|
crafting_queue_progress = crafting_queue_progress,
|
||||||
ingredients = difference,
|
ingredients = difference,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -236,6 +265,8 @@ function serialize.deserialize_crafting_queue(player, serialized)
|
|||||||
|
|
||||||
-- Remove extra inventory slots
|
-- Remove extra inventory slots
|
||||||
player.character_inventory_slots_bonus = player.character_inventory_slots_bonus - 1000
|
player.character_inventory_slots_bonus = player.character_inventory_slots_bonus - 1000
|
||||||
|
-- Set progress of current craft
|
||||||
|
player.crafting_queue_progress = serialized.crafting_queue_progress
|
||||||
end
|
end
|
||||||
|
|
||||||
local controller_to_name = {}
|
local controller_to_name = {}
|
||||||
@@ -306,8 +337,8 @@ function serialize.serialize_player(player)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Serialize crafting queue, TODO implement v2_storage_api
|
-- Serialize crafting queue
|
||||||
if player.character and not v2_storage_api then
|
if player.character then
|
||||||
serialized.crafting_queue = serialize.serialize_crafting_queue(player)
|
serialized.crafting_queue = serialize.serialize_crafting_queue(player)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user