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

Solved setitem

Discussion in 'Plugin Help' started by AlexSima, Jun 28, 2020.

  1. AlexSima

    AlexSima Spider Jockey

    Messages:
    38
    GitHub:
    AlexSima003
    I need a code for my plugin that will give an specific item when someone join in hub, and a way to control where they will be in inventory.
    Thank you.
    Also if i connect twice, i dont want to give me the item twice..
     
    Last edited: Jun 28, 2020
  2. Seeker

    Seeker Spider Jockey

    Messages:
    45
    GitHub:
    seeker-devs
    If you use set Item, even if you join twice the item can be replaced. Make sure to import the following:
    PHP:
    use pocketmine\Player;
    use 
    pocketmine\inventory\PlayerInventory;
    use 
    pocketmine\item\Item//I suggest using item ids too, like this:
    use pocketmine\item\ItemIds;
    use 
    pocketmine\event\player\PlayerJoinEvent;
    And then when the player joins:
    PHP:
    public function playerJoin(PlayerJoinEvent $pjevar){
        
    //your code
        
    $player $pjevar->getPlayer();
        
    //example: $player->getInventory()->setItem(Slot, Item::get(ItemIDorName, metadata, amount)->setCustomName("Item"); actual code example:
        
    $player->getInventory()->setItem(1 Item::get(345064)->setCustomName("Seeker");
    (i typed this on phone so sorry for any mistakes)
    Relevant files:
    Item.php (src/pocketmine/item), PlayerJoinEvent.php for advanced understanding for methods like this (src/pocketmine/event/player), Player.php which is the best for your knowledge (src/pocketmine) and for more inventory related stuff, check out src/pocketmine/inventory on their GH repo!
     
  3. AlexSima

    AlexSima Spider Jockey

    Messages:
    38
    GitHub:
    AlexSima003
    Thank you so much, i tried most of the codes that i could find on internet but they didnt work.
    (PlayerJoinEvent $pjevar) Also, can you tell me why its $pjevar and not $e ? And what they mean? I started to learn dev. plugins so i just wanna know
    Right now im learning how to make a menu for /info but i will let you know if it worked in an hour.
     
  4. Seeker

    Seeker Spider Jockey

    Messages:
    45
    GitHub:
    seeker-devs
    It's okay, I'm here to help! Let me explain it to you.
    Here we declare the function parameter PlayerJoinEvent as a $variable. This way, we can import all the functions in PlayerJoinEvent.php and use them as
    PHP:
    $variable->useThisFunction($params_if_needed);
    For your understanding, let's say src/Seeker/PMMP/Test.php has a public (it needs to be public) function getSeeker() which outputs 'Hello Seeker'.
    We can use it in Main.php like:
    PHP:
    use Seeker\PMMP\Test;
    //code
    //our function should be public too to use the getSeeker() function
    public function testSeeker(Test $test){
        
    $bruh $test->getSeeker();
        
    var_dump($bruh);
        echo(
    $bruh);
    }
    I'm sorry if I made any mistakes. I recommend using CodeCademy or SoloLearn (it's mostly for web development but the OOP part can be useful) to learn PHP and OOP.
     
    AlexSima likes this.
  5. AlexSima

    AlexSima Spider Jockey

    Messages:
    38
    GitHub:
    AlexSima003
    [15:47:08] [Server thread/CRITICAL]: ParseError: "syntax error, unexpected 'Item' (T_STRING), expecting ')'" (EXCEPTION) in "plugins/JoinItems/src/Alexander/JoinItems/Main" at line 58
     
  6. AlexSima

    AlexSima Spider Jockey

    Messages:
    38
    GitHub:
    AlexSima003
    PHP:
    public function playerJoin(PlayerJoinEvent $pjevar){
        
    //your code
        
    $player $pjevar->getPlayer();
        
    //example: $player->getInventory()->setItem(Slot, Item::get(ItemIDorName, metadata, amount)->setCustomName("Item"); actual code example:
        
    $player->getInventory()->setItem(1 Item::get(345064)->setCustomName("Seeker");
    setItem -> I changed (1 to (1)
    [15:54:10] [Server thread/CRITICAL]: ParseError: "syntax error, unexpected 'Item' (T_STRING)" (EXCEPTION) in "plugins/JoinItems/src/Alexander/JoinItems/Main" at line 58
     
    Last edited: Jun 28, 2020
  7. Seeker

    Seeker Spider Jockey

    Messages:
    45
    GitHub:
    seeker-devs
    Oh, sorry! I forgot a comma N bracket, here's the updated code:
    PHP:
    $player->getInventory()->setItem(1Item::get(345064))->setCustomName("Seeker");
     
    AlexSima likes this.
  8. AlexSima

    AlexSima Spider Jockey

    Messages:
    38
    GitHub:
    AlexSima003
    It worked but when i try to join the game i got internal server error..

    [17:00:13] [Server thread/CRITICAL]: Error: "Call to a member function setCustomName() on bool" (EXCEPTION) in "plugins/JoinItems/src/Alexander/JoinItems/Main" at line 58
     
  9. wolfdale

    wolfdale Zombie Pigman

    Messages:
    535
    GitHub:
    diamond-gold
    The error is in the code you gave him.
    This is the corrected one
    PHP:
    $player->getInventory()->setItem(1Item::get(345064)->setCustomName("Seeker"));
     
  10. Seeker

    Seeker Spider Jockey

    Messages:
    45
    GitHub:
    seeker-devs
    Guess I forgot to do it. Sorry, my pc wasn't available and I was just scrolling through the forums. I'm making a LobbyCore plugin after I'm done with SW for my server soon! I hope it can help you.
     
  11. Seeker

    Seeker Spider Jockey

    Messages:
    45
    GitHub:
    seeker-devs
  12. AlexSima

    AlexSima Spider Jockey

    Messages:
    38
    GitHub:
    AlexSima003
    Last edited: Jun 29, 2020
    Seeker likes this.
  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.