• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!
  • New resources must be posted under Resources tab. A discussion thread will be created automatically, you can't open threads manually anymore.

NPC [Module] Advanced BuyContainer

Mokerhamer

Retired Global Mod
Senator
Joined
Aug 6, 2007
Messages
1,767
Reaction score
36
Oke for the people who really needed it, here's my ADVANCED BuyContainer i could have make it with cap+slot check but i'm simply to lazy:thumbup:

Lets start! follow this exactly!

Minor Bug fix: Thank you tala for fixing it
Cap check was made by slawkens

Modules.lua

After:
Code:
SHOPMODULE_BUY_ITEM 	= 2

Add:
Code:
SHOPMODULE_BUY_CONTAINER  = 3

Before:
Code:
	function ShopModule:addBuyableItem(names, itemid, cost, charges, realname)
		for i, name in pairs(names) do
			local parameters = {
					itemid = itemid,
					cost = cost,
					eventType = SHOPMODULE_BUY_ITEM,
					module = self
				}
			if(realname ~= nil) then
				parameters.realname = realname
			end
			if(isItemRune(itemid) == TRUE or isItemFluidContainer(itemid) == TRUE) then
				parameters.charges = charges
			end
			keywords = {}
			table.insert(keywords, name)
			local node = self.npcHandler.keywordHandler:addKeyword(keywords, ShopModule.tradeItem, parameters)
			node:addChildKeywordNode(self.yesNode)
			node:addChildKeywordNode(self.noNode)
		end
	end

Add:
Code:
function ShopModule:buyContainer(names, container, itemid, cost, charges, realname)
		for i, name in pairs(names) do
			local parameters = {
			        container = container,
					itemid = itemid,
					cost = cost,
					charges = charges,
					eventType = SHOPMODULE_BUY_CONTAINER,
					module = self
				}
			if(realname ~= nil) then
				parameters.realname = realname
			end
			if(isItemRune(itemid) == TRUE or isItemFluidContainer(itemid) == TRUE) then
				parameters.charges = charges
			end
			keywords = {}
			table.insert(keywords, name)
			local node = self.npcHandler.keywordHandler:addKeyword(keywords, ShopModule.tradeItem, parameters)
			node:addChildKeywordNode(self.yesNode)
			node:addChildKeywordNode(self.noNode)
		end
	end

After:
Code:
if(parameters.eventType == SHOPMODULE_SELL_ITEM) then
			tmpName = node:getKeywords()[2]

Add
Code:
		elseif(parameters.eventType == SHOPMODULE_BUY_CONTAINER) then
			tmpName = node:getKeywords()[3]

After
Code:
if(parameters.eventType == SHOPMODULE_SELL_ITEM) then
			local msg = module.npcHandler:getMessage(MESSAGE_SELL)
			msg = module.npcHandler:parseMessage(msg, parseInfo)
			selfSay(msg)

Add
Code:
		elseif(parameters.eventType == SHOPMODULE_BUY_CONTAINER) then
			local msg = module.npcHandler:getMessage(MESSAGE_BUY)
			msg = module.npcHandler:parseMessage(msg, parseInfo)
			selfSay(msg)
Code:
After
Code:
		if(parentParameters.eventType == SHOPMODULE_SELL_ITEM) then
			local ret = doPlayerSellItem(cid, parentParameters.itemid, module.amount, parentParameters.cost*module.amount)
			if(ret == LUA_NO_ERROR) then
				local msg = module.npcHandler:getMessage(MESSAGE_ONSELL)
				msg = module.npcHandler:parseMessage(msg, parseInfo)
				selfSay(msg)
			else
				local msg = module.npcHandler:getMessage(MESSAGE_NOTHAVEITEM)
				msg = module.npcHandler:parseMessage(msg, parseInfo)
				selfSay(msg)
			end
Add
Code:
elseif(parentParameters.eventType == SHOPMODULE_BUY_CONTAINER) then
			local ret = doPlayerbuyContainer(cid, parentParameters.container, parentParameters.itemid, parentParameters.charges, parentParameters.cost*module.amount, module.amount)
			if(ret == LUA_NO_ERROR) then
				local msg = module.npcHandler:getMessage(MESSAGE_ONBUY)
				msg = module.npcHandler:parseMessage(msg, parseInfo)
				selfSay(msg)
			elseif(ret == LUA_ERROR) then
				local msg = module.npcHandler:getMessage(MESSAGE_NEEDMOREMONEY)
				msg = module.npcHandler:parseMessage(msg, parseInfo)
				selfSay(msg)
			else
				local msg = module.npcHandler:getMessage(MESSAGE_NEEDMORESPACE)
				msg = module.npcHandler:parseMessage(msg, parseInfo)
				selfSay(msg, cid)
			end

Global.lua

Before
Code:
function doPlayerBuyItem(cid, itemid, count, cost, charges)
	if(doPlayerRemoveMoney(cid, cost) == TRUE) then
		return doPlayerGiveItem(cid, itemid, count, charges)
	end
	return LUA_ERROR
end

Add
Code:
function doPlayerBuyItemContainer(cid, container, itemid, charges, cost, count)
	if getPlayerFreeCap(cid) > (getItemWeightById(itemid, 20)*count + getItemWeightById(container, count)) then
		if doPlayerRemoveMoney(cid, cost) == TRUE then
			for i = 1, count do
				local containerItem = doPlayerAddItem(cid, container, 1)
				for x = 1, 20 do
					doAddContainerItem(containerItem, itemid, charges)
				end
			end
			return LUA_NO_ERROR
		end
	else
		return
	end
	return LUA_ERROR
end


And u are done!!! How to use this new npc functions? like this =)

dont forget to edit the ids of shopModule:buyContainer({'bp sd'}, Backpack, Rune, Prise, Charges, 'Backpack of sudden death') in runes.lua (line 26) or it will bug.

Runes.lua
Code:
-- OTServ event handling functions start
function onCreatureAppear(cid)				npcHandler:onCreatureAppear(cid) end
function onCreatureDisappear(cid) 			npcHandler:onCreatureDisappear(cid) end
function onCreatureSay(cid, type, msg) 	npcHandler:onCreatureSay(cid, type, msg) end
function onThink() 						npcHandler:onThink() end
-- OTServ event handling functions end

local shopModule = ShopModule:new()
npcHandler:addModule(shopModule)

shopModule:addBuyableItem({'light wand', 'lightwand'}, 		2163, 500, 		'magic light wand')
shopModule:addBuyableItem({'heavy magic missile'}, 	2311, 125, 	10,	'heavy magic missile rune')
shopModule:addBuyableItem({'great fireball'}, 			2304, 180, 	4, 	'great fireball rune')
shopModule:addBuyableItem({'explosion'}, 					2313, 250, 	6, 	'explosion rune')
shopModule:addBuyableItem({'ultimate healing'}, 		2273, 175, 	2, 	'ultimate healing rune')
shopModule:addBuyableItem({'sudden death'}, 			2268, 325, 	2, 	'sudden death rune')
shopModule:addBuyableItem({'blank'}, 					2260, 10, 		'blank rune')


shopModule:buyContainer({'bp sd'}, 	             Backpack, Rune, Price, Charges,  	'Backpack of sudden death')
shopModule:buyContainer({'bp uh'}, 	             2003, 2273, 1, 1,  	'Backpack of ultimate healing')
shopModule:buyContainer({'bp ih'}, 	             2003, 2265, 300, 1,  	'Backpack of intense healing')
npcHandler:setCallback(CALLBACK_MESSAGE_DEFAULT, creatureSayCallback)
npcHandler:addModule(FocusModule:new())

The only way u can really thank me is giving me rep+!, SO i know for what i am doing it for:blink:
 
Last edited:
Heh nice, i have small bug i write to him "bp of manafluid" and he tell me "You don't have enought money." he gives me bp of manafluid. Sorry for my English.
 
Heh nice, i have small bug i write to him "bp of manafluid" and he tell me "You don't have enought money." he gives me bp of manafluid. Sorry for my English.


ONly with mana fluid??? gone fix that bug as soon as posible then =)
 
Try this:
Code:
function doPlayerBuyContainer(cid, container, itemid, charges, cost, count)
	if doPlayerRemoveMoney(cid, cost) == TRUE then
		for i = 1, count do
			local backpack = doPlayerAddItem(cid, container, 1)
			for x = 1, 20 do
				doAddContainerItem(backpack, itemid, charges)
				return LUA_NO_ERROR
			end
		end
	end
	return LUA_ERROR
end
 
Try this:
Code:
function doPlayerBuyContainer(cid, container, itemid, charges, cost, count)
	if doPlayerRemoveMoney(cid, cost) == TRUE then
		for i = 1, count do
			local backpack = doPlayerAddItem(cid, container, 1)
			for x = 1, 20 do
				doAddContainerItem(backpack, itemid, charges)
				return LUA_NO_ERROR
			end
		end
	end
	return LUA_ERROR
end

Gone test it now =)
 
After this function:
Code:
function ShopModule:addBuyableItem(names, itemid, cost, charges, realname)

Add
Code:
function ShopModule:buyContainer(names, container, itemid, cost, charges, realname)
		for i, name in pairs(names) do
			local parameters = {
			        container = container,
					itemid = itemid,
					cost = cost,
					charges = charges,
					eventType = SHOPMODULE_BUY_CONTAINER,
					module = self
				}
			if(realname ~= nil) then
				parameters.realname = realname
			end
			if(isItemRune(itemid) == TRUE or isItemFluidContainer(itemid) == TRUE) then
				parameters.charges = charges
			end
			keywords = {}
			table.insert(keywords, name)
			local node = self.npcHandler.keywordHandler:addKeyword(keywords, ShopModule.tradeItem, parameters)
			node:addChildKeywordNode(self.yesNode)
			node:addChildKeywordNode(self.noNode)
		end
	end
Do you have to make i like:
Code:
function ShopModule:addBuyableItem(names, itemid, cost, charges, realname) function ShopModule:buyContainer(names, container, itemid, cost, charges, realname)
		for i, name in pairs(names) do
			local parameters = {
			        container = container,
					itemid = itemid,
					cost = cost,
					charges = charges,
					eventType = SHOPMODULE_BUY_CONTAINER,
					module = self
				}
			if(realname ~= nil) then
				parameters.realname = realname
			end
			if(isItemRune(itemid) == TRUE or isItemFluidContainer(itemid) == TRUE) then
				parameters.charges = charges
			end
			keywords = {}
			table.insert(keywords, name)
			local node = self.npcHandler.keywordHandler:addKeyword(keywords, ShopModule.tradeItem, parameters)
			node:addChildKeywordNode(self.yesNode)
			node:addChildKeywordNode(self.noNode)
		end
	end
Looks abit strange 2times same function :P.
 
After this function:
Code:
function ShopModule:addBuyableItem(names, itemid, cost, charges, realname)

Add
Code:
function ShopModule:buyContainer(names, container, itemid, cost, charges, realname)
		for i, name in pairs(names) do
			local parameters = {
			        container = container,
					itemid = itemid,
					cost = cost,
					charges = charges,
					eventType = SHOPMODULE_BUY_CONTAINER,
					module = self
				}
			if(realname ~= nil) then
				parameters.realname = realname
			end
			if(isItemRune(itemid) == TRUE or isItemFluidContainer(itemid) == TRUE) then
				parameters.charges = charges
			end
			keywords = {}
			table.insert(keywords, name)
			local node = self.npcHandler.keywordHandler:addKeyword(keywords, ShopModule.tradeItem, parameters)
			node:addChildKeywordNode(self.yesNode)
			node:addChildKeywordNode(self.noNode)
		end
	end
Do you have to make i like:
Code:
function ShopModule:addBuyableItem(names, itemid, cost, charges, realname) function ShopModule:buyContainer(names, container, itemid, cost, charges, realname)
		for i, name in pairs(names) do
			local parameters = {
			        container = container,
					itemid = itemid,
					cost = cost,
					charges = charges,
					eventType = SHOPMODULE_BUY_CONTAINER,
					module = self
				}
			if(realname ~= nil) then
				parameters.realname = realname
			end
			if(isItemRune(itemid) == TRUE or isItemFluidContainer(itemid) == TRUE) then
				parameters.charges = charges
			end
			keywords = {}
			table.insert(keywords, name)
			local node = self.npcHandler.keywordHandler:addKeyword(keywords, ShopModule.tradeItem, parameters)
			node:addChildKeywordNode(self.yesNode)
			node:addChildKeywordNode(self.noNode)
		end
	end
Looks abit strange 2times same function :P.

Know but you need to do it or it wont work..
 
Try this:
Code:
function doPlayerBuyContainer(cid, container, itemid, charges, cost, count)
	if doPlayerRemoveMoney(cid, cost) == TRUE then
		for i = 1, count do
			local backpack = doPlayerAddItem(cid, container, 1)
			for x = 1, 20 do
				doAddContainerItem(backpack, itemid, charges)
				return LUA_NO_ERROR
			end
		end
	end
	return LUA_ERROR
end

getting this error


Code:
Lua Script Error: [Npc interface] 
data/npc/scripts/runes.lua:onCreatureSay

data/npc/lib/npcsystem/modules.lua:774: attempt to call global 'doPlayerbuyContainer' (a nil value)

when i changed
Code:
function doPlayer[COLOR="Red"]b[/COLOR]uyContainer(cid, container, itemid, charges, cost, count)
i only get 1 bp with 1 rune lol..
 
Last edited:
Try this then:
Code:
function doPlayerBuyContainer(cid, container, itemid, charges, cost, count)
	if doPlayerRemoveMoney(cid, cost) == TRUE then
		for i = 1, count do
			local backpack = doPlayerAddItem(cid, container, 1)
			for x = 1, 20 do
				doAddContainerItem(backpack, itemid, charges)
			end
		end
		return LUA_NO_ERROR
	end
	return LUA_ERROR
end
 
Try this then:
Code:
function doPlayerBuyContainer(cid, container, itemid, charges, cost, count)
	if doPlayerRemoveMoney(cid, cost) == TRUE then
		for i = 1, count do
			local backpack = doPlayerAddItem(cid, container, 1)
			for x = 1, 20 do
				doAddContainerItem(backpack, itemid, charges)
			end
		end
		return LUA_NO_ERROR
	end
	return LUA_ERROR
end


Thank you but it's not needed anymore =)

ANd it works to lol +)
 
Last edited:
Ehh, why you removed this? Pleeease back, i understand why you removed it, becouse of spam on MSN? You can't ingnore it? :(

Someone have copied id already? Please send me this, if someone have. ;)
 
Thank you very much. ;)

But can you make that it will check that player have required cap?

Becouse on my OTS i have enabled cap, and what if someone buy and it will be on floor? :(
 
Thank you very much. ;)

But can you make that it will check that player have required cap?

Becouse on my OTS i have enabled cap, and what if someone buy and it will be on floor? :(

I'm actually to lazy o make cap check but i could add it later if u want
 
Back
Top