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..
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(345, 0, 64)->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!
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.
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() functionpublic 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.
[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
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(345, 0, 64)->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
Oh, sorry! I forgot a comma N bracket, here's the updated code: PHP: $player->getInventory()->setItem(1, Item::get(345, 0, 64))->setCustomName("Seeker");
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
The error is in the code you gave him. This is the corrected one PHP: $player->getInventory()->setItem(1, Item::get(345, 0, 64)->setCustomName("Seeker"));
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.
@AlexSima please mark this as solved, for further information check this out: https://github.com/pmmp/PocketMine-MP/blob/stable/src/pocketmine/item/Item.php.
I will test it real quick to see if its working, then i will mark it as solved. Edit: Its working, thanks.