PHP: <?phpnamespace MSelector;use pocketmine\event\Listener;use pocketmine\event\PlayerInteractEvent;use pocketmine\Player;use pocketmine\Server;use pocketmine\plugin\PluginBase;use pocketmine\utils\TextFormat as c;class Main extends PluginBase implements Listener{ public function onEnable() { $this->getServer()->getPluginManager()->registerEvents($this,$this); } public function onInteract(PlayerInteractEvent $event) { //Interacter $player = $event->getPlayer(); $item = $player->getInventory()->getItemInHand(); if($item->getId() == Item::get(1, 0, 1)){ $player->getInventory()->addItem(Item::get(1, 0, 1)); $player->getInventory()->addItem(Item::get(35, 0, 1)); $player->getInventory()->addItem(Item::get(35, 13, 1)); $player->getInventory()->addItem(Item::get(35, 3, 1)); $player->getInventory()->addItem(Item::get(35, 10, 1)); } }} Can someone help me fix this function? I cant get it to work. Thanks I used this type of functjon on a soup plugin I made and the item would interact, now it doesnt wanna interact this way any help thanks!
Use the PlayerItemHeldEvent. Also, you don’t really need to add an amount in your if's Item::get() (argument 3). This will give the player the items if he/she holds Stone.
Also how can I add the Item in a specific slot? When you onJoin how can I give the item in a specific slot?
Yes, Item::get(1, 0) is correct, see: https://github.com/pmmp/PocketMine-MP/blob/master/src/pocketmine/item/Item.php#L99 If you want you could also remove the meta argument.
PHP: $item->getId() === Item::get(1, 0, 1) is equivalent to PHP: integer === Item Object which will obviously not work. You're not looking to get a new item object, but instead are supposed to just check the ID directly. PHP: $item->getId() === 1
Use the PlayerJoinEvent. You can use the setItem() function, e.g.: PHP: /* * @var Player $player * @arg int $index * @arg Item $item */$item = Item::get(1);$player->getInventory()->setItem(0, $item); // adds Stone to first inventory slot.
PHP: <?phpnamespace MSelector;use pocketmine\entity\Effect;use pocketmine\event\Listener;use pocketmine\event\player\PlayerItemHeldEvent;use pocketmine\item\Item;use pocketmine\Player;use pocketmine\plugin\PluginBase;class Main extends PluginBase implements Listener{ public function onEnable() { $this->getServer()->getPluginManager()->registerEvents($this,$this); } public function onHold(PlayerItemHeldEvent $event){ $item = $event->getItem(); $player = $event->getPlayer(); //STAR if($item->getId() === Item::get(399, 0)){ $player->getInventory()->removeItem(Item::get(399, 0)); $player->getInventory()->addItem(Item::get(35, 0));//Guest $player->getInventory()->addItem(Item::get(35, 13));//VIP $player->getInventory()->addItem(Item::get(35, 3));//MVP $player->getInventory()->addItem(Item::get(35, 10));//VALOR } //WHITE if($item->getId() === Item::get(35, 0)){ $player->getInventory()->removeItem(Item::get(35, 0)); $this->getServer()->dispatchCommand($event->getPlayer(), "kit"); } //GREEN if($item->getId() === Item::get(35, 13)){ $player->getInventory()->removeItem(Item::get(35, 13)); $this->getServer()->dispatchCommand($evenr->getPlayer(), "kit"); } //BLUE if($item->getId() === Item::get(35, 3)){ $player->getInventory()->removeItem(Item::get(35, 3)); $this->getServer()->dispatchCommand($evenr->getPlayer(), "kit"); } //PURPLE if($item->getId() === Item::get(35, 10)){ $player->getInventory()->removeItem(Item::get(35, 10)); $this->getServer()->dispatchCommand($evenr->getPlayer(), "kit"); } }} I changed it to item held event. But it still wont work. Its not because im trying to get an Item Object because I tried using Item:IAMOND_BLOCK and it worked on my other plugin by giving speed 2 effect.
I tried that as well didn't work, can you try and give me a DIFFERENT way of how to setup the function? Thanks
Try reading what @Sandertv said maybe your brain figure it out then EDIT: You are also using $evenr instead of $event sometimes
PHP: public function onHold(PlayerItemHeldEvent $event){ $item = $event->getItem(); $player = $event->getPlayer(); //STAR if($item->getId() === Item::get(399, 0)){ $player->getInventory()->removeItem(Item::get(399, 0)); $player->getInventory()->addItem(Item::get(35, 0));//Guest $player->getInventory()->addItem(Item::get(35, 13));//VIP $player->getInventory()->addItem(Item::get(35, 3));//MVP $player->getInventory()->addItem(Item::get(35, 10));//VALOR } if($item->getId() === 35){ $player->getInventory()->addItem(Item::get(322, 0, 1)); }} I use the get()) on some because those are actual object items like he said. The orher “35” should work because I got the id like he said.
The $player->getInv->addItem etc i left that there just to test if the direct id method works with the giving item 322 but it didnt
I cleaned up your code, try and see if this works: PHP: public function onHold(PlayerItemHeldEvent $event) { $item = $event->getItem(); $player = $event->getPlayer(); // STAR if($item == Item::get(399)) { // don’t use getId() ... $player->getInventory()->removeItem(Item::get(399)); $player->getInventory()->addItem(Item::get(35, 0)); // Guest $player->getInventory()->addItem(Item::get(35, 13)); // VIP $player->getInventory()->addItem(Item::get(35, 3)); // MVP $player->getInventory()->addItem(Item::get(35, 10)); // VALOR } if($item->getId() == 35) { $player->getInventory()->addItem(Item::get(322, 0, 1)); }} Try and add elses to your ifs to find out your exact problem, like this: PHP: if($item->getId() == 35) { $player->getInventory()->addItem(Item::get(322, 0, 1));} else { $player->sendMessage("No, you are holding Item ID: " . $item->getId());}
Those are called typos, and if you didnt look at the code I did what he said and it still didnt activate, maybe your brain will figure out that out.
PHP: if($item->getId() === 35){ $player->getInventory()->addItem(Item::get(322, 0, 1)); }} This is the only part of the function that has to work. I left the rest as it was. And the 399 is an Object so I dont need to get the direct id like sandertv said.