From babe4380acf1fcd89c3658e72ded9942fbec21bf Mon Sep 17 00:00:00 2001 From: HybridDog Date: Sun, 16 Aug 2015 16:40:49 +0200 Subject: [PATCH] disallow repairing tools using the 'to craft grid', allow it to put items of specific groups and (maybe) make it keep the meta of items, remove unused elseif, change a bit translation and use table.concat at one place because I read somewhere that it's faster than .. --- api.lua | 10 ++--- callbacks.lua | 78 ++++++++++++++++++---------------- internal.lua | 114 ++++++++++++++++++++++++++++---------------------- locale/de.txt | 64 ++++++++++++++-------------- register.lua | 103 +++++++++++++++++++++++++++++++++++++-------- 5 files changed, 226 insertions(+), 143 deletions(-) diff --git a/api.lua b/api.lua index 5c87af4..59296db 100644 --- a/api.lua +++ b/api.lua @@ -117,7 +117,7 @@ end -- register_craft function unified_inventory.register_craft(options) - if options.output == nil then + if not options.output then return end local itemstack = ItemStack(options.output) @@ -127,7 +127,7 @@ function unified_inventory.register_craft(options) if options.type == "normal" and options.width == 0 then options = { type = "shapeless", items = options.items, output = options.output, width = 0 } end - if unified_inventory.crafts_for.recipe[itemstack:get_name()] == nil then + if not unified_inventory.crafts_for.recipe[itemstack:get_name()] then unified_inventory.crafts_for.recipe[itemstack:get_name()] = {} end table.insert(unified_inventory.crafts_for.recipe[itemstack:get_name()],options) @@ -220,9 +220,7 @@ end function unified_inventory.is_creative(playername) - if minetest.check_player_privs(playername, {creative=true}) or - minetest.setting_getbool("creative_mode") then - return true - end + return minetest.check_player_privs(playername, {creative=true}) + or minetest.setting_getbool("creative_mode") end diff --git a/callbacks.lua b/callbacks.lua index 3a41c32..c0f9d04 100644 --- a/callbacks.lua +++ b/callbacks.lua @@ -1,7 +1,9 @@ local function default_refill(stack) stack:set_count(stack:get_stack_max()) local itemdef = minetest.registered_items[stack:get_name()] - if itemdef and (itemdef.wear_represents or "mechanical_wear") == "mechanical_wear" and stack:get_wear() ~= 0 then + if itemdef + and (itemdef.wear_represents or "mechanical_wear") == "mechanical_wear" + and stack:get_wear() ~= 0 then stack:set_wear(0) end return stack @@ -52,7 +54,8 @@ minetest.register_on_player_receive_fields(function(player, formname, fields) local player_name = player:get_player_name() -- always take new search text, even if not searching on it yet - if fields.searchbox ~= nil and fields.searchbox ~= unified_inventory.current_searchbox[player_name] then + if fields.searchbox + and fields.searchbox ~= unified_inventory.current_searchbox[player_name] then unified_inventory.current_searchbox[player_name] = fields.searchbox unified_inventory.set_inventory_formspec(player, unified_inventory.current_page[player_name]) end @@ -98,7 +101,7 @@ minetest.register_on_player_receive_fields(function(player, formname, fields) if start_i > pagemax then start_i = pagemax end - if not (start_i == start) then + if start_i ~= start then minetest.sound_play("paperflip1", {to_player=player_name, gain = 1.0}) unified_inventory.current_index[player_name] = (start_i - 1) * unified_inventory.items_per_page + 1 @@ -106,7 +109,7 @@ minetest.register_on_player_receive_fields(function(player, formname, fields) unified_inventory.current_page[player_name]) end - local clicked_item = nil + local clicked_item for name, value in pairs(fields) do if string.sub(name, 1, 12) == "item_button_" then local new_dir, mangled_item = string.match(name, "^item_button_([a-z]+)_(.*)$") @@ -116,7 +119,8 @@ minetest.register_on_player_receive_fields(function(player, formname, fields) unified_inventory.apply_filter(player, clicked_item, new_dir) return end - if new_dir == "recipe" or new_dir == "usage" then + if new_dir == "recipe" + or new_dir == "usage" then unified_inventory.current_craft_direction[player_name] = new_dir end break @@ -126,22 +130,20 @@ minetest.register_on_player_receive_fields(function(player, formname, fields) minetest.sound_play("click", {to_player=player_name, gain = 0.1}) local page = unified_inventory.current_page[player_name] - if not unified_inventory.is_creative(player_name) then + local player_creative = unified_inventory.is_creative(player_name) + if not player_creative then page = "craftguide" end if page == "craftguide" then unified_inventory.current_item[player_name] = clicked_item unified_inventory.alternate[player_name] = 1 - unified_inventory.set_inventory_formspec(player, - "craftguide") - else - if unified_inventory.is_creative(player_name) then - local inv = player:get_inventory() - local stack = ItemStack(clicked_item) - stack:set_count(stack:get_stack_max()) - if inv:room_for_item("main", stack) then - inv:add_item("main", stack) - end + unified_inventory.set_inventory_formspec(player, "craftguide") + elseif player_creative then + local inv = player:get_inventory() + local stack = ItemStack(clicked_item) + stack:set_count(stack:get_stack_max()) + if inv:room_for_item("main", stack) then + inv:add_item("main", stack) end end end @@ -156,27 +158,29 @@ minetest.register_on_player_receive_fields(function(player, formname, fields) end -- alternate button - if fields.alternate then - minetest.sound_play("click", - {to_player=player_name, gain = 0.1}) - local item_name = unified_inventory.current_item[player_name] - if item_name then - local alternates = 0 - local alternate = unified_inventory.alternate[player_name] - local crafts = unified_inventory.crafts_for[unified_inventory.current_craft_direction[player_name]][item_name] - if crafts ~= nil then - alternates = #crafts - end - if alternates > 1 then - alternate = alternate + 1 - if alternate > alternates then - alternate = 1 - end - unified_inventory.alternate[player_name] = alternate - unified_inventory.set_inventory_formspec(player, - unified_inventory.current_page[player_name]) - end - end + if not fields.alternate then + return end + minetest.sound_play("click", + {to_player=player_name, gain = 0.1}) + local item_name = unified_inventory.current_item[player_name] + if not item_name then + return + end + local crafts = unified_inventory.crafts_for[unified_inventory.current_craft_direction[player_name]][item_name] + if not crafts then + return + end + local alternates = #crafts + if alternates <= 1 then + return + end + local alternate = unified_inventory.alternate[player_name] + 1 + if alternate > alternates then + alternate = 1 + end + unified_inventory.alternate[player_name] = alternate + unified_inventory.set_inventory_formspec(player, + unified_inventory.current_page[player_name]) end) diff --git a/internal.lua b/internal.lua index 28fbde7..a93b847 100644 --- a/internal.lua +++ b/internal.lua @@ -23,28 +23,30 @@ function unified_inventory.get_formspec(player, page) unified_inventory.current_page[player_name] = page local pagedef = unified_inventory.pages[page] - local formspec = "size[14,10]" - -- Background - formspec = formspec .. "background[-0.19,-0.25;14.4,10.75;ui_form_bg.png]" + local formspec = { + "size[14,10]", + "background[-0.19,-0.25;14.4,10.75;ui_form_bg.png]" -- Background + } + local n = 3 if unified_inventory.lite_mode then - formspec = "size[11,7.7]" - formspec = formspec .. "background[-0.19,-0.2;11.4,8.4;ui_form_bg.png]" + formspec[1] = "size[11,7.7]" + formspec[2] = "background[-0.19,-0.2;11.4,8.4;ui_form_bg.png]" end - if unified_inventory.is_creative(player_name) and page == "craft" then - formspec = formspec.."background[0,"..(unified_inventory.formspec_y + 2)..";1,1;ui_single_slot.png]" + if unified_inventory.is_creative(player_name) + and page == "craft" then + formspec[n] = "background[0,"..(unified_inventory.formspec_y + 2)..";1,1;ui_single_slot.png]" + n = n+1 end - local fsdata = nil - -- Current page - if unified_inventory.pages[page] then - fsdata = pagedef.get_formspec(player) - formspec = formspec .. fsdata.formspec - else + if not unified_inventory.pages[page] then return "" -- Invalid page name end + local fsdata = pagedef.get_formspec(player) + formspec[n] = fsdata.formspec + n = n+1 local button_row = 0 local button_col = 0 @@ -57,34 +59,35 @@ function unified_inventory.get_formspec(player, page) button_col = 1 end - local tooltip = def.tooltip or "" if def.type == "image" then - formspec = formspec.."image_button[" - ..( unified_inventory.main_button_x + 0.65 * (i - 1) - button_col * 0.65 * 4) - ..","..(unified_inventory.main_button_y + button_row * 0.7)..";0.8,0.8;" - ..minetest.formspec_escape(def.image)..";" - ..minetest.formspec_escape(def.name)..";]" - .."tooltip["..minetest.formspec_escape(def.name) - ..";"..tooltip.."]" + formspec[n] = "image_button[" + formspec[n+1] = ( unified_inventory.main_button_x + 0.65 * (i - 1) - button_col * 0.65 * 4) + formspec[n+2] = ","..(unified_inventory.main_button_y + button_row * 0.7)..";0.8,0.8;" + formspec[n+3] = minetest.formspec_escape(def.image)..";" + formspec[n+4] = minetest.formspec_escape(def.name)..";]" + formspec[n+5] = "tooltip["..minetest.formspec_escape(def.name) + formspec[n+6] = ";"..(def.tooltip or "").."]" + n = n+7 end end if fsdata.draw_inventory ~= false then -- Player inventory - formspec = formspec.."listcolors[#00000000;#00000000]" - formspec = formspec .. "list[current_player;main;0,"..(unified_inventory.formspec_y + 3.5)..";8,4;]" + formspec[n] = "listcolors[#00000000;#00000000]" + formspec[n+1] = "list[current_player;main;0,"..(unified_inventory.formspec_y + 3.5)..";8,4;]" + n = n+2 end if fsdata.draw_item_list == false then - return formspec + return table.concat(formspec, "") end -- Controls to flip items pages local start_x = 9.2 if not unified_inventory.lite_mode then - formspec = formspec - .. "image_button[" .. (start_x + 0.6 * 0) + formspec[n] = + "image_button[" .. (start_x + 0.6 * 0) .. ",9;.8,.8;ui_skip_backward_icon.png;start_list;]" .. "tooltip[start_list;" .. minetest.formspec_escape(S("First page")) .. "]" @@ -106,8 +109,8 @@ function unified_inventory.get_formspec(player, page) .. ",9;.8,.8;ui_skip_forward_icon.png;end_list;]" .. "tooltip[end_list;" .. minetest.formspec_escape(S("Last page")) .. "]" else - formspec = formspec - .. "image_button[" .. (8.2 + 0.65 * 0) + formspec[n] = + "image_button[" .. (8.2 + 0.65 * 0) .. ",5.8;.8,.8;ui_skip_backward_icon.png;start_list;]" .. "tooltip[start_list;" .. minetest.formspec_escape(S("First page")) .. "]" .. "image_button[" .. (8.2 + 0.65 * 1) @@ -120,20 +123,22 @@ function unified_inventory.get_formspec(player, page) .. ",5.8;.8,.8;ui_skip_forward_icon.png;end_list;]" .. "tooltip[end_list;" .. minetest.formspec_escape(S("Last page")) .. "]" end + n = n+1 -- Search box if not unified_inventory.lite_mode then - formspec = formspec .. "field[9.5,8.325;3,1;searchbox;;" + formspec[n] = "field[9.5,8.325;3,1;searchbox;;" .. minetest.formspec_escape(unified_inventory.current_searchbox[player_name]) .. "]" - formspec = formspec .. "image_button[12.2,8.1;.8,.8;ui_search_icon.png;searchbutton;]" + formspec[n+1] = "image_button[12.2,8.1;.8,.8;ui_search_icon.png;searchbutton;]" .. "tooltip[searchbutton;" ..S("Search") .. "]" else - formspec = formspec .. "field[8.5,5.225;2.2,1;searchbox;;" + formspec[n] = "field[8.5,5.225;2.2,1;searchbox;;" .. minetest.formspec_escape(unified_inventory.current_searchbox[player_name]) .. "]" - formspec = formspec .. "image_button[10.3,5;.8,.8;ui_search_icon.png;searchbutton;]" + formspec[n+1] = "image_button[10.3,5;.8,.8;ui_search_icon.png;searchbutton;]" .. "tooltip[searchbutton;" ..S("Search") .. "]" end + n = n+2 local no_matches = "No matching items" if unified_inventory.lite_mode then @@ -142,7 +147,7 @@ function unified_inventory.get_formspec(player, page) -- Items list if #unified_inventory.filtered_items_list[player_name] == 0 then - formspec = formspec.."label[8.2,"..unified_inventory.form_header_y..";" .. S(no_matches) .. "]" + formspec[n] = "label[8.2,"..unified_inventory.form_header_y..";" .. S(no_matches) .. "]" else local dir = unified_inventory.active_search_direction[player_name] local list_index = unified_inventory.current_index[player_name] @@ -155,35 +160,39 @@ function unified_inventory.get_formspec(player, page) for x = 0, unified_inventory.pagecols - 1 do local name = unified_inventory.filtered_items_list[player_name][list_index] if minetest.registered_items[name] then - formspec = formspec.."item_image_button[" - ..(8.2 + x * 0.7).."," - ..(unified_inventory.formspec_y + unified_inventory.page_y + y * 0.7)..";.81,.81;" - ..name..";item_button_"..dir.."_" - ..unified_inventory.mangle_for_formspec(name)..";]" + formspec[n] = "item_image_button[" + ..(8.2 + x * 0.7).."," + ..(unified_inventory.formspec_y + unified_inventory.page_y + y * 0.7)..";.81,.81;" + ..name..";item_button_"..dir.."_" + ..unified_inventory.mangle_for_formspec(name)..";]" + n = n+1 list_index = list_index + 1 end end end - formspec = formspec.."label[8.2,"..unified_inventory.form_header_y..";"..S("Page") .. ": " + formspec[n] = "label[8.2,"..unified_inventory.form_header_y..";"..S("Page") .. ": " .. S("%s of %s"):format(page,pagemax).."]" end + n= n+1 + if unified_inventory.activefilter[player_name] ~= "" then - formspec = formspec.."label[8.2,"..(unified_inventory.form_header_y + 0.4)..";" .. S("Filter") .. ":]" - formspec = formspec.."label[9.1,"..(unified_inventory.form_header_y + 0.4)..";"..minetest.formspec_escape(unified_inventory.activefilter[player_name]).."]" + formspec[n] = "label[8.2,"..(unified_inventory.form_header_y + 0.4)..";" .. S("Filter") .. ":]" + formspec[n+1] = "label[9.1,"..(unified_inventory.form_header_y + 0.4)..";"..minetest.formspec_escape(unified_inventory.activefilter[player_name]).."]" end - return formspec + return table.concat(formspec, "") end function unified_inventory.set_inventory_formspec(player, page) if player then - local formspec = unified_inventory.get_formspec(player, page) - player:set_inventory_formspec(formspec) + player:set_inventory_formspec(unified_inventory.get_formspec(player, page)) end end --apply filter to the inventory list (create filtered copy of full one) function unified_inventory.apply_filter(player, filter, search_dir) - if not player then return false end + if not player then + return false + end local player_name = player:get_player_name() local lfilter = string.lower(filter) local ffilter @@ -191,7 +200,8 @@ function unified_inventory.apply_filter(player, filter, search_dir) local groups = lfilter:sub(7):split(",") ffilter = function(name, def) for _, group in ipairs(groups) do - if not ((def.groups[group] or 0) > 0) then + if not def.groups[group] + or def.groups[group] <= 0 then return false end end @@ -206,11 +216,13 @@ function unified_inventory.apply_filter(player, filter, search_dir) end unified_inventory.filtered_items_list[player_name]={} for name, def in pairs(minetest.registered_items) do - if (def.groups.not_in_creative_inventory or 0) == 0 - and (def.description or "") ~= "" - and ffilter(name, def) - and (unified_inventory.is_creative(player_name) - or unified_inventory.crafts_for.recipe[def.name]) then + if (not def.groups.not_in_creative_inventory + or def.groups.not_in_creative_inventory == 0) + and def.description + and def.description ~= "" + and ffilter(name, def) + and (unified_inventory.is_creative(player_name) + or unified_inventory.crafts_for.recipe[def.name]) then table.insert(unified_inventory.filtered_items_list[player_name], name) end end diff --git a/locale/de.txt b/locale/de.txt index 9f12d8c..d678b68 100644 --- a/locale/de.txt +++ b/locale/de.txt @@ -1,4 +1,4 @@ -# Translation by Xanthin +# Translation mostly by Xanthin ### bags.lua ### Bags = Rucksaecke @@ -11,23 +11,23 @@ Medium Bag = Rucksack (mittel) Large Bag = Rucksack (gross) ### inernal.lua ### -First page = -Back three pages = -Back one page = -Forward one page = -Forward three pages = -Last page = -No matching items = +First page = Erste Seite +Back three pages = Drei Seiten zurueckblaettern +Back one page = Eine Seiten zurueckblaettern +Forward one page = Eine Seiten vorblaettern +Forward three pages = Drei Seiten vorblaettern +Last page = Letzte Seite +No matching items = Keine passenden Gegenstände Page = Seite %s of %s = %s von %s Filter = Suche -Search = +Search = Suchen ### register.lua ### Can use the creative inventory = Kann das Kreativinventar nutzen Home position set to: %s = Ausgangsposition nach: %s gesetzt Time of day set to 6am = Tageszeit auf 6 Uhr morgens geaendert -You don't have the settime priviledge! = Du hast nicht das "settime" Privileg! +You don't have the settime privilege! = Du hast nicht das "settime" Privileg! Time of day set to 9pm = Tageszeit auf 9 Uhr abends geaendert This button has been disabled outside of creative mode to prevent accidental inventory trashing. Use the trash slot instead. = Diese Funktion ist ausserhalb des Kreativmodus deaktiviert um ein versehentliches Loeschen des ganzen Inventars zu verhindern.\nNutze stattdessen das Muellfeld. Inventory Cleared! = Inventar geleert! @@ -45,27 +45,27 @@ Copy to craft grid: = Kopiere ins Baufeld: All = Alles Recipe %s of %s = Rezept %s von %s Alternate = Alternative -Crafting Grid = +Crafting Grid = ### waypoints.lua ### -White = -Yellow = -Red = -Green = -Blue = -Waypoints = -Waypoint active = -Waypoint inactive = -World position = -Name = -HUD text color = -Edit waypoint name = -Rename waypoint = -Change color of waypoint display = -Set waypoint to current location = -Make waypoint visible = -Make waypoint invisible = -Disable display of waypoint coordinates = -Enable display of waypoint coordinates = -Finish editing = -Select Waypoint #%d = +White = Weiß +Yellow = Gelb +Red = Rot +Green = Gruen +Blue = Blau +Waypoints = Markierungen +Waypoint active = Markierung aktiv +Waypoint inactive = Markierung inaktiv +World position = Welt Position +Name = +HUD text color = +Edit waypoint name = Name der Markierung aendern +Rename waypoint = Markierung umbenennen +Change color of waypoint display = Farbe der Darstellung der Markierung aendern +Set waypoint to current location = Setze Markierung zur derzeitigen Position +Make waypoint visible = Markierung sichtbar machen +Make waypoint invisible = Markierung verstecken +Disable display of waypoint coordinates = +Enable display of waypoint coordinates = +Finish editing = +Select Waypoint #%d = diff --git a/register.lua b/register.lua index ff18d30..b46cea5 100644 --- a/register.lua +++ b/register.lua @@ -87,7 +87,7 @@ if not unified_inventory.lite_mode then S("Time of day set to 6am")) else minetest.chat_send_player(player_name, - S("You don't have the settime priviledge!")) + S("You don't have the settime privilege!")) end end, }) @@ -106,7 +106,7 @@ if not unified_inventory.lite_mode then S("Time of day set to 9pm")) else minetest.chat_send_player(player_name, - S("You don't have the settime priviledge!")) + S("You don't have the settime privilege!")) end end, }) @@ -326,6 +326,80 @@ local function craftguide_giveme(player, formname, fields) player_inv:add_item("main", {name = output, count = amount}) end +-- tells if an item can be moved and returns an index if so +local function item_fits(player_inv, craft_item, needed_item) + local need_group = string.sub(needed_item, 1, 6) == "group:" + if need_group then + need_group = string.sub(needed_item, 7) + end + if craft_item + and not craft_item:is_empty() then + local ciname = craft_item:get_name() + + -- abort if the item there isn't usable + if ciname ~= needed_item + and not need_group then + return + end + + -- abort if no item fits onto it + if craft_item:get_count() >= craft_item:get_definition().stack_max then + return + end + + -- use the item there if it's in the right group and a group item is needed + if need_group then + if minetest.get_item_group(ciname, need_group) == 0 then + return + end + needed_item = ciname + need_group = false + end + end + + if need_group then + -- search an item of the specific group + for i,item in pairs(player_inv:get_list("main")) do + if not item:is_empty() + and minetest.get_item_group(item:get_name(), need_group) > 0 then + return i + end + end + + -- no index found + return + end + + -- search an item with a the name needed_item + for i,item in pairs(player_inv:get_list("main")) do + if not item:is_empty() + and item:get_name() == needed_item then + return i + end + end + + -- no index found +end + +-- modifies the player inventory and returns the changed craft_item if possible +local function move_item(player_inv, craft_item, needed_item) + local stackid = item_fits(player_inv, craft_item, needed_item) + if not stackid then + return + end + local wanted_stack = player_inv:get_stack("main", stackid) + local taken_item = wanted_stack:take_item() + player_inv:set_stack("main", stackid, wanted_stack) + + if not craft_item + or craft_item:is_empty() then + return taken_item + end + + craft_item:add_item(taken_item) + return craft_item +end + local function craftguide_craft(player, formname, fields) local amount for k, v in pairs(fields) do @@ -358,11 +432,13 @@ local function craftguide_craft(player, formname, fields) width = 3 end + amount = tonumber(amount) or 99 + --[[ if amount == "max" then amount = 99 -- Arbitrary; need better way to do this. else amount = tonumber(amount) - end + end--]] for iter = 1, amount do local index = 1 @@ -372,17 +448,9 @@ local function craftguide_craft(player, formname, fields) if needed_item then local craft_index = ((y - 1) * 3) + x local craft_item = craft_list[craft_index] - if (not craft_item) or (craft_item:is_empty()) or (craft_item:get_name() == needed_item) then - itemname = craft_item and craft_item:get_name() or needed_item - local needed_stack = ItemStack(needed_item) - if player_inv:contains_item("main", needed_stack) then - local count = (craft_item and craft_item:get_count() or 0) + 1 - if count <= needed_stack:get_definition().stack_max then - local stack = ItemStack({name=needed_item, count=count}) - craft_list[craft_index] = stack - player_inv:remove_item("main", needed_stack) - end - end + local newitem = move_item(player_inv, craft_item, needed_item) + if newitem then + craft_list[craft_index] = newitem end end index = index + 1 @@ -399,10 +467,11 @@ minetest.register_on_player_receive_fields(function(player, formname, fields) for k, v in pairs(fields) do if k:match("craftguide_craft_") then craftguide_craft(player, formname, fields) - break - elseif k:match("craftguide_giveme_") then + return + end + if k:match("craftguide_giveme_") then craftguide_giveme(player, formname, fields) - break + return end end end)