Getting amount of drops

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

Getting amount of drops

by qwrwed » Post

See most recent problem.
Spoiler
I am trying to make a pickaxe that automatically smelts whatever it mines, but am having trouble. I have typed this code in the minetest.register_tool:

Code: Select all

after_use = function(itemstack, user, node, digparams)
    local node = node.name
    print(node)
    local product = minetest.get_craft_result({method = "cooking", width = 1, items = {node}})
    print(product)
    user:get_inventory():add_item("main", (product))
    return itemstack
end
This just prints something similar to:

Code: Select all

default:stone
table: 0x0a828580
and does not add the smelted item.
I have also tried returning "product" instead of "itemstack", but this also does not work as intended; it just removes the tools.
How do I find out the name of the craft result?
Last edited by qwrwed on Wed Jan 08, 2014 21:56, edited 1 time in total.

GreenTech
New member
Posts: 9
Joined: Wed Jan 08, 2014 16:31
Location: My Home in My World

by GreenTech » Post

qwrwed wrote:I am trying to make a pickaxe that automatically smelts whatever it mines, but am having trouble. I have typed this code in the minetest.register_tool:
after_use = function(itemstack, user, node, digparams)
local node = node.name
print(node)
local product = minetest.get_craft_result({method = "cooking", width = 1, items = {node}})
print(product)
user:get_inventory():add_item("main", (product))
return itemstack
end
This just prints something similar to:

Code: Select all

default:stone
table: 0x0a828580
and does not add the smelted item.
I have also tried returning "product" instead of "itemstack", but this also does not work as intended; it just removes the tools.
How do I find out the name of the craft result?
Maybe on_dig?

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

by qwrwed » Post

Code: Select all

on_dig = function(pos, node, digger)
    print("digged")
end
Nothing is printed.
On_dig only works for nodes; you can't dig a pickaxe.

User avatar
LionsDen
Member
Posts: 530
Joined: Thu Jun 06, 2013 03:19

by LionsDen » Post

Yeah, I don't think the print lua command works because there isn't any text display in the game (No specific area anyway). Type the chat message, that is what I use whenever I want to show information to myself. Here is an example:

Code: Select all

minetest.chat_send_player(user:get_player_name(), "Text you want to display here.")
As further information, I used this as part of a node's on_use so was able to use the user:get_player_name() function.
Last edited by LionsDen on Wed Jan 08, 2014 17:31, edited 1 time in total.

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

by qwrwed » Post

It does - it prints to the terminal.
Spoiler
Image
This screenshot also shows the "table: 0x0<string of random characters>" that is printed. I have just realised there is no cooking recipe for stone, but digging cobble or sand does not give stone or glass either.
Last edited by qwrwed on Wed Jan 08, 2014 17:50, edited 1 time in total.

User avatar
kaeza
Moderator
Posts: 2162
Joined: Thu Oct 18, 2012 05:00
GitHub: kaeza
IRC: kaeza diemartin blaaaaargh
In-game: kaeza
Location: Montevideo, Uruguay
Contact:

by kaeza » Post

Your signature is not the place for a blog post. Please keep it as concise as possible. Thank you!

Check out my stuff! | Donations greatly appreciated! PayPal

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

by qwrwed » Post

I looked at that code again and saw that had "cooked.item", whereas I just had "product". Adding .item meant that it worked almost as intended - digging cobble gives stone AND cobble - how do I prevent the item that is being dug from being added to the inventory?

User avatar
rubenwardy
Moderator
Posts: 6978
Joined: Tue Jun 12, 2012 18:11
GitHub: rubenwardy
IRC: rubenwardy
In-game: rubenwardy
Location: Bristol, United Kingdom
Contact:

by rubenwardy » Post

after_use = function(itemstack, user, node, digparams)
local node = node.name
print(node)
local product = minetest.get_craft_result({method = "cooking", width = 1, items = {node}})
print(dump(product))
user:get_inventory():add_item("main", (product))
return itemstack
end
Tried deleting local node = ...?
Renewed Tab (my browser add-on) | Donate | Mods | Minetest Modding Book

Hello profile reader

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

by qwrwed » Post

I have prevented more than one dug item from being added to the inventory (although the is a problem I will solve completely in the future) but is there a way to get the amount of drops? I have used this:

Code: Select all

after_use = function(itemstack, user, node, digparams)
        local drops = table.concat(minetest.get_node_drops(node.name, "default:pick_mese"))
        local product = minetest.get_craft_result({method = "cooking", width = 1, items = {drops}})
        local inv = user:get_inventory()
        print(product.name)
        inv:add_item("main", product.item)
        inv:remove_item("main", drops)
        return itemstack
    end,
It works perfectly when mining metals, but when mining clay, only 1 brick is added instead of 4. I have looked in doc/lua_api, but I didn't see anything for getting the amount of drops - how do I do this?

User avatar
rubenwardy
Moderator
Posts: 6978
Joined: Tue Jun 12, 2012 18:11
GitHub: rubenwardy
IRC: rubenwardy
In-game: rubenwardy
Location: Bristol, United Kingdom
Contact:

by rubenwardy » Post

You could read the drop definition from minetest.registered_nodes[name]
Renewed Tab (my browser add-on) | Donate | Mods | Minetest Modding Book

Hello profile reader

User avatar
Krock
Developer
Posts: 4650
Joined: Thu Oct 03, 2013 07:48
GitHub: SmallJoker
Location: Switzerland
Contact:

by Krock » Post

Well more than my codes I can't give because explaining could be hard:

Code: Select all

local output_drops = {}
local drops = minetest.get_node_drops(node.name)
for _, item in ipairs(drops) do
    local stack = ItemStack(item)
    local item_name = stack:get_name()
    local item_count = stack:get_count()
    if(item_count == nil) then
        item_count = 1
    end
    if(output_drops[item_name] ~= nil) then
        output_drops[item_name] = output_drops[item_name] + item_count
    else
        output_drops[item_name] = item_count
    end
end

~ original source 'mining_plus' by Krock
EDIT: typos
Last edited by Krock on Thu Jan 09, 2014 19:30, edited 1 time in total.
Look, I programmed a bug for you. >> Mod Search Engine << - Mods by Krock - DuckDuckGo mod search bang: !mtmod <keyword here>

prestidigitator
Member
Posts: 647
Joined: Thu Feb 21, 2013 23:54

by prestidigitator » Post

qwrwed wrote:I have prevented more than one dug item from being added to the inventory (although the is a problem I will solve completely in the future) but is there a way to get the amount of drops? I have used this:

Code: Select all

after_use = function(itemstack, user, node, digparams)
        local drops = table.concat(minetest.get_node_drops(node.name, "default:pick_mese"))
        local product = minetest.get_craft_result({method = "cooking", width = 1, items = {drops}})
        local inv = user:get_inventory()
        print(product.name)
        inv:add_item("main", product.item)
        inv:remove_item("main", drops)
        return itemstack
    end,
It works perfectly when mining metals, but when mining clay, only 1 brick is added instead of 4. I have looked in doc/lua_api, but I didn't see anything for getting the amount of drops - how do I do this?
There are a couple odd things about this code. Changing them MIGHT help.
  1. Using table.concat() on the list of ItemStacks returned by minetest.get_node_drops() will result in undefined behavior. It might be doing something right now, but note that the Lua 5.1 API only specifies the behavior of table.concat() when the elements are strings or numbers, not when they are references to tables or userdata (an ItemStack is one of those—not sure which). Fortunately, minetest.get_craft_result() takes a table whose 'items' property is an array of ItemStacks, so you shouldn't really have to do that table.concat() conversion anyway. You may want to iterate through the list of ItemStacks from minetest.get_node_drops() and deal with each individually, however, instead of just using the whole list.
  2. Since the result of the minetest.get_craft_result() call has an ItemStack, I believe you should actually be calling "product.item:get_name()" rather than just using the expression "product.name" on it. Likewise, you should be able to get the count of the cooked product using "product.item:get_count()". However, since you are using "inv:add_item(..., product.item)" which takes a whole ItemStack as its second parameter anyway, I think that logic should already be handling the item count just fine....
  3. Your call of "inv:remove_item(...)" will need to be dealt with in a way similar to how you will need to use ItemStacks for minetest.get_craft_result(), as I described above.
  4. You'll probably want to test whether the stack given by minetest.get_craft_result() is empty (meaning there is no craft recipe for the inputs you are describing). As it is this will remove any drop that doesn't get cooked into anything, rather than leaving it unchanged.
Last edited by prestidigitator on Fri Jan 10, 2014 10:09, edited 1 time in total.

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

by qwrwed » Post

prestidigitator: Ah. Now I see why things such as coal weren't added to the inventory.

Thanks for the help of everyone who posted here - the problem with the original code isn't fixed, but kaeza has provided an alternate solution (overwriting minetest.handle_node_drops) which works fine.

Post Reply

Who is online

Users browsing this forum: No registered users and 57 guests