Implement renaming with anvils
[MineClone/MineClone2.git] / mods / ITEMS / mcl_anvils / init.lua
blob245a3cd5aa55dfae49a03f0da26f56a2b0258e15
1 local function get_anvil_formspec(set_name)
2 if not set_name then
3 set_name = ""
4 end
5 return "size[9,8.75]"..
6 "background[-0.19,-0.25;9.41,9.49;mcl_anvils_inventory.png]"..
7 mcl_vars.inventory_header..
8 "list[current_player;main;0,4.5;9,3;9]"..
9 "list[current_player;main;0,7.74;9,1;]"..
10 "list[context;input;1,2.5;1,1;]"..
11 "list[context;input;4,2.5;1,1;1]"..
12 "list[context;output;8,2.5;1,1;]"..
13 "field[3.25,1;4,1;name;;"..minetest.formspec_escape(set_name).."]"..
14 "button[7,0.7;2,1;name_button;Set name]"..
15 "listring[context;output]"..
16 "listring[current_player;main]"..
17 "listring[context;input]"..
18 "listring[current_player;main]"
19 end
21 -- Update the inventory slots of an anvil node.
22 -- meta: Metadata of anvil node
23 local function update_anvil_slots(meta)
24 local inv = meta:get_inventory()
25 local new_name = meta:get_string("set_name")
26 local input1, input2, output
27 input1 = inv:get_stack("input", 1)
28 input2 = inv:get_stack("input", 2)
29 output = inv:get_stack("output", 1)
30 local new_output
32 -- Just rename
33 if (not input1:is_empty() and input2:is_empty()) or (input1:is_empty() and not input2:is_empty()) then
34 if new_name ~= nil and new_name ~= "" then
35 local name_item
36 if input1:is_empty() then
37 name_item = input2
38 else
39 name_item = input1
40 end
41 local meta = name_item:get_meta()
42 -- Limit name length
43 new_name = string.sub(new_name, 1, 30)
44 meta:set_string("description", new_name)
45 new_output = name_item
46 else
47 new_output = ""
48 end
49 else
50 new_output = ""
51 end
53 -- Set the new output slot
54 if new_output ~= nil then
55 inv:set_stack("output", 1, new_output)
56 end
57 end
59 local anvildef = {
60 groups = {pickaxey=1, falling_node=1, crush_after_fall=1, deco_block=1, anvil=1},
61 tiles = {"mcl_anvils_anvil_top_damaged_0.png^[transformR90", "mcl_anvils_anvil_base.png", "mcl_anvils_anvil_side.png"},
62 paramtype = "light",
63 sunlight_propagates = true,
64 is_ground_content = false,
65 paramtype2 = "facedir",
66 drawtype = "nodebox",
67 node_box = {
68 type = "fixed",
69 fixed = {
70 {-8/16, 2/16, -5/16, 8/16, 8/16, 5/16}, -- top
71 {-6/16, -4/16, -1/16, 6/16, 5/16, 1/16}, -- middle
72 {-8/16, -8/16, -5/16, 8/16, -4/16, 5/16}, -- base
75 sounds = mcl_sounds.node_sound_metal_defaults(),
76 _mcl_blast_resistance = 6000,
77 _mcl_hardness = 5,
78 allow_metadata_inventory_put = function(pos, listname, index, stack, player)
79 if listname == "output" then
80 return 0
81 else
82 return stack:get_count()
83 end
84 end,
85 allow_metadata_inventory_move = function(pos, from_list, from_index, to_list, to_index, count, player)
86 if to_list == "output" then
87 return 0
88 elseif from_list == "output" and to_list == "input" then
89 local meta = minetest.get_meta(pos)
90 local inv = meta:get_inventory()
91 if inv:get_stack(to_list, to_index):is_empty() then
92 return count
93 else
94 return 0
95 end
96 else
97 return count
98 end
99 end,
100 on_metadata_inventory_put = function(pos, listname, index, stack, player)
101 local meta = minetest.get_meta(pos)
102 update_anvil_slots(meta)
103 end,
104 on_metadata_inventory_move = function(pos, from_list, from_index, to_list, to_index, count, player)
105 local meta = minetest.get_meta(pos)
106 if from_list == "output" and to_list == "input" then
107 local inv = meta:get_inventory()
108 inv:set_stack("output", 1, "")
109 for i=1, inv:get_size("input") do
110 if i ~= to_index then
111 inv:set_stack("input", i, "")
115 update_anvil_slots(meta)
116 end,
117 on_metadata_inventory_take = function(pos, listname, index, stack, player)
118 local meta = minetest.get_meta(pos)
119 if listname == "output" then
120 local inv = meta:get_inventory()
121 inv:set_list("input", {"",""})
123 update_anvil_slots(meta)
124 end,
125 on_construct = function(pos)
126 local meta = minetest.get_meta(pos)
127 local inv = meta:get_inventory()
128 inv:set_size("input", 2)
129 inv:set_size("output", 1)
130 local form = "size[9,8.75]"..
131 "background[-0.19,-0.25;9.41,9.49;mcl_anvils_inventory.png]"..
132 mcl_vars.inventory_header..
133 "list[current_player;main;0,4.5;9,3;9]"..
134 "list[current_player;main;0,7.74;9,1;]"..
135 "list[context;input;1,2.5;1,1;]"..
136 "list[context;input;4,2.5;1,1;1]"..
137 "list[context;output;8,2.5;1,1;]"..
138 "field[3.25,1;4,1;name;;]"..
139 "button[7,0.7;2,1;name_button;Set name]"..
140 "listring[context;output]"..
141 "listring[current_player;main]"..
142 "listring[context;input]"..
143 "listring[current_player;main]"
144 meta:set_string("formspec", form)
145 end,
146 on_receive_fields = function(pos, formname, fields, sender)
147 if fields.name_button then
148 local set_name
149 if fields.name == nil then
150 set_name = ""
151 else
152 set_name = fields.name
154 local meta = minetest.get_meta(pos)
155 meta:set_string("set_name", set_name)
156 update_anvil_slots(meta)
157 meta:set_string("formspec", get_anvil_formspec(set_name))
159 end,
161 if minetest.get_modpath("screwdriver") then
162 anvildef.on_rotate = screwdriver.rotate_simple
165 local anvildef0 = table.copy(anvildef)
166 anvildef0.description = "Anvil"
168 local anvildef1 = table.copy(anvildef)
169 anvildef1.description = "Slightly Damaged Anvil"
170 anvildef1.groups.not_in_creative_inventory = 1
171 anvildef1._doc_items_create_entry = false
172 anvildef1.tiles = {"mcl_anvils_anvil_top_damaged_1.png^[transformR90", "mcl_anvils_anvil_base.png", "mcl_anvils_anvil_side.png"}
174 local anvildef2 = table.copy(anvildef)
175 anvildef2.description = "Very Damaged Anvil"
176 anvildef2.groups.not_in_creative_inventory = 1
177 anvildef2._doc_items_create_entry = false
178 anvildef2.tiles = {"mcl_anvils_anvil_top_damaged_2.png^[transformR90", "mcl_anvils_anvil_base.png", "mcl_anvils_anvil_side.png"}
180 minetest.register_node("mcl_anvils:anvil", anvildef0)
181 minetest.register_node("mcl_anvils:anvil_damage_1", anvildef1)
182 minetest.register_node("mcl_anvils:anvil_damage_2", anvildef2)
184 minetest.register_craft({
185 output = "mcl_anvils:anvil",
186 recipe = {
187 { "mcl_core:ironblock", "mcl_core:ironblock", "mcl_core:ironblock" },
188 { "", "mcl_core:iron_ingot", "" },
189 { "mcl_core:iron_ingot", "mcl_core:iron_ingot", "mcl_core:iron_ingot" },
193 dofile(minetest.get_modpath(minetest.get_current_modname()).."/falling_anvil.lua")