do not use yet

This commit is contained in:
Evert Prants 2018-04-07 21:32:41 +03:00
parent 363d12194e
commit 7474e79099
Signed by: evert
GPG Key ID: 1688DA83D222D0B5
12 changed files with 257 additions and 8 deletions

View File

@ -10,5 +10,8 @@ storagetest.devices = {}
-- Network
dofile(modpath.."/network.lua")
-- Items
dofile(modpath.."/items.lua")
-- Nodes
dofile(modpath.."/nodes.lua")

4
items.lua Normal file
View File

@ -0,0 +1,4 @@
-- Storagetest items
-- Drives
dofile(storagetest.modpath.."/items/storage_disk.lua")

133
items/storage_disk.lua Normal file
View File

@ -0,0 +1,133 @@
-- Storage disks
storagetest.disks = {}
local function inv_to_table(inv)
local t = {}
for listname, list in pairs(inv:get_lists()) do
local size = inv:get_size(listname)
if size then
t[listname] = {}
for i = 1, size, 1 do
t[listname][i] = inv:get_stack(listname, i):to_table()
end
end
end
return t
end
local function table_to_inv(inv, t)
for listname, list in pairs(t) do
for i, stack in pairs(list) do
inv:set_stack(listname, i, stack)
end
end
end
local function save_inv_itemstack(inv, stack)
local meta = stack:get_meta()
meta:set_string("storagetest_inventory", minetest.serialize(inv_to_table(inv)))
return stack
end
function storagetest.disks.register_disk(index, desc, capacity)
local mod = minetest.get_current_modname()
minetest.register_craftitem(mod..":storage_disk"..index, {
description = desc.."\nStores "..capacity.." Stacks",
inventory_image = "storagetest_disk"..index..".png",
groups = {storagetest_disk = 1},
storagetest_capacity = capacity,
storagetest_name = "disk"..index,
stack_max = 1,
on_secondary_use = function (itemstack, user, pointed_thing)
local inv, stack = storagetest.disks.add_stack(itemstack, ItemStack("default:cobble 99"))
if not inv then print("full!"); return itemstack end
return stack
end
})
end
-- Make sure stack is disk
function storagetest.disks.is_valid_disk(stack)
local stack_name = stack:get_name()
return minetest.get_item_group(stack_name, "storagetest_disk") > 0
end
function storagetest.disks.get_stack_inventory(stack)
if not storagetest.disks.is_valid_disk(stack) then return nil end
local stack_name = stack:get_name()
local meta = stack:get_meta()
local name = minetest.registered_items[stack_name].storagetest_name
local capacity = minetest.registered_items[stack_name].storagetest_capacity
local inv = minetest.create_detached_inventory(name, {
})
inv:set_size("main", capacity)
local invmetastring = meta:get_string("storagetest_inventory")
if invmetastring ~= "" then
table_to_inv(inv, minetest.deserialize(invmetastring))
save_inv_itemstack(inv, stack)
end
return inv, stack
end
function storagetest.disks.save_stack_inventory(inv, stack)
if not storagetest.disks.is_valid_disk(stack) then return nil end
stack = save_inv_itemstack(inv, stack)
local meta = stack:get_meta()
local capacity = minetest.registered_items[stack:get_name()].storagetest_capacity
local desc = minetest.registered_items[stack:get_name()].description
meta:set_string("description", desc.."\nContains "..storagetest.disks.get_stack_count(nil, inv).."/"..capacity)
return inv, stack
end
function storagetest.disks.get_stack_count(stack, invn)
local inv = invn or storagetest.disks.get_stack_inventory(stack)
if not inv then return 0 end
local count = 0
for _,v in pairs(inv:get_list("main")) do
if not v:is_empty() then
count = count + 1
end
end
return count
end
function storagetest.disks.add_stack(stack, item)
local inv = storagetest.disks.get_stack_inventory(stack)
if not inv then return nil end
if not inv:room_for_item("main", item) then return nil end
inv:add_item("main", item)
return storagetest.disks.save_stack_inventory(inv, stack)
end
function storagetest.disks.has_stack(stack, item)
local inv = storagetest.disks.get_stack_inventory(stack)
if not inv then return nil end
return inv:contains_item("main", item, true)
end
function storagetest.disks.take_stack(stack, item)
local inv = storagetest.disks.get_stack_inventory(stack)
if not inv then return nil end
local item = inv:remove_item("main", item)
inv, stack = storagetest.disks.save_stack_inventory(inv, stack)
return item, stack
end
local capacities = {1000, 8000, 16000, 32000, 64000}
local descriptions = {"1K Disk", "8K Disk", "16K Disk", "32K Disk", "64K Disk"}
for i = 1, 5 do
storagetest.disks.register_disk(i, descriptions[i], capacities[i])
end

View File

@ -1,3 +1,3 @@
name = storagetest
description = A solution to your hoarding addiction.
optional_depends = default
depends = default

View File

@ -171,13 +171,6 @@ minetest.register_chatcommand("storagectl", {
end
})
local function concatinate(t1,t2)
for i=1,#t2 do
t1[#t1+1] = t2[i]
end
return t1
end
function storagetest.network.register_abm_controller(name)
minetest.register_abm({
nodenames = {name},

View File

@ -1,13 +1,87 @@
-- Disk Drive
local function get_formspec()
return "size[8,8.5]"..
default.gui_bg..
default.gui_bg_img..
default.gui_slots..
"label[0,0;Disk Drive]"..
"list[context;main;1,1;6,1;]"..
"list[current_player;main;0,4.25;8,1;]"..
"list[current_player;main;0,5.5;8,3;8]"..
"listring[context;main]"..
"listring[current_player;main]"..
default.get_hotbar_bg(0, 4.25)
end
local function count_inv(inv)
local count = 0
for _,stack in pairs(inv:get_list("main")) do
if not stack:is_empty() then
count = count + 1
end
end
return count
end
local function timer(pos, elapsed)
local refresh = false
local meta = minetest.get_meta(pos)
local node = minetest.get_node(pos)
local inv = meta:get_inventory()
local count = count_inv(inv)
local cnname = minetest.registered_nodes[node.name]["_basename"]
node.name = cnname..count
storagetest.helpers.swap_node(pos, node)
return refresh
end
local function allow_metadata_inventory_put (pos, listname, index, stack, player)
if minetest.is_protected(pos, player:get_player_name()) then
return 0
end
if minetest.get_item_group(stack:get_name(), "storagetest_disk") == 0 then
return 0
end
return stack:get_count()
end
local function allow_metadata_inventory_move (pos, from_list, from_index, to_list, to_index, count, player)
local meta = minetest.get_meta(pos)
local inv = meta:get_inventory()
local stack = inv:get_stack(from_list, from_index)
return allow_metadata_inventory_put(pos, to_list, to_index, stack, player)
end
local function allow_metadata_inventory_take (pos, listname, index, stack, player)
if minetest.is_protected(pos, player:get_player_name()) then
return 0
end
return stack:get_count()
end
function storagetest.get_all_inventories(pos)
local node = minetest.get_node(pos)
if minetest.get_item_group(node.name, "storagetest_storage") == 0 then return nil end
local meta = minetest.get_meta(pos)
local inv = meta:get_inventory()
local drives = inv:get_list("meta")
local inventories = {}
for i, v in pairs(drives) do
if not v:is_empty() then
local inv1, stack = storagetest.disks.get_stack_inventory(v)
inventories[i] = {inventory = inv1, stack = stack}
end
end
return inventories
end
local function register_disk_drive(index)
local groups = {
cracky = 1,
@ -29,13 +103,32 @@ local function register_disk_drive(index)
"storagetest_drive_side.png", "storagetest_drive_side.png", "storagetest_drive.png"..driveoverlay,
},
drop = "storagetest:disk_drive0",
_basename = "storagetest:disk_drive",
paramtype2 = "facedir",
on_timer = timer,
groups = groups,
on_construct = function (pos)
storagetest.network.clear_networks(pos)
local meta = minetest.get_meta(pos)
meta:set_string("formspec", get_formspec())
local inv = meta:get_inventory()
inv:set_size("main", 6)
end,
on_destruct = storagetest.network.clear_networks,
allow_metadata_inventory_put = allow_metadata_inventory_put,
allow_metadata_inventory_take = allow_metadata_inventory_take,
allow_metadata_inventory_move = allow_metadata_inventory_move,
on_metadata_inventory_move = function(pos)
minetest.get_node_timer(pos):start(0.02)
end,
on_metadata_inventory_put = function(pos)
minetest.get_node_timer(pos):start(0.02)
end,
on_metadata_inventory_take = function(pos)
minetest.get_node_timer(pos):start(0.02)
end,
})
storagetest.devices["storagetest:disk_drive"..index] = true

View File

@ -1,5 +1,22 @@
-- Storage Grid
storagetest.grid = {}
function storagetest.grid.get_formspec(inventories, scroll_lvl, craft_inv)
return "size[8,12]"..
default.gui_bg..
default.gui_bg_img..
default.gui_slots..
"label[0,0;Grid]"..
"list[context;main;0,0;1,1;]"..
"list[context;grid;0,1;7,6;]"..
"list[current_player;main;0,8;8,1;]"..
"list[current_player;main;0,9.2;8,3;8]"..
"listring[context;main]"..
"listring[current_player;main]"..
default.get_hotbar_bg(0, 8)
end
local function timer(pos, elapsed)
local refresh = false
local meta = minetest.get_meta(pos)
@ -23,6 +40,12 @@ minetest.register_node("storagetest:grid", {
},
on_construct = function (pos)
storagetest.network.clear_networks(pos)
local meta = minetest.get_meta(pos)
meta:set_string("formspec", storagetest.grid.get_formspec(nil, 1))
local inv = meta:get_inventory()
inv:set_size("main", 1)
inv:set_size("grid", 7*6)
end,
on_destruct = storagetest.network.clear_networks,
storagetest_run = storagetest.helpers.grid_refresh,

Binary file not shown.

After

Width:  |  Height:  |  Size: 513 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 546 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 535 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 543 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 518 B