2 Commits

Author SHA1 Message Date
a9c5e54b00 Node: minor change 2026-07-23 19:00:09 +03:00
6935673be7 Add new admin command /create_locale 2026-07-23 18:59:39 +03:00
3 changed files with 80 additions and 10 deletions

View File

@@ -72,6 +72,8 @@ function Node:new(nodeDef)
local settings = nodeDef.settings
settings._type = 'node'
local instance = Item:new(nodeDef) --[[@as Node]]
-- Registration of segmented blocks.
-- If you specify a specific group with a specific index for a node,
-- the slabs and stairs will be registered automatically.
@@ -88,8 +90,6 @@ function Node:new(nodeDef)
registerStair(nodeDef)
end
local instance = Item:new(nodeDef) --[[@as Node]]
return instance
end

View File

@@ -99,11 +99,11 @@ core.register_chatcommand('get_missing_locale', {
local langCodes = params.lang_codes
if shortModName ~= nil and type(shortModName) ~= 'string' then
return false
return false, 'Incorrect mod name'
end
if langCodes ~= nil and type(langCodes) ~= 'table' then
return false
return false, 'Incorrect lang code'
end
local items
@@ -130,3 +130,60 @@ core.register_chatcommand('get_missing_locale', {
return true, ''
end
})
core.register_chatcommand('create_locale', {
privs = { server = true },
description = '<short_mod_name> <lang_code>',
func = function(name, params)
local shortModName, langCode = string.match(params, '(%S+) (%S+)')
if shortModName == nil or shortModName == '' then
return false, 'Incorrect mod name!'
end
if langCode == nil or langCode == '' or not table.contains(LANG_CODES, langCode)then
return false, 'Incorrect lang code!'
end
local items = Smc.getRegisteredItemsListByMod(shortModName)
if items == nil then
return false, 'There are no items registered for this mod (or the entire game)'
end
local filePath = core.get_worldpath()..('/%s.%s.tr'):format(shortModName, langCode)
local file, err = io.open(filePath, 'w')
if not file then
return false, 'Cannot open file: '..err
end
file:write('# textdomain: '..shortModName..'\n\n')
for _, item in ipairs(items) do
local itemSettings = item.itemDef.settings
local name = string.match(itemSettings.name, ('%S+:(%S+)'))
local title = core.strip_colors(core.get_translated_string(langCode, itemSettings._title))
local desc = core.strip_colors(core.get_translated_string(langCode, itemSettings._description))
if title ~= name..'-title' then
title = '[locale already exists]'
else
title = ''
end
if desc ~= name..'-desc' then
desc = '[locale already exists]'
else
desc = ''
end
file:write(('%s-title=%s\n%s-desc=%s\n'):format(name, title, name, desc))
end
file:write('\n')
file:close()
return true, 'File created: '..filePath
end
})

View File

@@ -1,23 +1,30 @@
local tableInsert = table.insert
---@type table<string, Item|Node>
local registeredItems = {}
---@type table<string, table<string, Item|Node>>
local registeredItemsByMod = {}
---@type table<string, (Item|Node)[]>
local registeredItemsListByMod = {}
---@param shortModName string
---@param item Item|Node
function Smc.addRegisteredItem(shortModName, item)
local itemName = item.itemDef.settings.name
local modItems = registeredItemsByMod[shortModName]
if modItems == nil then
registeredItemsByMod[shortModName] = {[itemName]=item}
registeredItems[itemName] = item
return
if registeredItemsByMod[shortModName] == nil then
registeredItemsByMod[shortModName] = {}
end
if registeredItemsListByMod[shortModName] == nil then
registeredItemsListByMod[shortModName] = {}
end
registeredItems[itemName] = item
modItems[itemName] = item
registeredItemsByMod[shortModName] = item
tableInsert(registeredItemsListByMod[shortModName], item)
end
---@param shortModName string
@@ -26,6 +33,12 @@ function Smc.getRegisteredItemsByMod(shortModName)
return table.copy_with_metatables(registeredItemsByMod[shortModName])
end
---@param shortModName string
---@return table<string, Item>?
function Smc.getRegisteredItemsListByMod(shortModName)
return table.copy_with_metatables(registeredItemsListByMod[shortModName])
end
---@param itemName ItemStackString
---@return Item|Node
function Smc.getRegisteredItemByName(itemName)