Here's what I used for adding items.
You can use the item cache object's size and randomize it.
What findItemByName does is creates a cache, and doesn't rescan unless the cache got deleted somehow. You can find item names much easier with it, but currently I forgot that it has to be exact with some stuff, "hammer" will not work, but "Hammer" will. Can anyone fix that?
%player.addNewItem(ItemCache.item[getRandom(0, ItemCache.itemCount-1)]);
function findItemByName(%item, %val)
{
if(isObject(%item))
return %item.getName();
if(!isObject(ItemCache)) new ScriptObject(ItemCache);
if(ItemCache.itemCount <= 0 || %val) //We don't need to cause lag everytime we try to find an item
{
ItemCache.itemCount = 0;
for(%i=0;%i<DatablockGroup.getCount();%i++)
{
%obj = DatablockGroup.getObject(%i);
if(%obj.getClassName() $= "ItemData" && strLen(%obj.uiName) > 0)
{
ItemCache.item[ItemCache.itemCount] = %obj;
ItemCache.itemCount++;
}
}
}
//First let's see if we find something to be exact
if(ItemCache.itemCount > 0)
{
for(%a=0;%a<ItemCache.itemCount;%a++)
{
%objA = ItemCache.item[%a];
if(%objA.getClassName() $= "ItemData")
if(%objA.uiName $= %item)
return %objA.getName();
}
}
//Okay, we found nothing, let's see if we can find it.
if(ItemCache.itemCount > 0)
{
for(%a=0;%a<ItemCache.itemCount;%a++)
{
%objA = ItemCache.item[%a];
if(%objA.getClassName() $= "ItemData")
if(strPos(%objA.uiName, %item) == 0)
return %objA.getName();
}
}
return -1;
}
function Player::addNewItem(%player, %item)
{
%client = %player.client;
if(isObject(%item))
{
if(%item.getClassName() !$= "ItemData") return false;
%item = %item.getName();
}
else
%item = findItemByName(%item);
if(!isObject(%item)) return -1;
%item = nameToID(%item);
for(%i = 0; %i < %player.getDatablock().maxTools; %i++)
{
%tool = %player.tool[%i];
if(!isObject(%tool))
{
%player.tool[%i] = %item;
%player.weaponCount++;
messageClient(%client,'MsgItemPickup','',%i,%item);
return true;
}
}
return false;
}
//registerOutputEvent(Player,addNewItem,"string 50 50");