forked from rollerozxa/chest_with_everything
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinit.lua
176 lines (153 loc) · 4.94 KB
/
init.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
-- Simple formspec wrapper that does variable substitution.
local function formspec_wrapper(formspec, variables)
local retval = formspec
for k,v in pairs(variables) do
retval = retval:gsub("${"..k.."}", v)
end
return retval
end
-- Declare custom permission
minetest.register_privilege("chest_with_everything", {
description = "Can use the chest_with_everything",
give_to_singleplayer = true
})
-- Create a detached inventory
local inv_everything = minetest.create_detached_inventory("everything", {
allow_move = function(inv, from_list, from_index, to_list, to_index, count, player)
if not minetest.check_player_privs(player, 'chest_with_everything') or
to_list == "main" then
return 0
end
return count
end,
allow_put = function(inv, listname, index, stack, player)
return 0
end,
allow_take = function(inv, listname, index, stack, player)
if not minetest.check_player_privs(player, 'chest_with_everything') then
return 0
end
return -1
end,
on_move = function(inv, from_list, from_index, to_list, to_index, count, player2)
end,
on_take = function(inv, listname, index, stack, player2)
if stack and stack:get_count() > 0 then
end
end,
})
local inv_trash = minetest.create_detached_inventory("trash", {
allow_take = function(inv, listname, index, stack, player)
return 0
end,
allow_move = function(inv, from_list, from_index, to_list, to_index, count, player)
return 0
end,
on_put = function(inv, listname, index, stack, player)
inv:set_list("main", {})
end,
})
inv_trash:set_size("main", 1)
local max_page = 1
local items_per_page = 60
local show_nici = minetest.settings:get("cwe_show_nici") or false
local function get_chest_formspec(page)
local start = 0 + ( page - 1 ) * items_per_page
return formspec_wrapper([[
formspec_version[4]
size[15.7,10.5]
list[detached:everything;main;0.5,0.3;12,5;${start}]
button[0.5,6.4;1,1;cwe_prev;\<]
style[pagelbl;border=false]
button[1.5,6.4;4,1;pagelbl;Page: ${page} / ${max_page}]
button[5.52,6.4;1,1;cwe_next;\>]
style[trashlbl;border=false]
button[12.75,6.4;1.5,1;trashlbl;Trash:]
list[detached:trash;main;14.25,6.4;1,1]
listring[current_player;main]
list[current_player;main;1.8,8;10,2;0]
listring[detached:everything;main]
listring[current_player;main]
listring[detached:trash;main]
field[-10,-10;0,0;internal_paginator;;${page}]
]], {
start = start,
page = page,
max_page = max_page
})
end
local function sheet(id)
return "chest_with_everything.png^[sheet:2x2:"..(id % 2)..","..math.floor(id / 2)
end
minetest.register_node("chest_with_everything:chest", {
description = "Chest with Everything",
tiles = {
sheet(0), sheet(0),
sheet(1), sheet(1),
sheet(1), sheet(2)},
paramtype2 = "facedir",
groups = {dig_immediate=2,choppy=3},
on_rightclick = function(pos, node, clicker)
local player_name = clicker:get_player_name()
if not minetest.check_player_privs(clicker, 'chest_with_everything') and false then
minetest.chat_send_player(player_name, minetest.colorize("#ff0000", "Hey, no touching!"))
minetest.log("action", player_name.." tried to access a Chest with Everything")
return
end
minetest.show_formspec(clicker:get_player_name(), "chest_with_everything:chest", get_chest_formspec(1))
end
})
minetest.register_on_player_receive_fields(function(player, formname, fields)
if formname ~= "chest_with_everything:chest" then return end
if fields.cwe_prev == nil and fields.cwe_next == nil then return end
local page = fields.internal_paginator
if fields.cwe_prev then page = page - 1
elseif fields.cwe_next then page = page + 1 end
-- Wrap-around
if page < 1 then page = max_page end
if page > max_page then page = 1 end
minetest.show_formspec(player:get_player_name(), "chest_with_everything:chest", get_chest_formspec(page))
end)
minetest.register_on_mods_loaded(function()
local items = {}
for itemstring, def in pairs(minetest.registered_items) do
if def.groups.not_in_creative_inventory ~= 1 or show_nici then
table.insert(items, itemstring)
end
end
--[[ Sort items in this order:
* Chest with Everything
* Test tools
* Other tools
* Craftitems
* Other items ]]
local function compare(item1, item2)
local def1 = minetest.registered_items[item1]
local def2 = minetest.registered_items[item2]
local tool1 = def1.type == "tool"
local tool2 = def2.type == "tool"
local craftitem1 = def1.type == "craft"
local craftitem2 = def2.type == "craft"
if item1 == "chest_with_everything:chest" then
return true
elseif item2 == "chest_with_everything:chest" then
return false
elseif tool1 and not tool2 then
return true
elseif not tool1 and tool2 then
return false
elseif craftitem1 and not craftitem2 then
return true
elseif not craftitem1 and craftitem2 then
return false
else
return item1 < item2
end
end
table.sort(items, compare)
inv_everything:set_size("main", #items)
max_page = math.ceil(#items / items_per_page)
for i=1, #items do
inv_everything:add_item("main", items[i])
end
end)