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

How to cancel PlayerGameModeChangeEvent?

Comments in 'Plugin Development' started by Jazzwhistle, May 4, 2016.

  1. Jazzwhistle
    Offline

    Jazzwhistle Notable Member

    Joined:
    Dec 27, 2014
    Posts:
    364
    Minecraft User:
    Awzaw
    Hi, I'm having trouble hooking into PlayerGameModeChangeEvent to cancel it, but can't find any API 2.0.0 examples of how to use it. The source code says that it returns a player object and gamemode int, and no event - which is what I'm trying to cancel :-/

    I've tried this (adapted from some code I found), and variations with only the 2 arguments for player/newgamemode, and using $player instead of $c->getPlayer... but it doesn't get called whatever I try, even though all other listeners are working fine.

    PHP:
    public function onGameModeChange(PlayerKickEvent $kPlayer $playerPlayerGameModeChangeEvent $cPermission $permissionNewGameMode $newGamemode) {
                if (isset(
    $this->sessions[$c->$player->getName()])) {
                
    $c->setCancelled(true);
                
    $c->getPlayer->setGamemode(0);
                
    $c->getPlayer->sendMessage($this->getMessage("no-cheat"));
                }
            }
    Thanks
  2. MCPEPIG
    Offline

    MCPEPIG Notable Member

    Joined:
    Jun 22, 2014
    Posts:
    349
    Minecraft User:
    MCPEPIG
    Why set a player's gamemode if you cancel the event?
    applqpak likes this.
  3. udwarf
    Offline

    udwarf Active Member

    Joined:
    Apr 17, 2016
    Posts:
    61
    Minecraft User:
    1Zaza
    PHP:
    use pocketmine\event\player\PlayerGameModeChangeEvent;

        public function 
    onPlayerGameModeChangeEvent(PlayerGameModeChangeEvent $event){
            
    $event->setCancelled(true);
            
    $event->getPlayer()->sendMessage("no-no!");
        }

    however, this code will cancell ALL gamemode changes. even from the console. You have to add more checks in the code
    Jazzwhistle and applqpak like this.
  4. Jazzwhistle
    Offline

    Jazzwhistle Notable Member

    Joined:
    Dec 27, 2014
    Posts:
    364
    Minecraft User:
    Awzaw
    Thank you very much, I'll try that now.

    @MCPEPIG I'm writing a parkour plugin, and need to prevent players from switching gamemode (or having a friend change gamemode for them etc)
    applqpak likes this.
  5. Jazzwhistle
    Offline

    Jazzwhistle Notable Member

    Joined:
    Dec 27, 2014
    Posts:
    364
    Minecraft User:
    Awzaw
    Nice and simple after all... that's perfect, thank you!
    Last edited: May 4, 2016
    applqpak likes this.
  6. udwarf
    Offline

    udwarf Active Member

    Joined:
    Apr 17, 2016
    Posts:
    61
    Minecraft User:
    1Zaza
    Goog luck in plugins writing!
  7. TheDeibo
    Offline

    TheDeibo Notable Member

    Joined:
    Sep 8, 2014
    Posts:
    693
    Minecraft User:
    TheDeibo
    It all seems like a great idea. Good luck with that.

    Just pointing put ( just in case anyone is unaware )
    Within the SERVER.PROPERTIES there is a part saying
    Code:
    force-gamemode=
    
    Make that "on" for it to PUBLICALLY force the gamemode to what ever you set
    the
    Code:
    gamemode= (0/1/2/3)
    
    to.

    Just a helpful hint just incase thats what anyone needs
    Jazzwhistle and applqpak like this.

Share This Page

Advertisement