From 103e772d81f87df438805f32b732aad600013a5b Mon Sep 17 00:00:00 2001 From: cale Date: Tue, 10 Jan 2017 18:51:40 +0100 Subject: [PATCH] allow players to trade with npcs --- mods/npcs/init.lua | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/mods/npcs/init.lua b/mods/npcs/init.lua index 75c46eb..78a65c6 100644 --- a/mods/npcs/init.lua +++ b/mods/npcs/init.lua @@ -38,7 +38,6 @@ function npcs.register_npc(name, def) for i, q in ipairs(my_quests) do - print(q.title) if not(quests.has_quest(player:get_player_name(), q.title)) then d:add_option(q.title, function(name) quests.add_quest(name, q) @@ -46,6 +45,26 @@ function npcs.register_npc(name, def) end end + d:show(player:get_player_name()) + end + elseif def.npc_type == "shop" then + def.on_rightclick = function(pos, node, player, itemstack, pt) + local d = dialogue.new(def.npc_text) + + + for i, item in ipairs(def.npc_items) do + d:add_option(item.text, function(name) + if player then + local inv = player:get_inventory() + + if inv:contains_item("main", item.input) and inv:room_for_item("main", item.output) then + inv:remove_item("main", item.input) + inv:add_item("main", item.output) + end + end + end) + end + d:show(player:get_player_name()) end end @@ -55,7 +74,7 @@ end --TEST npcs.register_npc("npcs:farmer", { - npc_type = "quests", + npc_type = "shop", npc_text = "Hi!", npc_quest_title = "Test", npc_get_quest = function(pos, player) @@ -80,4 +99,11 @@ npcs.register_npc("npcs:farmer", { return my_quests end, + npc_items = { + { + input = "default:stone_item", + output = "default:pick", + text = "Stone -> Pick", + } + }, })