Page 1 of 1

How to get item ID from item name.

Posted: 30 May 2008, 11:05
by Kissa2k
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!

Re: How to get item ID from item name.

Posted: 30 May 2008, 12:08
by sli
Not sure if it works, but this should help you out. Creates a new variable that allows you to access item names by their ID. It simply reverses the %items_lut hash.

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;

Re: How to get item ID from item name.

Posted: 30 May 2008, 12:36
by Kissa2k
I wonder why there is no function like GetItemIdByName($itemname) :D :D

Re: How to get item ID from item name.

Posted: 30 May 2008, 17:07
by sli
The only reference I could find for items.txt (outside functions.pl) was this:

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;
				}
			}
Which leads me to believe there's no function for it, or whoever wrote this didn't know if there was a function, either.

Re: How to get item ID from item name.

Posted: 30 May 2008, 20:07
by Bibian

Code: Select all

my $item = InventoryList::InventoryList->getByName("Fly Wing");
my $id = $item->{nameID};
easy, read the item.pm in actor::item for more info

Re: How to get item ID from item name.

Posted: 31 May 2008, 07:30
by Kissa2k
Bibian, I just write a test plugin and it does not work!
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;

Re: How to get item ID from item name.

Posted: 31 May 2008, 08:12
by UltimaWeapon
Try this.

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;
}

Re: How to get item ID from item name.

Posted: 31 May 2008, 08:47
by Kissa2k
UltimaWeapon, thx.