-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathah.lua
200 lines (176 loc) · 6.11 KB
/
ah.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
local S = minetest.get_translator(minetest.get_current_modname())
--local C = minetest.colorize
local F = minetest.formspec_escape
local modname = minetest.get_current_modname()
local pairs = pairs
local table = table
local string = string
local tostring = tostring
---------
--UTILS--
---------
--credit http://lua-users.org/wiki/FormattingNumbers
--credit http://richard.warburton.it
local function money_format(n)
local left, num, right = string.match(n, '^([^%d]*%d)(%d*)(.-)$')
return left..(num:reverse():gsub('(%d%d%d)', '%1,'):reverse())..right
end
--taken from https://stackoverflow.com/questions/25110721/format-long-number-to-shorter-version-in-lua
local function money_format_short(n)
if n >= 10^6 then
return string.format("%.0fM", n / 10^6)
elseif n >= 10^3 then
return string.format("%.0fK", n / 10^3)
else
return tostring(n)
end
end
---------
---------
mc_economy.ah = {}
mc_economy.ah.categories = {
blocs = {
desc = S("Blocks"),
items = {},
},
minerals = {
desc = S("Minerals"),
items = {},
},
plants = {
desc = S("Plants"),
items = {},
},
other = {
desc = S("Other"),
items = {},
},
}
function mc_economy.ah.add_item(category, itemname)
if not mc_economy.ah.categories[category] then
error("[mc_economy] Mod '"..modname.."' tried to add item to ah with wrong category '"..category.."'")
end
table.insert(mc_economy.ah.categories[category].items, itemname)
end
function mc_economy.ah.remove_item(category, itemname)
if not mc_economy.ah.categories[category] then
error("[mc_economy] Mod '"..modname.."' tried to remove item from ah with wrong category '"..category.."'")
end
for i,name in pairs(mc_economy.ah.categories[category].items) do
if name == itemname then
table.remove(mc_economy.ah.categories[category].items, i)
break
end
end
end
--mc_economy.ah.add_item("ttt", itemname)
mc_economy.ah.add_item("blocs", "mcl_core:stone")
mc_economy.ah.add_item("blocs", "mcl_core:cobblestone")
mc_economy.ah.add_item("blocs", "mcl_core:dirt")
mc_economy.ah.add_item("blocs", "mcl_core:dirt_with_grass")
--[[
formspec_version[4]
size[7.8,9]
style[ah_home;bgimg=blank.png;bgimg_pressed=blank.png;bgimg_middle=blank.png]
button[0,0;1.3,0.50;ah_home;Back <--]
box[5.5,1;2,0.7;#313131]
tooltip[5.5,1;2,0.7:you]
hypertext[0.4,0.3;7,1;shop;<global valign=middle halign=center size=18 color=#313131>]
box[0.4,2;6.7,5;#313131]
scrollbaroptions[min=1;max=8;smallstep=1;largestep=1;thumbsize=1]
scrollbar[7.1,2;0.3,5;vertical;scroll_bar;1]
scroll_container[0.4,2;6.7,5;scroll_bar;vertical;]
button[0,0;6.7,0.75;air;air]
button[0,0.75;6.7,0.75;air;air]
button[0,1.5;6.7,0.75;air;air]
button[0,2.25;6.7,0.75;air;air]
button[0,3;6.7,0.75;air;air]
button[0,3.75;6.7,0.75;airg;airg]
button[0,4.5;6.7,0.75;air;air]
button[0,5.25;6.7,0.75;air;air]
scroll_container_end[]
]]
local function get_money_form(amount)
return table.concat({
"box[5.5,1;2,0.7;#313131]", --tmp gray border
--"image[5.5,1;2,0.7;mc_economy_gray_background9.png;7]", --activate then 9 sliced mage are availlable
"image[5.55,1.1;0.5,0.5;mc_economy_coins.png]", --coins image
"hypertext[5.6,1.05;2,0.7;balance;<global valign=middle halign=center size=18>"..
money_format_short(amount).."]", --rounded amount
"tooltip[5.5,1;2,0.7;"..F(S("You have @1 dollars", money_format(amount))).."]", --full amount in a tooltip
})
end
local function get_categorie_formspec(playername, type)
local amount = mc_economy.get_player_balance(playername)
local form = table.concat({
"formspec_version[4]",
"size[7.8,9]",
"style[ah_home;bgimg=blank.png;bgimg_pressed=blank.png;bgimg_middle=blank.png]",
"button[0,0;1.3,0.50;ah_home;Back <--]",
get_money_form(amount),
"hypertext[0.4,0.3;7,1;shop;<global valign=middle halign=center size=18 color=#313131>"
..mc_economy.ah.categories[type].desc.."]",
"box[0.3,1.9;7.2,5.2;#313131]",
--"image[0.3,1.9;7.2,5.2;mc_economy_gray_background9.png;7]",
"container[0.4,2]", --;7,6]",
"style_type[button;font_size=18]",
"box[0,0;7,5;#313131]",
})
local nb = 0
for _,item in pairs(mc_economy.ah.categories[type].items) do
form = form.."button[0,"..nb..";7,0.75;"..item..";"..item.."]"
nb = nb + 0.75
end
form = form.."container_end[]"
return form
end
--Categories:
--Blocs
--Minerals
--Plants
--Other
--formspec_version[4]size[7.8,9]label[0.2,0.3;Close <--]box[0.4,2;7,6;]
--https://youtu.be/tQwDGtxZiHs?t=467
--mc_economy_gray_background9.png
--mc_economy_coins.png
local function get_main_formspec(playername)
local amount = mc_economy.get_player_balance(playername)
return table.concat({
"formspec_version[4]",
"size[7.8,9]",
"style[exitbutton;bgimg=blank.png;bgimg_pressed=blank.png;bgimg_middle=blank.png]",
"button_exit[0,0;1.3,0.50;exitbutton;Close <--]",
get_money_form(amount),
--[["hypertext[0.2,0.3;7,1;close_label;
<global valign=top halign=right size=16 color=#313131><action name=quit color=#313131>Close <--</action>]",]]
"hypertext[0.4,0.3;7,1;shop;<global valign=middle halign=center size=18 color=#313131>Shop]",
--"image[0.3,1.9;7.2,5.2;mc_economy_gray_background9.png;7]",
"box[0.3,1.9;7.2,5.2;#313131]",
"container[0.4,2]", --;7,6]",
"style_type[button;font_size=18]",
--"box[0,0;7,5;#313131]",
"button[0,0;7,0.75;blocs;Blocs]",
"button[0,0.75;7,0.75;minerals;Minerals]",
"button[0,1.5;7,0.75;plants;Plants]",
"button[0,2.25;7,0.75;other;Other]",
"container_end[]",
})
end
minetest.register_on_player_receive_fields(function(player, formname, fields)
if formname == "mc_economy:ah" then
local playername = player:get_player_name()
if fields.ah_home then
minetest.show_formspec(playername, "mc_economy:ah", get_main_formspec(playername))
elseif fields.blocs then
minetest.show_formspec(playername, "mc_economy:ah", get_categorie_formspec(player:get_player_name(), "blocs"))
end
end
end)
minetest.register_chatcommand("ah", {
params = "",
description = "Get your money",
func = function(name, param)
minetest.show_formspec(name, "mc_economy:ah", get_main_formspec(name))
return true
end,
})