Author Topic: Making a servercmd that adds an item to a players tool slot  (Read 1132 times)

I've been trying to make a servercmd that adds an item to a clients players inventory (tool slot).
I've tried looking at Event_addItem, nothing but failed attempts.

What I mean is something like this:
Code: [Select]
function serverCmdBuy(%client,%image)
{
     if(%image $= "Gun")
     {
          //weapon is added to players inventory code here
     }
}

But it reads if there is an empty tool slot like addItem.

EDIT: NVM.  I got that massively wrong.

Put this function in your code:
Code: [Select]
function GameConnection::forceEquip(%this,%slot,%item)
{
%player = %this.player;
if(!isObject(%player))
return;

if(!isObject(%item))
%item = 0;

%oldTool = %player.tool[%slot];
%player.tool[%slot] = %item;

messageClient(%this,'MsgItemPickup','',%slot,%item);

if(!isObject(%oldTool))
%player.weaponCount ++;
}

Then do this to add the item:
Code: [Select]
%client.forceEquip(0,gunImage.getID());

Put this function in your code:
Code: [Select]
function GameConnection::forceEquip(%this,%slot,%item)
{
%player = %this.player;
if(!isObject(%player))
return;

if(!isObject(%item))
%item = 0;

%oldTool = %player.tool[%slot];
%player.tool[%slot] = %item;

messageClient(%this,'MsgItemPickup','',%slot,%item);

if(!isObject(%oldTool))
%player.weaponCount ++;
}

Then do this to add the item:
Code: [Select]
%client.forceEquip(0,gunImage.getID());
Thanks.

*EDIT: Doesn't work, it doesn't add the item but replaces the first slot with nothing and a question mark in the slot.
« Last Edit: September 25, 2012, 05:58:39 PM by tkepahama »

Try this
Code: [Select]
function serverCmdBuy(%client,%image)
{
     if (%image $= "Gun")
     {
      buyThing(%client, GunItem);
     }
}

function buyThing(%client, %thing)
  {
   %slot = -1;
   %item = %thing.getID();
   for (%x=0; %x <= 4; %x++)
     {
      if (%client.player.tool[%x]==0)
        %slot = %x;

      if (%client.player.tool[%x] == %item)
        {
         MessageClient(%client, '', "You already have a " @ %thing.uiName);
         return;
        }
     }
   if (%slot == -1)
     {
      MessageClient(%client, '', "Inventory full");
      return;
     }
   %client.player.tool[%slot] = %item;
   MessageClient(%client, 'MsgItemPickup', '', %slot, %item);
  }
(code ripped from my mining server and tweaked a little)

you can use "buyThing" to add any item to your player inventory if you know the "Item" datablock name.

Try this
Code: [Select]
function serverCmdBuy(%client,%image)
{
     if (%image $= "Gun")
     {
      buyThing(%client, GunItem);
     }
}

function buyThing(%client, %thing)
  {
   %slot = -1;
   %item = %thing.getID();
   for (%x=0; %x <= 4; %x++)
     {
      if (%client.player.tool[%x]==0)
        %slot = %x;

      if (%client.player.tool[%x] == %item)
        {
         MessageClient(%client, '', "You already have a " @ %thing.uiName);
         return;
        }
     }
   if (%slot == -1)
     {
      MessageClient(%client, '', "Inventory full");
      return;
     }
   %client.player.tool[%slot] = %item;
   MessageClient(%client, 'MsgItemPickup', '', %slot, %item);
  }
(code ripped from my mining server and tweaked a little)

you can use "buyThing" to add any item to your player inventory if you know the "Item" datablock name.

Thanks, this helped a lot.

Try this
Code: [Select]
function serverCmdBuy(%client,%image)
{
     if (%image $= "Gun")
     {
      buyThing(%client, GunItem);
     }
}

function buyThing(%client, %thing)
  {
   %slot = -1;
   %item = %thing.getID();
   for (%x=0; %x <= 4; %x++)
     {
      if (%client.player.tool[%x]==0)
        %slot = %x;

      if (%client.player.tool[%x] == %item)
        {
         MessageClient(%client, '', "You already have a " @ %thing.uiName);
         return;
        }
     }
   if (%slot == -1)
     {
      MessageClient(%client, '', "Inventory full");
      return;
     }
   %client.player.tool[%slot] = %item;
   MessageClient(%client, 'MsgItemPickup', '', %slot, %item);
  }
(code ripped from my mining server and tweaked a little)

you can use "buyThing" to add any item to your player inventory if you know the "Item" datablock name.
You didn't take into account if the player has a larger than normal inventory.

Try this
God your indenting is just all over the place

*EDIT: Doesn't work, it doesn't add the item but replaces the first slot with nothing and a question mark in the slot.
It sounds like an issue of adding an inexistant object to the inventory:
%client.forceEquip(0,gunImage.getID());
I could have sworn the tools/weapon system used items for the inventory instead of images, try changing this to
%client.forceEquip(0,gunItem.getID());

God your indenting is just all over the place
It sounds like an issue of adding an inexistant object to the inventory:
%client.forceEquip(0,gunImage.getID());
I could have sworn the tools/weapon system used items for the inventory instead of images, try changing this to
%client.forceEquip(0,gunItem.getID());
Well I tried it, it works but it replaces the first tool slot with the weapon no matter if there is already a weapon in the slot or not.

And if there's another weapon slot that is empty, it still only adds to the first slot replacing any item that is already in that slot.

For the first problem, I think you can just add
if(%player.tool[%slot]
    return;

In with the other ifs


For the second problem, this is because the code Greek gave you just shoves it in the slot you give the function in the first argument.
If you want it to go in the first available slot, you'd need to check for an available slot, like this:
Code: [Select]
%max = %player.getDatablock().maxTools;
for(%i=0;%i<%max;%i++)
{
if(!%player.slot[%i])
{
%player.client.forceEquip(%i,GunItem.getId());
break;
}
}