How to get item ID from item name.
Posted: 30 May 2008, 11:05
I need to get ID of item with some name (item not in inventory or storage, I have just name like Grape, Oridecon, Apple only). Help me please!
Code: Select all
package itemrev;
use strict;
use Plugins;
use Globals qw(%items_lut);
Plugins::register('itemrev', 'Reverse item table.', \&onUnload);
my $hooks = Plugins::addHooks(
['start3', \&ItemRev, undef]
);
my %items_rev;
sub onLoad {
return 1;
}
sub ItemRev {
foreach (keys %items_lut) {
$items_rev{$items_lut{$_}} = $_;
}
}
sub onUnload {
Plugins::delHooks($hooks);
}
1;
Code: Select all
# find the item ID if we don't know it yet
if ($args->{itemID} eq "") {
if ($args->{invIndex} && $char->inventory->get($args->{invIndex})) {
# if we have the item in our inventory, we can quickly get the nameID
$args->{itemID} = $char->inventory->get($args->{invIndex})->{nameID};
} else {
# scan the entire items.txt file (this is slow)
foreach (keys %items_lut) {
if (lc($items_lut{$_}) eq lc($config{"buyAuto_$args->{index}"})) {
$args->{itemID} = $_;
}
}
}
if ($args->{itemID} eq "") {
# the specified item doesn't even exist
# don't try this index again
$args->{index_failed}{$args->{index}} = 1;
debug "buyAuto index $args->{index} failed, item doesn't exist\n", "npc";
return;
}
}
Code: Select all
my $item = InventoryList::InventoryList->getByName("Fly Wing");
my $id = $item->{nameID};
package test;
use strict;
use Globals;
use Plugins;
use Log qw(message);
use Commands;
use InventoryList;
Plugins::register('itemid', 'show item id by its name', \&unload, \&unload);
my $command = Commands::register(
['itemid','show item id by its name',\&show]
);
sub unload {
Commands::unregister($command);
}
sub show {
my (undef, $args) = @_;
my @args = split(/ /, $args);
my $item = InventoryList::InventoryList->getByName($args[0]);
my $id = $item->{nameID};
message($args[0]." - ".$id."\n");
}
return 1;
Code: Select all
my $id = findKeyString2(\%items_lut, $args[0]);
if (ref($id) eq 'ARRAY') {
message($args[0]." - ".$_."\n") foreach (@{$id});
} else {
message($args[0]." - ".$id."\n");
}
sub findKeyString2 {
my ($r_hash, $ID) = @_;
my @ret;
foreach (sort {$a <=> $b} keys %{$r_hash}) {
push(@ret, $_) if ($ID eq $r_hash->{$_});
}
return ($#ret <= 0) ? $ret[0] : \@ret;
}