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

X Y Z not working as it should.

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

  1. gsdgamer

    gsdgamer Spider Jockey

    Messages:
    28
    Hi all again. I am creating a Skyblock plugin. I have set my minX and maxX position in my config file. When I try to check if the player is in inbetween x1 and x2 it tells me my player is out of the area, even though I am in it. my code to check this is:
    Code:
    case "create":
                            if($sender instanceof Player)
                            {
                                if($sender->getPosition()->getX() > $this->getConfig()->get("X1") and $sender->getPosition()->getX() < $this->getConfig()->get("X2"))
                                {
                                    $sender->sendMessage("Your in the area.");
                                }else{
                                    $sender->sendMessage("You are out of the area.");
                                }
                            }
    My config code is:
    Code:
    ---
    World: Lobby
    X1: -11.353900
    X2: -115.711800
    Y1: 61.288300
    Y2: 122.038300
    Z1: 86.709800
    Z2: -30.057300
    ...
    
     
  2. Intyre

    Intyre Administrator Staff Member PMMP Team

    Messages:
    81
    GitHub:
    Intyre
    PHP:
    $playerX = -31.232983;
    $x1 = -11.353900;
    $x2 = -115.711800;
    if(
    $playerX > - $x1 and $playerX < -$x2){
        echo 
    "IM IN!";
    }else{
      echo 
    "Nah";
    }
     
  3. gsdgamer

    gsdgamer Spider Jockey

    Messages:
    28
    I could do that but when I put it onto my server I will have different coordinates and I wanted the switch to get them from the config file.
     
  4. Intyre

    Intyre Administrator Staff Member PMMP Team

    Messages:
    81
    GitHub:
    Intyre
    I guess that wasn't clear enough.
    PHP:
    // to make it easier
    $x = -12;
    $x1 = -11;
    $x2 = -115;
    if(
    $x $x1 and $x $x2){
      echo 
    "1 ";
    }
    if(
    $x $x1){
      echo 
    "2 ";
    }
    if(
    $x $x2){
      echo 
    "3 ";
    }
    Which message(s) get printed?
    PHP:
    $x = -12;
    // correct check (for negative numbers only!)
    if( $x $x1 and  $x $x2){
      echo 
    "4 ";
    }
     
    Last edited: May 9, 2017
  5. gsdgamer

    gsdgamer Spider Jockey

    Messages:
    28
    Sorry about that my battery went on laptop. Nothing is getting printed.
     
  6. Intyre

    Intyre Administrator Staff Member PMMP Team

    Messages:
    81
    GitHub:
    Intyre
    PHP:
    $x = -12;
    $x1 = -11;
    $x2 = -115;
    if(
    $x $x1 and $x $x2){
      echo 
    "1 ";
    }
    if(
    $x $x1){
      echo 
    "2 ";
    }
    if(
    $x $x2){
      echo 
    "3 ";
    }
    if(
    $x $x1){
      echo 
    "4 ";
    }
    if(
    $x $x2){
      echo 
    "5 ";
    }

    if( 
    $x $x1 and  $x $x2){
      echo 
    "Win!";
    }
    dubble checked and it should write `4 5 Win!` oops :oops:
     
  7. gsdgamer

    gsdgamer Spider Jockey

    Messages:
    28
    Yeah it does.
     
  8. BouncyJeffer

    BouncyJeffer Spider Jockey

    Messages:
    39
    GitHub:
    bouncyjeffer
    $sender->getX();
    $sender->getY();
    $sender->getZ();

    Will work.
     
  9. gsdgamer

    gsdgamer Spider Jockey

    Messages:
    28
    I've tried it but for some reason it just doesn't want to work. I don't know if i'm doing something daft and not noticing, which is usually the case lol.
     
  10. BouncyJeffer

    BouncyJeffer Spider Jockey

    Messages:
    39
    GitHub:
    bouncyjeffer
    Could you show us what you're importing?
     
  11. gsdgamer

    gsdgamer Spider Jockey

    Messages:
    28
    Ok this is the Main file:
    Code:
    <?php
    
    namespace SkyBlocks;
    
    use pocketmine\plugin\PluginBase;
    use pocketmine\event\Listener;
    use pocketmine\utils\TextFormat;
    use pocketmine\command\Command;
    use pocketmine\command\CommandExecutor;
    use pocketmine\command\CommandSender;
    use pocketmine\Player;
    use pocketmine\math\Vector3;
    use pocketmine\utils\Config;
    use pocketmine\level\Position;
    class Main extends PluginBase implements Listener, CommandExecutor{
        
        
        public function onEnable(){
            
            if(!file_exists($this->getDataFolder() . "config.yml")){
                @mkdir($this->getDataFolder());
                $this->saveResource("config.yml");
                $this->myConfig = new Config($this->getDataFolder() . "config.yml", Config::YAML);
            }
            $this->getServer()->getPluginManager()->registerEvents($this, $this);
            $this->getLogger()->info(TextFormat::GREEN . "Sky Blocks has been enabled.");
            
        }
        
        
        
        
        public function onCommand(CommandSender $sender, Command $cmd, $label, array $args)
        {
            
            
            static $level;
            static $level2;
            static $minpos;
            static $maxpos;
            
        
            if(strtolower($cmd->getName() == "sb")){
                if(!(isset($args[0]))){
                    $sender->sendMessage(TextFormat::RED . "Use /sb help for more info.");
                }elseif(isset($args)){
                    switch ($args[0]){
                        case "pos1": //This sets position one x,y,z
                            if($sender instanceof Player){
                                $level = $sender->getLevel()->getName();
                                $minpos = new Vector3($sender->getX(),$sender->getY(),$sender->getZ());
                                $sender->sendMessage(TextFormat::GREEN . "Position one done.");
                                
                            }
                            break;
                        case "pos2": //This sets position two x,y,z
                            if($sender instanceof Player){
                                if($level == null){
                                    $sender->sendMessage("Please set position one first.");
                                }elseif ($level == $sender->getLevel()->getName()){   
                                $maxpos = new Vector3($sender->getX(),$sender->getY(),$sender->getZ());
                                $sender->sendMessage(TextFormat::GREEN . "Position two set.");
                                }else{
                                    $sender->sendMessage(TextFormat::RED . "Pos 2 isn't in the same world as pos 1.");
                                }
                            }
                            break;
                        case "spawn":
                            if($sender instanceof Player){
                                if(min($minpos->x, $minpos->y, $minpos->z) < (max($maxpos->x, $maxpos->y, $maxpos->z)) && $level !== null)
                                {
                                    $this->getConfig()->set("X1" , $minpos->x);
                                    $this->getConfig()->set("Y1" , $minpos->y);
                                    $this->getConfig()->set("Z1" , $minpos->z);
                                    $this->getConfig()->set("X2" , $maxpos->x);
                                    $this->getConfig()->set("Y2" , $maxpos->y);
                                    $this->getConfig()->set("Z2" , $maxpos->z);
                                    $this->getConfig()->set("World", $level);
                                    $this->getConfig()->save();
                                    $pos=$this->getConfig()->get("X1", "Y1", "Z1", "X2", "Y2", "Z2");
                                    $sender->sendMessage(TextFormat::GREEN . "Your Sky Block spawn has been set.");
                                }else{
                                    $sender->sendMessage(TextFormat::RED . "Error: Please ensure pos 1 and 2 have been set.");
                                }
                            }
                            break;
                        case "create":
                            if($sender instanceof Player)
                            {
                                
                                if($sender->getX() >= $this->getConfig()->get("X1"))
                                {
                                    //This is just a test message to see if it registers me in the area
                                    $sender->sendMessage("You are in the area");
                                }
                                else{
                                    $sender->sendMessage("You are out of the area");
                                }
                            }
                        }
                    }
                }
            }
        public function onDisable(){
            $this->getLogger()->info(TextFormat::RED . "Sky Blocks has been disabled.");
        }
        
    }
    The config file that I have set up is:
    Code:
    ---
    World: Lobby
    X1: -12.251100
    X2: -114.653700
    Y1: 64.288300
    Y2: 117.538300
    Z1: 88.525500
    Z2: -28.500200
    ...
    
    The X,Y,Z are what I already set with pos1 and pos2.
     
  12. Thunder33345

    Thunder33345 Moderator Staff Member

    Messages:
    2,137
    GitHub:
    Thunder33345
    no no no no no
    it wouldnt work if console sent that command
     
  13. gsdgamer

    gsdgamer Spider Jockey

    Messages:
    28
    The command would just be used by the player I need to put the code to check if it's just the player.
     
  14. BouncyJeffer

    BouncyJeffer Spider Jockey

    Messages:
    39
    GitHub:
    bouncyjeffer
    if($sender instanceof Player){
    // is a player
    } else {
    // not a player.
    }
     
  15. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    Yeah because X1 !< X2. Same for Z1 and Z2.
    It should've been:
    Code:
    ---
    World: Lobby
    X1: -114.653700
    X2: -12.251100
    Y1: 64.288300
    Y2: 117.538300
    Z1: -28.500200
    Z2: 88.525500
    ...
    
     
    corytortoise likes this.
  16. gsdgamer

    gsdgamer Spider Jockey

    Messages:
    28
    What I want is to check the players current x pos and check if they are in the area of the config file positions.
     
  17. Irish

    Irish Baby Zombie

    Messages:
    156
    GitHub:
    irishpacks
    I'd say to use the min and max functions.

    Documentation for min(): http://php.net/manual/en/function.min.php
    Documentation for max(): http://php.net/manual/en/function.max.php

    i.e.
    PHP:
    $x1 = -12;
    $x2 = -45;

    $xMin min($x1$x2);
    $xMax max($x1$x2);

    //assuming $sender is an instance of a Player
    if($sender->getX() <= $xMin){
        echo 
    "HELLO!";
    } elseif(
    $sender->getX() >= $xMax){
        echo 
    "WORLD";
    } else {
        echo 
    "I COME IN PEACE!";
    }
     
  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.