1. The forums will be archived and moved to a read only mode in about 2 weeks (mid march).

Solved Need help with my Plugin

Discussion in 'Development' started by WinterBuild7074, May 2, 2017.

  1. WinterBuild7074

    WinterBuild7074 Zombie Pigman

    Messages:
    693
    GitHub:
    winterbuild7074
    It gives me this error: http://i.cubeupload.com/OFWvCo.png

    What is wrong here?
    PHP:
    <?php

    namespace ExtraProtector;

    use 
    pocketmine\event\Listener;
    use 
    pocketmine\Player;
    use 
    pocketmine\plugin\PluginBase;
    use 
    pocketmine\Server;
    use 
    pocketmine\event\player\PlayerInteractEvent;
    use 
    pocketmine\event\entity\EntityDamageEvent;
    use 
    pocketmine\level\Level;

    class 
    Main extends PluginBase implements Listener {
        
        public function 
    onEnable() {
            
    $this->getServer()->getPluginManager()->registerEvents($this$this);
        }
        
        public function 
    onInteract(PlayerInteractEvent $event) { // I don't want players go break and touch the Glass Pane in front of the ItemFrame, that they cannot get the item out of it.
            
    $player $event->getPlayer();
            
    $block $event->getBlock();
            
    $level $event->getLevel();
            if(
    $block->getId() === "102" && $level->getName() === "Shopp") {
                if(!
    $player->hasPermission("breakshop")) {
                    
    $event->setCancelled();
                }
            }
        }
        
        public function 
    onDamage(EntityDamageEvent $event) { // I need to do this because my iProtector plugin doesn't allow me to set god mode on.
            
    $player $event->getPlayer();
            
    $level $event->getLevel();
            if(
    $player instanceof Player) {
                if(
    $level->getName() === "Plots" || $level->getName() === "Contest-Plots" || $level->getName() === "Roleplay") {
                    
    $event->setCancelled();
                }
            }
        }
    }
     
  2. Lowkey

    Lowkey Slime

    Messages:
    94
    PlayerInteractEvent and getLevel()? HUH?
     
    Sandertv likes this.
  3. Lowkey

    Lowkey Slime

    Messages:
    94
    To get the level of the player that interacts, use $player->getLevel().
     
    OnTheVerge likes this.
  4. WinterBuild7074

    WinterBuild7074 Zombie Pigman

    Messages:
    693
    GitHub:
    winterbuild7074
    I fixed it, but it get the same error for getPlayer().
     
  5. TheDiamondYT

    TheDiamondYT Zombie

    Messages:
    298
    GitHub:
    TheDiamondYT1
    Why are you checking if $event->getPlayer() is an instance of a Player? Read the function name. The only thing it can return is a player object.
     
    Muqsit and corytortoise like this.
  6. 0x15f

    0x15f Baby Zombie

    Messages:
    145
    GitHub:
    0x15f
    Please look at pocketmine's source code and learn the API before you attempt making plugins. Learning PHP would be a plus as well, if you knew PHP (you obviously don't) you would realize that "Error: call to undefined method" means that the method you are trying to call DOESN'T exist. #ReadTheDocs #LearnPHP
     
    Croc, Muqsit and HimbeersaftLP like this.
  7. WinterBuild7074

    WinterBuild7074 Zombie Pigman

    Messages:
    693
    GitHub:
    winterbuild7074
    @ImagicalGamer I know what that error means, but I don't know why he says "unknown function" on the getPlayer() function.
    @TheDiamondYT But I don't think removing the "is instanceof" will fix the error...
     
  8. Sandertv

    Sandertv Zombie Pigman Poggit Reviewer

    Messages:
    786
    GitHub:
    Sandertv
    EntityDamageEvent doesn't have a getPlayer function. It has a getEntity() function to get the entity that's being damaged. If you want the attacker, you have to check if the event is instanceof EntityDamageByEntityEvent. After that check if $event->getDamage instanceof Player.
     
    corytortoise and WinterBuild7074 like this.
  9. WinterBuild7074

    WinterBuild7074 Zombie Pigman

    Messages:
    693
    GitHub:
    winterbuild7074
    I tried touching a Glass Pane Block, but the event doesn't get cancelled, there's still the breaking animation (before the blocks really gets broken). How can I stop people doing that?
    On another server, the player is in Adventure Mode, but cannot "touch" blocks. I tried switching to Adventure Mode on my server, but I can still "touch" blocks.

    Maybe there's a PlayerTouchEvent? Then I can cancel that?
     
  10. TheDiamondYT

    TheDiamondYT Zombie

    Messages:
    298
    GitHub:
    TheDiamondYT1
    #ReadTheSource https://github.com/pmmp/PocketMine-...cketmine/event/player/PlayerInteractEvent.php
     
  11. GamakCZ

    GamakCZ Zombie Pigman

    Messages:
    598
    GitHub:
    GamakCZ
    Use this.

    PHP:
    <?php

    namespace ExtraProtector;

    use 
    pocketmine\event\Listener;
    use 
    pocketmine\Player;
    use 
    pocketmine\plugin\PluginBase;
    use 
    pocketmine\Server;
    use 
    pocketmine\event\player\PlayerInteractEvent;
    use 
    pocketmine\event\entity\EntityDamageEvent;
    use 
    pocketmine\level\Level;

    class 
    Main extends PluginBase implements Listener {
        
        public function 
    onEnable() {
            
    $this->getServer()->getPluginManager()->registerEvents($this$this);
        }
        
        public function 
    onInteract(PlayerInteractEvent $event) { // I don't want players go break and touch the Glass Pane in front of the ItemFrame, that they cannot get the item out of it.
            
    $player $event->getPlayer();
            
    $block $event->getBlock();
            
    $level $player->getLevel();
            if(
    $block->getId() == "102" && $level->getName() == "Shopp") {
                if(!
    $player->hasPermission("breakshop")) {
                    
    $event->setCancelled();
                }
            }
        }
        
        public function 
    onDamage(EntityDamageEvent $event) { // I need to do this because my iProtector plugin doesn't allow me to set god mode on.
            
    $player $event->getPlayer();
            
    $level $player->getLevel();
            if(
    $level->getName() == "Plots" || $level->getName() == "Contest-Plots" || $level->getName()  == "Roleplay") {
                    
    $event->setCancelled(); 
            }
        }
    }
     
  12. Teamblocket

    Teamblocket Zombie

    Messages:
    301
    GitHub:
    teamblocket
    on the last function
     
  13. WinterBuild7074

    WinterBuild7074 Zombie Pigman

    Messages:
    693
    GitHub:
    winterbuild7074
  14. AnkitM252

    AnkitM252 Spider Jockey

    Messages:
    29
    GitHub:
    AnkitM252
    Change the PlayerInteractEvent to BlockBreakEvent if you don't want players to break a block and EntityDamageEvent doesn't have getPlayer() function, instead it has getEntity()!
     
    CortexPE likes this.
  15. Lowkey

    Lowkey Slime

    Messages:
    94
    You can then check if the entity is instanceof Player.
     
  16. WinterBuild7074

    WinterBuild7074 Zombie Pigman

    Messages:
    693
    GitHub:
    winterbuild7074
    Will it also stop the breaking "animation"? I want it to stop that too, or players can still get out the items.
     
  17. Thunder33345

    Thunder33345 Moderator Staff Member

    Messages:
    2,137
    GitHub:
    Thunder33345
    no that's client sided AFAIK
     
  18. WinterBuild7074

    WinterBuild7074 Zombie Pigman

    Messages:
    693
    GitHub:
    winterbuild7074
    That means, it is impossible to stop the person from touching? But it must be possible, I saw a server that blocks people from getting out the item of an Item Frame.
     
  19. Lowkey

    Lowkey Slime

    Messages:
    94
    You can block this, but not the actual animation of the player breaking a block. This is client-side
     
  20. WinterBuild7074

    WinterBuild7074 Zombie Pigman

    Messages:
    693
    GitHub:
    winterbuild7074
    The problem is, that after the breaking-animation the Glass Pane disappears for 0.2 secs – this time can sometimes can be enough to take out the item of the Item Frame.
    This sounds complicated to stop, but I need to fix this, or all the items will be gone soon.
     
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.