Custom inventory that behaves the same as the craft output.

BobbyBonsaimind
Member
 
Posts: 97
Joined: Tue Apr 14, 2015 19:32
GitHub: RobertZenz
IRC: Robert_Zenz
In-game: Bobby

Custom inventory that behaves the same as the craft output.

by BobbyBonsaimind » Sun Jun 21, 2015 18:25

What I need is an inventory that behaves the same as the craft output field, meaning that taking something from it is already an action and that nothing can be put into it.

I could not find any documentation on how to achieve this. I looked into custom inventories but it seemed like that those do not provide the necessary events for it (taking something from it is only fired if it is put into the target inventory).

Required is such a field for my [Artisanry mod](https://github.com/RobertZenz/minetest- ... -artisanry) which allows to have multiple outputs for the same receipt.

So, does somebody have an idea how to do is, or should I open a feature request?
 

TeTpaAka
Member
 
Posts: 131
Joined: Sat Dec 28, 2013 21:54

Re: Custom inventory that behaves the same as the craft outp

by TeTpaAka » Sun Jun 21, 2015 18:53

If it is in a node definition, you can use allow_metadata_inventory_put on the input field and use the elements of the inventory to call get_craft_result(). You can put the output in the output slot and then, with allow_inventory_take, you can decrement the input if someone takes the craft result.
 

BobbyBonsaimind
Member
 
Posts: 97
Joined: Tue Apr 14, 2015 19:32
GitHub: RobertZenz
IRC: Robert_Zenz
In-game: Bobby

Re: Custom inventory that behaves the same as the craft outp

by BobbyBonsaimind » Tue Jun 23, 2015 16:49

If I understood you correctly, that is mostly what I tried and what did not work.

The default output slot works as follows:

1. Something is in it, you click on it to take it.
2. Now you have it in your hand, the slot "becomes empty" and is immediately refilled.
3. You can now click again to take more.

In my tests I was only able to achieve the following behavior:

1. Something is in it, you click on it to take it.
2. You now have it in your hand, but the system treats it as being dragged.
3. The slot is only visually empty, the items remains in the slot until you drop it.

Maybe I did miss something?
 

TeTpaAka
Member
 
Posts: 131
Joined: Sat Dec 28, 2013 21:54

Re: Custom inventory that behaves the same as the craft outp

by TeTpaAka » Tue Jun 23, 2015 18:15

Can you show me your code?
 

BobbyBonsaimind
Member
 
Posts: 97
Joined: Tue Apr 14, 2015 19:32
GitHub: RobertZenz
IRC: Robert_Zenz
In-game: Bobby

Re: Custom inventory that behaves the same as the craft outp

by BobbyBonsaimind » Tue Jun 23, 2015 19:07

This is the test code I wrote up.

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

local out_inventory = minetest.create_detached_inventory("out", {
   allow_move = function(inv, from_list, from_index, to_list, to_index, count, player)
      print("out - allow_move")
      
      return count
   end,
   
   allow_put = function(inv, listname, index, stack, player)
      print("out - allow_put")
      
      return stack:get_count()
   end,
   
   allow_take = function(inv, listname, index, stack, player)
      print("out - allow_take")
      
      return stack:get_count()
   end,
   
   on_move = function(inv, from_list, from_index, to_list, to_index, count, player)
      print("out - on_move")
   end,
   
   on_put = function(inv, listname, index, stack, player)
      print("out - on_put")
   end,
   
   on_take = function(inv, listname, index, stack, player)
      print("out - on_take")
   end
})
out_inventory:set_size("out", 1)

local formspec = "size[10,8;]"
formspec = formspec .. "list[current_player;in;1,1;1,1;]"
formspec = formspec .. "list[detached:out;out;4,1;1,1;]"
formspec = formspec .. "list[current_player;main;1,3;8,4;]"


local timer = 0
minetest.register_globalstep(function(elapsed_time)
   timer = timer + elapsed_time
   
   if timer >= 0.33 then
      
      if out_inventory:get_stack("out", 1):is_empty() then
         print("output is empty")
      end
      
      -- Update the inventories of all players.
      for index, player in ipairs(minetest.get_connected_players()) do
         local inventory = player:get_inventory()
         
         if inventory:is_empty("in") then
            out_inventory:set_stack("out", 1, nil)
         else
            local input_stack = inventory:get_stack("in", 1)
            input_stack:set_count(1)
            
            out_inventory:set_stack("out", 1, input_stack)
         end
      end
      
      timer = 0
   end
end)

minetest.register_on_joinplayer(function(player)
   player:get_inventory():set_size("in", 1)
   player:set_inventory_formspec(formspec)
end)


This will replace the player inventory with the custom inventory which has two additional slots, the left for input, the right for output (it's basically copying the item you give it).

No if I take something from the output slot, none of the events of the inventory register, neither is the output directory empty. Only if I actually place the item in another inventory "something happens".
 

TeTpaAka
Member
 
Posts: 131
Joined: Sat Dec 28, 2013 21:54

Re: Custom inventory that behaves the same as the craft outp

by TeTpaAka » Tue Jun 23, 2015 22:17

Strange. For me, the code works as expected. It prints "output is empty" until I put something in "in" and triggers allow_take and on_take when I take something.

Though I had to add "default" as a dependency, so it doesn't override the inventory formspec.

And you are right. The take event is only triggered once you drop the item somewhere. The same effect as in the default crafting grid is impossible with the current engine, since default crafting is client predicted and all we can do now is server side.
 

BobbyBonsaimind
Member
 
Posts: 97
Joined: Tue Apr 14, 2015 19:32
GitHub: RobertZenz
IRC: Robert_Zenz
In-game: Bobby

Re: Custom inventory that behaves the same as the craft outp

by BobbyBonsaimind » Wed Jun 24, 2015 16:54

I feared such a thing, thank you, I'll open a feature request on GitHub for it.
 


Return to Modding Discussion

Who is online

Users browsing this forum: Bing [Bot] and 1 guest

cron