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

onRun with setBlock

Discussion in 'Development' started by esh123cookie, Jun 24, 2020.

  1. esh123cookie

    esh123cookie Baby Zombie

    Messages:
    104
    GitHub:
    esh123cookie
    $level = $this->getPlugin()->getServer()->getLevelByName("test");
    $pos = new Position(100, 78, 100, $level);
    $dirt = BlockFactory::get(Block:DIRT);
    $air = BlockFactory::get(Block::AIR);
    if($this->getPlugin()->time > 0) {
    $level->setBlock($pos, $dirt, true, true);
    }
    if($this->getPlugin()->time == 0) {
    $level->setBlock($pos, $air, true, true);
    }
    }

    ^^^^^
    My code keeps giving an error "Call to a member function setBlock() on null" how would I fix this?
     
    Last edited: Jun 25, 2020
  2. PJZ9n

    PJZ9n Silverfish

    Messages:
    17
    GitHub:
    pjz9n
    What is onRun? Anyway, $level is assigned a null instead of a Level object. That means I couldn't get the world. You can either make sure the world name is correct or you can use the Server::loadLevel() function I will.
     
  3. esh123cookie

    esh123cookie Baby Zombie

    Messages:
    104
    GitHub:
    esh123cookie
    public function onRun($tick) {
    It is a function.
    Second I get that the code cannot find the world. How do I make it so the code can register it as NonNull?
     
  4. PJZ9n

    PJZ9n Silverfish

    Messages:
    17
    GitHub:
    pjz9n
    The function name can be freely decided anywhere. I guess I'm using Task, but I'm not sure. Writing it in the question is a better way.
    The solution to this problem is general error handling. You can see that getLevelByName returns Level or null (?Level).
    GitHub(Server.php)
    Check the return value is Level.
    PHP:
    $level $this->getPlugin()->getServer()->getLevelByName("test");
    if (!(
    $level instanceof Level)) {
      
    //error handling...
    }
     
  5. esh123cookie

    esh123cookie Baby Zombie

    Messages:
    104
    GitHub:
    esh123cookie
    Okay thank you, let me try that out
     
  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.