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

Solved listing homes from config

Discussion in 'Development' started by Teamblocket, Jun 9, 2017.

  1. Teamblocket

    Teamblocket Zombie

    Messages:
    301
    GitHub:
    teamblocket
    hello so am trying to code a home plugin into my core and i cant get the /home , and then a list of homes show up , work :{ if someone can help me please do

    HomeCommand.php
    PHP:
    public function execute(CommandSender $sender$commandLabel, array $args) {
            if(isset(
    $args[0])){
                
    $homes = new Config($this->plugin->getDataFolder()."Homes/".$sender->getName().".yml"CONFIG::YAML, array());
                
    $home $homes->get($args[0]);
                if(
    $home["world"] instanceof Level){
                    
    $sender->setLevel($home["world"]);
                    
    $sender->teleport(new Vector3($home["x"], $home["y"], $home["z"]));
                    
    $sender->sendMessage($this->plugin->prefix "Teleporting to home ".$args[0].".");
                } else {
                    
    $sender->sendMessage($this->plugin->prefix "This home doesnt exists!");
                    }
            } else {
                
    $homes = new Config($this->plugin->getDataFolder()."Homes/".$sender->getName().".yml"CONFIG::YAML, array());
                foreach(
    $homes as $listed){
                    
    $sender->sendMessage($this->plugin->prefix "Listed Homes: " $listed ".");
                }
            }
        }
    }
    this works just fine i just get this error,
    Code:
    08.06 18:32:28 [Server] INFO Notice: Array to string conversion in /plugins/VortexCore/src/TB/VortexCore/commands/HomeCommand.php on line 39
     
  2. corytortoise

    corytortoise Skeleton

    Messages:
    825
    GitHub:
    corytortoise
    Can we see what the config looks like, and how you are adding homes to the config?
     
  3. Sandertv

    Sandertv Zombie Pigman Poggit Reviewer

    Messages:
    786
    GitHub:
    Sandertv
    :facepalm:
    For the second time, you can't store a level object directly in a file. That means $home["world"] will NEVER be an instanceof Level, because it's a string (I assume/hope) you put in. There's also no point in constructing a new config object for every single action you do with it. Could you point out where line 39 is?
     
    jasonwynn10 likes this.
  4. WinterBuild7074

    WinterBuild7074 Zombie Pigman

    Messages:
    693
    GitHub:
    winterbuild7074
    Maybe
    PHP:
    $level $this->getLevel($home["world"]); // now an instance of Level
    $sender->setLevel($level);
    ?
     
  5. Sandertv

    Sandertv Zombie Pigman Poggit Reviewer

    Messages:
    786
    GitHub:
    Sandertv
    No-o. You can only use getLevel() if you have a level ID, which you do not. Instead use $server->getLevelByName($name) as I mentioned before.
     
  6. Keith

    Keith Spider Jockey

    Messages:
    42
    GitHub:
    k3ithos
    PHP:
    public function execute (CommandSender $sender$commandLabel, array $args) {
            
    $homes = new Config($this->plugin->getDataFolder() . "Homes/" $sender->getName() . ".yml"Config::YAML);
            if (isset(
    $args[0])) {
                
    $home $homes->get($args[0]);
                if (
    $this->plugin->getServer()->getLevelByName($home["world"]) instanceof Level) {
                    
    $sender->teleport(new Position($home["x"], $home["y"], $home["z"], $home["world"]));
                    
    $sender->sendMessage($this->plugin->prefix "Teleporting to home " $args[0] . ".");
                } else {
                    
    $sender->sendMessage($this->plugin->prefix "This home doesn't exist!");
                }
            } else {
                
    $listedHomes = array();
                foreach (
    $homes->getAll() as $name => $values) {
                    
    $listedHomes[] = $name;
                }
                
    $sender->sendMessage($this->plugin->prefix "Your homes: " implode(", "$listedHomes));
            }
        }
    }
     
    corytortoise and Sandertv like this.
  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.