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

Solved How do I get a item that is in a random slot

Discussion in 'Plugin Help' started by Diamondkorben, Aug 17, 2020.

  1. Diamondkorben

    Diamondkorben Spider

    Messages:
    11
    GitHub:
    diamondkorben
    I am trying to make a plugin that has a feature that when you swap 2 items in the inventory it takes one of each then gives you a new item.

    I have the new item part taken care of just not the finding the item part
     
  2. JustJ0rd4n

    JustJ0rd4n Creeper

    Messages:
    3
    GitHub:
    justj0rd4n
    Try this
    PHP:
    $inventory $player->getInventory();
    $num rand(0,35);
    $slot $num;

    while(
    $inventory->isSlotEmpty($slot)) {
         
    $slot rand(0,35);
    }

    $item $inventory->get($slot);
     
    Last edited: Aug 17, 2020
  3. GodWeedZao

    GodWeedZao Zombie Pigman

    Messages:
    401
    GitHub:
    godweedzao
    check if $player->getInventory()->getHotbarSlot(rand(1, 30)); is emptyslot, so some thing (im not in ide, mybe i have typo in code :p
     
  4. Diamondkorben

    Diamondkorben Spider

    Messages:
    11
    GitHub:
    diamondkorben
    @JustJ0rd4n pocketmine/inventory/PlayerInventory::get() is a undefined method
     
  5. GodWeedZao

    GodWeedZao Zombie Pigman

    Messages:
    401
    GitHub:
    godweedzao
    do this, its working
     
  6. Diamondkorben

    Diamondkorben Spider

    Messages:
    11
    GitHub:
    diamondkorben
  7. GodWeedZao

    GodWeedZao Zombie Pigman

    Messages:
    401
    GitHub:
    godweedzao
    inventory->get???!!!!!! what is that?
     
    Last edited: Aug 18, 2020
  8. Diamondkorben

    Diamondkorben Spider

    Messages:
    11
    GitHub:
    diamondkorben
    How do I get it to remove a itemas I'm trying to make a plugin that when you swap 2 items it takes one of each
     
  9. GodWeedZao

    GodWeedZao Zombie Pigman

    Messages:
    401
    GitHub:
    godweedzao
    swap 2 items? you should use config, really i dont think there isn't way to do that without using cfg, because you need to save swap numbers, and if swap num of player in 2, remove random item from player inventory.
     
  10. GodWeedZao

    GodWeedZao Zombie Pigman

    Messages:
    401
    GitHub:
    godweedzao
    i think you should save players name like array and set in to players name in cfg like:
    GodWeedZao:
    - 0
    when they join or...
    and then onTransAction(InventoryTransactionEvent)
    check if event instanceof slotchange, and if under name player in cfg !== 2, set player under name to $num++ and its working for when player using 2 item, doing stuff, and for remove items from player inventory random, you need to get all items in player inv (array) and check
    PHP:
    if (!is_null($inv)) { //this is for check if player have an item in his inventory.
    //do stuff
     
    }
    and for get random items:
    https://3v4l.org/0Diqd
    see this code.
    and for set item:
    $inv->setItem($b, Item::get(0, 0, 0));
     
  11. GodWeedZao

    GodWeedZao Zombie Pigman

    Messages:
    401
    GitHub:
    godweedzao
    this is my idea and its working for me, if you have problem - errors, tell me here, or if its bad idea (its laggy for server iknow) wait for other people posts, i cant think good rn, tomorrow i'll checl your thread again to find better anwser
     
  12. Diamondkorben

    Diamondkorben Spider

    Messages:
    11
    GitHub:
    diamondkorben
    Thanks for the help it works :D
     
  13. GodWeedZao

    GodWeedZao Zombie Pigman

    Messages:
    401
    GitHub:
    godweedzao
    no problem ;)
     
  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.