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

Nothing happens >PlayerBreak

Comments in 'Plugin Development' started by cake23, Nov 30, 2015.

  1. cake23
    Offline

    cake23 Active Member

    Joined:
    Aug 10, 2015
    Posts:
    124
    Minecraft User:
    TheEnd
    Hey PocketMine-Community! :D I have a little problem...
    PHP:
    public function onBreak(BlockBreakEvent $event) {
        
    $player $event->getPlayer();
        
    $block $event->getBlock();
        if(
    $block->getID() === BLOCK::GOLD_BLOCK){
          switch(
    mt_rand(15)){
            case 
    1:
            
    $player->getInventory()->addItem(Item::get(Item::DIAMOND_SWORD01));
            case 
    2:
            
    $player->getInventory()->addItem(Item::get(Item::STONE_SWORD01));
            case 
    3:
            
    $player->getInventory()->addItem(Item::get(Item::WOODEN_SWORD01));
            case 
    4:
            
    $player->getInventory()->addItem(Item::get(Item::IRON_SWORD01));
            case 
    5:
            
    $player->getInventory()->addItem(Item::get(Item::GOLD_SWORD01));
          }
        }
      }
    If i touch a Goldblock, nothing happens.
    (What i want: If a player breaks a Goldblock, he gets a random sword!)
    Does anyone know why?
    Thank you! :)
    Last edited: Nov 30, 2015
  2. CraftYourBukkit
    Offline

    CraftYourBukkit Notable Member Plugin Developer

    Joined:
    Jan 20, 2015
    Posts:
    1,022
    Plugins:
    2
    Minecraft User:
    CraftYourBukkit
    You have to break the block, for touching use PlayerInteractEvent
    cake23 likes this.
  3. cake23
    Offline

    cake23 Active Member

    Joined:
    Aug 10, 2015
    Posts:
    124
    Minecraft User:
    TheEnd
    Oh sorry, i mean breaks*! :'D If a player breaks a Goldblock, he get a random sword! :D
  4. CraftYourBukkit
    Offline

    CraftYourBukkit Notable Member Plugin Developer

    Joined:
    Jan 20, 2015
    Posts:
    1,022
    Plugins:
    2
    Minecraft User:
    CraftYourBukkit
    Did you register the events? Well, you should also use something like this:
    PHP:
    public function onBreak(BlockBreakEvent $event) {
        
    $player $event->getPlayer();
        
    $block $event->getBlock();
        if(
    $block->getID() === BLOCK::GOLD_BLOCK){
          switch(
    mt_rand(15)){
            case 
    1:
            
    $item Item::get(Item::DIAMOND_SWORD01);
            case 
    2:
            
    $item Item::get(Item::STONE_SWORD01);
            case 
    3:
            
    $item Item::get(Item::WOODEN_SWORD01);
            case 
    4:
            
    $item Item::get(Item::IRON_SWORD01);
            case 
    5:
            
    $item Item::get(Item::GOLD_SWORD01);
          }
    $player->getInventory()->addItem($item);    }
      }
  5. cake23
    Offline

    cake23 Active Member

    Joined:
    Aug 10, 2015
    Posts:
    124
    Minecraft User:
    TheEnd
    Yes, I register the events!
    If I use your code, i get an error. :/
    [Server] Parse error: syntax error, unexpected 'public function onBreak' (T_STRING), expecting function (T_FUNCTION) in....
  6. CraftYourBukkit
    Offline

    CraftYourBukkit Notable Member Plugin Developer

    Joined:
    Jan 20, 2015
    Posts:
    1,022
    Plugins:
    2
    Minecraft User:
    CraftYourBukkit
    I think you forgot a "}"
  7. cake23
    Offline

    cake23 Active Member

    Joined:
    Aug 10, 2015
    Posts:
    124
    Minecraft User:
    TheEnd
    PHP:
    public function onBreak(BlockBreakEvent $event) {
        
    $player $event->getPlayer();
        
    $block $event->getBlock();
        if(
    $block->getID() === BLOCK::GOLD_BLOCK){
          switch(
    mt_rand(15)){
            case 
    1:
            
    $item Item::get(Item::DIAMOND_SWORD01);
            case 
    2:
            
    $item Item::get(Item::STONE_SWORD01);
            case 
    3:
            
    $item Item::get(Item::WOODEN_SWORD01);
            case 
    4:
            
    $item Item::get(Item::IRON_SWORD01);
            case 
    5:
            
    $item Item::get(Item::GOLD_SWORD01);
          }
    $player->getInventory()->addItem($item);    }
      }
    Nope.. I think I don't forget a "}" or "{".
  8. Hotshot_9930
    Offline

    Hotshot_9930 Notable Member Plugin Developer

    Joined:
    May 26, 2014
    Posts:
    665
    Plugins:
    2
    Minecraft User:
    HotshotHD
    Post your full code.
    cake23 likes this.
  9. cake23
    Offline

    cake23 Active Member

    Joined:
    Aug 10, 2015
    Posts:
    124
    Minecraft User:
    TheEnd
    PHP:
    <?php

    namespace test;

    use 
    pocketmine\plugin\PluginBase;
    use 
    pocketmine\event\Listener;

    use 
    pocketmine\event\block\BlockBreakEvent;
    use 
    pocketmine\block\Block;

    class 
    Main extends PluginBase implements Listener {
     
      public function 
    onEnable(){
        
    $this->getLogger()->info("┬žaAktiviert!");
        
    $this->getServer()->getPluginManager()->registerEvents($this$this);
      }
     
      public function 
    onBreak(BlockBreakEvent $event) {
        
    $player $event->getPlayer();
        
    $block $event->getBlock();
        if(
    $block->getID() === BLOCK::GOLD_BLOCK){
          switch(
    mt_rand(15)){
            case 
    1:
            
    $item Item::get(Item::DIAMOND_SWORD01);
            case 
    2:
            
    $item Item::get(Item::STONE_SWORD01);
            case 
    3:
            
    $item Item::get(Item::WOODEN_SWORD01);
            case 
    4:
            
    $item Item::get(Item::IRON_SWORD01);
            case 
    5:
            
    $item Item::get(Item::GOLD_SWORD01);
          }
    $player->getInventory()->addItem($item);    }
      }
    }
  10. Legoboy0215
    Offline

    Legoboy0215 Notable Member

    Joined:
    Nov 1, 2014
    Posts:
    1,724
    Minecraft User:
    Legoboy0215
    Block, not BLOCK.
  11. CraftYourBukkit
    Offline

    CraftYourBukkit Notable Member Plugin Developer

    Joined:
    Jan 20, 2015
    Posts:
    1,022
    Plugins:
    2
    Minecraft User:
    CraftYourBukkit
    No, he doesn't have to.
  12. Hotshot_9930
    Offline

    Hotshot_9930 Notable Member Plugin Developer

    Joined:
    May 26, 2014
    Posts:
    665
    Plugins:
    2
    Minecraft User:
    HotshotHD
    Look at the pocketmine's source code files. Its pocketmine\Player.
    And no he doesn't have to use it in this case.
    CraftYourBukkit likes this.
  13. cake23
    Offline

    cake23 Active Member

    Joined:
    Aug 10, 2015
    Posts:
    124
    Minecraft User:
    TheEnd
    Thank you, but i get the same error again. :/

Share This Page

Advertisement