summaryrefslogtreecommitdiff
path: root/unified_inventory/bags.lua
blob: 7e925db974eb7b42c0d5cc085b47ff739733f18b (plain)
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
-- Bags for Minetest

-- Copyright (c) 2012 cornernote, Brett O'Donnell <cornernote@gmail.com>
-- License: GPLv3

local S = unified_inventory.gettext
local F = unified_inventory.fgettext

unified_inventory.register_page("bags", {
	get_formspec = function(player)
		local player_name = player:get_player_name()
		local formspec = "background[0.06,0.99;7.92,7.52;ui_bags_main_form.png]"
		formspec = formspec.."label[0,0;"..F("Bags").."]"
		formspec = formspec.."button[0,2;2,0.5;bag1;"..F("Bag 1").."]"
		formspec = formspec.."button[2,2;2,0.5;bag2;"..F("Bag 2").."]"
		formspec = formspec.."button[4,2;2,0.5;bag3;"..F("Bag 3").."]"
		formspec = formspec.."button[6,2;2,0.5;bag4;"..F("Bag 4").."]"
		formspec = formspec.."listcolors[#00000000;#00000000]"
		formspec = formspec.."list[detached:"..minetest.formspec_escape(player_name).."_bags;bag1;0.5,1;1,1;]"
		formspec = formspec.."list[detached:"..minetest.formspec_escape(player_name).."_bags;bag2;2.5,1;1,1;]"
		formspec = formspec.."list[detached:"..minetest.formspec_escape(player_name).."_bags;bag3;4.5,1;1,1;]"
		formspec = formspec.."list[detached:"..minetest.formspec_escape(player_name).."_bags;bag4;6.5,1;1,1;]"
		return {formspec=formspec}
	end,
})

unified_inventory.register_button("bags", {
	type = "image",
	image = "ui_bags_icon.png",
	tooltip = S("Bags"),
	hide_lite=true
})


	unified_inventory.register_page("bag1", {
		get_formspec = function(player)
			local stack = player:get_inventory():get_stack("bag1", 1)
			local image = stack:get_definition().inventory_image
			local formspec = "image[7,0;1,1;"..image.."]"
			formspec = formspec.."label[0,0;"..F("Bag 1").."]"
			formspec = formspec.."listcolors[#00000000;#00000000]"
			formspec = formspec.."list[current_player;bag1contents;0,1;8,3;]"
			formspec = formspec.."listring[current_name;bag1contents]"
			formspec = formspec.."listring[current_player;main]"
			local slots = stack:get_definition().groups.bagslots
			if slots == 8 then
				formspec = formspec.."background[0.06,0.99;7.92,7.52;ui_bags_sm_form.png]"
			elseif slots == 16 then
				formspec = formspec.."background[0.06,0.99;7.92,7.52;ui_bags_med_form.png]"
			elseif slots == 24 then
				formspec = formspec.."background[0.06,0.99;7.92,7.52;ui_bags_lg_form.png]"
			end
			return {formspec=formspec}
		end,
	})
	unified_inventory.register_page("bag2", {
		get_formspec = function(player)
			local stack = player:get_inventory():get_stack("bag2", 1)
			local image = stack:get_definition().inventory_image
			local formspec = "image[7,0;1,1;"..image.."]"
			formspec = formspec.."label[0,0;"..F("Bag 2").."]"
			formspec = formspec.."listcolors[#00000000;#00000000]"
			formspec = formspec.."list[current_player;bag2contents;0,1;8,3;]"
			formspec = formspec.."listring[current_name;bag2contents]"
			formspec = formspec.."listring[current_player;main]"
			local slots = stack:get_definition().groups.bagslots
			if slots == 8 then
				formspec = formspec.."background[0.06,0.99;7.92,7.52;ui_bags_sm_form.png]"
			elseif slots == 16 then
				formspec = formspec.."background[0.06,0.99;7.92,7.52;ui_bags_med_form.png]"
			elseif slots == 24 then
				formspec = formspec.."background[0.06,0.99;7.92,7.52;ui_bags_lg_form.png]"
			end
			return {formspec=formspec}
		end,
	})
	unified_inventory.register_page("bag3", {
		get_formspec = function(player)
			local stack = player:get_inventory():get_stack("bag3", 1)
			local image = stack:get_definition().inventory_image
			local formspec = "image[7,0;1,1;"..image.."]"
			formspec = formspec.."label[0,0;"..F("Bag 3").."]"
			formspec = formspec.."listcolors[#00000000;#00000000]"
			formspec = formspec.."list[current_player;bag3contents;0,1;8,3;]"
			formspec = formspec.."listring[current_name;bag3contents]"
			formspec = formspec.."listring[current_player;main]"
			local slots = stack:get_definition().groups.bagslots
			if slots == 8 then
				formspec = formspec.."background[0.06,0.99;7.92,7.52;ui_bags_sm_form.png]"
			elseif slots == 16 then
				formspec = formspec.."background[0.06,0.99;7.92,7.52;ui_bags_med_form.png]"
			elseif slots == 24 then
				formspec = formspec.."background[0.06,0.99;7.92,7.52;ui_bags_lg_form.png]"
			end
			return {formspec=formspec}
		end,
	})
	unified_inventory.register_page("bag4", {
		get_formspec = function(player)
			local stack = player:get_inventory():get_stack("bag4", 1)
			local image = stack:get_definition().inventory_image
			local formspec = "image[7,0;1,1;"..image.."]"
			formspec = formspec.."label[0,0;"..F("Bag 4").."]"
			formspec = formspec.."listcolors[#00000000;#00000000]"
			formspec = formspec.."list[current_player;bag4contents;0,1;8,3;]"
			formspec = formspec.."listring[current_name;bag4contents]"
			formspec = formspec.."listring[current_player;main]"
			local slots = stack:get_definition().groups.bagslots
			if slots == 8 then
				formspec = formspec.."background[0.06,0.99;7.92,7.52;ui_bags_sm_form.png]"
			elseif slots == 16 then
				formspec = formspec.."background[0.06,0.99;7.92,7.52;ui_bags_med_form.png]"
			elseif slots == 24 then
				formspec = formspec.."background[0.06,0.99;7.92,7.52;ui_bags_lg_form.png]"
			end
			return {formspec=formspec}
		end,
	})

minetest.register_on_player_receive_fields(function(player, formname, fields)
	if formname ~= "" then
		return
	end
	for i = 1, 4 do
		if fields["bag"..i] then
			local stack = player:get_inventory():get_stack("bag"..i, 1)
			if not stack:get_definition().groups.bagslots then
				return
			end
			unified_inventory.set_inventory_formspec(player, "bag"..i)
			return
		end
	end
end)

minetest.register_on_joinplayer(function(player)
	local player_inv = player:get_inventory()
	local bags_inv = minetest.create_detached_inventory(player:get_player_name().."_bags",{
		on_put = function(inv, listname, index, stack, player)
			player:get_inventory():set_stack(listname, index, stack)
			player:get_inventory():set_size(listname.."contents",
					stack:get_definition().groups.bagslots)
		end,
		on_take = function(inv, listname, index, stack, player)
			player:get_inventory():set_stack(listname, index, nil)
		end,
		allow_put = function(inv, listname, index, stack, player)
			local new_slots = stack:get_definition().groups.bagslots
			if new_slots then
				local player_inv = player:get_inventory()
				local old_slots = player_inv:get_size(listname.."contents")

				if new_slots >= old_slots then
					return 1
				else
					-- using a smaller bag, make sure it fits
					local old_list = player_inv:get_list(listname.."contents")
					local new_list = {}
					local slots_used = 0
					local use_new_list = false

					for i, v in ipairs(old_list) do
						if v and not v:is_empty() then
							slots_used = slots_used + 1
							use_new_list = i > new_slots
							new_list[slots_used] = v
						end
					end
					if new_slots >= slots_used then
						if use_new_list then
							player_inv:set_list(listname.."contents", new_list)
						end
						return 1
					end
				end
			end
			return 0
		end,
		allow_take = function(inv, listname, index, stack, player)
			if player:get_inventory():is_empty(listname.."contents") then
				return stack:get_count()
			else
				return 0
			end
		end,
		allow_move = function(inv, from_list, from_index, to_list, to_index, count, player)
			return 0
		end,
	})
	for i=1,4 do
		local bag = "bag"..i
		player_inv:set_size(bag, 1)
		bags_inv:set_size(bag, 1)
		bags_inv:set_stack(bag, 1, player_inv:get_stack(bag, 1))
	end
end)

-- register bag tools
minetest.register_tool("unified_inventory:bag_small", {
	description = S("Small Bag"),
	inventory_image = "bags_small.png",
	groups = {bagslots=8},
})

minetest.register_tool("unified_inventory:bag_medium", {
	description = S("Medium Bag"),
	inventory_image = "bags_medium.png",
	groups = {bagslots=16},
})

minetest.register_tool("unified_inventory:bag_large", {
	description = S("Large Bag"),
	inventory_image = "bags_large.png",
	groups = {bagslots=24},
})

-- register bag crafts
if minetest.get_modpath("farming") ~= nil then
	minetest.register_craft({
		output = "unified_inventory:bag_small",
		recipe = {
			{"",           "farming:cotton", ""},
			{"group:wool", "group:wool",     "group:wool"},
			{"group:wool", "group:wool",     "group:wool"},
		},
	})

	minetest.register_craft({
		output = "unified_inventory:bag_medium",
		recipe = {
			{"",               "",                            ""},
			{"farming:cotton", "unified_inventory:bag_small", "farming:cotton"},
			{"farming:cotton", "unified_inventory:bag_small", "farming:cotton"},
		},
	})

	minetest.register_craft({
		output = "unified_inventory:bag_large",
		recipe = {
			{"",               "",                             ""},
			{"farming:cotton", "unified_inventory:bag_medium", "farming:cotton"},
			{"farming:cotton", "unified_inventory:bag_medium", "farming:cotton"},
	    },
	})
end