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

BlockBreakEvent not cancelling.

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

  1. gsdgamer

    gsdgamer Spider Jockey

    Messages:
    28
    When I try creating a BlockBreakevent I want it to cancel but can't seem get it to work.

    Code:
        public function blockbreaking(BlockBreakEvent $event){
            $p = $event->getPlayer();
            $x = $p->getX();
            $y = $p->getY();
            $z = $p->getZ();
            $cfg = $this->getConfig()->getAll();
            $x1 = $cfg->get("X1");
            $x2 = $cfg->get("X2");
            $y1 = $cfg->get("Y1");
            $y2 = $cfg->get("Y2");
            $z1 = $cfg->get("Z1");
            $z2 = $cfg->get("Z2");
            if($x > $x1 and $x <= $X2 and $y > $y1 and $y < $y2 and $z < $z1 and $z > $z2){
                $event->setCancelled(true);
                $p->sendMessage("It can't rain all the time.");
            }
        }
    I can't see what i'm doing wrong so any help would be good, thanks.
     
  2. Teamblocket

    Teamblocket Zombie

    Messages:
    301
    GitHub:
    teamblocket
    getX(); is a thing in break block event?
     
  3. Lorak9904

    Lorak9904 Witch

    Messages:
    58
    It has nothing to do with your event... I dont know what are u trying to do...
     
  4. Teamblocket

    Teamblocket Zombie

    Messages:
    301
    GitHub:
    teamblocket
    $cfg is set to get every config coord
     
  5. Teamblocket

    Teamblocket Zombie

    Messages:
    301
    GitHub:
    teamblocket
    try this
    PHP:
            $cfg $this->getConfig();
     
  6. corytortoise

    corytortoise Skeleton

    Messages:
    825
    GitHub:
    corytortoise
    Can we see the values of all of the coordinates in the config? Does your if statement ever return true?
     
  7. gsdgamer

    gsdgamer Spider Jockey

    Messages:
    28
    Sorry forgot to mention the area is a cuboid and it checks if anyone builds in it, if so it cancels the event. This is the full code.

    Code:
    <?php
    
    namespace SBMCPE;
    
    use pocketmine\Player;
    use pocketmine\plugin\PluginBase;
    use pocketmine\event\Listener;
    use pocketmine\utils\TextFormat;
    use pocketmine\command\CommandSender;
    use pocketmine\level\Level;
    use pocketmine\plugin\EventExecutor;
    use pocketmine\command\CommandExecutor;
    use pocketmine\command\Command;
    use pocketmine\math\Vector3;
    use pocketmine\event\player\PlayerDeathEvent;
    use pocketmine\event\block\BlockBreakEvent;
    
    class Main extends PluginBase implements Listener, CommandExecutor{
        
        public function onLoad(){
            $this->getLogger()->info(TextFormat::GRAY . "Sky Block Loading....");
        }
        
        public function onEnable(){
            if(!file_exists($this->getDataFolder() . "config.yml")){
                @mkdir($this->getDataFolder());
                file_put_contents($this->getDataFolder()."config.yml", $this->getResource("config.yml"));
                
            }
            $this->getLogger()->info(TextFormat::GREEN . "Sky Block Loaded.");
        }
        
        
        public function onCommand(CommandSender $sender, Command $cmd, $label, array $args){
            switch ($cmd){
                case "sb pos1":
                    if(!($sender instanceof Player)){
                        $sender->sendMessage("You cannot use this in console.");
                    }elseif ($sender instanceof Player){
                        if(!(isset($args[0]))){
                            if($sender->hasPermission("sb.spawn") || $sender->isOp()){
                                $pos1 = $sender->getPosition();
                                $area1 = new Vector3($pos1->getX(), $pos1->getY(), $pos1->getZ());
                                $area1World = $pos1->getLevel()->getName();
                                $this->getConfig()->set("X1", $area1->x);
                                $this->getConfig()->set("Y1", $area1->y);
                                $this->getConfig()->set("Z1", $area1->z);
                                $this->getConfig()->set("world1", $pos1->getLevel()->getName());
                                $sender->sendMessage(TextFormat::BLUE . "Position One has been set. Please proceed to set position two.");
                   
                            }
                        }
                    }
                    break;   
                case "sb pos2":
                    if(!($sender instanceof Player)){
                        $sender->sendMessage("You cannot use this in console.");
                }elseif($sender instanceof Player){
                    if(!(isset($args[0]))){
                        if($sender->hasPermission("sb.spawn") || $sender->isOp()){
                            $pos2 = $sender->getPosition();
                            $area2 = new Vector3($pos2->getX(), $pos2->getY(), $pos2->getZ());
                            $area2World = $pos2->getLevel()->getName();
                                if($this->getConfig()->get("world1") != $pos2->getLevel()->getName())
                                    {
                                    $sender->sendMessage("You must be in the same world as position one.");
                                }else {
                            $this->getConfig()->set("X2", $area2->x);
                            $this->getConfig()->set("Y2", $area2->y);
                            $this->getConfig()->set("Z2", $area2->z);
                            $this->getConfig()->set("world2", $pos2->getLevel()->getName());
                            $sender->sendMessage(TextFormat::BLUE . "Position two has been set. Now stand where you would like players to spawn in and use /sb spawn");
                        }
                    }
                }
            }
                break;
                case "sb spawn":
                    if(!($sender instanceof Player)){
                        $this->getLogger()->info(TextFormat::RED . "You cannot use this in console");
                    }elseif($sender instanceof Player){
                        if(!(isset($args[0]))){
                            if($sender->hasPermission("sb.spawn") || $sender->isOp()){
                                $pos3 = $sender->getPosition();
                                $spawn = new Vector3($pos3->getX(), $pos3->getY(), $pos3->getZ());
                                if($this->getConfig()->get("world1") != $pos3->getLevel()->getName()){
                                    $sender->sendMessage("You must be in the same world as position one and two");
                                }    else    {
                                $this->getConfig()->set("Spawnx", $spawn->x);
                                $this->getConfig()->set("Spawny", $spawn->y);
                                $this->getConfig()->set("Spawnz", $spawn->z);
                                $this->getConfig()->set("SpawnWorld", $pos3->getLevel()->getName());
                                $this->saveConfig();
                                $sender->sendMessage(TextFormat::GREEN . "Your Spawn area has been set. PVP and Building in this area has been disabled.");
                                }
                            }
                        }
                    }
                    break;
            }
        }
    
        public function blockbreaking(BlockBreakEvent $event){
            $p = $event->getPlayer();
            $x = $p->getX();
            $y = $p->getY();
            $z = $p->getZ();
            $w = $p->getLevel()->getName();
            $cfg = $this->getConfig()->getAll();
            $x1 = $cfg->get("X1");
            $x2 = $cfg->get("X2");
            $y1 = $cfg->get("Y1");
            $y2 = $cfg->get("Y2");
            $z1 = $cfg->get("Z1");
            $z2 = $cfg->get("Z2");
            $w2 = $cfg->get("SpawnWorld");
            
            if($x > $x1 and $x <= $X2 and $y > $y1 and $y < $y2 and $z > $z1 and $z < $z2 and $w2 = $w){
                $event->setCancelled(true);
                $p->sendMessage("It can't rain all the time.");
            }
        }
        
    }
     
    Last edited: May 3, 2017
  8. Teamblocket

    Teamblocket Zombie

    Messages:
    301
    GitHub:
    teamblocket
    BlockPlaceEvent then
     
  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.