Advertisement
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

What code should i use to give a player an item as they join

Comments in 'Plugin Development' started by Hotshot_9930, May 2, 2015.

  1. Hotshot_9930
    Offline

    Hotshot_9930 Notable Member Plugin Developer

    Joined:
    May 26, 2014
    Posts:
    665
    Plugins:
    2
    Minecraft User:
    HotshotHD
    Hi im trying to create a sort of starter kit plugin. People that have never joined before will get armour and tools and old players will get an iron ingot as they join.
    I am using this code for the old players but it doesnt seem to work. Any suggestions?

    PHP:
    public function playerJoin(PlayerJoinEvent $pje){
        
    $player $pje->getPlayer();
        if(
    $player->hasPlayedBefore()) {
            
    $player->getInventory()->addItem(265);
              
            }
  2. Gamecrafter
    Offline

    Gamecrafter

    Joined:
    Nov 20, 2014
    Posts:
    978
    Plugins:
    9
    You got addItem wrong. It should be:
    PHP:
    $player->getInventory()->addItem(new Item($id$damage$amount);
    Remember to follow for more help! ;)
    Falk and xiaoq like this.
  3. Hotshot_9930
    Offline

    Hotshot_9930 Notable Member Plugin Developer

    Joined:
    May 26, 2014
    Posts:
    665
    Plugins:
    2
    Minecraft User:
    HotshotHD
    Thanks :) I followed.
  4. Gamecrafter
    Offline

    Gamecrafter

    Joined:
    Nov 20, 2014
    Posts:
    978
    Plugins:
    9
    :)
  5. xiaoq
    Offline

    xiaoq Active Member

    Joined:
    Dec 23, 2014
    Posts:
    232
    Minecraft User:
    xiaoq
    :p
  6. Hotshot_9930
    Offline

    Hotshot_9930 Notable Member Plugin Developer

    Joined:
    May 26, 2014
    Posts:
    665
    Plugins:
    2
    Minecraft User:
    HotshotHD
    Is this correct?
    PHP:
    public function playerJoin(PlayerJoinEvent $pje){
        
    $player $pje->getPlayer();
        if(
    $player->hasPlayedBefore()) {
            
    $player->getInventory()->addItem(newItem(26501));
  7. Gamecrafter
    Offline

    Gamecrafter

    Joined:
    Nov 20, 2014
    Posts:
    978
    Plugins:
    9
    You forgot a space between "new" and "Item".
    HotFireyDeath likes this.
  8. BlubberBoy333
    Offline

    BlubberBoy333 Active Member

    Joined:
    Aug 31, 2014
    Posts:
    177
    Minecraft User:
    Blubberboy333
    I would make it something like
    PHP:
    public function onPlayerJoinEvent(PlayerJoinEvent $event){
        if(
    $player haspermission("example.permission"){
            
    $items = array("1:0""2:0""3:0")
            
    $player->getInventory()->addItem(new Item(array_rand($items1));
        return 
    true;
        }else{
            
    $event->setCancelled();
        return 
    false;
        }
    }
    In the $item array you could set the items the players could get. I would also make a plugin.yml file to be something like this:

    Code:
    name: name
    api: api
    version: version
    main: main
    permissions:
      example.permission:
        default: op
        description: "Allows players to receive a random item when they join"
    
  9. Gamecrafter
    Offline

    Gamecrafter

    Joined:
    Nov 20, 2014
    Posts:
    978
    Plugins:
    9
    Do you even know PHP and the PocketMine API? Cancelling the event will cause the player to not join. :p
  10. Hotshot_9930
    Offline

    Hotshot_9930 Notable Member Plugin Developer

    Joined:
    May 26, 2014
    Posts:
    665
    Plugins:
    2
    Minecraft User:
    HotshotHD
    Thabks, it seems to be working. But what class do i use? Ive been using
    use pocketmine\item\IronIngot;
    But that doesnt work.
  11. Gamecrafter
    Offline

    Gamecrafter

    Joined:
    Nov 20, 2014
    Posts:
    978
    Plugins:
    9
    Just use pocketmine\item\Item.
    Hotshot_9930 likes this.
  12. BlubberBoy333
    Offline

    BlubberBoy333 Active Member

    Joined:
    Aug 31, 2014
    Posts:
    177
    Minecraft User:
    Blubberboy333
    :confused: I didn't know it would make the player not join...
  13. Hotshot_9930
    Offline

    Hotshot_9930 Notable Member Plugin Developer

    Joined:
    May 26, 2014
    Posts:
    665
    Plugins:
    2
    Minecraft User:
    HotshotHD
    Thank you both :D its working flawlessly now.
  14. BlubberBoy333
    Offline

    BlubberBoy333 Active Member

    Joined:
    Aug 31, 2014
    Posts:
    177
    Minecraft User:
    Blubberboy333
    You should submit the plugin, it sounds like something cool to have on servers :D
  15. Gamecrafter
    Offline

    Gamecrafter

    Joined:
    Nov 20, 2014
    Posts:
    978
    Plugins:
    9
  16. Hotshot_9930
    Offline

    Hotshot_9930 Notable Member Plugin Developer

    Joined:
    May 26, 2014
    Posts:
    665
    Plugins:
    2
    Minecraft User:
    HotshotHD
    Mine will work a bit different than spawnwithitems. I will consider submitting it if everything turns out well :)
  17. BlubberBoy333
    Offline

    BlubberBoy333 Active Member

    Joined:
    Aug 31, 2014
    Posts:
    177
    Minecraft User:
    Blubberboy333
  18. Gamecrafter
    Offline

    Gamecrafter

    Joined:
    Nov 20, 2014
    Posts:
    978
    Plugins:
    9
  19. Hotshot_9930
    Offline

    Hotshot_9930 Notable Member Plugin Developer

    Joined:
    May 26, 2014
    Posts:
    665
    Plugins:
    2
    Minecraft User:
    HotshotHD
  20. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    No. For the last time, you should not use new Item() directly. Use Item::get() instead.

Share This Page

Advertisement