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

Solved Undefined offset: 1

Comments in 'Plugin Development' started by Taha_The_Hacker, Oct 14, 2015.

  1. PocketKiller
    Offline

    PocketKiller Notable Member

    Joined:
    Jul 20, 2015
    Posts:
    741
    Hey,

    I have a problem , it always says that when running my command :
    [18:54:05] [Server thread/CRITICAL]: Unhandled exception executing command 'ms start' in ms: Undefined offset: 1

    $args[1] has to be defined, Idk what does it, even if i wrote it just same. here's my code :
    PHP:
      public function onCommand(CommandSender $senderCommand $cmd$label, array $args){
        switch(
    $cmd->getName()){
          case 
    "ms":
          switch(
    $args[1]){//Here is the Problem
          
    case "start":

          
    $this->GameStart();

          case 
    "stop":
          
    $this->getServer()->getScheduler()->cancelTask($this->TaskID1);

          }
    //switch2
                  
    if(count($args) < 1){
            
    $sender->sendMessage("Use /ms <start/stop>");
                return 
    true;
            }
        }
    //switch1

      
    }//onCommand
    So do you know what's wrong or causing it?
    I have events registered, Listensers used, Events used
    Thanks for you reply :)
    Last edited: Oct 14, 2015
  2. Kvetinac97
    Offline

    Kvetinac97 Active Member Plugin Developer

    Joined:
    Nov 17, 2014
    Posts:
    276
    Plugins:
    1
    Minecraft User:
    Kvetinac97
    You can't switch to $args[1] when you don't know if they exist. Add
    PHP:
     if (isset($args[1])){
    before switch ($args[1])
    Taha_The_Hacker likes this.
  3. CraftYourBukkit
    Offline

    CraftYourBukkit Notable Member Plugin Developer

    Joined:
    Jan 20, 2015
    Posts:
    1,022
    Plugins:
    2
    Minecraft User:
    CraftYourBukkit
    It's $args[0].. You start counting from 0 in the most coding languages/coding in General. Tho, you should first check if it even is set, as @Kvetinac97 explained.
    Legoboy0215 and Kvetinac97 like this.
  4. PocketKiller
    Offline

    PocketKiller Notable Member

    Joined:
    Jul 20, 2015
    Posts:
    741
    I want to get this:
    /ms <HERE>
  5. CraftYourBukkit
    Offline

    CraftYourBukkit Notable Member Plugin Developer

    Joined:
    Jan 20, 2015
    Posts:
    1,022
    Plugins:
    2
    Minecraft User:
    CraftYourBukkit
    As I said, it is $args[0], still. The command itself is $command->getName(), it is not set in the Array $args!
    Taha_The_Hacker likes this.
  6. Primus
    Offline

    Primus Notable Member

    Joined:
    Apr 7, 2015
    Posts:
    1,470
    Minecraft User:
    PrimusLV
    As i remember there was something about CamelCase for function like this gameStart() instead of GameStart().
  7. PocketKiller
    Offline

    PocketKiller Notable Member

    Joined:
    Jul 20, 2015
    Posts:
    741
    that works perfectly, and i named the function GameStart()
  8. Primus
    Offline

    Primus Notable Member

    Joined:
    Apr 7, 2015
    Posts:
    1,470
    Minecraft User:
    PrimusLV
  9. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
  10. zombie_Power
    Offline

    zombie_Power Active Member

    Joined:
    Sep 7, 2015
    Posts:
    239
    Minecraft User:
    Zombie_Power
    PHP:
     $cmd strtolower($command->getName());
       switch(
    $cmd){
           case 
    "ms":
               if(isset(
    $args[0])){
                     
    $args[0] = strtolower($args[0]);
                     if(
    $args[0]=="help"){
                                
    $sender->sendMessage(TextFormat::BOLDTextFormat::BLUE "Hey" $sender);
                                 
    $sender->sendMessage(TextFormat::BOLDTextFormat::BLUE "whatever...");
                                 break;
    try this
  11. CraftYourBukkit
    Offline

    CraftYourBukkit Notable Member Plugin Developer

    Joined:
    Jan 20, 2015
    Posts:
    1,022
    Plugins:
    2
    Minecraft User:
    CraftYourBukkit
    No Need for strtolower() in that case. Also, you should return true, as it would send you the command Usage now
    zombie_Power likes this.

Share This Page

Advertisement