Skip to content

Commit

Permalink
create way to fix an order
Browse files Browse the repository at this point in the history
  • Loading branch information
fluxionary committed Aug 31, 2022
1 parent 8953062 commit 1586b3b
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 42 deletions.
3 changes: 2 additions & 1 deletion init.lua
Expand Up @@ -26,7 +26,8 @@ name_monoid.dofile("monoid")

minetest.register_on_joinplayer(function(player)
if name_monoid.settings.show_name then
name_monoid.monoid:add_change(player, {text = player:get_player_name()}, "name_monoid")
name_monoid.monoid:add_change(player, {order = 0, text = player:get_player_name()}, "name_monoid")

else
name_monoid.monoid:add_change(player, {text = "", text_separator = ""}, "name_monoid")
end
Expand Down
162 changes: 122 additions & 40 deletions monoid.lua
@@ -1,12 +1,5 @@
local function remove_empty(t)
local t2 = {}
for _, v in pairs(t) do
if v then
table.insert(t2, v)
end
end
return t2
end
local component_separator = name_monoid.settings.component_separator
local invert_composition = name_monoid.settings.invert_composition

name_monoid.monoid_def = {
identity = {
Expand All @@ -19,42 +12,83 @@ name_monoid.monoid_def = {
return {
hide_all = true,
}
else
if name_monoid.settings.invert_composition then
local bgcolor
if nametag_attributes1.bgcolor == nil then
bgcolor = nametag_attributes2.bgcolor
else
bgcolor = nametag_attributes1.bgcolor
end

return {
text = table.concat(
remove_empty({nametag_attributes2.text, nametag_attributes1.text}),
nametag_attributes2.text_separator or name_monoid.settings.text_separator
),
color = nametag_attributes1.color or nametag_attributes2.color,
bgcolor = bgcolor,
}
end

local bgcolor, color, texts

if invert_composition then
if nametag_attributes1.bgcolor == nil then
bgcolor = nametag_attributes2.bgcolor
else
local bgcolor
if nametag_attributes2.bgcolor == nil then
bgcolor = nametag_attributes1.bgcolor
else
bgcolor = nametag_attributes2.bgcolor
end
bgcolor = nametag_attributes1.bgcolor
end

return {
text = table.concat(
remove_empty({nametag_attributes1.text, nametag_attributes2.text}),
nametag_attributes2.text_separator or name_monoid.settings.text_separator
),
color = nametag_attributes2.color or nametag_attributes1.color,
bgcolor = bgcolor,
}
color = nametag_attributes1.color or nametag_attributes2.color

else
if nametag_attributes2.bgcolor == nil then
bgcolor = nametag_attributes1.bgcolor
else
bgcolor = nametag_attributes2.bgcolor
end

color = nametag_attributes2.color or nametag_attributes1.color
end

if nametag_attributes2._texts then
texts = nametag_attributes2._texts

if nametag_attributes1._texts then
table.insert_all(texts, nametag_attributes1._texts)

elseif nametag_attributes1.text then
table.insert(texts, {
text = nametag_attributes1.text,
text_separator = nametag_attributes1.text_separator,
order = nametag_attributes1.order
})
end

elseif nametag_attributes1._texts then
texts = nametag_attributes1._texts

if nametag_attributes2._texts then
table.insert_all(texts, nametag_attributes2._texts)

elseif nametag_attributes2.text then
table.insert(texts, {
text = nametag_attributes2.text,
text_separator = nametag_attributes2.text_separator,
order = nametag_attributes2.order
})
end

else
texts = {}

if nametag_attributes1.text then
table.insert(texts, {
text = nametag_attributes1.text,
text_separator = nametag_attributes1.text_separator,
order = nametag_attributes1.order
})
end

if nametag_attributes2.text then
table.insert(texts, {
text = nametag_attributes2.text,
text_separator = nametag_attributes2.text_separator,
order = nametag_attributes2.order
})
end
end

return {
bgcolor = bgcolor,
color = color,
_texts = texts
}
end,

fold = function(values)
Expand All @@ -74,8 +108,56 @@ name_monoid.monoid_def = {
})

else
local text
if nametag_attributes._texts then
local texts = nametag_attributes._texts
if invert_composition then
table.sort(texts, function(a, b)
if a.order and b.order then
return b.order < a.order

elseif b.order then
return true

elseif a.order then
return false
end

return false
end)

else
table.sort(texts, function(a, b)
if a.order and b.order then
return a.order < b.order

elseif a.order then
return true

elseif b.order then
return false
end

return true
end)
end

local parts = {}
for i = 1, #texts do
table.insert(parts, texts[i].text)
if i < #texts then
table.insert(parts, texts[i].text_separator or component_separator)
end
end

text = table.concat(parts, "")

else
text = nametag_attributes.text
end

player:set_nametag_attributes({
text = nametag_attributes.text,
text = text,
color = nametag_attributes.color,
bgcolor = nametag_attributes.bgcolor or false,
})
Expand Down
2 changes: 1 addition & 1 deletion settings.lua
@@ -1,7 +1,7 @@
local s = minetest.settings

name_monoid.settings = {
tag_separator = s:get("name_monoid.tag_separator") or " ",
tag_separator = s:get("name_monoid.component_separator") or " ",
show_name = s:get_bool("name_monoid.show_name", true),
invert_composition = s:get_bool("name_monoid.invert_composition", false),
}

0 comments on commit 1586b3b

Please sign in to comment.