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

Save a random player name from a specific map into a array.

Comments in 'Plugin Development' started by AndrewBit, Jan 1, 2016.

  1. AndrewBit
    Offline

    AndrewBit Notable Member

    Joined:
    Jun 18, 2015
    Posts:
    435
    Minecraft User:
    AndrewBit4
    I'm really confused with this, I'm trying to do a gamemode, but I get constantly errors.
    Right now I use:
    PHP:
        public $player = [];

        public function 
    setBeast($game)
        {
            
    $server $this->getServer();
            
    $players $server->getLevelByName($game)->getPlayers();
            
    $this->beast[$game] = strtolower($players[array_rand($players)]->getName());
        }

        public function 
    getBeast($game)
        {
            return 
    $this->beast[$game];
        }
    But, when I try to check if player is the random selected player I get errors telling me that the array is undefined :/
    Example:
    PHP:
    public function onPvP(EntityDamageEvent $eventPvP)
        {
            if (
    $eventPvP instanceof EntityDamageByEntityEvent) {
                
    $entity $eventPvP->getEntity();
                
    $damager $eventPvP->getDamager();
                
    $games = ["RB-1""RB-2""RB-3""RB-4""RB-5""RB-6""RB-7""RB-8""RB-9""RB-10"];
                if (
    $entity instanceof Player && $damager instanceof Player) {
                    if (
    $eventPvP->getEntity()->getLevel()->getName() == "ZeSpawn") {
                        
    $eventPvP->setCancelled(true);
                    }
                    else{
                    foreach (
    $games as $game) {
                        if (
    strtolower($entity->getName()) != $this->beast[$game] and strtolower($damager->getName()) != $this->beast[$game] or $this->pvpEnabled[$game] == false) {
                            
    $eventPvP->setCancelled(true);
                        }
                    }
                    }
                }
            }
        }
    Also in my timer! - Large file in 3...2...1
    PHP:
    class TimerTask extends PluginTask{
        public function 
    __construct(Main $plugin$game){
            
    $this->plugin $plugin;
            
    $this->game $game;
            
    parent::__construct($plugin);
        }
        public function 
    onRun($tick){
            
    $this->plugin->gameSec[$this->game] -= 1;
            
    $level $this->plugin->getServer()->getLevelByName($this->game);
            
    $players $level->getPlayers();
            if(
    $this->plugin->gameSec[$this->game] <= 410 and $this->plugin->gameSec[$this->game] >= 396){
                foreach(
    $players as $p){
                    
    $seconds $this->plugin->gameSec[$this->game] - 395;
                    
    $p->sendPopup(TextFormat::WHITE TextFormat::BOLD "Game will start in " $seconds);
                }
            }
            elseif(
    $this->plugin->gameSec[$this->game] <= 394 and $this->plugin->gameSec[$this->game] >= 380){
                foreach(
    $players as $p){
                    
    $seconds $this->plugin->gameSec[$this->game] - 379;
                    
    $p->sendPopup(TextFormat::WHITE TextFormat::BOLD "Beast will be free in " $seconds);
                }
            }
            elseif(
    $this->plugin->gameSec[$this->game] == 390){
                
    $this->plugin->setBeast($this->game);
            }
            elseif(
    $this->plugin->gameSec[$this->game] <= 378){
                foreach(
    $players as $p){
                    
    $p->sendPopup("Game will end in " $this->plugin->gameSec[$this->game] . " seconds");
                }
            }
            switch(
    $this->plugin->gameSec[$this->game]){
                case 
    395:
                    foreach(
    $players as $p) {
                        
    $p->sendTip("Game started");
                        
    $this->plugin->pvpEnabled[$this->game] = true;
                        if (
    $this->plugin->getBeast($this->game) == strtolower($p->getName())) {
                            
    $p->teleport(new Position($this->plugin->beastPosition[0],$this->plugin->beastPosition[1],$this->plugin->beastPosition[2],$this->plugin->getServer()->getLevelByName($this->game)));
                            
    $this->plugin->addKit($p);
                        }
                        else{
                            
    $p->teleport(new Position($this->plugin->startPointPosition[0], $this->plugin->startPointPosition[1], $this->plugin->startPointPosition[2], $this->plugin->getServer()->getLevelByName($this->game)));
                        }
                    }
                    break;
                case 
    379:
                    foreach(
    $players as $p){
                        
    $p->sendMessage(TextFormat::BOLD TextFormat::WHITE $this->plugin->beast[$this->game] . " was selected as beast!");
                        
    $this->plugin->addKit($this->plugin->beast[$this->game]);
                        
    $p->sendTip("Beast is free");
                        if(
    strtolower($p->getName()) == $this->plugin->getBeast($this->game)){
                            
    $p->teleport(new Position($this->plugin->startPointPosition[0], $this->plugin->startPointPosition[1], $this->plugin->startPointPosition[2],$this->plugin->getServer()->getLevelByName($this->game)));
                        }
                    }
                    break;
                case 
    0:
                    
    $this->plugin->getServer()->broadcastMessage(TextFormat::WHITE TextFormat::BOLD "Runners won the game " $this->game);
                    foreach(
    $players as $p){
                        
    $p->teleport($this->plugin->getServer()->getDefaultLevel()->getSafeSpawn());
                    }
                    
    $this->plugin->stopGame($this->game);
                break;
            }

        }
    }
    I don't what happen, any player get teleported to the start point, and I think the *big* problem is the array. If you see another problem, only tell me, please!
    Last edited: Jan 1, 2016
  2. Tim // robske Büba
    Offline

    Tim // robske Büba Notable Member

    Joined:
    Feb 26, 2014
    Posts:
    606
    Minecraft User:
    robske_110
    Wich errors? (Post crash logs)
  3. hoyinm14mc
    Offline

    hoyinm14mc Notable Member Plugin Developer

    Joined:
    Jun 4, 2014
    Posts:
    501
    Plugins:
    9
    Minecraft User:
    hoyinm14mc
    To save a random player's name into an array,
    PHP:
    // Level $level
    $players = [];
    $lp $level->getPlayers();
    $rand $lp[rand(0count($lp) - 1)]->getName();
    $players[$rand] = $rand;
    applqpak likes this.
  4. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Always, try doing a var_dump() on the array if you have its elements undefined.
    Or did you mean the whole array rather than the specific element to be undefined?
    applqpak likes this.
  5. AndrewBit
    Offline

    AndrewBit Notable Member

    Joined:
    Jun 18, 2015
    Posts:
    435
    Minecraft User:
    AndrewBit4
    That was the first method that I use, and I get the same error :')
  6. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    That method only works when hour array is ordered linearly, i.e. with array keys incrementing starting from 0.

Share This Page

Advertisement