How to get item ID from item name.

Forum closed. All further discussion to be discussed at https://github.com/OpenKore/

Moderator: Moderators

Message
Author
Kissa2k
Human
Human
Posts: 46
Joined: 27 Apr 2008, 12:52
Noob?: No
Location: Russia

How to get item ID from item name.

#1 Post 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!

sli
Perl Monk
Perl Monk
Posts: 810
Joined: 04 Apr 2008, 17:26
Noob?: No

Re: How to get item ID from item name.

#2 Post 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;
cs : ee : realist

Kissa2k
Human
Human
Posts: 46
Joined: 27 Apr 2008, 12:52
Noob?: No
Location: Russia

Re: How to get item ID from item name.

#3 Post by Kissa2k »

I wonder why there is no function like GetItemIdByName($itemname) :D :D

sli
Perl Monk
Perl Monk
Posts: 810
Joined: 04 Apr 2008, 17:26
Noob?: No

Re: How to get item ID from item name.

#4 Post 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.
cs : ee : realist

Bibian
Perl Monk
Perl Monk
Posts: 416
Joined: 04 Apr 2008, 03:08

Re: How to get item ID from item name.

#5 Post 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

Kissa2k
Human
Human
Posts: 46
Joined: 27 Apr 2008, 12:52
Noob?: No
Location: Russia

Re: How to get item ID from item name.

#6 Post 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;

UltimaWeapon
Human
Human
Posts: 37
Joined: 04 Apr 2008, 22:55
Noob?: Yes
Location: Thailand
Contact:

Re: How to get item ID from item name.

#7 Post 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;
}
I may make you misunderstand. Because my English isn't good enough. So Sorry.
Image

Kissa2k
Human
Human
Posts: 46
Joined: 27 Apr 2008, 12:52
Noob?: No
Location: Russia

Re: How to get item ID from item name.

#8 Post by Kissa2k »

UltimaWeapon, thx.

Locked