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

How to fix the eror in array?

Comments in 'Plugin Development' started by Eddir, Jul 8, 2015.

  1. Eddir
    Offline

    Eddir New Member

    Joined:
    Jan 23, 2015
    Posts:
    5
    Minecraft User:
    Eddir
    When I use my plugin in logs displayed here the error:

    Code:
    2015-06-10 [12:06:39]  [Server thread/NOTICE]: ArrayOutOfBoundsException: "Undefined index: TheMrAlex" (E_NOTICE) in "/BarStatus_v0_1_for_MineDest.phar/src/eddir/BarStatus" at line 76
    
    The plugin should to send popup (tip) message with data (kill count, online count, user name and balance).
    But sometime the popup disappears and I get this error.:(

    How to fix this?

    Code:

    PHP:
    class BarStatus extends PluginBase implements Listener{
        private 
    $timer$target;
        private 
    $function_a1$killCount;
        private 
    $rankDatabase = [];
        
    /** @var Config */
        
    private $userDatabase;
        private 
    $format1$format2$format3$format4$format5$EconomyS;
       
       
        public function 
    onEnable(){
    $this->getServer()->getPluginManager()->registerEvents($this$this);
        
    $this->EconomyS $this->getServer()->getPluginManager()->getPlugin("EconomyAPI");
        
    $this->getServer()->getScheduler()->scheduleRepeatingTask(new CallbackTask(array($this,"function_a1")),10);
    $this->saveDefaultConfig();
    $config $this->getConfig();
            
    $this->format1 $config->get("1");
            
    $this->format2 $config->get("2");
            
    $this->format3 $config->get("3");
            
    $this->format4 $config->get("4");
            
    $this->saveResource("users.yml"false);
            
    $this->userDatabase = new Config($this->getDataFolder() . "users.yml");
            foreach(
    $this->userDatabase->get("ranks") as $player => $rank){
                
    $this->rankDatabase[strtolower($player)] = $rank;
    }
    }
      public function 
    onJoin(PlayerJoinEvent $event){
            
    $this->userDatabase = new Config($this->getDataFolder() . "users.yml");
            foreach(
    $this->userDatabase->get("ranks") as $player => $rank){
                
    $this->rankDatabase[strtolower($player)] = $rank;
            }
            
    $count $this->getRank($event->getPlayer());
            
    $this->killCount[$event->getPlayer()->getName()] = $count;
           
        }
       
         public function 
    getRank(Player $player){
            if(isset(
    $this->rankDatabase[strtolower($player->getName())])){
                return 
    $this->rankDatabase[strtolower($player->getName())];
            }else{
                return 
    0;
            }
        }
       

    public function 
    function_a1(){
               foreach(
    $this->getServer()->getOnlinePlayers() as $p){
                          
    $pName $p->getPlayer()->getName();
                          
    $pMoney $this->EconomyS->mymoney($pName);
                       
    $p->sendTip("                                                 ".$this->format1."\n                                                   ".$this->format2.$pMoney."\n                                                  ".$this->format3.count($this->getServer()->getOnlinePlayers())."\n.                                                   ".$this->format4.$this->killCount[$p->getPlayer()->getName()]);
    #76 line ________
             
    }
         }
        
             
             public function 
    onPlayerDeath(PlayerDeathEvent $event){
                  
    $entity $event->getEntity();
    $cause $entity->getLastDamageCause();
    if(
    $cause instanceof EntityDamageByEntityEvent) {
    $killer $cause->getDamager()->getPlayer();
             
    $this->killCount[$killer->getName()] = $this->killCount[$killer->getName()] + 1;
             
    $this->rankDatabase[$killer->getName()] = $this->killCount[$killer->getName()];
             
    $this->userDatabase->set("ranks"$this->rankDatabase);
            
    $this->userDatabase->save();
                }
                 }

    }
  2. Legoboy0215
    Offline

    Legoboy0215 Notable Member

    Joined:
    Nov 1, 2014
    Posts:
    1,747
    Minecraft User:
    Legoboy0215
    You are smart. Some one posted the plugin and you edited it for your own needs.
  3. Eddir
    Offline

    Eddir New Member

    Joined:
    Jan 23, 2015
    Posts:
    5
    Minecraft User:
    Eddir
    Yes, I used the code from CustomRunks to set data to config file. But the plugin was written by me in the beginning of June.
  4. Legoboy0215
    Offline

    Legoboy0215 Notable Member

    Joined:
    Nov 1, 2014
    Posts:
    1,747
    Minecraft User:
    Legoboy0215
    Ok then..
  5. Primus
    Offline

    Primus Notable Member

    Joined:
    Apr 7, 2015
    Posts:
    1,472
    Minecraft User:
    PrimusLV
    It means index in array does'nt exist!
    PHP:
    $foo = array('chocolate''banana');
    echo 
    $foo[2];
    // Line above will cause error cause index 2 is'nt set!
    Legoboy0215 likes this.
  6. Legoboy0215
    Offline

    Legoboy0215 Notable Member

    Joined:
    Nov 1, 2014
    Posts:
    1,747
    Minecraft User:
    Legoboy0215
    Every where I learn arrays, they all start with food. XD :);)
    PEMapModder and iksaku like this.
  7. Primus
    Offline

    Primus Notable Member

    Joined:
    Apr 7, 2015
    Posts:
    1,472
    Minecraft User:
    PrimusLV
    Everytime i look how to use array, i see food as an example :D
    Legoboy0215 likes this.
  8. Legoboy0215
    Offline

    Legoboy0215 Notable Member

    Joined:
    Nov 1, 2014
    Posts:
    1,747
    Minecraft User:
    Legoboy0215
    No, I literally mean EVERYWHERE.
  9. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,325
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Arrays start counting at index 0. You don't understand why yet, but when you are more experienced, you would be grateful that that is true.
    Legoboy0215 likes this.

Share This Page

Advertisement