Page 1 of 1

[Mod] instafill [1.30] - create cuboids

PostPosted: Mon Mar 25, 2013 12:38
by bdjnk
This is currently a work in progess. It does the right thing in single player.

A mod for Minetest which allows players to create a solid cube of a given type of node by placing two nodes of that type at opposite corners of the desired cube. This enables the easy creation of lines, planes, and rectagles of nodes.

Instafill is toggled by means of the "/f" command. Chat messages will let you know what state you're in.


Code license : GPLv2/later

Mod dependencies: default

Download link: 1.30

Github link: instafill source

Video demos:

Changelog:
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
1.30 - better safety (single player) and messaging
1.20 - supports creative mode properly + removed "env:" (requires >= 4.7)
1.10 - now pulls from multiple inventory stacks correctly
1.00 - feature complete, adds taking nodes from inventory only
0.91 - fix to ensure dug nodes no longer act as instafill corners
0.90 - initial release, missing taking nodes from inventory only

PostPosted: Mon Mar 25, 2013 14:35
by Tedypig
This would be useful to a creative only server. +0.5

PostPosted: Mon Mar 25, 2013 14:42
by Traxie21
It could also be useful for any server owner who wanted to rapidly build his server.

However, the WorldEdit mod has this and more.

PostPosted: Mon Mar 25, 2013 16:30
by bdjnk
Changes in 1.0
Only nodes from your inventory are placed.


Hey Tedypig,
I was mostly thinking it was useful in the context of my creative, single-player environment. Still, with the latest changes it should be fine for stricter environments :)

Hey Traxie21,
Someone else also mentioned WorldEdit, but that is a tool outside of regular gameplay (and very complex). This is a simple tool to remove some of the tedium involved in larger scale building project.

PostPosted: Mon Mar 25, 2013 18:03
by bdjnk
Okay, so 1.0 doesn't work perfectly. If I have several ItemStacks of the relevant node type in my inventory, only the one currently in hand gets used. I've been looking at the documentation and reading the forum, and I could use some help. Here is the relevant snippets of code where I attempt to handle the issue:

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 inv = placer:get_inventory()
...
                        minetest.debug('before take   '..itemstack:to_string())
                        itemstack:take_item(1)
                        minetest.debug(' after take   '..itemstack:to_string())
                        if not itemstack:is_empty() then
                            minetest.env:add_node(npos, {name = pname})
                        elseif inv:contains_item("main", pname) then
                            minetest.debug('before remove '..itemstack:to_string())
                            itemstack = inv:remove_item("main", {name=pname, count=99})
                            minetest.debug(' after remove '..itemstack:to_string())
...
        minetest.debug('before add    '..itemstack:to_string())
        inv:add_item("main", itemstack)
        minetest.debug(' after add    '..itemstack:to_string())


Let's say I have four glass in my hand, and four elsewhere in my inventory. The output of placing those four in a shape requiring five nodes is as follows:

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
before take   default:glass 3
 after take   default:glass 2
before take   default:glass 2
 after take   default:glass
before take   default:glass
 after take   
before remove
 after remove default:glass 7
before add    default:glass 7
 after add    default:glass 7


Everything up to 'after remove' makes sense to me. Once we do 'after remove', I completely lose track. I've already placed four glass, so all I should have is four, unless the other nodes have been taken from the itemstack but remain in the inventory. And what about the failure to add the items back to the inventory. Indeed, the second stack of glass is gone from the inventory.

I must be reading the documentation badly, but I've even been delving into the source and I can't see where I'm going wrong. Any assistance is appreciated.

PostPosted: Mon Mar 25, 2013 18:59
by prestidigitator
You asked the player's inventory to remove up to 99 items from its "main" list. Apparently it could only find 7 (I THINK this should try to find all "default:glass" stacks and pull from multiple if necessary, but you could try putting it in a loop until you either fill your whole request or get nothing back). Note that InvRef:add_item(...) isn't going to change the ItemStack you pass into it; instead it returns the leftovers (which would presumably be none in this case. So for your "after add" result to be different from your "before add" result, you'll need to do:

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
itemstack = inv:add_item("main", itemstack)


EDIT: Oh. I see. Your other problem is that ItemStacks are NOT live objects. They are simply a structure used to keep track of a list of items. If you change an ItemStack you got back from an inventory (InvRef), the inventory is NOT changed. To change the inventory you have to send the changed ItemStack back to it somehow. So you have to do something with the ItemStack you originally called take_item() on in order to actually push that change back to the inventory. For example, if this was on the placer's wielded item list, you'd have to call:

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
itemstack = placer:get_wielded_item()
...
itemstack.take_item(...)
...
placer:set_wielded_item(itemstack)


This seems to be a common mistake, so I've added notes about this in the dev. wiki articles for InvRef and ObjectRef.

PostPosted: Thu Mar 28, 2013 04:40
by bdjnk
Changes in 1.10 - Now pulls from multiple inventory stacks correctly.

Thanks prestidigitator!

I still think, even with the additional notes in the documentation, the concept is not so intuitive. My code now works, but I may have an excess of set_wielded_item() calls. If you're bored you can take a quick look and let me know if I'm doing things in a sensible way.

PostPosted: Mon Jun 10, 2013 19:05
by bdjnk
Changes in 1.20 - Creative mode is now properly supported. I've also removed "env:" from all relevant calls, so instafill now requires minetest 4.7 or greater.

Re: [Mod] instafill [1.20] - create cuboids (save time, buil

PostPosted: Wed Aug 17, 2016 18:21
by mahmutelmas06
Amazing mod and still works well after all years :)