Page 1 of 1

Finding the amount of items in a stack(SOLVED)

PostPosted: Tue Dec 31, 2013 23:05
by qwrwed
In Minearriatest's Jungletools mod, there is a grinder node. This grinder's formspec has 2 slots (input, output) and a button (Grind). However, on clicking Grind, only one item is removed from the input slot, and only 1 is added to the output. Is there a way to find the amount of jungletools:jungle_spore craftitems in the input slot and add that many jungletools:jungle_dust to the output slot on_recieve_fields?

[spoiler=code]
Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
Code: Select all
minetest.register_craftitem("jungletools:jungle_spore", {
    description = "Jungle Spore",
    inventory_image = "jungletools_jungle_spore.png",
})

minetest.register_craftitem("jungletools:jungle_dust", {
    description = "Jungle Dust",
    inventory_image = "jungletools_jungle_dust.png",
})

local sporegrinderformspec =
    "size[8,8]"..
    "label[4,0;Spore Grinder]"..
    "list[current_name;spore;3,1;1,1;]"..
    "label[2,1;  Spore:]"..
    "list[current_name;dust;3,3;1,1;]"..
    "label[2,3; Dust:]"..
    "list[current_player;main;0,4;8,4;]"..
    "button[4,2;1,1;grind;Grind]"
   

minetest.register_node("jungletools:spore_grinder", {
    description = "Spore Grinder",
    tiles = {"spore_grinder_top.png", "spore_grinder_bottom.png", "spore_grinder_side.png", "spore_grinder_side.png", "spore_grinder_side.png", "spore_grinder_front.png"},
    paramtype2 = "facedir",
    on_construct = function(pos)
        local meta = minetest.get_meta(pos)
        meta:set_string("formspec",sporegrinderformspec)
        meta:set_string("infotext", "Spore Grinder")
        local inv = meta:get_inventory()
        inv:set_size("main", 8*4)
        inv:set_size("spore", 1*1)
        inv:set_size("dust", 1*1)   
    end,
    allow_metadata_inventory_put = function(pos, listname, index, stack, player)
        if listname == "spore" then
            return stack:get_count()
        else
            return 0
        end
    end,
    allow_metadata_inventory_move = function(pos, from_list, from_index, to_list, to_index, count, player)
        if to_list == "dust" then
            return 0
        else
            return 1
        end
    end,
    can_dig = function(pos,player)
        local meta = minetest.get_meta(pos);
        local inv = meta:get_inventory()
        if not inv:is_empty("spore") then
            return false
        elseif not inv:is_empty("dust") then
            return false
        end
        return true
    end,
    on_receive_fields = function(pos, formname, fields, sender)
        local meta = minetest.get_meta(pos);
        local inv = meta:get_inventory()
        if fields.grind then
            if inv:get_stack("spore", 1):get_name() == "jungletools:jungle_spore" and (inv:is_empty("dust") or inv:get_stack("dust", 1):get_name() == "jungletools:jungle_dust") then
                inv:add_item("dust", "jungletools:jungle_dust")
                inv:remove_item("spore", "jungletools:jungle_spore")
            end
        end
    end,
    groups = {choppy=3,oddly_breakable_by_hand=3},
})
[/spoiler]

EDIT: I used stack:get_count().

PostPosted: Wed Jan 01, 2014 07:32
by Krock
I don't like to say this but: use ABM's
There are serval examples in minetest_game, just browse through them, you'll surely find some.

PostPosted: Wed Jan 01, 2014 15:16
by qwrwed
I found a better solution for what I have in mind: stack:get_count(). I found this while looking for the furnace ABM so thanks, Krock.

PostPosted: Wed Jan 01, 2014 20:23
by Minearriatest
Modding a Mod, seems legit.