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

How to check on ground

Discussion in 'Development' started by TheStepKla, May 29, 2021.

  1. TheStepKla

    TheStepKla Silverfish

    Messages:
    20
    PHP:
    if(($this->onGround) === true){
                        
    $this->motion->3.5;
    }
    Not work is jump evertime
     
  2. minijaham

    minijaham Skeleton

    Messages:
    801
    GitHub:
    minijaham
    1. You didn't tell us what you're trying to achieve
    2. How exactly is your code not working
     
  3. TheStepKla

    TheStepKla Silverfish

    Messages:
    20
    is my code in modifly Vehicle plugin for jump when is have block in front

    PHP:
    <?php
    /*
     * Vehicles, PocketMine-MP Plugin.
     *
     * Licensed under the Open Software License version 3.0 (OSL-3.0)
     * Copyright (C) 2019-2021 JaxkDev
     *
     * Twitter :: @JaxkDev
     * Discord :: JaxkDev#2698
     * Email   :: [email protected]
     */

    declare(strict_types=1);

    namespace 
    JaxkDev\Vehicles;

    use 
    LogicException;
    use 
    pocketmine\network\mcpe\protocol\MovePlayerPacket;
    use 
    pocketmine\Player;
    use 
    pocketmine\level\Level;
    use 
    pocketmine\nbt\tag\CompoundTag;
    use 
    pocketmine\network\mcpe\protocol\types\EntityLink;
    use 
    pocketmine\utils\UUID;
    use 
    pocketmine\block\Block;
    use 
    pocketmine\math\Vector3;
    use 
    pocketmine\math\VoxelRayTrace;
    use 
    pocketmine\item\Item;

    class 
    Vehicle extends VehicleBase
    {
        
    /** @var Player|null */
        
    private $driver null;

        
    /** @var Player[] */
        
    private $passengers = [];

        
    /**
         * Vehicles constructor.
         * @param Level $level
         * @param CompoundTag $nbt
         */
        
    public function __construct(Level $levelCompoundTag $nbt){
            
    parent::__construct($level$nbt);
            
    $this->setCanSaveWithChunk(true);
            
    $this->saveNBT();
        }

        
    //---------- Logic below... ------------

        /**
         * Handle player input.
         * @param float $x
         * @param float $y
         */
        
    public function updateMotion(float $xfloat $y): void{
            
    //    (1 if only one button, 0.7 if two)
            //+y = forward. (+1/+0.7)
            //-y = backward. (-1/-0.7)
            //+x = left (+1/+0.7)
            //-x = right (-1/-0.7)
            
    if($x !== 0){
                if(
    $x 0$this->yaw -= $x*$this->getVehicleSpeed()["left"];
                if(
    $x 0$this->yaw -= $x*$this->getVehicleSpeed()["right"];
                
    $this->motion $this->getDirectionVector();
            }

            if(
    $y 0){
                
    //forward
                
    $block $this->getTargetBlockss(21);
                
    $blocks $this->getTargetBlockss(10);
                
    $this->motion $this->getDirectionVector()->multiply($y*$this->getVehicleSpeed()["forward"]);
                
    $this->yaw $this->driver->getYaw();// - turn based on players rotation
                
    if(($block) != "AIR"){
                    if((
    $this->onGround) === true){
                        
    $this->motion->3.5;
                    }
                }
            } elseif (
    $y 0){
                
    //reverse
                
    $block $this->getTargetBlockss(21);
                
    $blocks $this->getTargetBlockss(10);
                
    $this->motion $this->getDirectionVector()->multiply($y*$this->getVehicleSpeed()["backward"]);
                
    $isY $this->getY();
                if((
    $block) != "Air"){
                    
    $blockss $this->getLevel()->getBlock($this->subtract(03.50));
                    if((
    $this->onGround) === true){
                        
    $this->motion->=     3.5;
                    }
                }
            }
        }

        protected function 
    broadcastMovement(bool $teleport false) : void{
            
    $pk = new MovePlayerPacket();
            
    $pk->entityRuntimeId $this->getId();
            
    $pk->position $this->getOffsetPosition($this->getPosition());
            
    $pk->pitch $this->getPitch();
            
    $pk->headYaw $this->getYaw();
            
    $pk->yaw $this->getYaw();
            
    $pk->mode MovePlayerPacket::MODE_NORMAL;
            
    $this->getLevel()->broadcastPacketToViewers($this->getPosition(), $pk);
        }
        
        protected function 
    getLineOfSightss(int $maxDistanceint $maxHightint $maxLength 0, array $transparent = []) : array{
            if(
    $maxDistance 120){
                
    $maxDistance 120;
            }

            if(
    count($transparent) === 0){
                
    $transparent null;
            }

            
    $blocks = [];
            
    $nextIndex 0;

            foreach(
    VoxelRayTrace::inDirection($this->add(0$maxHight0), $this->getDirectionVector(), $maxDistance) as $vector3){
                
    $block $this->level->getBlockAt($vector3->x$vector3->y$vector3->z);
                
    $blocks[$nextIndex++] = $block;

                if(
    $maxLength !== and count($blocks) > $maxLength){
                    
    array_shift($blocks);
                    --
    $nextIndex;
                }

                
    $id $block->getId();

                if(
    $transparent === null){
                    if(
    $id !== 0){
                        break;
                    }
                }else{
                    if(!isset(
    $transparent[$id])){
                        break;
                    }
                }
            }

            return 
    $blocks;
        }

        
    /**
         * @param true[] $transparent
         * @phpstan-param array<int, true> $transparent
         */
        
    protected function getTargetBlockss(int $maxDistanceint $maxHight, array $transparent = []) : ?Block{
            
    $line $this->getLineOfSightss($maxDistance$maxHight1$transparent);
            if(
    count($line) > 0){
                return 
    array_shift($line);
            }

            return 
    null;
        }

        public function 
    isVehicleEmpty(): bool{
            return (
    $this->driver === null && count($this->passengers) === 0);
        }

        public function 
    getDriver(): ?Player{
            return 
    $this->driver;
        }

        public function 
    setDriver(Player $playerbool $override false): bool{
            if(
    $this->driver !== null) {
                if(
    $override$this->removeDriver();
                else return 
    false;
            }

            
    $player->setGenericFlag(self::DATA_FLAG_RIDINGtrue);
            
    $player->setGenericFlag(self::DATA_FLAG_SITTINGtrue);
            
    $player->setGenericFlag(self::DATA_FLAG_WASD_CONTROLLEDtrue);
            
    $player->getDataPropertyManager()->setVector3(self::DATA_RIDER_SEAT_POSITION$this->getVehicleDriverSeat());

            
    $this->setGenericFlag(self::DATA_FLAG_SADDLEDtrue);
            
    $this->driver $player;
            
    Main::$inVehicle[$this->driver->getRawUniqueId()] = $this;
            
    $player->sendMessage("You are now driving this vehicle.");
            
    $this->broadcastLink($this->driver);
            
    $player->sendTip("Sneak/Jump to leave the vehicle.");
            return 
    true;
        }

        public function 
    removeDriver(?string $message "You are no longer driving this vehicle."): bool{
            if(
    $this->driver === null) return false;
            
    $this->driver->setGenericFlag(self::DATA_FLAG_RIDINGfalse);
            
    $this->driver->setGenericFlag(self::DATA_FLAG_SITTINGfalse);
            
    $this->driver->setGenericFlag(self::DATA_FLAG_WASD_CONTROLLEDfalse);

            
    $this->setGenericFlag(self::DATA_FLAG_SADDLEDfalse);
            if(
    $message !== null$this->driver->sendMessage($message);
            
    $this->broadcastLink($this->driverEntityLink::TYPE_REMOVE);
            unset(
    Main::$inVehicle[$this->driver->getRawUniqueId()]);
            
    $this->driver null;
            return 
    true;
        }

        
    /**
         * @return Player[]
         */
        
    public function getPassengers(){
            return 
    $this->passengers;
        }

        public function 
    addPassenger(Player $player, ?int $seat nullbool $force false): bool{
            if((
    count($this->getPassengers())) === count($this->getVehiclePassengerSeats()) || isset($this->getPassengers()[$seat])){
                if(
    $force && $seat === null) return false;
                if(!
    $force) return false;
                if(!
    $this->removePassengerBySeat($seat"Your seat has been given to '{$player->getName()}'")) throw new LogicException("Well this is embarrassing... (who knew 1 !== 1)");
            }
            if(
    $seat === null){
                
    $seat $this->getNextPassengerSeat();
                if(
    $seat === null) return false//No space...
            
    }
            
    $this->passengers[$seat] = $player;
            
    Main::$inVehicle[$player->getRawUniqueId()] = $this;
            
    $player->setGenericFlag(self::DATA_FLAG_RIDINGtrue);
            
    $player->setGenericFlag(self::DATA_FLAG_SITTINGtrue);
            
    $player->getDataPropertyManager()->setVector3(self::DATA_RIDER_SEAT_POSITION$this->getVehiclePassengerSeats()[$seat]);
            
    $this->broadcastLink($playerEntityLink::TYPE_PASSENGER);
            
    $player->sendTip("Sneak/Jump to leave the vehicle.");
            return 
    true;
        }

        public function 
    removePassengerBySeat(int $seat, ?string $message null): bool{
            if(isset(
    $this->passengers[$seat])){
                
    $player $this->passengers[$seat];
                unset(
    $this->passengers[$seat]);
                unset(
    Main::$inVehicle[$player->getRawUniqueId()]);
                
    $player->setGenericFlag(self::DATA_FLAG_RIDINGfalse);
                
    $player->setGenericFlag(self::DATA_FLAG_SITTINGfalse);
                
    $this->broadcastLink($playerEntityLink::TYPE_REMOVE);
                if(
    $message !== null$player->sendMessage($message);
                return 
    true;
            }
            return 
    false;
        }

        
    /**
         * @param Player|UUID $player
         * @param string|null $message
         * @return bool
         */
        
    public function removePassenger($player, ?string $message null): bool{
            if(
    $player instanceof Player$player $player->getUniqueId();
            foreach(
    array_keys($this->passengers) as $i){
                if(
    $this->passengers[$i]->getUniqueId() === $player){
                    return 
    $this->removePassengerBySeat($i$message);
                }
            }
            return 
    false;
        }

        public function 
    removePlayer(Player $player): bool{
            if(
    $this->driver !== null){
                if(
    $this->driver->getUniqueId() === $player->getUniqueId()) return $this->removeDriver();
            }
            return 
    $this->removePassenger($player);
        }

        public function 
    getNextPassengerSeat(): ?int{
            
    $max count($this->getVehiclePassengerSeats());
            
    $current count($this->passengers);
            if(
    $max === $current) return null;
            for(
    $i 0$i $max$i++){
                if(!isset(
    $this->passengers[$i])) return $i;
            }
            throw new 
    LogicException("No seat found when max seats doesnt match currently used seats.");
        }
    }
    is jump evertime
     
  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.