init.lua 7.94 KB
Newer Older
1
2
-- minetest/creative/init.lua

3
4
creative = {}
local player_inventory = {}
5
local creative_mode = minetest.setting_getbool("creative_mode")
6
7

-- Create detached creative inventory after loading all mods
Tim's avatar
Tim committed
8
9
10
11
12
13
14
15
creative.init_creative_inventory = function(owner)
	local owner_name = owner:get_player_name()
	player_inventory[owner_name] = {
		size = 0,
		filter = "",
		start_i = 1,
		tab_id = 2,
	}
16

Tim's avatar
Tim committed
17
	minetest.create_detached_inventory("creative_" .. owner_name, {
18
		allow_move = function(inv, from_list, from_index, to_list, to_index, count, player)
19
			if creative_mode and not to_list == "main" then
20
21
22
23
24
25
26
27
28
				return count
			else
				return 0
			end
		end,
		allow_put = function(inv, listname, index, stack, player)
			return 0
		end,
		allow_take = function(inv, listname, index, stack, player)
29
			if creative_mode then
30
31
32
33
34
35
36
37
38
39
				return -1
			else
				return 0
			end
		end,
		on_move = function(inv, from_list, from_index, to_list, to_index, count, player)
		end,
		on_put = function(inv, listname, index, stack, player)
		end,
		on_take = function(inv, listname, index, stack, player)
40
41
			local player_name, stack_name = player:get_player_name(), stack:get_name()
			--print(player_name .. " takes item from creative inventory; listname = " .. listname .. ", index = " .. index .. ", stack = " .. dump(stack:to_table()))
42
			if stack then
43
44
				minetest.log("action", player_name .. " takes " .. stack_name .. " from creative inventory")
				--print("Stack name: " .. stack_name .. ", Stack count: " .. stack:get_count())
45
46
47
			end
		end,
	})
48

Tim's avatar
Tim committed
49
	creative.update_creative_inventory(owner_name)
50
	--print("creative inventory size: " .. player_inventory[player_name].size)
51
52
end

53
54
55
56
57
58
59
60
61
62
63
64
65
local function tab_category(tab_id)
	local id_category = {
		nil, -- Reserved for crafting tab.
		minetest.registered_items,
		minetest.registered_nodes,
		minetest.registered_tools,
		minetest.registered_craftitems
	}

	-- If index out of range, show default ("All") page.
	return id_category[tab_id] or id_category[2]
end

66
function creative.update_creative_inventory(player_name)
67
	local creative_list = {}
68
69
	local player_inv = minetest.get_inventory({type = "detached", name = "creative_" .. player_name})
	local inv = player_inventory[player_name]
70

71
	for name, def in pairs(tab_category(inv.tab_id)) do
72
		if not (def.groups.not_in_creative_inventory == 1) and
73
				def.description and def.description ~= "" and
74
75
				(def.name:find(inv.filter, 1, true) or
					def.description:lower():find(inv.filter, 1, true)) then
76
			creative_list[#creative_list+1] = name
77
78
		end
	end
79

80
	table.sort(creative_list)
81
82
83
	player_inv:set_size("main", #creative_list)
	player_inv:set_list("main", creative_list)
	inv.size = #creative_list
84
end
85
86
87
88
89
90

-- Create the trash field
local trash = minetest.create_detached_inventory("creative_trash", {
	-- Allow the stack to be placed and remove it in on_put()
	-- This allows the creative inventory to restore the stack
	allow_put = function(inv, listname, index, stack, player)
91
		if creative_mode then
92
93
94
95
96
			return stack:get_count()
		else
			return 0
		end
	end,
97
98
	on_put = function(inv, listname)
		inv:set_list(listname, {})
99
100
101
102
	end,
})
trash:set_size("main", 1)

103
104
creative.formspec_add = ""

105
creative.set_creative_formspec = function(player, start_i)
106
	local player_name = player:get_player_name()
107
108
109
	local inv = player_inventory[player_name]
	local pagenum = math.floor(start_i / (3*8) + 1)
	local pagemax = math.ceil(inv.size / (3*8))
110
111
112

	player:set_inventory_formspec([[
		size[8,8.6]
113
		image[4.06,3.4;0.8,0.8;creative_trash_icon.png]
114
115
116
		list[current_player;main;0,4.7;8,1;]
		list[current_player;main;0,5.85;8,3;8]
		list[detached:creative_trash;main;4,3.3;1,1;]
117
		listring[]
118
119
120
121
		tablecolumns[color;text;color;text]
		tableoptions[background=#00000000;highlight=#00000000;border=false]
		button[5.4,3.2;0.8,0.9;creative_prev;<]
		button[7.25,3.2;0.8,0.9;creative_next;>]
122
123
124
125
		button[2.1,3.4;0.8,0.5;creative_search;?]
		button[2.75,3.4;0.8,0.5;creative_clear;X]
		tooltip[creative_search;Search]
		tooltip[creative_clear;Reset]
126
127
		listring[current_player;main]
		]] ..
128
		"field[0.3,3.5;2.2,1;creative_filter;;" .. minetest.formspec_escape(inv.filter) .. ";false]" ..
129
130
131
132
133
		"listring[detached:creative_" .. player_name .. ";main]" ..
		"tabheader[0,0;creative_tabs;Crafting,All,Nodes,Tools,Items;" .. tostring(inv.tab_id) .. ";true;false]" ..
		"list[detached:creative_" .. player_name .. ";main;0,0;8,3;" .. tostring(start_i) .. "]" ..
		"table[6.05,3.35;1.15,0.5;pagenum;#FFFF00," .. tostring(pagenum) .. ",#FFFFFF,/ " .. tostring(pagemax) .. "]" ..
		default.get_hotbar_bg(0,4.7) ..
134
		default.gui_bg .. default.gui_bg_img .. default.gui_slots
135
		.. creative.formspec_add
136
137
138
	)
end

139
creative.set_crafting_formspec = function(player)
140
	player:set_inventory_formspec([[
141
142
143
144
145
146
		size[8,8.6]
		list[current_player;craft;2,0.75;3,3;]
		list[current_player;craftpreview;6,1.75;1,1;]
		list[current_player;main;0,4.7;8,1;]
		list[current_player;main;0,5.85;8,3;8]
		list[detached:creative_trash;main;0,2.75;1,1;]
147
		image[0.06,2.85;0.8,0.8;creative_trash_icon.png]
148
		image[5,1.75;1,1;gui_furnace_arrow_bg.png^[transformR270]
149
		tabheader[0,0;creative_tabs;Crafting,All,Nodes,Tools,Items;1;true;false]
150
151
152
		listring[current_player;main]
		listring[current_player;craft]
		]] ..
153
		default.get_hotbar_bg(0,4.7) ..
154
		default.gui_bg .. default.gui_bg_img .. default.gui_slots
155
	)
156
end
157

158
159
minetest.register_on_joinplayer(function(player)
	-- If in creative mode, modify player's inventory forms
160
	if not creative_mode then
161
162
		return
	end
163
	creative.init_creative_inventory(player)
164
	creative.set_creative_formspec(player, 0)
165
end)
166

167
minetest.register_on_player_receive_fields(function(player, formname, fields)
168
	if formname ~= "" or not creative_mode then
169
170
		return
	end
171

172
	local player_name = player:get_player_name()
173
	local inv = player_inventory[player_name]
174

175
	if fields.quit then
176
		if inv.tab_id == 1 then
177
			creative.set_crafting_formspec(player)
178
		end
179
180
	elseif fields.creative_tabs then
		local tab = tonumber(fields.creative_tabs)
181
		inv.tab_id = tab
182
		player_inventory[player_name].start_i = 1
183

184
		if tab == 1 then
185
			creative.set_crafting_formspec(player)
186
		else
187
188
			creative.update_creative_inventory(player_name)
			creative.set_creative_formspec(player, 0)
189
		end
190
	elseif fields.creative_clear then
191
		player_inventory[player_name].start_i = 1
192
193
194
		inv.filter = ""
		creative.update_creative_inventory(player_name)
		creative.set_creative_formspec(player, 0)
195
196
	elseif fields.creative_search or
			fields.key_enter_field == "creative_filter" then
197
		player_inventory[player_name].start_i = 1
198
199
200
		inv.filter = fields.creative_filter:lower()
		creative.update_creative_inventory(player_name)
		creative.set_creative_formspec(player, 0)
201
	else
202
		local start_i = player_inventory[player_name].start_i or 0
203

204
205
206
		if fields.creative_prev then
			start_i = start_i - 3*8
			if start_i < 0 then
207
208
209
				start_i = inv.size - (inv.size % (3*8))
				if inv.size == start_i then
					start_i = math.max(0, inv.size - (3*8))
210
				end
211
212
213
			end
		elseif fields.creative_next then
			start_i = start_i + 3*8
214
			if start_i >= inv.size then
215
216
				start_i = 0
			end
217
		end
218

219
		player_inventory[player_name].start_i = start_i
220
		creative.set_creative_formspec(player, start_i)
221
222
223
	end
end)

224
if creative_mode then
225
	local digtime = 0.5
226
227
	local caps = {times = {digtime, digtime, digtime}, uses = 0, maxlevel = 3}

228
229
230
	minetest.register_item(":", {
		type = "none",
		wield_image = "wieldhand.png",
231
		wield_scale = {x = 1, y = 1, z = 2.5},
232
		range = 10,
233
234
235
236
		tool_capabilities = {
			full_punch_interval = 0.5,
			max_drop_level = 3,
			groupcaps = {
237
238
239
240
241
				crumbly = caps,
				cracky  = caps,
				snappy  = caps,
				choppy  = caps,
				oddly_breakable_by_hand = caps,
242
243
244
245
			},
			damage_groups = {fleshy = 10},
		}
	})
246

247
248
249
	minetest.register_on_placenode(function(pos, newnode, placer, oldnode, itemstack)
		return true
	end)
250

251
252
253
254
255
256
	function minetest.handle_node_drops(pos, drops, digger)
		if not digger or not digger:is_player() then
			return
		end
		local inv = digger:get_inventory()
		if inv then
257
			for _, item in ipairs(drops) do
258
259
260
261
262
263
264
265
				item = ItemStack(item):get_name()
				if not inv:contains_item("main", item) then
					inv:add_item("main", item)
				end
			end
		end
	end
end