initial storage items handling

This commit is contained in:
Alexander Weber 2017-12-11 22:07:47 +01:00
parent 701390de57
commit 14a3809430
7 changed files with 88 additions and 26 deletions

3
API.md
View File

@ -8,6 +8,7 @@
- `hwdef.sequence = { "variant_1_name", "variant_2_name", "variant_3_name" }` On punch swaps sequence. the first variant is in creative inventory
- `hwdef.custom_launcer` - optional - custom launcher name
- `hwdef.custom_theme` - optional - custom initial theme name
- `hwdef.allowed_removable_groups` - optional. allowed removable groups. Supported "laptop_removable" = all, "laptop_removable_floppy" or "laptop_removable_usb"
- `hwdef.node_defs = {
variant_1_name = {
hw_state = "resume", "power_on" or "power_off", -- Hardware state
@ -40,7 +41,7 @@ Usable from node functions, from apps or outsite
- `mtos:set_app(appname)` - Start/Enable/navigate to appname. If no appname given the launcher is called
- `mtos:get_theme(theme)`- Get theme data current or requested (theme parameter is optional)
- `mtos:set_theme(theme)`- Activate theme
- `mtos:get_node_inventory()` - Access to the item in node inventory (low-level)
## App Definition
`laptop.register_app(internal_shortname, { definitiontable })` - add a new app or view

36
apps/removable_app.lua Normal file
View File

@ -0,0 +1,36 @@
laptop.register_app("removable", {
app_name = "Removable storage",
app_icon = "laptop_setting_wrench.png",
app_info = "Work with removable media.",
formspec_func = function(app, mtos)
local inv = mtos:get_node_inventory()
local formspec =
"list[nodemeta:"..mtos.pos.x..','..mtos.pos.y..','..mtos.pos.z..";main;0,0.3;1,1;]" ..
"list[current_player;main;0,4.85;8,1;]" ..
"list[current_player;main;0,6.08;8,3;8]" ..
"listring[nodemeta:"..mtos.pos.x..','..mtos.pos.y..','..mtos.pos.z..";main]" ..
"listring[current_player;main]"
return formspec
end,
-- check if the item is compatible to the computer
allow_metadata_inventory_put = function(app, mtos, sender, listname, index, stack)
local def = stack:get_definition()
if not def or not def.groups.laptop_removable then return end -- not supported
local removable = mtos.hwdef.allowed_removable_groups or { "laptop_removable" }
for _, v in ipairs(removable) do
if def.groups[v] then
return 1
end
end
end,
-- item removal always allowed from inventory
allow_metadata_inventory_take = function(app, mtos, sender, listname, index, stack)
return 1
end,
receive_fields_func = function(app, mtos, sender, fields)
end
})

View File

@ -5,6 +5,7 @@ dofile(minetest.get_modpath('laptop')..'/app_fw.lua')
dofile(minetest.get_modpath('laptop')..'/os.lua')
dofile(minetest.get_modpath('laptop')..'/node_fw.lua')
dofile(minetest.get_modpath('laptop')..'/nodes.lua')
dofile(minetest.get_modpath('laptop')..'/items.lua')
-- uncomment this line to disable demo apps in production
--dofile(minetest.get_modpath('laptop')..'/demo_apps.lua')

14
items.lua Normal file
View File

@ -0,0 +1,14 @@
minetest.register_craftitem("laptop:floppy", {
description = 'High density floppy',
inventory_image = "laptop_diskette.png",
groups = {laptop_removable = 1, laptop_removable_floppy = 1},
stack_max = 1,
})
minetest.register_craftitem("laptop:usbstick", {
description = 'USB storage stick',
inventory_image = "laptop_usb.png",
groups = {laptop_removable = 1, laptop_removable_usb = 1},
stack_max = 1,
})

View File

@ -2,29 +2,29 @@
laptop.node_config = {}
local function after_place_node(pos, placer, itemstack, pointed_thing)
local appdata = minetest.deserialize(itemstack:get_meta():get_string("laptop_appdata"))
if appdata then
local mtos = laptop.os_get(pos)
mtos.appdata = appdata
mtos.appdata.launcher = mtos.appdata.launcher or {}
mtos.appdata.os = mtos.appdata.os or {}
mtos.appdata.os.stack = mtos.appdata.os.stack or {}
mtos:save()
local save = minetest.deserialize(itemstack:get_meta():get_string("laptop_metadata"))
if save then
local meta = minetest.get_meta(pos)
meta:from_table(save.fields)
meta:get_inventory():set_list("main", save.invlist)
end
end
local function after_dig_node(pos, oldnode, oldmetadata, digger)
local appdata = oldmetadata.fields['laptop_appdata']
if appdata then
local item_name = minetest.registered_items[oldnode.name].drop or oldnode.name
local inventory = digger:get_inventory()
for idx = inventory:get_size("main"), 1, -1 do
local stack = inventory:get_stack("main", idx)
if stack:get_name() == item_name and stack:get_meta():get_string("laptop_appdata") == "" then
stack:get_meta():set_string("laptop_appdata", appdata)
digger:get_inventory():set_stack("main", idx, stack)
break
end
local save = { fields = oldmetadata.fields, invlist = {} }
if oldmetadata.inventory and oldmetadata.inventory.main then
for _, stack in ipairs(oldmetadata.inventory.main) do
table.insert(save.invlist, stack:to_string())
end
end
local item_name = minetest.registered_items[oldnode.name].drop or oldnode.name
local inventory = digger:get_inventory()
for idx, stack in ipairs(inventory:get_list("main")) do
if stack:get_name() == item_name and stack:get_meta():get_string("laptop_metadata") == "" then
stack:get_meta():set_string("laptop_metadata", minetest.serialize(save))
digger:get_inventory():set_stack("main", idx, stack)
break
end
end
end
@ -45,6 +45,7 @@ local function on_construct(pos)
end
local function on_punch(pos, node, puncher)
-- TODO: Check if wielded item is an removable. Replace stack in this case
local mtos = laptop.os_get(pos)
local hwdef = laptop.node_config[node.name]
if hwdef.next_node then
@ -66,32 +67,32 @@ end
local function allow_metadata_inventory_move(pos, from_list, from_index, to_list, to_index, count, player)
local mtos = laptop.os_get(pos)
return mtos:pass_to_app("allow_metadata_inventory_move", false, player, from_list, from_index, to_list, to_index, count)
return mtos:pass_to_app("allow_metadata_inventory_move", false, player, from_list, from_index, to_list, to_index, count) or 0
end
local function allow_metadata_inventory_put(pos, listname, index, stack, player)
local mtos = laptop.os_get(pos)
return mtos:pass_to_app("allow_metadata_inventory_put", false, player, listname, index, stack)
return mtos:pass_to_app("allow_metadata_inventory_put", false, player, listname, index, stack) or 0
end
local function allow_metadata_inventory_take(pos, listname, index, stack, player)
local mtos = laptop.os_get(pos)
return mtos:pass_to_app("allow_metadata_inventory_take", false, player, listname, index, stack)
return mtos:pass_to_app("allow_metadata_inventory_take", false, player, listname, index, stack) or 0
end
local function on_metadata_inventory_move(pos, from_list, from_index, to_list, to_index, count, player)
local mtos = laptop.os_get(pos)
return mtos:pass_to_app("on_metadata_inventory_move", true, player, from_list, from_index, to_list, to_index, count)
mtos:pass_to_app("on_metadata_inventory_move", true, player, from_list, from_index, to_list, to_index, count)
end
local function on_metadata_inventory_put(pos, listname, index, stack, player)
local mtos = laptop.os_get(pos)
return mtos:pass_to_app("on_metadata_inventory_put", true, player, listname, index, stack)
mtos:pass_to_app("on_metadata_inventory_put", true, player, listname, index, stack)
end
local function on_metadata_inventory_take(pos, listname, index, stack, player)
local mtos = laptop.os_get(pos)
return mtos:pass_to_app("on_metadata_inventory_take", true, player, listname, index, stack)
mtos:pass_to_app("on_metadata_inventory_take", true, player, listname, index, stack)
end
function laptop.register_hardware(name, hwdef)

View File

@ -74,6 +74,7 @@ laptop.register_hardware("laptop:cube", {
infotext = "CUBE PC",
sequence = { "off", "on"},
custom_theme = "Red",
allowed_removable_groups = { "laptop_removable_floppy" },
node_defs = {
["on"] = {
hw_state = "power_on",
@ -254,6 +255,7 @@ laptop.register_hardware("laptop:monitor3", {
infotext = "Pentium 3",
sequence = { "off", "on"},
custom_launcher = "launcher_insert_floppy",
allowed_removable_groups = { "laptop_removable_floppy" },
node_defs = {
["on"] = {
hw_state = "power_on",

7
os.lua
View File

@ -7,6 +7,13 @@ laptop.class_lib.os = os_class
local mod_storage = minetest.get_mod_storage()
-- Setup internal inventory slot
function os_class:get_node_inventory()
local inv = self.meta:get_inventory()
inv:set_size("main", 1) -- 1 disk supported
return inv
end
-- Swap the node
function os_class:swap_node(new_node_name)
local node = minetest.get_node(self.pos)