autostore/init.lua

133 lines
4.4 KiB
Lua
Raw Normal View History

2016-04-27 13:22:08 -07:00
minetest.register_privilege("autostore", {
description = "Place, Dig, and configure autostores.",
give_to_singleplayer = false
})
formspec_owner =
'size[8,7;]'..
'label[0,0.25;Selling What:]'..
'list[context;selling;2,0;1,1;]'..
'field[3.3,0.4;1,1;sellinga;;1]'..
'label[0,1.25;For How Much:]'..
'list[context;cost;2,1;1,1;]'..
'field[3.3,1.4;1,1;costa;;1]'..
'field[5.3,0.4;3,1;sname;store name;Unconfigured Store]'..
'field[0.3,2.5;8,1;desc;What the item is or is used for. (Optional);]'..
'button_exit[5,1;3,1;save;Save Store]'..
'list[current_player;main;0,3;8,4;]'
2016-04-27 13:22:08 -07:00
2016-04-28 18:21:30 -07:00
minetest.register_node('autostore:store_1', {
2017-09-08 18:46:20 -07:00
description = 'Unconfigured Store',
2016-05-04 09:13:17 -07:00
tiles = {'autostore_top.png', 'autostore_side.png', 'autostore_side.png', 'autostore_side.png', 'autostore_side.png', 'autostore_off.png'},
2017-09-08 18:46:20 -07:00
groups = {oddly_breakable_by_hand=3, choppy=2},
2016-04-23 08:32:58 -07:00
paramtype = 'light',
paramtype2 = 'facedir',
2016-04-28 18:21:30 -07:00
2016-04-23 08:32:58 -07:00
on_construct = function(pos)
2016-04-27 13:22:08 -07:00
local meta = minetest.get_meta(pos)
2016-04-23 08:32:58 -07:00
local inv = meta:get_inventory()
2016-04-30 05:47:11 -07:00
meta:set_string('infotext', 'Unconfigured Store')
meta:set_string('formspec', formspec_owner)
2016-04-27 13:22:08 -07:00
inv:set_size("selling", 1)
inv:set_size("cost", 1)
2016-04-30 05:47:11 -07:00
inv:set_size("input", 1)
inv:set_size("output", 1)
2016-04-27 13:22:08 -07:00
end,
2016-04-30 05:47:11 -07:00
after_place_node = function(pos, placer)
2016-04-27 13:22:08 -07:00
if minetest.check_player_privs(placer:get_player_name(), {autostore = true}) then
else
minetest.remove_node(pos)
return true
end
2016-04-23 08:32:58 -07:00
end,
2016-04-27 13:22:08 -07:00
2016-04-30 05:47:11 -07:00
can_dig = function(pos, player)
if minetest.check_player_privs(player:get_player_name(), {autostore = true}) then
return true
else
return false
end
end,
2016-04-27 13:22:08 -07:00
2016-04-30 05:47:11 -07:00
on_receive_fields = function(pos, formname, fields, sender)
local meta = minetest.get_meta(pos)
local inv = meta:get_inventory()
local out = inv:get_stack("output", 1)
local node = minetest.get_node(pos)
if fields ['save'] then
minetest.swap_node(pos,{name = 'autostore:store', param2=node.param2})
local selling_1 = inv:get_stack("selling", 1)
local cost_1 = inv:get_stack('cost', 1)
local selling_thing = selling_1:get_name()
local cost_thing = cost_1:get_name()
2016-05-04 09:13:17 -07:00
local selling_amount = tonumber(fields.sellinga)
local cost_amount = tonumber(fields.costa)
local desc = fields.desc
2016-04-30 05:47:11 -07:00
meta:set_string('infotext', fields.sname)
2016-05-04 09:13:17 -07:00
meta:set_string('sellinga', selling_amount)
meta:set_string('costa', cost_amount)
2016-04-30 05:47:11 -07:00
meta:set_string('selling', selling_thing)
meta:set_string('cost', cost_thing)
meta:set_string('formspec',
2016-04-30 05:47:11 -07:00
'size[8,6;]'..
'label[0,0.25;Selling:]'..
'item_image_button[1,0;1,1;'..selling_thing..' '..selling_amount..';blah;]'..
'label[0,1.25;For:]'..
'item_image_button[1,1;1,1;'..cost_thing..' '..cost_amount..';blah;]'..
'label[2,0.25;Pay:]'..
'list[context;input;3,0;1,1;]'..
'label[2,1.25;Take:]'..
'list[context;output;3,1;1,1;]'..
'textarea[4.3,0;4,2;;'..desc..';]'..
'button[6,1;2,1;buy;Buy Now]'..
2016-04-30 05:47:11 -07:00
'list[current_player;main;0,2;8,4;]')
2016-04-30 05:47:11 -07:00
end
2016-04-28 18:21:30 -07:00
end,
})
minetest.register_node('autostore:store', {
2017-09-08 18:46:20 -07:00
description = 'Configured Store (Something Broke)',
2016-04-30 05:47:11 -07:00
tiles = {'autostore_top.png', 'autostore_side.png', 'autostore_side.png', 'autostore_side.png', 'autostore_side.png', 'autostore_front.png'},
2017-09-08 18:46:20 -07:00
groups = {oddly_breakable_by_hand=3, choppy=2, not_in_creative_inventory=1},
2016-04-28 18:21:30 -07:00
paramtype = 'light',
paramtype2 = 'facedir',
2016-04-30 05:47:11 -07:00
drop = 'autostore:store_1',
2016-04-28 18:21:30 -07:00
2016-04-30 05:47:11 -07:00
after_place_node = function(pos, placer)
2016-04-28 18:21:30 -07:00
if minetest.check_player_privs(placer:get_player_name(), {autostore = true}) then
else
minetest.remove_node(pos)
return true
end
end,
2016-04-30 05:47:11 -07:00
can_dig = function(pos, player)
if minetest.check_player_privs(player:get_player_name(), {autostore = true}) then
return true
else
return false
end
end,
2016-04-27 13:22:08 -07:00
2016-04-30 05:47:11 -07:00
on_receive_fields = function(pos, formname, fields, sender)
local meta = minetest.get_meta(pos)
local inv = meta:get_inventory()
local out = inv:get_stack("output", 1)
local cost_thing = meta:get_string('cost')
2016-05-04 09:13:17 -07:00
local cost_amount = tonumber(meta:get_string('costa'))
2016-04-30 05:47:11 -07:00
local selling_thing = meta:get_string('selling')
2016-05-04 09:13:17 -07:00
local selling_amount = tonumber(meta:get_string('sellinga'))
local sell_output = selling_thing..' '..selling_amount
2016-04-30 05:47:11 -07:00
if fields['buy'] then
local instack = inv:get_stack("input", 1)
2016-05-04 09:13:17 -07:00
if instack:get_name() == cost_thing and instack:get_count() >= cost_amount and out:item_fits(sell_output) then
instack:take_item(cost_amount)
2016-04-30 05:47:11 -07:00
inv:set_stack("input",1,instack)
2016-05-04 09:13:17 -07:00
inv:add_item("output",sell_output)
2016-04-30 05:47:11 -07:00
end
end
end,
2016-04-23 08:32:58 -07:00
})