Advertisement
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Solved teleport to open game [HG]

Comments in 'Plugin Development' started by xBeastMode, Dec 24, 2015.

  1. xBeastMode
    Offline

    xBeastMode Active Member

    Joined:
    Nov 27, 2015
    Posts:
    292
    Hello, I'm working on a hg plugin for my server and I need someone to check if this function would work. The function basically teleports a player to the game it finds from the config. I don't think it will work but I need some feedback from someone to tell me how it could work.
    PHP:
    public static function tpToOpenGame(Player $p$gamename)
        {
            
    $available true;
            foreach(
    self::$main->getConfig()->getAll()["hg_games"][$gamename]["slots"] as $s)
            {
                if (
    $s["status"] !== "running") {
                    
    $p->teleport(new Position($s["x"], $s["y"], $s["z"], $s["level"]), $p->getYaw(), $p->getPitch());
                    
    $s["status"] = "running";
                    
    self::$main->getConfig()->setAll(self::$main->getConfig()->getAll());
                    
    self::$main->getConfig()->save();
                    
    $available false;
                }
                else{
                    
    $available false;
                }
                if(!
    $available) break;
            }
        }
    Last edited: Dec 24, 2015
  2. wolfdale
    Offline

    wolfdale Active Member Plugin Developer

    Joined:
    Dec 3, 2014
    Posts:
    276
    Plugins:
    1
    Minecraft User:
    ace
    Why do you think it wont work?
    PHP:
    public static function tpToOpenGame(Player $p$gamename)
        {
            foreach(
    self::$main->getConfig()->getAll()["hg_games"][$gamename]["slots"] as $s)
            {
                if (
    $s["status"] !== "running") {
                    
    $p->teleport(new Position($s["x"], $s["y"], $s["z"], $s["level"]), $p->getYaw(), $p->getPitch());
                    
    $s["status"] = "running";//you set the game to running when only 1 player joined? This will prevent other players from joining
                    
    self::$main->getConfig()->setAll(self::$main->getConfig()->getAll());
                    
    self::$main->getConfig()->save();
                    break;
    //stop loop when successfully teleported
                
    }
            }
        }
  3. xBeastMode
    Offline

    xBeastMode Active Member

    Joined:
    Nov 27, 2015
    Posts:
    292
    PHP:
    private static $waiting = [];
    public static function 
    tpToOpenGame(Player $p$gamename)
        {
            foreach(
    self::$main->getConfig()->getAll()["hg_games"][$gamename]["slots"] as $s)
            {
                if (
    $s["status"] !== "running") {
                    
    $p->teleport(new Position($s["x"], $s["y"], $s["z"], $s["level"]), $p->getYaw(), $p->getPitch());
                    if(!isset(
    self::$waiting[$gamename])
                     {
                       
    self::$waiting[$gamename] = 0;
                       }             
    self::$waiting[$gamename] += 1;
    if(
    self::$waiting[$gamename] == self::getMinPlayers())
    {
    //todo: iniciate waiting task, once done task is done, set status to running and unset $gamename from waiting
    }
                    break;
                }
            }
        }
  4. xBeastMode
    Offline

    xBeastMode Active Member

    Joined:
    Nov 27, 2015
    Posts:
    292
    Here's my new method, the only thing is that it's saying "undefined offset: 1"
    PHP:
    public static function tpToOpenSlot(Player $p)
        {
            
    self::$count count(self::$main->getConfig()->getAll()["hg_games"][self::$game]["slots"])-1;
            
    $slot self::$main->getConfig()->getAll()["hg_games"][self::$game]["slots"][self::$count];//undefined offset here
            
    $p->teleport(new Position($slot["x"], $slot["y"], $slot["z"], $slot["level"]), $p->getYaw(), $p->getPitch());
            
    self::$count -= 1;
            if(!isset(
    self::$onWait[self::$game]))
            {
                
    self::$onWait[self::$game] = 0;
            }
            
    self::$onWait[self::$game] += 1;
            if(
    self::$count 0)
            {
                
    $p->sendMessage(color::RED "Sorry, no slots available for game '" self::$game."'.");
            }
            if(
    self::isGameRunning())
            {
                
    $p->sendMessage(color::RED "The game '".self::$game "' is already running.");
            }
        }
    Edit: I made a var_dump and I don't see why it doesn't work.
    array(2) {
    'slot1' =>
    array(4) {
    'x' =>
    int(99)
    [1] =>
    int(99)
    'z' =>
    int(99)
    'level' =>
    string(5) "world"
    }
    'slot2' =>
    array(4) {
    'x' =>
    int(99)
    [1] =>
    int(99)
    'z' =>
    int(99)
    'level' =>
    string(5) "world"
    }
    }
    Last edited: Dec 25, 2015
  5. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Wait, self::$main?
  6. xBeastMode
    Offline

    xBeastMode Active Member

    Joined:
    Nov 27, 2015
    Posts:
    292
    xD it's the main file(Loader.php). Why?
  7. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Avoid depending on objects stored in static fields. Bad practice. Same reason why not to use Server::getInstance().
  8. xBeastMode
    Offline

    xBeastMode Active Member

    Joined:
    Nov 27, 2015
    Posts:
    292
    Thanks for the suggestion, but still, can you tell me how I can make this function work successfully?
  9. wolfdale
    Offline

    wolfdale Active Member Plugin Developer

    Joined:
    Dec 3, 2014
    Posts:
    276
    Plugins:
    1
    Minecraft User:
    ace
    In your config, how is your "slots" part stored? Is it like
    slots
    - test
    - test2
  10. xBeastMode
    Offline

    xBeastMode Active Member

    Joined:
    Nov 27, 2015
    Posts:
    292
    ---
    #HG config by xBeastMode
    hg_games:
    test:
    running: false
    waiting: true
    min_players: 2
    max_players: 6
    game_time: 300
    wait_time: 60
    slots:
    slot1:
    x: 99
    y: 99
    z: 99
    level: world
    slot2:
    x: 99
    y: 99
    z: 99
    level: world
  11. Primus
    Offline

    Primus Notable Member

    Joined:
    Apr 7, 2015
    Posts:
    1,470
    Minecraft User:
    PrimusLV
    [ CODE][/ CODE]
    AndrewBit and HotFireyDeath like this.
  12. xBeastMode
    Offline

    xBeastMode Active Member

    Joined:
    Nov 27, 2015
    Posts:
    292
    ...
  13. wolfdale
    Offline

    wolfdale Active Member Plugin Developer

    Joined:
    Dec 3, 2014
    Posts:
    276
    Plugins:
    1
    Minecraft User:
    ace
    PHP:
    $slot self::$main->getConfig()->getAll()["hg_games"][self::$game]["slots"][self::$count];
    should be
    PHP:
    $slot self::$main->getConfig()->getAll()["hg_games"][self::$game]["slots"]["slot".self::$count];
  14. xBeastMode
    Offline

    xBeastMode Active Member

    Joined:
    Nov 27, 2015
    Posts:
    292
    PHP:
    echo $this->getConfig()->getAll()["hg_games"]["test"]["slots"][$slot]["x"];
    and
    PHP:
    echo $this->getConfig()->getAll()["hg_games"]["test"]["slots"][$slot]["z"];
    echo the correct thing(99) but
    PHP:
    echo $this->getConfig()->getAll()["hg_games"]["test"]["slots"][$slot]["y"];
    sends
    "Undefined index: y"
  15. wolfdale
    Offline

    wolfdale Active Member Plugin Developer

    Joined:
    Dec 3, 2014
    Posts:
    276
    Plugins:
    1
    Minecraft User:
    ace
    PHP:
    var_dump($this->getConfig()->getAll()["hg_games"]["test"]["slots"][$slot]);
    The problem seems to be with the config, what does this show?
  16. xBeastMode
    Offline

    xBeastMode Active Member

    Joined:
    Nov 27, 2015
    Posts:
    292
    o.o
    array(4) {
    'x' =>
    int(99)
    [1] =>
    int(99)
    'z' =>
    int(99)
    'level' =>
    string(5) "world"
    }
  17. xBeastMode
    Offline

    xBeastMode Active Member

    Joined:
    Nov 27, 2015
    Posts:
    292
    It workedddddd!! Thanks so much for your help wolfdale. :)
    $p->teleport(new Position($slot[0], $slot[1], $slot[2], self::$main->getServer()->getLevelByName($slot["level"]), $p->getYaw(), $p->getPitch());

Share This Page

Advertisement