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

Help please! Why are player.block.touch and setBlockRaw() so easy to have bugs?

Comments in 'Plugin Development' started by PEMapModder, Nov 29, 2013.

  1. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,325
    Plugins:
    11
    Minecraft User:
    PEMapModder
    What is the problem of the code?
    PHP:
            $this->api->addHandler("player.block.place",array($this,"blockPlace"));
    #.....................................
        
    public function blockPlace(&$data,$event){
            
    $player=$data["player"];
            
    $level=$player->entity->level;
            if (
    $level->getName()==="lobby2" and $this->syncOn($player->username)===true) {
                
    $oB=$data["target"];
                
    $vT=new Vector3($oB->x,$oB->y,$oB->z);
                
    $block=BlockAPI::get($oB->getID());
                
    $vX=new Vector3(255-$oB->x,$oB->y,$oB->z);
                
    $vXZ=new Vector3($oB->x,$oB->y,255-$oB->z);
                
    $vZ=new Vector3(255-$oB->x,$oB->y,255-$oB->z);
                
    $level->setBlockRaw($vX,$block);
                
    $level->setBlockRaw($vXZ,$block);
                
    $level->setBlockRaw($vZ,$block);
            }
        }
    However another function in the script works excellently:
    init():
    PHP:
            $this->api->addHandler("player.block.touch",array($this,"blockTouch"));
    #.....................
    public function blockTouch(&$data){
            
    $player=$this->api->player->get($data["player"]);
            
    $target=$data["target"];
            
    $tX=$target->x;
            
    $tY=$target->y;
            
    $tZ=$target->z;
            
    $tLevel=$player->entity->level->getName();
            
    $tId=$target->getID();
            if(
    $data["type"]=="break"){
            if(
    $tLevel==="lobby2" and $tLevel!="lobby"){#just to ensure things
                
    if($data["type"]=="break"){
                    
    $this->editBlock($tX,$tY,$tZ,$tLevel,$tId,$player);
    #                return;
                
    }
            }
            return;
            }
    PHP:
    public function editBlock($x,$y,$z,$level,$id=0,$player){
            if(
    $this->syncOn($player->username)===true){
                
    $level=$player->entity->level;
                
    $vT=new Vector3($x,$y,$z);
                
    $vX=new Vector3(255-$x,$y,$z);
                
    $vXZ=new Vector3(255-$x,$y,255-$z);
                
    $vZ=new Vector3($x,$y,255-$z);
                
    $air=BlockAPI::get(0);
                
    $level->setBlockRaw($vX,$air);
                
    $level->setBlockRaw($vXZ,$air);
                
    $level->setBlockRaw($vZ,$air);
            }
        }
    Please help! Just need a bit more help!
  2. wies
    Offline

    wies Notable Member

    Joined:
    Aug 23, 2013
    Posts:
    392
    Can you give the full code so I can see what $this->syncOn($player->username) does?
    And remove the & so method blockTouch will look like this: public function blockTouch($data){
  3. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,325
    Plugins:
    11
    Minecraft User:
    PEMapModder
    In fact i just saw ppl do &$data. Idk what is the meaning of the &.
  4. wies
    Offline

    wies Notable Member

    Joined:
    Aug 23, 2013
    Posts:
    392
    This simple php code will output "hello world!" :
    PHP:
    function changemsg($data){
      
    $data "good bye!";
    }

    $msg "hello world!";
    changemsg($msg);
    echo 
    $msg;
    But this code will output "good bye!":
    PHP:
    function changemsg(&$data){
      
    $data "good bye!";
    }

    $msg "hello world!";
    changemsg($msg);
    echo 
    $msg;
    & will make the $data variable the same variable as $msg , so if you change $data then $msg will change too.

    In player.block.touch it doesn't matter because the dhandler will returns an new array so it's a new variable, but their are cases where the & operator can be dangerous
    Last edited: Nov 30, 2013
    PEMapModder likes this.
  5. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,325
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Full code:
    Code:
    <?php
    /*
    __PocketMine Plugin__
    apiversion=10
    author=PEMapModder
    name=CNR Plugin alpha
    description=Capture Nether Reactor plugin
    version=0.0.0
    class=CNR
    */
    class CNR implements Plugin{
        private $api,$dir,$syncs=array();
        public function __construct(ServerAPI $api,$server=false) {
            $this->api=$api;
        }
        public function __destruct(){
        }
        public function init() {
            $this->syncs["PEMapModder"]=true;
            $this->dir=$this->api->plugin->configPath($this);
            $this->config=new Config($this->dir."MembersProfiles.yml",CONFIG_YAML,array(
    #deleted/hidden
            ));
            $this->api->console->register("getPos","getPos",array($this,"getPos"));
            $this->api->ban->cmdWhitelist("getPos");
            $this->api->console->register("sync","sync quarters",array($this,"toggleSync"));
            $this->api->addHandler("player.block.touch",array($this,"blockTouch"));
            $this->api->addHandler("player.block.place",array($this,"blockPlace"));
    #        $this->api->addHandler("player.join",array($this,"playerJoin"));
    #        $this->api->addHandler("tile.update",array($this,"tileUpdate"));
        }
        public function getPos($cmd,$args,$issuer){
            if(!($issuer instanceof Player)){
                return "Only usable by players.";
            }
            $issuer->sendChat("X: ".$issuer->data->get("position")["x"]." Y: ".$issuer->data->get("position")["y"]." Z: ".$issuer->data->get("position")["x"]);
        }
        public function toggleSync($c,$p,$player){
            if(sizeof($p)!==1 or !($player instanceof Player)){
                return "[ERROR] Wrong usage of /$c";
            }if ($p[0]==="on") {
                $this->syncs[$player->username]=true;
            }elseif ($p[0]==="off") {
                $this->syncs[$player->username]=false;
            }else{
                $player->sendChat("[ERROR] Wrong usage of /$c");
            }
        }
        public function blockTouch(&$data){
            $player=$this->api->player->get($data["player"]);
            $target=$data["target"];
            $tX=$target->x;
            $tY=$target->y;
            $tZ=$target->z;
            $tLevel=$player->entity->level->getName();
            $tId=$target->getID();
            if($data["type"]=="break"){
            if($tLevel==="lobby2" and $tLevel!="lobby"){#just to ensure things
                if($data["type"]=="break"){
                    $this->editBlock($tX,$tY,$tZ,$tLevel,$tId,$player);
    #                return;
                }
            }
            return;
            }
            if($this->api->ban->isOp($player->username)){
                $player->sendChat("You tapped block at w:".$tLevel."(".$tX.", ".$tY.", ".$tZ.").");
            }
            if($tId==323 or $tId==63 or $tId==68){
                # next
                if($tX==135 and $tY==57 and $tZ==125){
                    $this->api->console->run("tp ".$player->username." 135 56 128");return false;
                }
                if($tX==135 and $tY==57 and $tZ==131){
                    $this->api->console->run("tp ".$player->username." 135 55 134");return false;
                }
                if($tX==135 and $tY==56 and $tZ==134){
                    $this->api->console->run("tp ".$player->username." 135 63 134");return false;
                }
                if($tX==135 and $tY==64 and $tZ==134){
                    $this->api->console->run("tp ".$player->username." 135 64 131");return false;
                }
                if($tX==135 and $tY==65 and $tZ==131){
                    $this->api->console->run("tp ".$player->username." 135 63 128");return false;
                }
                if($tX==135 and $tY==64 and $tZ==128){
                    $this->api->console->run("tp ".$player->username." 135 63 125");return false;
                }
                if($tX==135 and $tY==64 and $tZ==125){
                    $this->api->console->run("tp ".$player->username." 135 71 133");return false;
                }
                if($tX==135 and $tY==72 and $tZ==134){
                    $this->api->console->run("tp ".$player->username." 135 71 130");return false;
                }
                if($tX==136 and $tY==56 and $tZ==125){
                    $this->api->console->run("tp ".$player->username." 125.5 55 128.5");return false;
                }
                if(($tX==127 and $tY==56 and $tZ==127) or (($tX==128 or $tX==129)) and $tZ==127){
                    $this->api->console->run("tp ".$player->username." 135 56 125");return false;
                }
                # previous
                if($tX==135 and $tY==57 and $tZ==124){
                    $player->sendChat(FORMAT_RED."N/A");return false;
                }
            }
        }
        public function playerJoin($player){
            $this->syncs[$player->username]=false;
        }
        public function editBlock($x,$y,$z,$level,$id=0,$player){
            if($this->syncOn($player->username)===true){
                $level=$player->entity->level;
                $vT=new Vector3($x,$y,$z);
                $vX=new Vector3(255-$x,$y,$z);
                $vXZ=new Vector3(255-$x,$y,255-$z);
                $vZ=new Vector3($x,$y,255-$z);
                $air=BlockAPI::get(0);
                $level->setBlockRaw($vX,$air);
                $level->setBlockRaw($vXZ,$air);
                $level->setBlockRaw($vZ,$air);
            }
        }
        public function blockPlace(&$data,$event){
            $player=$data["player"];
            $level=$player->entity->level;
            if ($level->getName()==="lobby2" and $this->syncOn($player->username)===true) {
                $oB=$data["target"];
                $vT=new Vector3($oB->x,$oB->y,$oB->z);
                $block=BlockAPI::get($oB->getID());
                $vX=new Vector3(255-$oB->x,$oB->y,$oB->z);
                $vXZ=new Vector3($oB->x,$oB->y,255-$oB->z);
                $vZ=new Vector3(255-$oB->x,$oB->y,255-$oB->z);
                $level->setBlockRaw($vX,$block);
                $level->setBlockRaw($vXZ,$block);
                $level->setBlockRaw($vZ,$block);
            }
        }
      
        public function syncOn($ign){
            return $this->syncs[$ign];
        }
    }
    
    The ConsoleAPI::run() things in the middle is the main reason I didn't give the full code in the first place.
  6. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,325
    Plugins:
    11
    Minecraft User:
    PEMapModder
    BTW I know Player::sendChat(FORMAT_RED."blah blah") won't send red presently.
  7. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,325
    Plugins:
    11
    Minecraft User:
    PEMapModder
    What is the problem?

Share This Page

Advertisement