Skip to content

Instantly share code, notes, and snippets.

@Ekdohibs
Last active December 26, 2015 15:19
Show Gist options
  • Save Ekdohibs/7172171 to your computer and use it in GitHub Desktop.
Save Ekdohibs/7172171 to your computer and use it in GitHub Desktop.
local CRAFT_TABLE_SIZE = 4
local update_craft_table_inv = function(inv)
inv:set_stack("craftpreview", 1, minetest.get_craft_result({method = "normal", items = inv:get_list("craft"), width = CRAFT_TABLE_SIZE}).item)
end
local update_craft_table = function(pos)
local meta = minetest.get_meta(pos)
local inv = meta:get_inventory()
update_craft_table_inv(inv)
end
local craft_table_drop = function(inv, player)
local craft_list = inv:get_list("craft")
local player_pos = player:getpos()
player_pos.y = player_pos.y + 1.5
local dir = player:get_look_dir()
local item_pos = vector.add(player_pos, dir)
for i = 1, inv:get_size("craft") do
local stack = craft_list[i]
local item = minetest.add_item(item_pos, stack)
if item then
item:setvelocity({
x = dir.x+(math.random()-0.5)/3,
y = dir.y+(math.random()-0.5)/3,
z = dir.z+(math.random()-0.5)/3
})
end
craft_list[i] = ItemStack("")
end
inv:set_list("craft", craft_list)
end
local CRAFTING_FORMSPEC = "size["..(6+CRAFT_TABLE_SIZE)..","..(6+CRAFT_TABLE_SIZE).."]"..
"list[current_player;main;0,"..(1.5+CRAFT_TABLE_SIZE)..";8,4;]"..
"list[current_name;craft;1.25,0.5;"..CRAFT_TABLE_SIZE..","..CRAFT_TABLE_SIZE..";]"..
"list[current_name;craftpreview;"..(3.48+CRAFT_TABLE_SIZE)..","..(CRAFT_TABLE_SIZE/2)..";1,1;]"
minetest.register_node("crafting_table:crafting_table", {
description = "Crafting Table",
tiles = {"crafting_table.png"},
paramtype2 = "facedir",
paramtype = "light",
groups = {choppy=2,oddly_breakable_by_hand=1,flammable=2},
sounds = default.node_sound_wood_defaults(),
on_construct = function(pos, node)
local meta = minetest.get_meta(pos)
local inv = meta:get_inventory()
inv:set_size("craft", CRAFT_TABLE_SIZE*CRAFT_TABLE_SIZE)
inv:set_width("craft", CRAFT_TABLE_SIZE)
inv:set_size("craftresult", 1)
inv:set_size("craftpreview", 1)
inv:set_size("I_am_a_craft_table", 1) -- HACK!
meta:set_string("formspec", CRAFTING_FORMSPEC)
end,
on_receive_fields = function(pos, formname, fields, player)
if fields.quit then
local meta = minetest.get_meta(pos)
local inv = meta:get_inventory()
craft_table_drop(inv, player)
inv:set_stack("craftpreview", 1, ItemStack(""))
end
end,
on_metadata_inventory_take = update_craft_table,
on_metadata_inventory_move = update_craft_table,
on_metadata_inventory_put = update_craft_table,
})
minetest.register_on_player_receive_fields(function(player, formname, fields)
if fields.quit then
local inv = player:get_inventory()
craft_table_drop(inv, player)
end
end)
minetest.register_on_craft(function(itemstack, player, old_craft_grid, craft_inv)
if craft_inv:get_size("I_am_a_craft_table") == 1 then
update_craft_table_inv(craft_inv)
end
end)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment