From 888367b4f6ad542b2f272fac00ad105d38aed71c Mon Sep 17 00:00:00 2001 From: octacian Date: Thu, 9 Feb 2017 07:37:14 -0800 Subject: [PATCH] Add ME Chest (#1) Allows the player to interact with items stored in a ME drive. --- api.lua | 9 ++ depends.txt | 1 + init.lua | 3 + machines.lua | 176 ++++++++++++++++++++++++ textures/microexpansion_chest_front.png | Bin 0 -> 331 bytes textures/microexpansion_chest_side.png | Bin 0 -> 292 bytes textures/microexpansion_chest_top.png | Bin 0 -> 338 bytes 7 files changed, 189 insertions(+) create mode 100644 depends.txt create mode 100644 machines.lua create mode 100644 textures/microexpansion_chest_front.png create mode 100644 textures/microexpansion_chest_side.png create mode 100644 textures/microexpansion_chest_top.png diff --git a/api.lua b/api.lua index 0b9edbb..3513177 100644 --- a/api.lua +++ b/api.lua @@ -68,6 +68,7 @@ function microexpansion.register_cell(itemstring, def) minetest.register_craftitem(BASENAME..":"..itemstring, { description = def.description, inventory_image = BASENAME.."_"..def.inventory_image..".png", + groups = {microexpansion_cell = 1}, microexpansion = { drive = { capacity = def.capacity or 5000, @@ -83,3 +84,11 @@ function microexpansion.register_cell(itemstring, def) end end end + +-- [function] Get cell size +function microexpansion.get_cell_size(name) + local item = minetest.registered_craftitems[name] + if item then + return item.microexpansion.drive.capacity + end +end diff --git a/depends.txt b/depends.txt new file mode 100644 index 0000000..4ad96d5 --- /dev/null +++ b/depends.txt @@ -0,0 +1 @@ +default diff --git a/init.lua b/init.lua index c4868bd..0b4d73b 100644 --- a/init.lua +++ b/init.lua @@ -15,3 +15,6 @@ dofile(modpath.."/api.lua") -- Load storage devices dofile(modpath.."/storage.lua") + +-- Load machines +dofile(modpath.."/machines.lua") diff --git a/machines.lua b/machines.lua new file mode 100644 index 0000000..9e585fe --- /dev/null +++ b/machines.lua @@ -0,0 +1,176 @@ +-- microexpansion/machines.lua + +-- [me chest] Get formspec +local function chest_formspec(start_id, listname, page_max, query) + local list + local page_number = "" + local query = query or "" + if not listname then + list = "label[3,2;" .. minetest.colorize("red", "No cell!") .. "]" + else + list = "list[current_name;" .. listname .. ";0,0.3;8,4;" .. (start_id - 1) .. "]" + end + if page_max then + page_number = "label[6.05,4.5;" .. math.floor((start_id / 32)) + 1 .. + "/" .. page_max .."]" + end + return "size[9,10]" .. + default.gui_bg .. + default.gui_bg_img .. + default.gui_slots .. + list .. + "list[current_name;cells;8,1.8;1,1;]" .. + "list[current_player;main;0,5.5;8,1;]" .. + "list[current_player;main;0,6.73;8,3;8]" .. + "button[5.4,4.35;0.8,0.9;prev;<]" .. + "button[7.25,4.35;0.8,0.9;next;>]" .. + "field[0.3,4.6;2.2,1;filter;;" .. query .. "]" .. + "button[2.1,4.5;0.8,0.5;search;?]" .. + "button[2.75,4.5;0.8,0.5;clear;X]" .. + "tooltip[search;Search]" .. + "tooltip[clear;Reset]" .. + "listring[current_name;main]" .. + "listring[current_player;main]" .. + "field_close_on_enter[filter;false]" .. + page_number .. + default.get_hotbar_bg(0,5.50) +end + +-- [me chest] Register node +minetest.register_node("microexpansion:chest", { + description = "ME Chest\nCan interact with items in ME storage cells", + tiles = { + "microexpansion_chest_top.png", + "microexpansion_chest_top.png", + "microexpansion_chest_side.png", + "microexpansion_chest_side.png", + "microexpansion_chest_side.png", + "microexpansion_chest_front.png", + }, + is_ground_content = false, + groups = { cracky = 1 }, + paramtype = "light", + paramtype2 = "facedir", + + on_construct = function(pos) + local meta = minetest.get_meta(pos) + meta:set_string("formspec", chest_formspec(1)) + meta:set_string("inv_name", "none") + meta:set_int("page", 1) + local inv = meta:get_inventory() + inv:set_size("cells", 1) + end, + can_dig = function(pos, player) + local meta = minetest.get_meta(pos) + local inv = meta:get_inventory() + return inv:is_empty("main") + end, + allow_metadata_inventory_put = function(pos, listname, index, stack, player) + if listname == "main" then + return stack:get_count() + elseif listname == "cells" then + if minetest.get_item_group(stack:get_name(), "microexpansion_cell") ~= 0 then + return 1 + else + return 0 + end + else + return 0 + end + end, + on_metadata_inventory_put = function(pos, listname, index, stack, player) + if listname == "main" then + local inv = minetest.get_meta(pos):get_inventory() + inv:remove_item(listname, stack) + inv:add_item(listname, stack) + elseif listname == "cells" then + local meta = minetest.get_meta(pos) + local inv = meta:get_inventory() + local items = minetest.deserialize(stack:get_meta():get_string("items")) + local size = microexpansion.get_cell_size(stack:get_name()) + local page_max = math.floor(size / 32) + 1 + inv:set_size("main", size) + if items then + inv:set_list("main", items) + end + meta:set_string("inv_name", "main") + meta:set_string("formspec", chest_formspec(1, "main", page_max)) + end + end, + on_metadata_inventory_take = function(pos, listname, index, stack, player) + local inv = minetest.get_meta(pos):get_inventory() + if listname == "search" then + inv:remove_item("main", stack) + end + end, + allow_metadata_inventory_take = function(pos, listname, index, stack, player) + if listname == "cells" then + local t = minetest.get_us_time() + local meta = minetest.get_meta(pos) + local inv = meta:get_inventory() + local tab = {} + local new_stack = inv:get_stack(listname, 1) + local item_meta = new_stack:get_meta() + for i = 1, inv:get_size("main") do + if inv:get_stack("main", i):get_name() ~= "" then + tab[#tab + 1] = inv:get_stack("main", i):to_string() + end + end + item_meta:set_string("items", minetest.serialize(tab)) + inv:set_stack(listname, 1, new_stack) + inv:set_size("main", 0) + meta:set_int("page", 1) + meta:set_string("formspec", chest_formspec(1)) + return new_stack:get_count() + end + return stack:get_count() + end, + on_receive_fields = function(pos, formname, fields, sender) + local meta = minetest.get_meta(pos) + local page = meta:get_int("page") + local inv_name = meta:get_string("inv_name") + local inv = meta:get_inventory() + local page_max = math.floor(inv:get_size("main") / 32) + 1 + local cell_stack = inv:get_stack("cells", 1) + if inv_name == "none" then + return + end + if fields.next then + if page + 32 > inv:get_size(inv_name) then + return + end + meta:set_int("page", page + 32) + meta:set_string("formspec", chest_formspec(page + 32, inv_name, page_max)) + elseif fields.prev then + if page - 32 < 1 then + return + end + meta:set_int("page", page - 32) + meta:set_string("formspec", chest_formspec(page - 32, inv_name, page_max)) + elseif fields.search or fields.key_enter_field == "filter" then + inv:set_size("search", 0) + if fields.filter == "" then + meta:set_int("page", 1) + meta:set_string("inv_name", "main") + meta:set_string("formspec", chest_formspec(1, "main", page_max)) + else + local tab = {} + for i = 1, microexpansion.get_cell_size(cell_stack:get_name()) do + local match = inv:get_stack("main", i):get_name():find(fields.filter) + if match then + tab[#tab + 1] = inv:get_stack("main", i) + end + end + inv:set_list("search", tab) + meta:set_int("page", 1) + meta:set_string("inv_name", "search") + meta:set_string("formspec", chest_formspec(1, "search", page_max, fields.filter)) + end + elseif fields.clear then + inv:set_size("search", 0) + meta:set_int("page", 1) + meta:set_string("inv_name", "main") + meta:set_string("formspec", chest_formspec(1, "main", page_max)) + end + end, +}) diff --git a/textures/microexpansion_chest_front.png b/textures/microexpansion_chest_front.png new file mode 100644 index 0000000000000000000000000000000000000000..8706d600dcfcb1bdd9e24d3b86d413aac6d28ff6 GIT binary patch literal 331 zcmeAS@N?(olHy`uVBq!ia0vp^0wB!61|;P_|4#%`Y)RhkE)4%caKYZ?lYt_f1s;*b z3=G`DAk4@xYmNj^kiEpy*OmPt6DPltLSsw(4xo^1W=KSdbAE1aYF-JD%fR4Vl$uzQ znxasiS(2gP?&%wlqL<1J6kqS@;uvCa`sk#+d`$)-Zv0D~CmR`Mc<@NDvBe*3R8Rk4 z+HvG;W8h&Wadu`+o_`k(THoFB++^=taha~91B*=#Y}5%qxqOw=!^nH=ES0nPCT_L) zRd-j~<=UpxMhxdl&u-1V&o$d3ZT@rN_qI)JFLu3CKlya4^=~szyHnn7{k9X4CtN~U$>KCTok-F}Za_xNH2A2JNm)_R>&lmbzoY3>|lF90nWFG(G#jg3% SH+D|}`N`AO&t;ucLK6TUiFxw? literal 0 HcmV?d00001 diff --git a/textures/microexpansion_chest_side.png b/textures/microexpansion_chest_side.png new file mode 100644 index 0000000000000000000000000000000000000000..e39998b4ebac604541e4ec55ddd8f9a24578f60f GIT binary patch literal 292 zcmeAS@N?(olHy`uVBq!ia0vp^0wB!61|;P_|4#%`Y)RhkE)4%caKYZ?lYt_f1s;*b z3=G`DAk4@xYmNj^kiEpy*OmPt6DL2Xn1!FY2T({hGbEzKIX^cyHLnE7WngeFN=+y85}Sb4q9e0Ihvt%>V!Z literal 0 HcmV?d00001 diff --git a/textures/microexpansion_chest_top.png b/textures/microexpansion_chest_top.png new file mode 100644 index 0000000000000000000000000000000000000000..42f1c2047e56f2ae9b2e63897483920d6f40f821 GIT binary patch literal 338 zcmeAS@N?(olHy`uVBq!ia0vp^0wB!61|;P_|4#%`Y)RhkE)4%caKYZ?lYt_f1s;*b z3=G`DAk4@xYmNj^kiEpy*OmPt6DL0>EtUHFZ?*~z?hJjkdUyUv_!@G^TWTt71<1c z;LVL)Qypi_ovYECU;i)V!-sEfI~p1r8$m(~z2fF+FyE51bE$ZEykA~pQcX@yg=zh- zuiU0Q6+3nsif}%<@JB9OA&J>0kLF5GyBy4`