2017-06-14 23:37:18 -07:00
|
|
|
--[[ LICENSE HEADER
|
|
|
|
|
|
|
|
MIT Licensing
|
|
|
|
|
|
|
|
Copyright © 2017 Jordan Irwin
|
|
|
|
|
|
|
|
See: LICENSE.txt
|
|
|
|
--]]
|
|
|
|
|
|
|
|
|
2017-07-23 15:24:27 -07:00
|
|
|
-- Boilerplate to support localized strings if intllib mod is installed.
|
|
|
|
local S
|
2017-07-24 12:00:01 -07:00
|
|
|
if core.global_exists('intllib') then
|
2017-07-23 15:24:27 -07:00
|
|
|
if intllib.make_gettext_pair then
|
|
|
|
S = intllib.make_gettext_pair()
|
|
|
|
else
|
|
|
|
S = intllib.Getter()
|
|
|
|
end
|
|
|
|
else
|
|
|
|
S = function(s) return s end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
-- Invoking command string
|
2017-07-24 12:00:49 -07:00
|
|
|
local cmd_item = S('listitems')
|
2017-07-23 15:24:27 -07:00
|
|
|
|
|
|
|
|
2017-06-14 23:37:18 -07:00
|
|
|
-- Retrieves a simplified table containing string names of registered items
|
|
|
|
local function getRegisteredItemNames()
|
|
|
|
local item_names = {}
|
2017-07-24 12:00:01 -07:00
|
|
|
for item, def in pairs(core.registered_items) do
|
2017-06-14 23:37:18 -07:00
|
|
|
table.insert(item_names, item)
|
|
|
|
end
|
|
|
|
|
|
|
|
table.sort(item_names)
|
|
|
|
|
|
|
|
return item_names
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
-- Compares a string from a list of substrings
|
|
|
|
local function compareSubstringList(ss_list, s_value)
|
|
|
|
for index, substring in ipairs(ss_list) do
|
|
|
|
-- Tests for substring (does not need to match full string)
|
|
|
|
if string.find(s_value, substring) then
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
-- Checks if value is contained in list
|
|
|
|
local function listContains(tlist, v)
|
|
|
|
for index, value in ipairs(tlist) do
|
|
|
|
if v == value then
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
-- Replaces duplicates found in a list
|
|
|
|
local function removeListDuplicates(tlist)
|
|
|
|
local cleaned = {}
|
2017-07-24 14:27:54 -07:00
|
|
|
if tlist ~= nil then
|
|
|
|
for index, value in ipairs(tlist) do
|
|
|
|
if not listContains(cleaned, value) then
|
|
|
|
table.insert(cleaned, value)
|
|
|
|
end
|
2017-06-14 23:37:18 -07:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return cleaned
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2017-07-24 15:21:17 -07:00
|
|
|
-- Searches & formats list for matching strings
|
|
|
|
local function formatMatching(nlist, params)
|
|
|
|
local matching = {}
|
|
|
|
|
|
|
|
-- Use entire list if no parameters supplied
|
|
|
|
if next(params) == nil then
|
|
|
|
matching = nlist
|
|
|
|
else
|
|
|
|
-- Fill matching list
|
|
|
|
for index in pairs(nlist) do
|
|
|
|
if compareSubstringList(params, string.lower(nlist[index])) then
|
|
|
|
table.insert(matching, nlist[index])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return matching
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2017-07-24 02:45:12 -07:00
|
|
|
local bullet_list = core.settings:get_bool('listitems.bullet_list')
|
|
|
|
if bullet_list == nil then
|
|
|
|
-- Default is true
|
|
|
|
bullet_list = true
|
|
|
|
end
|
|
|
|
|
|
|
|
local bullet = ''
|
|
|
|
if bullet_list then
|
|
|
|
bullet = S('•') .. ' '
|
|
|
|
end
|
|
|
|
|
2017-07-24 12:06:31 -07:00
|
|
|
|
2017-07-24 14:32:39 -07:00
|
|
|
-- Displays list to player
|
|
|
|
local function displayList(player, dlist)
|
|
|
|
if dlist ~= nil then
|
|
|
|
for i, n in ipairs(dlist) do
|
|
|
|
core.chat_send_player(player, bullet .. n)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
-- Show player number of items listed
|
|
|
|
core.chat_send_player(player, S('Items listed:') .. ' ' .. tostring(#dlist))
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2017-07-24 14:30:05 -07:00
|
|
|
-- Custom registration function for chat commands
|
|
|
|
local function registerChatCommand(cmd_name, def)
|
|
|
|
listitems.logInfo('Registering chat command "' .. cmd_name .. '"')
|
|
|
|
core.register_chatcommand(cmd_name, def)
|
|
|
|
end
|
2017-07-24 12:06:31 -07:00
|
|
|
|
2017-07-24 12:00:01 -07:00
|
|
|
|
2017-07-24 14:30:05 -07:00
|
|
|
-- listitems command
|
|
|
|
registerChatCommand(cmd_item, {
|
2017-07-23 15:24:27 -07:00
|
|
|
params = '[' .. S('string1') .. '] [' .. S('string2') .. '] ...',
|
|
|
|
description = S('List registered items'),
|
2017-06-14 23:37:18 -07:00
|
|
|
func = function(player, param)
|
2017-07-24 15:21:17 -07:00
|
|
|
-- Split parameters into case-insensitive list & remove duplicates
|
2017-06-14 23:37:18 -07:00
|
|
|
param = removeListDuplicates(string.split(string.lower(param), ' '))
|
|
|
|
|
2017-07-24 15:04:13 -07:00
|
|
|
local all_names = getRegisteredItemNames()
|
2017-07-24 15:21:17 -07:00
|
|
|
local found_names = formatMatching(all_names, param)
|
2017-06-14 23:37:18 -07:00
|
|
|
|
2017-07-24 14:32:39 -07:00
|
|
|
displayList(player, found_names)
|
2017-06-14 23:37:18 -07:00
|
|
|
|
|
|
|
return true
|
|
|
|
end,
|
2017-07-24 12:00:01 -07:00
|
|
|
})
|