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

Set position don't seem to be working.

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

  1. gsdgamer

    gsdgamer Spider Jockey

    Messages:
    28
    Hello again, I am having some trouble I am creating a SkyBlock plugin. I want to set a protected area so no islands spawn in that area. I set position one, which has to be the lowest point first. Then I set position two which is the higher point. Once I have done this I check to make sure everything is ok, then add the coordinates to a config file. When i go to create the config file I get a error which I set to say that the coordinates aren't correct or that your not in the same world as position one. Here is the code please let me know if you can see if I am doing something wrong, as my brain doesn't want to work with me today. Sometimes it lets me save the config and other times it does not.

    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;
    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":
                            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":
                            if($sender instanceof Player){
                                $level2 = $sender->getLevel()->getName();
                                $maxpos = new Vector3($sender->getX(),$sender->getY(),$sender->getZ());
                                $sender->sendMessage(TextFormat::GREEN . "Position two set.");
                            }
                            break;
                        case "spawn":
                            if($sender instanceof Player){
                                if ($level = $level2 && $minpos < $maxpos){
                                    $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", $level2);
                                    $this->getConfig()->save();
                                    $sender->sendMessage(TextFormat::GREEN . "Your Sky Block spawn has been set.");
                                }else{
                                    $sender->sendMessage(TextFormat::RED . "Error: Pos 1 and 2 must be in the same world. Make sure you set position 1 frst");
                                }
                            }
                            break;
                        case "create":
                            if($sender instanceof Player && $sender->hasPermission("sb.create") || $sender->isOp()){
                                if($sender->getPosition() < $this->getConfig()->get("X1")-200)
                                    $sender->sendMessage("You are 200 something away.");
                            }else{
                                $sender->sendMessage("Go further away");
                            }
                    }
                }
            }
            
        }
        public function onDisable(){
            $this->getLogger()->info(TextFormat::RED . "Sky Blocks has been disabled.");
        }
        
    }
     
  2. XdmingXD

    XdmingXD Baby Zombie

    Messages:
    158
    GitHub:
    xdmingxd
    Comparing a Vector 3 object is a bad idea(or even you can't)
    You should compare the x and z (maybe y if you want)
     
  3. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    Can you send us the actual error?
     
  4. gsdgamer

    gsdgamer Spider Jockey

    Messages:
    28
    Sorry for the delay in replying. I was able to fix this by checking the name of the world on the first two pos1 and 2 command and changed the
    Code:
    if ($level = $level2 && $minpos < $maxpos){
    to
    Code:
    if(min($minpos->x, $minpos->y, $minpos->z) < (max($maxpos->x, $maxpos->y, $maxpos->z)) && $level != null)
     
  5. jasonwynn10

    jasonwynn10 Moderator Poggit Reviewer

    Messages:
    1,489
    GitHub:
    jasonwynn10
    :facepalm::facepalm::facepalm:
    use two equal signs in if statements, not one
    this belongs in the facepalm section
     
    Muqsit, falk and Sandertv like this.
  6. gsdgamer

    gsdgamer Spider Jockey

    Messages:
    28
    Yeah I changed it after realizing what i'd done.
     
  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.