question about : hook vending_item_sold

All resolved question will be found here. It is recommended that you browse / search through this section first and see if your question has been answered before

Moderators: Moderators, Documentation Writers

Message
Author
exvee
Noob
Noob
Posts: 4
Joined: 31 May 2017, 01:12
Noob?: No

question about : hook vending_item_sold

#1 Post by exvee »

hi, i'm trying to make plugin that notify me if my vending item sold, so far i found hook/function in openkore that suit my needs

https://github.com/OpenKore/openkore/bl ... erType0.pm

Code: Select all

# Your shop has sold an item -- one packet sent per item sold.
#
sub shop_sold {
	my ($self, $args) = @_;

	# sold something
	my $number = $args->{number};
	my $amount = $args->{amount};

	$articles[$number]{sold} += $amount;
	my $earned = $amount * $articles[$number]{price};
	$shopEarned += $earned;
	$articles[$number]{quantity} -= $amount;
	my $msg = TF("sold: %s - %s %sz\n", $amount, $articles[$number]{name}, $earned);
	shopLog($msg);
	message($msg, "sold");

	# Call hook before we possibly remove $articles[$number] or
	# $articles itself as a result of the sale.
	Plugins::callHook(
		'vending_item_sold',
		{
			#These first two entries are equivalent to $args' contents.
			'vendShopIndex' => $number,
			'amount' => $amount,
			'vendArticle' => $articles[$number], #This is a hash
		}
	);

	# Adjust the shop's articles for sale, and notify if the sold
	# item and/or the whole shop has been sold out.
	if ($articles[$number]{quantity} < 1) {
		message TF("sold out: %s\n", $articles[$number]{name}), "sold";
		#$articles[$number] = "";
		if (!--$articles){
			message T("Items have been sold out.\n"), "sold";
			closeShop();
		}
	}
}##end shop_sold()
this is what i write so far, mostly based on sli's koreGrowl.
package tmNotify;

use Plugins;
use Globals;

Plugins::register('tmNotify','vending notification using terminal-notifier', \&onUnload);
my $hooks = Plugins::addHooks(
['start3', \&onLoad, undef],
['packet_privMsg', \&onPrivMsg, undef],
['map_loaded', \&onMapLoaded, undef],
['vending_item_sold', \&onItemSold, undef],
['packet_vender_store', \&onItemSold, undef]
);
my $cmd = Commands::register(
["tmnotify", "test notification", \&cmdNotify],
);

my $dead = 0;

sub cmdNotify {
my @args = @_;
print qx(terminal-notifier -title 'cmdNotify' -message 'tmNotify is working !');
}

sub onLoad {
print qx(terminal-notifier -title 'onLoad' -message 'Notify is loaded !');
}

sub onPrivMsg {
my @args = @_;
# growlMessage("From $args[1]{'privMsgUser'} : $args[1]{privMsg}", 1);
$user = $args[1]{'privMsgUser'};
$msg = $args[1]{'privMsg'};
print qx(terminal-notifier -title 'onPrivMsg' -message '$user : $msg');
}

sub onItemSold {
my $number = $args->{number};
my $amount = $args->{amount};

$articles[$number]{sold} += $amount;
my $earned = $amount * $articles[$number]{price};
$shopEarned += $earned;
$articles[$number]{quantity} -= $amount;

my $msg = $args->{RAW_MSG};

print qx(terminal-notifier -title 'onItemSold' -message '$number - $amount - $msg');
}

sub onMapLoaded {
# Prevents multiple notifications on death.
$dead = 0;
}

sub onUnload {
Plugins::delHooks($hooks);
undef $ucname;
}

1;
but i can only get values from $args->{amount}, other $args return nothing.
how to get other $args value so i can get item name ?
thanks

Niodan
Plain Yogurt
Plain Yogurt
Posts: 83
Joined: 03 Apr 2017, 00:19
Noob?: No

Re: question about : hook vending_item_sold

#2 Post by Niodan »

You are able to write This function by using macro.

If the item sold
Then does this or this to notify you.

exvee
Noob
Noob
Posts: 4
Joined: 31 May 2017, 01:12
Noob?: No

Re: question about : hook vending_item_sold

#3 Post by exvee »

Code: Select all

automacro soldout {
	hook vending_item_sold
	save number
	save amount
	save vendArticle
	call {
		log vendShopIndex $.hooksave0 amount $.hooksave1 vendArticle $.hooksave2
	}
}

Code: Select all

[b][Jun 6 09:25:23 2017.73] sold: 2 - Flower 4z[/b]
[Jun 6 09:25:23 2017.36] [macro] $args->{number} does not exist
[Jun 6 09:25:23 2017.36] [macro] $.hooksave2 is of type HASH. Take care!
[Jun 6 09:25:23 2017.36] [macro] automacro soldout triggered.
[Jun 6 09:25:23 2017.36] [macro log] vendShopIndex  amount 2 vendArticle HASH(0x7fb09f081420)
still got the same question even in macros, how to get those $args->{number} value ?how can i print like sold: 2 - Flower 4z
in macro log ?

User avatar
SkylorD
Moderators
Moderators
Posts: 1167
Joined: 16 Dec 2011, 02:53
Noob?: No
Location: Brazil
Contact:

Re: question about : hook vending_item_sold

#4 Post by SkylorD »

Your solution :
======================================================================================

Code: Select all

'vendShopIndex' => $number,
'amount' => $amount,
'vendArticle' => $articles[$number], #This is a hash

Code: Select all

$args->{vendShopIndex} to get the $number
$args->{amount} to get $amount
$args->{vendArticle} to get the article.
$.hooksave0 = vendShopIndex = $number
$.hooksave1 = amount = $amount
$.hooksave2 = vendArticle = $articles[$number] (is only missing the {name} hash access)

Using :

Code: Select all

save vendArticle
save amount
save vendShopIndex
Since you can't get $articles at a unusual way.

To get $articles, i believe you should use something such as :

Instead of use $.hooksave2 straight (#this is hash), try :

$realArticles = @eval ($.hooksave2[$.hooksave0]{name}) or
$realArticles = @eval (return $.hooksave2[$.hooksave0]{name}) or
$realArticles = @eval (return $.hooksave0{name}) or
$realArticles = @eval ($.hooksave0{name})

'cause i believe that using :
$realArticles = $articles[$number]

won't work. :/

======================================================================================
https://github.com/Slipxxliose/-unknown ... hopSold.pl

Code: Select all

use strict;
use warnings;
use Plugins;
use Log qw (warning message debug error);
#-----------------
# Plugin: settings
#-----------------
Plugins::register("shopSold", "shopSold", \&on_unload, \&on_reload);
# Log hook
my $logHook = Log::addHook(\&on_Log, "shopsold");
#---------------
# Plugin: on_unload
#---------------
sub on_unload {
Log::delHook($logHook);
}
sub on_reload {
&on_unload;
}
#-------------
# Log: handler
#-------------
sub on_Log {
my ($type, $domain, $level, $globalVerbosity, $message, $user_data) = @_;
	if ( $type eq "message" ){
		if ($message =~ /(sold|vendido): (\d+) - (.*) (\d+)z/ig) {
		Utils::Win32::playSound("C:\\coin.wav");
		}		
	}
}

It's my github post.
Well, i've done this code too for beginners!

How to use timeout :

Code: Select all

https://github.com/Slipxxliose/-unknown_plugins-openkore/blob/master/tips/st_timeout.pl
from kgRelogger.

How to get console messages :

Code: Select all

https://github.com/Slipxxliose/-unknown_plugins-openkore/blob/master/tips/koreLogConsole.pl
The same thing i've used to make the itemSold plugin !
Learn rules

Locked