1. The forums will be archived and moved to a read only mode in about 2 weeks (mid march).

Remove items from player inventory

Discussion in 'Development' started by iCirgio, Feb 21, 2018.

  1. iCirgio

    iCirgio Slime

    Messages:
    92
    GitHub:
    lolnova
    I'm trying to add item piece by piece but everything from victims inventory is added. How do i make it so that item is added one by one and not all at once
    PHP:
     $fighter $ev->getDamager();
     
    $victim $ev->getEntity();


    foreach(
    $victim->getInventory()->getContents() as $items){

       
    $fighter->getInventory()->addItem($items);
     
  2. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    Piece by piece may be a very expensive operation. If you could tell what you're trying to do, that will help.
    PHP:
    while(!$item->isNull()){
        
    $inventory->addItem($item->pop());
    }
     
  3. iCirgio

    iCirgio Slime

    Messages:
    92
    GitHub:
    lolnova
    I'm trying to make it so when I hit player some of their items come to my inventory and it removes it from the player inventory
     
  4. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    I assume you are trying to add all items from the victim's inventory to the fighter's inventory and throw the items on the ground if the fighter's inventory gets full in the process. Correct?
    (P.S. reload the page, I added a spoiler to the first reply).
     
  5. iCirgio

    iCirgio Slime

    Messages:
    92
    GitHub:
    lolnova
    I don't want to add all of the items thats the thing I want to take it slot by slot if possible or randomly
     
  6. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    the foreach($inventory->getContents() as $item) method takes it slot by slot.
     
  7. iCirgio

    iCirgio Slime

    Messages:
    92
    GitHub:
    lolnova
    For me it adds all the items and not slot by slot
     
  8. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    Yes because you are not calling anything inside the for-loop or breaking it in any way?

    For randomization you can do something like this:
    PHP:
    $contents $inventory->getContents();
    $slots array_rand($contents$numberOfItems);
    foreach(
    $slots as $slot){
        
    $fighter->getInventory()->addItem($contents[$slot]);
        
    $victim->getInventory()->clear($slot);
    }
     
  9. iCirgio

    iCirgio Slime

    Messages:
    92
    GitHub:
    lolnova
    What will $numberOfItems be?
     
  10. MalakasPlayzMCPE

    MalakasPlayzMCPE Zombie Pigman

    Messages:
    667
    PHP:
           $item Item::get(001);
           
    $inv->setItem(0$item); //sets the item of the first slot in the item bar to air (it is like removing it)
     
  11. DaPigGuy

    DaPigGuy Slime

    Messages:
    86
    GitHub:
    DaPigGuy
    @Muqsit I think the OP wants the items to disappear from the victim's inventory and appear in the attacker's inventory one by one.
     
  12. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    The max number of items to randomly choose
     
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.