fixed varous item vanishing bugs

This commit is contained in:
theFox6 2020-03-03 17:16:35 +01:00
parent 4c5b36fc88
commit 482eb7bebd
No known key found for this signature in database
GPG Key ID: C884FE8D3BCE128A
7 changed files with 100 additions and 51 deletions

@ -1,5 +1,5 @@
MIT License
Copyright (c) 2017 Elijah Duffy
Copyright (c) 2019-2020 theFox6
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
associated documentation files (the "Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell

@ -4,12 +4,16 @@ MicroExpansion - ME [microexpansion]
======================================
* **Licence:** Code: MIT (see LICENSE), Media: CC-BY-SA 3.0
* [Github Repository](https://github.com/octacian/microexpansion)
* **Downloads:**
* [Master (Unstable)](https://github.com/octacian/microexpansion/archive/master.zip)
* ...or browse the code on [GitHub](https://github.com/octacian/microexpansion)
When you really get into a survival world, you typically end up with a lot of items, like a ton of items. Sometimes literally. But with that huge amount of resources, comes something annoying and typically unwanted: chests. Well, of course. You have to have chests to store items, but no biggie, it's just chests. Then your storage room starts to grow, soon you have 25 chests, then 50, then 100. Management gets kinda hard. MicroExpansion, is the solution.
When you really get into a survival world, you typically end up with a lot of items, like a ton of items.
Sometimes literally.
But with that huge amount of resources, comes something annoying and typically unwanted: chests.
Well, of course. You have to have chests to store items, but no biggie, it's just chests.
Then your storage room starts to grow, soon you have 25 chests, then 50, then 100.
When management gets too hard. MicroExpansion, is the solution.
Remade by theFox, original mod by octacian.
Originally inspired by Applied Energistics 2 for Minecraft, MicroExpansion introduces many new nodes and items giving the player simpler and more compact ways to store thousands of items inside of a single ME drive.
Forked and continued by theFox6 from the original mod by octacian.
[Original Repository](https://github.com/octacian/microexpansion)
Original license: MIT
This mod was inspired by Applied Energistics 2 for Minecraft
MicroExpansion introduces many new nodes and items giving the player simpler and more compact ways to store thousands of items inside of a single ME drive.

@ -3,11 +3,7 @@ me.networks = {}
local networks = me.networks
local path = microexpansion.get_module_path("network")
function me.insert_item(stack, inv, listname)
if me.settings.huge_stacks == false then
inv:add_item(listname, stack)
return
end
local function split_stack_values(stack)
local stack_name
local stack_count
if type(stack) == "string" then
@ -22,6 +18,14 @@ function me.insert_item(stack, inv, listname)
stack_name = stack:get_name()
stack_count = stack:get_count()
end
return stack_name, stack_count
end
function me.insert_item(stack, inv, listname)
if me.settings.huge_stacks == false then
return inv:add_item(listname, stack)
end
local stack_name,stack_count = split_stack_values(stack)
local found = false
for i = 0, inv:get_size(listname) do
local inside = inv:get_stack(listname, i)
@ -40,7 +44,7 @@ function me.insert_item(stack, inv, listname)
end
end
if not found then
inv:add_item(listname, stack)
return inv:add_item(listname, stack)
end
end

@ -7,7 +7,8 @@ local network = {
power_load = 0,
power_storage = 0
}
microexpansion.network = network
local me = microexpansion
me.network = network
--- construct a new network
-- @function [parent=#network] new
@ -30,7 +31,7 @@ function network.can_connect(np)
if np.name then
nn = np.name
else
local node = microexpansion.get_node(np)
local node = me.get_node(np)
nn = node.name
end
end
@ -55,7 +56,7 @@ function network.adjacent_connected_nodes(pos, include_ctrl)
local nodes = {}
for _,apos in pairs(adjacent) do
local napos = microexpansion.get_node(apos)
local napos = me.get_node(apos)
local nn = napos.name
if network.can_connect(nn) then
if include_ctrl == false then
@ -123,7 +124,7 @@ local function get_drive_capacity(pos)
local meta = minetest.get_meta(pos)
local inv = meta:get_inventory()
for i = 1, inv:get_size("main") do
cap = cap + microexpansion.get_cell_size(inv:get_stack("main", i):get_name())
cap = cap + me.get_cell_size(inv:get_stack("main", i):get_name())
end
return cap
end
@ -133,8 +134,8 @@ end
-- @return #number the total number of items that can be stored in the network
function network:get_item_capacity()
local cap = 0
for npos in microexpansion.connected_nodes(self.controller_pos) do
if microexpansion.get_node(npos).name == "microexpansion:drive" then
for npos in me.connected_nodes(self.controller_pos) do
if me.get_node(npos).name == "microexpansion:drive" then
cap = cap + get_drive_capacity(npos)
end
end
@ -142,7 +143,22 @@ function network:get_item_capacity()
return cap
end
function network:add_storage_slots(count,listname)
local function remove_slots(inv,ln,target,csize)
for i = target, csize do
local s = inv:get_stack(ln,i)
if not s:is_empty() then
inv:set_stack(ln, i, "")
me.insert_item(s, inv, ln)
end
end
--perhaps allow list removal
if target < 0 then
target = 1
end
inv:set_size(ln, target)
end
function network:set_storage_space(count,listname)
local c = count or 1
local ln = listname or "main"
local inv = self:get_inventory()
@ -167,18 +183,17 @@ function network:add_storage_slots(count,listname)
end
end
local needed = c - space
if needed > 0 then
needed = needed + csize
--TODO allow list removal
if needed == 0 then
needed = 1
end
inv:set_size(ln, needed)
elseif needed < 0 then
needed = needed + csize
remove_slots(inv,ln,needed,csize)
end
end
--FIXME: add size removal function because items are removed when size decreases
function network:update()
self:add_storage_slots(true)
self:set_storage_space(true)
end
function network:get_inventory_name()
@ -217,14 +232,14 @@ local function create_inventory(net)
end,
on_put = function(inv, listname, _, stack)
inv:remove_item(listname, stack)
microexpansion.insert_item(stack, inv, listname)
net:add_storage_slots(true)
me.insert_item(stack, inv, listname)
net:set_storage_space(true)
end,
allow_take = function(_, _, _, stack)
return math.min(stack:get_count(),stack:get_stack_max())
end,
on_take = function()
net:add_storage_slots(true)
net:set_storage_space(true)
end
})
end

@ -51,17 +51,35 @@ function microexpansion.int_to_pagenum(int)
end
-- [function] Move items from inv to inv
function microexpansion.move_inv(inv1, inv2)
function microexpansion.move_inv(inv1, inv2, max)
if max <= 0 then return end
local finv, tinv = inv1.inv, inv2.inv
local fname, tname = inv1.name, inv2.name
local huge = inv2.huge
local inserted = 0
--FIXME only as many as allowed in a drive
for _,v in ipairs(finv:get_list(fname) or {}) do
local left = max-inserted
if left <= 0 then
break;
end
if not v:is_empty() then
if v:get_count() > left then
v = v:peek_item(left)
end
if tinv and tinv:room_for_item(tname, v) then
local leftover = tinv:add_item( tname, v )
if huge then
microexpansion.insert_item(v, tinv, tname)
finv:remove_item(fname, v)
else
local leftover = tinv:add_item(tname, v)
finv:remove_item(fname, v)
if leftover and not(leftover:is_empty()) then
finv:add_item(fname, v)
minetest.log("warning","leftover items when transfering inventory")
finv:add_item(fname, leftover)
end
end
inserted = inserted + v:get_count()
end
end
end

@ -80,7 +80,7 @@ microexpansion.register_node("drive", {
me.update_connected_machines(pos)
return
end
network:add_storage_slots(#items)
network:set_storage_space(#items)
for _,s in pairs(items) do
me.insert_item(s, ctrl_inv, "main")
end
@ -114,10 +114,10 @@ microexpansion.register_node("drive", {
if stack_name ~= "" then
local item_count = stack_inside:get_count()
while item_count ~= 0 and cell_idx ~= nil do
--print(("stack to store: %s %i"):format(stack_name,item_count))
if size < items_in_cell_count + item_count then
local space = size - items_in_cell_count
item_count = item_count - space
table.insert(cell_items,stack_name.." "..space)
table.insert(cell_items,("%s %i"):format(stack_name,space))
items_in_cell_count = items_in_cell_count + space
own_inv:set_stack("main", cell_idx, write_to_cell(cells[cell_idx],cell_items,items_in_cell_count))
@ -125,13 +125,14 @@ microexpansion.register_node("drive", {
size = microexpansion.get_cell_size(cells[cell_idx]:get_name())
items_in_cell_count = 0
cell_items = {}
item_count = item_count - space
if cell_idx == nil then
--there may be other drives within the network
minetest.log("info","too many items to store in drive")
end
else
items_in_cell_count = items_in_cell_count + item_count
table.insert(cell_items,stack_inside:to_string())
table.insert(cell_items, ("%s %i"):format(stack_name,item_count))
item_count = 0
end
end
@ -165,7 +166,7 @@ microexpansion.register_node("drive", {
--this returns 99 (max count) even if it removes more
ctrl_inv:remove_item("main", ostack)
end
print(stack:to_string())
--print(stack:to_string())
network:update()
me.update_connected_machines(pos)

@ -191,9 +191,16 @@ microexpansion.register_node("term", {
meta:set_string("formspec", chest_formspec(pos, 1, "main", page_max))
elseif fields.tochest then
local pinv = minetest.get_inventory({type="player", name=sender:get_player_name()})
net:add_storage_slots(pinv:get_size("main"))
microexpansion.move_inv({ inv=pinv, name="main" }, { inv=ctrl_inv, name="main" })
net:add_storage_slots(true)
net:set_storage_space(pinv:get_size("main"))
local space = net:get_item_capacity()
local contents = ctrl_inv:get_list("main") or {}
for _,s in pairs(contents) do
if not s:is_empty() then
space = space - s:get_count()
end
end
microexpansion.move_inv({ inv=pinv, name="main" }, { inv=ctrl_inv, name="main",huge=true }, space)
net:set_storage_space(true)
end
end,
})