Finding the amount of items in a stack(SOLVED)

User avatar
qwrwed
Member
 
Posts: 323
Joined: Sun Jul 22, 2012 20:56
In-game: qwrwed or Nitro

Finding the amount of items in a stack(SOLVED)

by qwrwed » Tue Dec 31, 2013 23:05

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().
Last edited by qwrwed on Wed Jan 01, 2014 15:17, edited 1 time in total.
 

User avatar
Krock
Member
 
Posts: 3598
Joined: Thu Oct 03, 2013 07:48
GitHub: SmallJoker

by Krock » Wed Jan 01, 2014 07:32

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.
Newest Win32 builds - Find a mod - All my mods
ALL YOUR DONATION ARE BELONG TO PARAMAT (Please support him and Minetest)
New DuckDuckGo !bang: !mtmod <keyword here>
 

User avatar
qwrwed
Member
 
Posts: 323
Joined: Sun Jul 22, 2012 20:56
In-game: qwrwed or Nitro

by qwrwed » Wed Jan 01, 2014 15:16

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.
 

User avatar
Minearriatest
Member
 
Posts: 34
Joined: Mon Nov 25, 2013 18:28

by Minearriatest » Wed Jan 01, 2014 20:23

Modding a Mod, seems legit.
Minetest user name: asg
Try out some mods which I contributed in:
Jungle Tools(made) Ice + Magma Tools(contributed) Ultimate Tools(made)
 


Return to WIP Mods

Who is online

Users browsing this forum: No registered users and 14 guests

cron