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

Solved Cancel another event within class

Comments in 'Plugin Development' started by 1vann, Jul 20, 2015.

  1. 1vann
    Offline

    1vann New Member

    Joined:
    Jul 20, 2015
    Posts:
    6
    Minecraft User:
    1vannn
    Hey everyone, so my little nephew and I have a server up and since my nephew is quite a bit younger, I wanted to create "magic blocks" as some type of way to get myself back into programming. So I want to do this by touch, so I used the playerBlockTouch event, which works perfectly fine, but if your holding a block, it would place a block in front of the "magic block".. I tried using variables to cancel out the place event, but it doesn't seem to work. Here's my code:

    PHP:
    <?php

        
    namespace IvansPlugin;

        use 
    pocketmine\event\player\PlayerInteractEvent;
        use 
    pocketmine\event\Listener;
        use 
    pocketmine\plugin\PluginBase;
        use 
    pocketmine\utils\TextFormat;
        use 
    pocketmine\Player;
        use 
    pocketmine\command;
        use 
    pocketmine\level;
        use 
    pocketmine\api;
        use 
    pocketmine\command\CommandSender;
        use 
    pocketmine\command\ConsoleCommandSender;
        use 
    pocketmine\block;
        use 
    pocketmine\event\block\BlockPlaceEvent;
        use 
    pocketmine\event\block\BlockBreakEvent;

        class 
    Main extends PluginBase implements Listener {

            private static 
    $iceBlock false// this is where i tried global vars (of sorts)

            
    public function onLoad() {
                
    $this->getLogger()->info(TextFormat::WHITE "Ivans Plugin: Loaded");
            }

            public function 
    onEnable() {
                
    $this->getServer()->getPluginManager()->registerEvents($this$this);
                
    $this->saveDefaultConfig();
                
    $this->reloadConfig();
                
    $this->getLogger()->info(TextFormat::DARK_GREEN "Ivans Plugin: Enabled");
            }

            public function 
    playerBlockTouch(PlayerInteractEvent $event){
                
    $player $event->getPlayer();
                
    $block $event->getBlock();
                if(
    $block->getID() == 79) {
                    
    self::$iceBlock true// this is where i tried global vars (of sorts)
                    
    $command "time set 0";
                    
    $this->getServer()->dispatchCommand(new ConsoleCommandSender(), $command);
                    
    self::$iceBlock false// this is where i tried global vars (of sorts)
                
    }
            }

            public function 
    onBlockBreak(BlockBreakEvent $event){
                
    $block $event->getBlock();
                if(
    $block->getID() == 79) {
                    
    $event->getPlayer()->sendMessage("You can't break ice blocks!");
                    
    $event->setCancelled();
                }       
            } 

            public function 
    onBlockPlace(BlockPlaceEvent $event) {
                if (
    self::$iceBlock === true) { // this is where i tried global vars (of sorts)
                    
    $event->getPlayer()->sendMessage("You placed an ice block next to another one!");
                    
    $event->setCancelled();
                }
            }  

            public function 
    onDisable() {
                
    $this->getLogger()->info(TextFormat::DARK_RED "Disabled");
            }

        }
    ?>
    Thank you all in advance!
    - Ivan
  2. ahoj_truhliku
    Offline

    ahoj_truhliku Banned

    Joined:
    Jul 9, 2015
    Posts:
    89
    Minecraft User:
    Creeperface001
    try to cancel interact event
  3. 1vann
    Offline

    1vann New Member

    Joined:
    Jul 20, 2015
    Posts:
    6
    Minecraft User:
    1vannn
    How might I do that? I'm not too familiar with PocketMine..
  4. Xenturio
    Offline

    Xenturio Active Member

    Joined:
    Sep 19, 2014
    Posts:
    333
    Minecraft User:
    Xenturio
    Code:
            public function onBlockPlace/onInteract(BlockPlaceEvent/PlayerInteractEvent $event) {$event->setCancelled();
    RekkuzaRage likes this.
  5. 1vann
    Offline

    1vann New Member

    Joined:
    Jul 20, 2015
    Posts:
    6
    Minecraft User:
    1vannn
    Thank you very much, I'll try that real quick :p
  6. Xenturio
    Offline

    Xenturio Active Member

    Joined:
    Sep 19, 2014
    Posts:
    333
    Minecraft User:
    Xenturio
    dont forget the like butten here and use pocketmine\event\player\PlayerInteractEvent; :p
  7. RekkuzaRage
    Offline

    RekkuzaRage Active Member

    Joined:
    Apr 15, 2014
    Posts:
    251
    Minecraft User:
    RekkuzaRage
    Woah, never seen that! Great job!
  8. Xenturio
    Offline

    Xenturio Active Member

    Joined:
    Sep 19, 2014
    Posts:
    333
    Minecraft User:
    Xenturio
    o_O thx but....hm....
  9. 1vann
    Offline

    1vann New Member

    Joined:
    Jul 20, 2015
    Posts:
    6
    Minecraft User:
    1vannn
    Hi, how would you code this? I'm not quite sure how to implement it into my situation.
  10. Xenturio
    Offline

    Xenturio Active Member

    Joined:
    Sep 19, 2014
    Posts:
    333
    Minecraft User:
    Xenturio
    insteadof
    public function onBlockPlace(BlockPlaceEvent $event) {$event->setCancelled();
    use
    public function onInteract(PlayerInteractEvent $event) {$event->setCancelled();
  11. 1vann
    Offline

    1vann New Member

    Joined:
    Jul 20, 2015
    Posts:
    6
    Minecraft User:
    1vannn
    That is awesome, thank you very much to all of you. ;) This is my final code for the future to enjoy and work off of. :)

    PHP:
    <?php

        
    namespace IvansPlugin;

        use 
    pocketmine\event\player\PlayerInteractEvent;
        use 
    pocketmine\event\Listener;
        use 
    pocketmine\plugin\PluginBase;
        use 
    pocketmine\utils\TextFormat;
        use 
    pocketmine\Player;
        use 
    pocketmine\command;
        use 
    pocketmine\level;
        use 
    pocketmine\api;
        use 
    pocketmine\command\CommandSender;
        use 
    pocketmine\command\ConsoleCommandSender;
        use 
    pocketmine\block;
        use 
    pocketmine\event\block\BlockPlaceEvent;
        use 
    pocketmine\event\block\BlockBreakEvent;

        class 
    Main extends PluginBase implements Listener {

            private static 
    $iceBlock false;

            public function 
    onLoad() {
                
    $this->getLogger()->info(TextFormat::WHITE "Ivans Plugin: Loaded");
            }

            public function 
    onEnable() {
                
    $this->getServer()->getPluginManager()->registerEvents($this$this);
                
    $this->saveDefaultConfig();
                
    $this->reloadConfig();
                
    $this->getLogger()->info(TextFormat::DARK_GREEN "Ivans Plugin: Enabled");
            }

            public function 
    playerBlockTouch(PlayerInteractEvent $event){
                
    $player $event->getPlayer();
                
    $block $event->getBlock();
                if(
    $block->getID() == 79) {
                    
    self::$iceBlock true;
                    
    $command "time set 0";
                    
    $this->getServer()->dispatchCommand(new ConsoleCommandSender(), $command);
                    
    self::$iceBlock false;
                }
                if(
    $block->getID() == 42) {
                    
    //self::$iceBlock = true;
                    
    $command "time set 19000";
                    
    $this->getServer()->dispatchCommand(new ConsoleCommandSender(), $command);
                    
    //self::$iceBlock = false;
                
    }
            }

            public function 
    onBlockBreak(BlockBreakEvent $event){
                
    $block $event->getBlock();
                
    $player $event->getPlayer();
                if ()
                if(
    $block->getID() == 79) {
                    
    $event->getPlayer()->sendMessage("You can't break ice blocks!");
                    
    $event->setCancelled();
                }
                if(
    $block->getID() == 42) {
                    
    $event->getPlayer()->sendMessage("You can't break iron blocks!");
                    
    $event->setCancelled();
                }                  
            } 

            public function 
    onInteract(PlayerInteractEvent $event) {
                
    $block $event->getBlock();
                if (
    $block->getID() == 79) {
                    
    $event->getPlayer()->sendMessage("You placed an ice block next to another one!");
                    
    $event->setCancelled();
                }
                if (
    $block->getID() == 42) {
                    
    $event->getPlayer()->sendMessage("You placed an iron block next to another one!");
                    
    $event->setCancelled();
                }               
            }  

            public function 
    onDisable() {
                
    $this->getLogger()->info(TextFormat::DARK_RED "Disabled");
            }

        }
    ?>
  12. Legoboy0215
    Offline

    Legoboy0215 Notable Member

    Joined:
    Nov 1, 2014
    Posts:
    1,724
    Minecraft User:
    Legoboy0215
    Why use pocketmine\api??? It doesn't exist or is not needed.
    xiaoq likes this.
  13. 1vann
    Offline

    1vann New Member

    Joined:
    Jul 20, 2015
    Posts:
    6
    Minecraft User:
    1vannn
    Earlier in the development I was getting a bunch of errors and I figured I didn't call the right thing, I didn't bother to take a closer look at the docs and just added them in.
  14. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Don't use static. It doesn't work well with /reload.
  15. Creeperface
    Offline

    Creeperface Notable Member Plugin Developer

    Joined:
    Nov 8, 2014
    Posts:
    1,346
    Plugins:
    4
    Minecraft User:
    CreeperFace
    And when you cancel interact event so blockplaceevent is cancelled too?
  16. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Yes
    Legoboy0215 likes this.

Share This Page

Advertisement