Add select_item support
parent
379a4e5911
commit
49e529a96e
|
@ -2,7 +2,7 @@
|
|||
Version: 0.4.3
|
||||
|
||||
Adds vending and depositing machines which allow to buy and sell items from
|
||||
other players.
|
||||
other players, using a currency item.
|
||||
|
||||
## Requirements
|
||||
Runs natively in Minetest Game.
|
||||
|
@ -13,6 +13,11 @@ May also run in other games if they have the `default` mod and locked chest
|
|||
Locked chests from other mods are not supported, but mods can choose
|
||||
to add support for Easyvend on their own (see developer information below).
|
||||
|
||||
You can optionally add the `select_item` mod. This adds a button to select
|
||||
an item from a list of items.
|
||||
This feature is very useful for depositing machines because you can select
|
||||
any item, not just those you have already in your inventory.
|
||||
|
||||
## How to use
|
||||
Help is also included as help entry for Item Documentation [`doc_items`].
|
||||
|
||||
|
@ -27,7 +32,7 @@ up, the machine is operational. You can stack these locked chests for
|
|||
extended storage.
|
||||
|
||||
### Currency item
|
||||
The currency is fixed during a game; all machines use the same currency.
|
||||
The currency of all machines is gold ingots by default.
|
||||
But it can be changed via the setting `easyvend_currency`.
|
||||
|
||||
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
default?
|
||||
screwdriver?
|
||||
select_item?
|
||||
doc?
|
||||
doc_items?
|
||||
awards?
|
||||
|
|
59
easyvend.lua
59
easyvend.lua
|
@ -13,12 +13,46 @@ local maxcost = cost_stack_max * slots_max
|
|||
|
||||
local joketimer_start = 3
|
||||
|
||||
local active_item_selection = {}
|
||||
|
||||
-- Allow for other mods to register custom chests
|
||||
easyvend.register_chest = function(node_name, inv_list, meta_owner)
|
||||
registered_chests[node_name] = { inv_list = inv_list, meta_owner = meta_owner }
|
||||
traversable_node_types[node_name] = true
|
||||
end
|
||||
|
||||
if minetest.get_modpath("select_item") then
|
||||
-- When player selects item via "select item" dialog, switch the
|
||||
-- machine's selected item and update the formspec.
|
||||
select_item.register_on_select_item(function(playername, itemstring)
|
||||
local player = minetest.get_player_by_name(playername)
|
||||
if not player then
|
||||
return
|
||||
end
|
||||
local pos = active_item_selection[playername]
|
||||
if pos then
|
||||
local node = minetest.get_node(pos)
|
||||
if not easyvend.is_machine(node.name) then
|
||||
return
|
||||
end
|
||||
local meta = minetest.get_meta(pos)
|
||||
local owner = meta:get_string("owner")
|
||||
if playername == owner then
|
||||
local inv = meta:get_inventory()
|
||||
local stack = ItemStack(itemstring)
|
||||
if stack == nil then
|
||||
inv:set_stack( "item", 1, nil )
|
||||
else
|
||||
inv:set_stack( "item", 1, stack)
|
||||
meta:set_string("itemname", itemstring)
|
||||
easyvend.set_formspec(pos, player)
|
||||
end
|
||||
end
|
||||
end
|
||||
active_item_selection[playername] = nil
|
||||
end)
|
||||
end
|
||||
|
||||
-- Partly a wrapper around contains_item, but does special treatment if the item
|
||||
-- is a tool. Basically checks whether the items exist in the supplied inventory
|
||||
-- list. If check_wear is true, only counts items without wear.
|
||||
|
@ -76,6 +110,10 @@ easyvend.buysell = function(nodename)
|
|||
return buysell
|
||||
end
|
||||
|
||||
easyvend.is_machine = function(nodename)
|
||||
return ( nodename == "easyvend:depositor_on" or nodename == "easyvend:vendor_on" or nodename == "easyvend:depositor" or nodename == "easyvend:vendor" )
|
||||
end
|
||||
|
||||
easyvend.is_active = function(nodename)
|
||||
if ( nodename == "easyvend:depositor_on" or nodename == "easyvend:vendor_on" ) then
|
||||
return true
|
||||
|
@ -153,6 +191,9 @@ easyvend.set_formspec = function(pos, player)
|
|||
.."tooltip[cost;"..itemcounttooltip.."]"
|
||||
.."button[6,2.8;2,0.5;save;Confirm]"
|
||||
.."tooltip[save;Confirm configuration and activate machine (only for owner)]"
|
||||
if minetest.get_modpath("select_item") then
|
||||
formspec = formspec .. "button[0,2.8;2,0.5;select_item;Select item]"
|
||||
end
|
||||
local weartext, weartooltip
|
||||
if buysell == "buy" then
|
||||
weartext = "Buy worn tools"
|
||||
|
@ -948,7 +989,7 @@ easyvend.on_receive_fields = function(pos, formname, fields, sender)
|
|||
end
|
||||
end
|
||||
elseif fields.config or fields.save or fields.usermode then
|
||||
if sender:get_player_name() == owner then
|
||||
if sendername == owner then
|
||||
easyvend.on_receive_fields_config(pos, formname, fields, sender)
|
||||
else
|
||||
meta:set_string("message", "Only the owner may change the configuration.")
|
||||
|
@ -956,6 +997,18 @@ easyvend.on_receive_fields = function(pos, formname, fields, sender)
|
|||
easyvend.set_formspec(pos, sender)
|
||||
return
|
||||
end
|
||||
elseif fields.select_item then
|
||||
if minetest.get_modpath("select_item") then
|
||||
if sendername == owner then
|
||||
active_item_selection[sendername] = pos
|
||||
select_item.show_dialog(sendername, select_item.filters.creative)
|
||||
else
|
||||
meta:set_string("message", "Only the owner may change the configuration.")
|
||||
easyvend.sound_error(sendername)
|
||||
easyvend.set_formspec(pos, sender)
|
||||
return
|
||||
end
|
||||
end
|
||||
elseif fields.wear ~= nil then
|
||||
if sender:get_player_name() == owner then
|
||||
if fields.wear == "true" then
|
||||
|
@ -1179,7 +1232,7 @@ easyvend.find_chest = function(owner, pos, dy, itemname, check_wear, amount, rem
|
|||
else
|
||||
return nil, internal
|
||||
end
|
||||
elseif (node.name ~= "easyvend:vendor" and node.name~="easyvend:depositor" and node.name~="easyvend:vendor_on" and node.name~="easyvend:depositor_on") then
|
||||
elseif not easyvend.is_machine(node.name) then
|
||||
return nil, internal
|
||||
end
|
||||
|
||||
|
@ -1194,7 +1247,7 @@ easyvend.allow_metadata_inventory_put = function(pos, listname, index, stack, pl
|
|||
local name = player:get_player_name()
|
||||
if name == owner then
|
||||
local inv = meta:get_inventory()
|
||||
if stack==nil then
|
||||
if stack == nil then
|
||||
inv:set_stack( "item", 1, nil )
|
||||
else
|
||||
inv:set_stack( "item", 1, stack:get_name() )
|
||||
|
|
Loading…
Reference in New Issue