forked from minetest-mods/unified_inventory
		
	Change craft guide input slots to buttons
This commit is contained in:
		| @@ -3,45 +3,46 @@ minetest.register_on_joinplayer(function(player) | |||||||
| 	local player_name = player:get_player_name() | 	local player_name = player:get_player_name() | ||||||
| 	unified_inventory.players[player_name] = {} | 	unified_inventory.players[player_name] = {} | ||||||
| 	unified_inventory.current_index[player_name] = 1 | 	unified_inventory.current_index[player_name] = 1 | ||||||
| 	unified_inventory.filtered_items_list[player_name] = unified_inventory.items_list | 	unified_inventory.filtered_items_list[player_name] = | ||||||
|  | 			unified_inventory.items_list | ||||||
| 	unified_inventory.activefilter[player_name] = "" | 	unified_inventory.activefilter[player_name] = "" | ||||||
| 	unified_inventory.apply_filter(player, "") | 	unified_inventory.apply_filter(player, "") | ||||||
| 	unified_inventory.alternate[player_name] = 1 | 	unified_inventory.alternate[player_name] = 1 | ||||||
| 	unified_inventory.current_item[player_name] = nil | 	unified_inventory.current_item[player_name] = nil | ||||||
| 	unified_inventory.set_inventory_formspec(player, unified_inventory.default) | 	unified_inventory.set_inventory_formspec(player, | ||||||
|  | 			unified_inventory.default) | ||||||
| 	 | 	 | ||||||
| 	-- Crafting guide inventories | 	-- Crafting guide inventories | ||||||
| 	local inv = minetest.create_detached_inventory(player:get_player_name().."craftrecipe", { | 	local inv = minetest.create_detached_inventory(player_name.."craftrecipe", { | ||||||
| 		allow_put = function(inv, listname, index, stack, player) | 		allow_put = function(inv, listname, index, stack, player) | ||||||
| 			return 0 | 			return 0 | ||||||
| 		end, | 		end, | ||||||
| 		allow_take = function(inv, listname, index, stack, player) | 		allow_take = function(inv, listname, index, stack, player) | ||||||
| 			if unified_inventory.is_creative(player:get_player_name()) then |  | ||||||
| 				return stack:get_count() |  | ||||||
| 			else |  | ||||||
| 			return 0 | 			return 0 | ||||||
| 			end |  | ||||||
| 		end, | 		end, | ||||||
| 		allow_move = function(inv, from_list, from_index, to_list, to_index, count, player) | 		allow_move = function(inv, from_list, from_index, to_list, | ||||||
|  | 				to_index, count, player) | ||||||
| 			return 0 | 			return 0 | ||||||
| 		end, | 		end, | ||||||
| 	}) | 	}) | ||||||
| 	inv:set_size("output", 1) | 	inv:set_size("output", 1) | ||||||
| 	inv:set_size("build", 3 * 3) |  | ||||||
|  |  | ||||||
| 	-- Refill slot | 	-- Refill slot | ||||||
| 	local refill = minetest.create_detached_inventory(player_name.."refill", { | 	local refill = minetest.create_detached_inventory(player_name.."refill", { | ||||||
| 		allow_put = function(inv, listname, index, stack, player) | 		allow_put = function(inv, listname, index, stack, player) | ||||||
| 			if unified_inventory.is_creative(player:get_player_name()) then | 			local player_name = player:get_player_name() | ||||||
|  | 			if unified_inventory.is_creative(player_name) then | ||||||
| 				return stack:get_count() | 				return stack:get_count() | ||||||
| 			else | 			else | ||||||
| 				return 0 | 				return 0 | ||||||
| 			end | 			end | ||||||
| 		end, | 		end, | ||||||
| 		on_put = function(inv, listname, index, stack, player) | 		on_put = function(inv, listname, index, stack, player) | ||||||
|  | 			local player_name = player:get_player_name() | ||||||
| 			stack:set_count(stack:get_stack_max()) | 			stack:set_count(stack:get_stack_max()) | ||||||
| 			inv:set_stack(listname, index, stack) | 			inv:set_stack(listname, index, stack) | ||||||
| 			minetest.sound_play("electricity", {to_player=player_name, gain = 1.0}) | 			minetest.sound_play("electricity", | ||||||
|  | 					{to_player=player_name, gain = 1.0}) | ||||||
| 		end, | 		end, | ||||||
| 	}) | 	}) | ||||||
| 	refill:set_size("main", 1) | 	refill:set_size("main", 1) | ||||||
| @@ -60,9 +61,12 @@ minetest.register_on_player_receive_fields(function(player, formname, fields) | |||||||
| 	end | 	end | ||||||
| 	 | 	 | ||||||
| 	-- Inventory page controls | 	-- Inventory page controls | ||||||
| 	local start = math.floor(unified_inventory.current_index[player_name] / 80 + 1) | 	local start = math.floor( | ||||||
|  | 		unified_inventory.current_index[player_name] / 80 + 1) | ||||||
| 	local start_i = start | 	local start_i = start | ||||||
| 	local pagemax = math.floor((unified_inventory.filtered_items_list_size[player_name] - 1) / (80) + 1) | 	local pagemax = math.floor( | ||||||
|  | 		(#unified_inventory.filtered_items_list[player_name] - 1) | ||||||
|  | 		/ (80) + 1) | ||||||
| 	 | 	 | ||||||
| 	if fields.start_list then | 	if fields.start_list then | ||||||
| 		minetest.sound_play("paperflip1", | 		minetest.sound_play("paperflip1", | ||||||
| @@ -106,43 +110,36 @@ minetest.register_on_player_receive_fields(function(player, formname, fields) | |||||||
| 				unified_inventory.current_page[player_name]) | 				unified_inventory.current_page[player_name]) | ||||||
| 	end | 	end | ||||||
|  |  | ||||||
| 	-- Item list buttons | 	local clicked_item = nil | ||||||
| 	local list_index = unified_inventory.current_index[player_name] | 	for name, value in pairs(fields) do | ||||||
| 	local page = unified_inventory.current_page[player_name] | 		if string.sub(name, 1, 12) == "item_button_" then | ||||||
| 	for i = 0, 80 do | 			clicked_item = string.sub(name, 13) | ||||||
| 		local button = "item_button"..list_index | 			break | ||||||
| 		if fields[button] then  | 		end | ||||||
|  | 	end | ||||||
|  | 	if clicked_item then | ||||||
| 		minetest.sound_play("click", | 		minetest.sound_play("click", | ||||||
| 				{to_player=player_name, gain = 0.1}) | 				{to_player=player_name, gain = 0.1}) | ||||||
|  | 		local page = unified_inventory.current_page[player_name] | ||||||
| 		if not unified_inventory.is_creative(player_name) then | 		if not unified_inventory.is_creative(player_name) then | ||||||
| 				unified_inventory.set_inventory_formspec(player, "craftguide") |  | ||||||
| 			page = "craftguide" | 			page = "craftguide" | ||||||
| 		end | 		end | ||||||
| 		if page == "craftguide" then | 		if page == "craftguide" then | ||||||
| 				unified_inventory.current_item[player_name] = | 			unified_inventory.current_item[player_name] = clicked_item | ||||||
| 						unified_inventory.filtered_items_list |  | ||||||
| 							[player_name][list_index]  |  | ||||||
| 			unified_inventory.alternate[player_name] = 1 | 			unified_inventory.alternate[player_name] = 1 | ||||||
| 				unified_inventory.update_recipe(player, |  | ||||||
| 						unified_inventory.filtered_items_list |  | ||||||
| 							[player_name][list_index], 1) |  | ||||||
| 			unified_inventory.set_inventory_formspec(player, | 			unified_inventory.set_inventory_formspec(player, | ||||||
| 						unified_inventory.current_page[player_name]) | 					"craftguide") | ||||||
| 		else | 		else | ||||||
| 			if unified_inventory.is_creative(player_name) then | 			if unified_inventory.is_creative(player_name) then | ||||||
| 				local inv = player:get_inventory() | 				local inv = player:get_inventory() | ||||||
| 					dst_stack = {} | 				local stack = ItemStack(clicked_item) | ||||||
| 					dst_stack.name = unified_inventory.filtered_items_list | 				stack:set_count(99) | ||||||
| 							[player_name][list_index]  | 				if inv:room_for_item("main", stack) then | ||||||
| 					dst_stack.count = 99 | 					inv:add_item("main", stack) | ||||||
| 					if inv:room_for_item("main", dst_stack) then |  | ||||||
| 						inv:add_item("main", dst_stack) |  | ||||||
| 				end | 				end | ||||||
| 			end | 			end | ||||||
| 		end | 		end | ||||||
| 	end | 	end | ||||||
| 		list_index = list_index + 1 |  | ||||||
| 	end |  | ||||||
|  |  | ||||||
| 	if fields.searchbutton then | 	if fields.searchbutton then | ||||||
| 		unified_inventory.apply_filter(player, fields.searchbox) | 		unified_inventory.apply_filter(player, fields.searchbox) | ||||||
| @@ -170,8 +167,6 @@ minetest.register_on_player_receive_fields(function(player, formname, fields) | |||||||
| 					alternate = 1 | 					alternate = 1 | ||||||
| 				end | 				end | ||||||
| 				unified_inventory.alternate[player_name] = alternate		 | 				unified_inventory.alternate[player_name] = alternate		 | ||||||
| 				unified_inventory.update_recipe(player, |  | ||||||
| 						unified_inventory.current_item[player_name], alternate) |  | ||||||
| 				unified_inventory.set_inventory_formspec(player, | 				unified_inventory.set_inventory_formspec(player, | ||||||
| 						unified_inventory.current_page[player_name]) | 						unified_inventory.current_page[player_name]) | ||||||
| 			end | 			end | ||||||
|   | |||||||
							
								
								
									
										105
									
								
								internal.lua
									
									
									
									
									
								
							
							
						
						
									
										105
									
								
								internal.lua
									
									
									
									
									
								
							| @@ -47,8 +47,9 @@ function unified_inventory.get_formspec(player, page) | |||||||
| 	-- Items list | 	-- Items list | ||||||
| 	local list_index = unified_inventory.current_index[player_name] | 	local list_index = unified_inventory.current_index[player_name] | ||||||
| 	local page = math.floor(list_index / (80) + 1) | 	local page = math.floor(list_index / (80) + 1) | ||||||
| 	local pagemax = math.floor((unified_inventory.filtered_items_list_size[player_name] - 1) / (80) + 1) | 	local pagemax = math.floor( | ||||||
| 	local image = nil | 		(#unified_inventory.filtered_items_list[player_name] - 1) | ||||||
|  | 			/ (80) + 1) | ||||||
| 	local item = {} | 	local item = {} | ||||||
| 	for y = 0, 9 do | 	for y = 0, 9 do | ||||||
| 	for x = 0, 7 do | 	for x = 0, 7 do | ||||||
| @@ -57,8 +58,8 @@ function unified_inventory.get_formspec(player, page) | |||||||
| 			formspec = formspec.."item_image_button[" | 			formspec = formspec.."item_image_button[" | ||||||
| 					..(8.2 + x * 0.7).."," | 					..(8.2 + x * 0.7).."," | ||||||
| 					..(1   + y * 0.7)..";.81,.81;" | 					..(1   + y * 0.7)..";.81,.81;" | ||||||
| 					..name..";item_button" | 					..name..";item_button_" | ||||||
| 					..list_index..";]" | 					..name..";]" | ||||||
| 			list_index = list_index + 1 | 			list_index = list_index + 1 | ||||||
| 		end | 		end | ||||||
| 	end | 	end | ||||||
| @@ -112,102 +113,6 @@ function unified_inventory.apply_filter(player, filter) | |||||||
| 			unified_inventory.current_page[player_name]) | 			unified_inventory.current_page[player_name]) | ||||||
| end | end | ||||||
|  |  | ||||||
|  |  | ||||||
| -- update_recipe |  | ||||||
| function unified_inventory.update_recipe(player, stack_name, alternate) |  | ||||||
| 	local inv = minetest.get_inventory({ |  | ||||||
| 		type = "detached", |  | ||||||
| 		name = player:get_player_name().."craftrecipe" |  | ||||||
| 	})	 |  | ||||||
| 	for i = 0, inv:get_size("build") do |  | ||||||
| 		inv:set_stack("build", i, nil) |  | ||||||
| 	end |  | ||||||
| 	inv:set_stack("output", 1, nil) |  | ||||||
| 	alternate = tonumber(alternate) or 1 |  | ||||||
| 	local crafts = unified_inventory.crafts_table[stack_name] |  | ||||||
| 	--print(dump(crafts)) |  | ||||||
| 	if next(crafts) == nil then -- No craft recipes |  | ||||||
| 		return |  | ||||||
| 	end |  | ||||||
| 	if alternate < 1 or alternate > #crafts then |  | ||||||
| 		alternate = 1 |  | ||||||
| 	end |  | ||||||
| 	local craft = crafts[alternate] |  | ||||||
| 	inv:set_stack("output", 1, craft.output) |  | ||||||
| 	local items = craft.items |  | ||||||
|  |  | ||||||
| 	if craft.type == "cooking" or |  | ||||||
| 	   craft.type == "fuel" or |  | ||||||
| 	   craft.type == "grinding" or |  | ||||||
| 	   craft.type == "extracting" or |  | ||||||
| 	   craft.type == "compressing" then |  | ||||||
| 		def = unified_inventory.find_item_def(craft["items"][1]) |  | ||||||
| 		if def then |  | ||||||
| 			inv:set_stack("build", 1, def) |  | ||||||
| 		end |  | ||||||
| 		return |  | ||||||
| 	end |  | ||||||
| 	if craft.width == 0 then |  | ||||||
| 		for i = 1, 3 do |  | ||||||
| 			if craft.items[i] then |  | ||||||
| 				def = unified_inventory.find_item_def(craft.items[i]) |  | ||||||
| 				if def then |  | ||||||
| 					inv:set_stack("build", i, def) |  | ||||||
| 				end |  | ||||||
| 			end |  | ||||||
| 		end |  | ||||||
| 	end |  | ||||||
| 	if craft.width == 1 then |  | ||||||
| 		local build_table={1, 4, 7} |  | ||||||
| 		for i = 1, 3 do |  | ||||||
| 			if craft.items[i] then |  | ||||||
| 				def = unified_inventory.find_item_def(craft.items[i]) |  | ||||||
| 				if def then |  | ||||||
| 					inv:set_stack("build", build_table[i], def) |  | ||||||
| 				end |  | ||||||
| 			end |  | ||||||
| 		end |  | ||||||
| 	end |  | ||||||
| 	if craft.width == 2 then |  | ||||||
| 		local build_table = {1, 2, 4, 5, 7, 8} |  | ||||||
| 		for i=1, 6 do |  | ||||||
| 			if craft.items[i] then |  | ||||||
| 				def = unified_inventory.find_item_def(craft.items[i]) |  | ||||||
| 				if def then |  | ||||||
| 					inv:set_stack("build", build_table[i], def) |  | ||||||
| 				end |  | ||||||
| 			end |  | ||||||
| 		end |  | ||||||
| 	end |  | ||||||
| 	if craft.width == 3 then |  | ||||||
| 		for i=1, 9 do |  | ||||||
| 			if craft.items[i] then |  | ||||||
| 				def = unified_inventory.find_item_def(craft.items[i]) |  | ||||||
| 				if def then |  | ||||||
| 					inv:set_stack("build", i, def) |  | ||||||
| 				end |  | ||||||
| 			end |  | ||||||
| 		end |  | ||||||
| 	end |  | ||||||
| end |  | ||||||
|  |  | ||||||
| function unified_inventory.find_item_def(def) |  | ||||||
| 	if type(def) ~= "string" then |  | ||||||
| 		return nil |  | ||||||
| 	end |  | ||||||
| 	if string.find(def, "group:") then |  | ||||||
| 		def = string.gsub(def, "group:", "") |  | ||||||
| 		def = string.gsub(def, "\"", "") |  | ||||||
| 		if minetest.registered_nodes["default:"..def] then |  | ||||||
| 			return "default:"..def |  | ||||||
| 		end |  | ||||||
| 		local items = unified_inventory.items_in_group(def) |  | ||||||
| 		return items[1] |  | ||||||
| 	else |  | ||||||
| 		return def |  | ||||||
| 	end |  | ||||||
| end |  | ||||||
|  |  | ||||||
| function unified_inventory.items_in_group(groups) | function unified_inventory.items_in_group(groups) | ||||||
| 	local items = {} | 	local items = {} | ||||||
| 	for name, item in pairs(minetest.registered_items) do | 	for name, item in pairs(minetest.registered_items) do | ||||||
|   | |||||||
							
								
								
									
										59
									
								
								register.lua
									
									
									
									
									
								
							
							
						
						
									
										59
									
								
								register.lua
									
									
									
									
									
								
							| @@ -1,6 +1,6 @@ | |||||||
|  |  | ||||||
| minetest.register_privilege("creative", { | minetest.register_privilege("creative", { | ||||||
| 	description="Can use the creative inventory", | 	description = "Can use the creative inventory", | ||||||
| 	give_to_singleplayer = false, | 	give_to_singleplayer = false, | ||||||
| }) | }) | ||||||
|  |  | ||||||
| @@ -137,27 +137,27 @@ unified_inventory.register_page("craftguide", { | |||||||
| 		local player_name = player:get_player_name() | 		local player_name = player:get_player_name() | ||||||
| 		formspec = formspec.."background[0.06,0.99;7.92,7.52;ui_craftguide_form.png]" | 		formspec = formspec.."background[0.06,0.99;7.92,7.52;ui_craftguide_form.png]" | ||||||
| 		formspec = formspec.."label[0,0;Crafting Guide]" | 		formspec = formspec.."label[0,0;Crafting Guide]" | ||||||
| 		formspec = formspec.."list[detached:"..player_name.."craftrecipe;build;2,1;3,3;]" |  | ||||||
| 		formspec = formspec.."list[detached:"..player_name.."craftrecipe;output;6,1;1,1;]" | 		formspec = formspec.."list[detached:"..player_name.."craftrecipe;output;6,1;1,1;]" | ||||||
| 		formspec = formspec.."label[2,0.5;Input:]" | 		formspec = formspec.."label[2,0.5;Input:]" | ||||||
| 		formspec = formspec.."label[6,0.5;Output:]" | 		formspec = formspec.."label[6,0.5;Output:]" | ||||||
| 		formspec = formspec.."label[6,2.6;Method:]" | 		formspec = formspec.."label[6,2.6;Method:]" | ||||||
| 		local item_name = unified_inventory.current_item[player_name] | 		local item_name = unified_inventory.current_item[player_name] | ||||||
|  | 		local craft = nil | ||||||
| 		if item_name then | 		if item_name then | ||||||
| 			formspec = formspec.."label[2,0;"..item_name.."]"	 | 			formspec = formspec.."label[2,0;"..item_name.."]"	 | ||||||
| 			local alternates = 0 | 			local alternates = 0 | ||||||
| 			local alternate = unified_inventory.alternate[player_name] | 			local alternate = unified_inventory.alternate[player_name] | ||||||
| 			local crafts = unified_inventory.crafts_table[item_name] | 			local crafts = unified_inventory.crafts_table[item_name] | ||||||
|  |  | ||||||
| 			if crafts ~= nil and #crafts > 0 then | 			if crafts ~= nil and #crafts > 0 then | ||||||
| 				alternates = #crafts | 				alternates = #crafts | ||||||
| 				local craft = crafts[alternate] | 				craft = crafts[alternate] | ||||||
| 				local method = craft.type | 				local method = craft.type | ||||||
| 				if craft.type == "shapeless" then | 				if craft.type == "normal" then | ||||||
| 					method="shapeless crafting" | 					method = "crafting" | ||||||
| 				end	 | 				elseif craft.type == "shapeless" then | ||||||
| 				if craft.type == "alloy" then | 					method = "shapeless crafting" | ||||||
| 					method="alloy cooking" | 				elseif craft.type == "alloy" then | ||||||
|  | 					method = "alloy cooking" | ||||||
| 				end | 				end | ||||||
| 				formspec = formspec.."label[6,3;"..method.."]" | 				formspec = formspec.."label[6,3;"..method.."]" | ||||||
| 			end | 			end | ||||||
| @@ -168,6 +168,47 @@ unified_inventory.register_page("craftguide", { | |||||||
| 				formspec = formspec.."button[0,3.15;2,1;alternate;Alternate]" | 				formspec = formspec.."button[0,3.15;2,1;alternate;Alternate]" | ||||||
| 			end | 			end | ||||||
| 		end | 		end | ||||||
|  |  | ||||||
|  | 		local craftinv = minetest.get_inventory({ | ||||||
|  | 			type = "detached", | ||||||
|  | 			name = player_name.."craftrecipe" | ||||||
|  | 		}) | ||||||
|  |  | ||||||
|  | 		if not craft then | ||||||
|  | 			craftinv:set_stack("output", 1, nil) | ||||||
|  | 			return formspec | ||||||
|  | 		end | ||||||
|  |  | ||||||
|  | 		craftinv:set_stack("output", 1, craft.output) | ||||||
|  |  | ||||||
|  | 		local width = craft.width | ||||||
|  | 		if width == 0 then | ||||||
|  | 			-- Shapeless recipe | ||||||
|  | 			width = 3 | ||||||
|  | 		end | ||||||
|  |  | ||||||
|  | 		local i = 1 | ||||||
|  | 		for y = 1, 3 do | ||||||
|  | 		for x = 1, width do | ||||||
|  | 			local item = craft.items[i] | ||||||
|  | 			if item then | ||||||
|  | 				if string.sub(item, 1, 6) == "group:" then | ||||||
|  | 					local group = string.sub(item, 7) | ||||||
|  | 					formspec = formspec.."image_button[" | ||||||
|  | 						..(1.05 + x)..","..(0.05 + y)..";0.9,0.9;" | ||||||
|  | 						.."ui_group.png;;" | ||||||
|  | 						..minetest.formspec_escape(group).."]" | ||||||
|  | 				else | ||||||
|  | 					formspec = formspec.."item_image_button[" | ||||||
|  | 						..(1.05 + x)..","..(0.05 + y)..";0.9,0.9;" | ||||||
|  | 						..minetest.formspec_escape(item)..";" | ||||||
|  | 						.."item_button_" | ||||||
|  | 						..minetest.formspec_escape(item)..";]" | ||||||
|  | 				end | ||||||
|  | 			end | ||||||
|  | 			i = i + 1 | ||||||
|  | 		end | ||||||
|  | 		end | ||||||
| 		return formspec | 		return formspec | ||||||
| 	end, | 	end, | ||||||
| }) | }) | ||||||
|   | |||||||
							
								
								
									
										
											BIN
										
									
								
								textures/ui_group.png
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										
											BIN
										
									
								
								textures/ui_group.png
									
									
									
									
									
										Normal file
									
								
							
										
											Binary file not shown.
										
									
								
							| After Width: | Height: | Size: 29 KiB | 
		Reference in New Issue
	
	Block a user