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

Undefined offset: 1 and Undefined offset: 2

Comments in 'Plugin Development' started by CyberPowerCrazii, May 14, 2016.

  1. CyberPowerCrazii
    Offline

    CyberPowerCrazii New Member

    Joined:
    Jan 9, 2016
    Posts:
    8
    Minecraft User:
    GlitchPlayer90
    Hello Everyone, Can you help me with this code?

    PHP:
          }elseif($args[0] === "tp" and $sender->hasPermission("pet.cmd.tp")) {
            if(
    $sender instanceof Player);
            if(
    $args[1] === null and $args[2] !== null) {
              
    $sender->sendMessage(TF::GREEN."Pet Teleporting...");
              
    $pet->teleport($sender->getPosition());
              
    $sender->sendMessage(TF::GREEN."Pet Teleported!");
              
    // $sender->sendMessage(TF::RED."Functionality not implemented yet");
              
    return true;
            }
            return 
    true;
          }
          return 
    false;
        }
        return 
    false;
      }
  2. applqpak
    Offline

    applqpak Active Member Plugin Developer

    Joined:
    Dec 16, 2015
    Posts:
    284
    Plugins:
    1
    Minecraft User:
    applqpak
    You didn't tell us the lines it was occurring at.. But that error usually means you're trying to access an array index that doesn't exist.
    Vaivez66 likes this.
  3. Vaivez66
    Offline

    Vaivez66 Notable Member Plugin Developer

    Joined:
    Jun 10, 2015
    Posts:
    402
    Plugins:
    2
    PHP:
    if(isset($args[1])){
        
    // Do something
    }
    applqpak likes this.
  4. CyberPowerCrazii
    Offline

    CyberPowerCrazii New Member

    Joined:
    Jan 9, 2016
    Posts:
    8
    Minecraft User:
    GlitchPlayer90
    Do you want to see the whole code?
    applqpak likes this.
  5. jojoe77777
    Offline

    jojoe77777 Notable Member Plugin Developer

    Joined:
    May 28, 2015
    Posts:
    653
    Plugins:
    1
    Minecraft User:
    jojoe77777
    Yes, and also the error. But I assume it's because you didn't supply a second or third arg.
    applqpak likes this.
  6. applqpak
    Offline

    applqpak Active Member Plugin Developer

    Joined:
    Dec 16, 2015
    Posts:
    284
    Plugins:
    1
    Minecraft User:
    applqpak
    I already said this :p
    jojoe77777 likes this.
  7. Tim // robske Büba
    Offline

    Tim // robske Büba Notable Member

    Joined:
    Feb 26, 2014
    Posts:
    606
    Minecraft User:
    robske_110
    Use isset instead of if $bla[bla] ==NULL
  8. CyberPowerCrazii
    Offline

    CyberPowerCrazii New Member

    Joined:
    Jan 9, 2016
    Posts:
    8
    Minecraft User:
    GlitchPlayer90
    PHP:
    <?php
    namespace Pets;

    use 
    Pets\Managers\PointlessManager;
    use 
    Pets\Managers\Sqlite3Manager;
    use 
    Pets\Managers\YamlManager;
    use 
    Pets\Listener\EventListener;

    use 
    pocketmine\plugin\PluginBase;
    use 
    pocketmine\nbt\tag\ByteTag;
    use 
    pocketmine\nbt\tag\ListTag;
    use 
    pocketmine\nbt\tag\DoubleTag;
    use 
    pocketmine\nbt\tag\FloatTag;
    use 
    pocketmine\nbt\tag\ShortTag;
    use 
    pocketmine\nbt\tag\StringTag;
    use 
    pocketmine\nbt\tag\IntTag;
    use 
    pocketmine\entity\Entity;
    use 
    pocketmine\nbt\tag\CompoundTag;
    use 
    pocketmine\command\Command;
    use 
    pocketmine\command\CommandSender;
    use 
    pocketmine\utils\Config;
    use 
    pocketmine\utils\TextFormat as TF;
    use 
    pocketmine\Player;

    class 
    Pets extends PluginBase {
      public 
    $Pets$cfg$provider;
      public function 
    onEnable() {
        
    $this->configProvider();
        
    Entity::registerEntity(PetWolf::class, true);
        
    Entity::registerEntity(PetOcelot::class, true);
        
    $this->getLogger()->debug("Entities have been registered!");
        
    $this->getServer()->getPluginManager()->registerEvents(new EventListener($this$this->provider), $this);
        
    $this->getLogger()->debug("Events have been registered!");
        
    $this->getLogger()->notice(TF::GREEN."Enabled!");
      }
      public function 
    onCommand(CommandSender $senderCommand $command$label, array $args) {
        if(
    $this->cfg instanceof Config);
        if(
    $this->provider instanceof PointlessManager);
        if(!
    $sender instanceof Player) {
          
    $sender->sendMessage(TF::DARK_RED."Console can't use the pet command!");
          return 
    true;
        }
        
    $pet $this->getPet($sender->getName());
        if(
    $pet instanceof Entity);
        if(
    strtolower($command) === "pet" and $sender->hasPermission("pet.cmd")) {
          if((
    count($args) == and $sender->hasPermission("pet.cmd.help")) or ($args[0] === "help" and $sender->hasPermission("pet.cmd.help"))) {
            
    $sender->sendMessage(TF::YELLOW."/pet help");
            
    $sender->sendMessage(TF::YELLOW."/pet disown");
            
    $sender->sendMessage(TF::YELLOW."/pet spawn <PetName>");
            
    $sender->sendMessage(TF::YELLOW."/pet rename [PetName] <NewName>");
            
    //$sender->sendMessage(TF::YELLOW."/pet items <add|Remove|List>");
            
    $sender->sendMessage(TF::YELLOW."/pet tp");
            return 
    true;
          }
          if(
    $args[0] === "spawn" and $sender->hasPermission("pet.cmd.make")) {
            if(
    $args[1] === "" or $args[1] === null) {
              
    $sender->sendMessage(TF::RED."You need to name your pet!");
              return 
    true;
            }
            if(
    $sender instanceof Player);
            
    $nbt $this->makeNBT($sender->getSkinData(), $sender->getSkinName(), $args[1], $sender->getInventory(), $sender->getYaw(), $sender->getPitch(), $sender->getX(), $sender->getY(), $sender->getZ());
            
    $petEntity Entity::createEntity("PetWolf"$sender->getLevel()->getChunk($sender->getX() >> 4$sender->getZ() >> 4), $nbt);
            
    $this->provider->makePet($petEntity->getId(), $sender->getName(),$args[1]);
            if(
    $petEntity) {
              
    $sender->sendMessage(TF::GREEN."Pet Created!");
              return 
    true;
            }
            
    $sender->sendMessage(TF::GREEN."Pet Creation Failed!");
              return 
    true;
          }elseif(
    $args[0] === "disown" and $sender->hasPermission("pet.cmd.disown")) {
            
    $this->provider->removePet($pet->getId(), $sender->getName());
            
    $pet->close();
          }elseif(
    $args[0] === "rename" and $sender->hasPermission("pet.cmd.name")) {
            if(
    $args[1] === "" or $args[1] === null) {
              
    $sender->sendMessage(TF::RED."You need to name your pet!");
              return 
    true;
            }elseif(
    strtolower($this->cfg->get("allow-rename")) === "false") {
              
    $sender->sendMessage(TF::RED."Pet renaming is Disabled!");
              return 
    true;
            }
            if(
    $sender instanceof Player);
            
    $pet->close();
            
    $this->provider->removePet($pet->getId(),$sender->getName());
            
    $nbt $this->makeNBT(nullnull$args[1], null$sender->getYaw(), $sender->getPitch(), $sender->getX(), $sender->getY(), $sender->getZ());
            
    $petEntity Entity::createEntity("PetWolf"$sender->getLevel()->getChunk($sender->getX() >> 4$sender->getZ() >> 4), $nbt);
            
    $this->provider->makePet($petEntity->getId(), $sender->getName(),$args[1]);
            
    $this->provider->setPetName($args[1], $sender->getName());
            
    $sender->sendMessage(TF::GREEN."Pet Renamed!");
            return 
    true;
          }elseif(
    $args[0] === "tp" and $sender->hasPermission("pet.cmd.tp")) {
            if(
    $sender instanceof Player);
            if(
    $args[1] === null and $args[2] !== null) {
              
    $sender->sendMessage(TF::GREEN."Pet Teleporting...");
              
    $pet->teleport($sender->getPosition());
              
    $sender->sendMessage(TF::GREEN."Pet Teleported!");
              
    // $sender->sendMessage(TF::RED."Functionality not implemented yet");
              
    return true;
            }
            return 
    true;
          }
          return 
    false;
        }
        return 
    false;
      }
      private function 
    makeNBT($skin$skinData$name$inv$yaw$pitch$x$y$z) {
        
    $nbt = new CompoundTag;
        
    $nbt->Pos = new ListTag("Pos", [
            new 
    DoubleTag(""$x),
            new 
    DoubleTag(""$y),
            new 
    DoubleTag(""$z)
        ]);
        
    $nbt->Rotation = new ListTag("Rotation", [
            new 
    FloatTag(""$yaw),
            new 
    FloatTag(""$pitch)
        ]);
        
    $nbt->Health = new ShortTag("Health"20);
        
    $nbt->Inventory = new ListTag("Inventory"$inv); //TODO v2 pets inventory
        
    $nbt->CustomName = new StringTag("CustomName"$name);
        
    $nbt->CustomNameVisible = new ByteTag("CustomNameVisible"1);
        
    // $nbt->Invulnerable = new Byte("Invulnerable", 0);
        /* Name visible */
        
    $nbt->CustomNameVisible = new ByteTag("CustomNameVisible"1);
        return 
    $nbt;
      }
      public function 
    getPet($name) {
        if(
    $this->provider instanceof PointlessManager);
        
    $petId $this->provider->getPetId($name);
        foreach(
    $this->getServer()->getLevels() as $level) {
          if(
    $level->getEntity($petId) != null) {
            return 
    $level->getEntity($petId);
          }
        }
        return 
    null;
      }
      public function 
    configProvider() {
        if(!
    file_exists($this->getDataFolder())) {
          @
    mkdir($this->getDataFolder());
        }
        
    $this->saveDefaultConfig();
        
    $this->cfg = new Config($this->getDataFolder()."config.yml");
        
    $DataProvider $this->cfg->get("dataProvider");
        if(
    strtolower($DataProvider) === "sqlite3" or strtolower($DataProvider) === "sql") {
          
    $this->provider = new Sqlite3Manager($this);
          
    $this->getLogger()->info("Using SQLITE3 data provider");
        }elseif(
    strtolower($DataProvider) === "yaml" or strtolower($DataProvider) === "yml") {
          
    $this->provider = new YamlManager($this);
          
    $this->getLogger()->info("Using YAML data provider");
        }else{
          
    $this->provider = new Sqlite3Manager($this);
          
    $this->getLogger()->info("Using SQLITE3 data provider");
          throw new \
    RuntimeException("Invalid data provider");
        }
      }
      public function 
    onDisable() {
        
    $this->getLogger()->notice(TF::GREEN."Disabled!");
      }
    }
  9. Extreme_Heat
    Offline

    Extreme_Heat Active Member

    Joined:
    Apr 19, 2016
    Posts:
    76
    Minecraft User:
    Extreme_Heat
    See what the others have been telling you...

    Use isset() to check if an index exists; use empty() to check if an index exists and also whether the value of the array index can evaluate to being 'true'. Since you want to check if the variable is also equal to "", you might want to use empty().

    When you do something like ($args[99] == null), PHP will immediately check if index '99' exists, before evaluating your statement. When it doesn't exist, it will return an error to you.

    Checking the size of $args is also something you can do.

    So instead of using

    PHP:
    if($args[1] === "" or $args[1] === null)
    You'd use:

    PHP:
    if(empty($args[1])) or if (sizeof($args) == 1)

Share This Page

Advertisement