# Why does addItem not work?

Comments in 'Plugin Development' started by myst02, May 29, 2015.

1. Offline

### myst02Active Member

Joined:
Mar 23, 2015
Posts:
97
Minecraft User:
Hi,

I coded a plugin, and it fails at this line of code:
PHP:
 $player->getInventory()->addItem(new ItemBlock(Block::get($itemname), "10", $count));  Error is: 13:36:01 [CRITICAL] Could not pass event pocketmine\event\player\PlayerInteractEvent to EasyRewards v1.1.2: Class kjjnetwork\EasyRewards\ItemBlock not found on kjjnetwork\EasyRewards\Main 13:36:01 [CRITICAL] ClassNotFoundException: "Class kjjnetwork\EasyRewards\ItemBlock not found" (EXCEPTION) in "/src/spl/BaseClassLoader" at line 144 Please help 2. Offline ### EvolSoftNotable MemberPlugin Developer Joined: Sep 10, 2014 Posts: 821 Plugins: 15 Minecraft User: The error is not related to the part of code you wrote here. Check plugin.yml file. The error is there 3. Offline ### Legoboy0215Notable Member Joined: Nov 1, 2014 Posts: 1,747 Minecraft User: It is an class error. Make sure that class name and file name is all same. This is a quote from @PEMapModder. 4. Offline ### myst02Active Member Joined: Mar 23, 2015 Posts: 97 Minecraft User: Maybe it doesnt work because my plugin does not have any commands or perms? 5. Offline ### Legoboy0215Notable Member Joined: Nov 1, 2014 Posts: 1,747 Minecraft User: It doea not matter with commands and permissions I think... 6. Offline ### aliulyNotable MemberPlugin Developer Joined: Feb 8, 2014 Posts: 1,086 Plugins: 17 You are missing an "use" statement iJoshuaHD likes this. 7. Offline ### myst02Active Member Joined: Mar 23, 2015 Posts: 97 Minecraft User: ok, and which one? Some with "ItemBlock"? thank you 8. Offline ### Legoboy0215Notable Member Joined: Nov 1, 2014 Posts: 1,747 Minecraft User: Maybe Code: use pocketmine\event\Player\PlayerInteractEvent; 9. Offline ### myst02Active Member Joined: Mar 23, 2015 Posts: 97 Minecraft User: I have already added this But it still doesn't work 10. Offline ### EvolSoftNotable MemberPlugin Developer Joined: Sep 10, 2014 Posts: 821 Plugins: 15 Minecraft User: Can you post your plugin.yml file and the entire main class? 11. Offline ### myst02Active Member Joined: Mar 23, 2015 Posts: 97 Minecraft User: Yes, I can: plugin.yml: Code: name: EasyRewards api: [1.3.1] version: 1.1.2 load: POSTWORLD main: kjjnetwork\EasyRewards\Main author: kjjnetwork website: http://www.kjjserver.com commands: permissions:  Main.php: PHP:  <?phpnamespace kjjnetwork\EasyRewards;use pocketmine\plugin\PluginBase;use pocketmine\event\Listener;use pocketmine\Server;use pocketmine\command\Command;use pocketmine\command\CommandSender;use pocketmine\event\player\PlayerInteractEvent;use pocketmine\math\Vector3;use pocketmine\tile\Sign;use pocketmine\event\block\SignChangeEvent;use pocketmine\level\Position;use pocketmine\entity\Entity;use pocketmine\event\block\BlockPlaceEvent;use pocketmine\event\block\BlockBreakEvent;use pocketmine\item\Item;use pocketmine\tile\Tile;use pocketmine\Player;use pocketmine\item/ItemBlock;use onebone\economyapi\EconomyAPI;class Main extends PluginBase implements Listener{ private$api, $server,$path;  public function onEnable(){  $this->getServer()->getPluginManager()->registerEvents($this, $this); } public function playerBlockTouch(PlayerInteractEvent$event){  if($event->getBlock()->getID() == 323 ||$event->getBlock()->getID() == 63 || $event->getBlock()->getID() == 68){$sign = $event->getPlayer()->getLevel()->getTile($event->getBlock());  if(!($sign instanceof Sign)){ return; }$sign = $sign->getText(); if($sign[0]=='[REWARD]'){  if(empty($sign[2]) !== true){$itemname = $sign[1];$count = $sign[2];$money = $sign[3];$player = $event->getPlayer();$playername = $player->getName(); EconomyAPI::getInstance()->addMoney($player, $money);$player->getInventory()->addItem(new ItemBlock(Block::get($itemname), "10",$count));  $this->getServer()->broadcastMessage("[server] Player '".$playername."' just got cool rewards!");  $event->getPlayer()->sendMessage("[server] Reward given! Thanks for playing on KJJ!"); }else{$event->getPlayer()->sendMessage("[server] Reward sign is broken! Please report to [email protected]");  }  }  }  }  } 

Thank you!
AddMoney part is working, but not giving items.
12. Offline

### aliulyNotable MemberPlugin Developer

Joined:
Feb 8, 2014
Posts:
1,086
Plugins:
17
You should just use Item::get or Item::fromString, which means you need to do use ....something...\Item
PEMapModder likes this.
13. Offline

### Legoboy0215Notable Member

Joined:
Nov 1, 2014
Posts:
1,747
Minecraft User:
14. Offline

### PEMapModderNotable MemberPlugin Developer

Joined:
Oct 9, 2013
Posts:
7,325
Plugins:
11
Minecraft User:
What? Why? He just forgot to use pocketmine\item\ItemBlock.
15. Offline

### EvolSoftNotable MemberPlugin Developer

Joined:
Sep 10, 2014
Posts:
821
Plugins:
15
Minecraft User:
Then I realised that the error was on ItemBlock class, sorry
16. Offline

### myst02Active Member

Joined:
Mar 23, 2015
Posts:
97
Minecraft User:
Yay! It works now! Thanks so much for all your help!