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

Something wring with my code

Comments in 'Plugin Development' started by Muqsit, Jun 22, 2016.

  1. Muqsit
    Offline

    Muqsit Active Member

    Joined:
    Sep 9, 2015
    Posts:
    175
    Minecraft User:
    Muqsit
    PHP:
        public function PlayerCommandPreprocessEvent(PlayerCommandPreprocessEvent $event){
        if(!
    file_exists($this->getDataFolder() . "help.yml")) {
          @
    mkdir($this->getDataFolder());
          }
          
    $this->helpcfg = new Config($this->getDataFolder()."help.yml"Config::YAML);
          
    $command explode(" "strtolower($event->getMessage()));
          
    $player $event->getPlayer();
          if(
    $command[0] === "/help" or $command[0] === "/?") {
             foreach(
    $this->helpcfg->get("page.1") as $page1){
             
    $player->sendMessage($page1);
             
    $event->setCancelled();
            }
          if(
    $command[1] === "1" or $command[1] === "one") {
             foreach(
    $this->helpcfg->get("page.1") as $page1){
             
    $player->sendMessage($page1);
             
    $event->setCancelled();
            }
          }elseif(
    $command[1] === "2" or $command[1] === "two") {
             foreach(
    $this->helpcfg->get("page.2") as $page2){
             
    $player->sendMessage($page2);
             
    $event->setCancelled();
            }
         }
          }elseif(
    $command[1] === "3" or $command[1] === "three") {
             foreach(
    $this->helpcfg->get("page.3") as $page3){
             
    $player->sendMessage($page2);
             
    $event->setCancelled();
            }
          }elseif(
    $command[1] === "4" or $command[1] === "four") {
             foreach(
    $this->helpcfg->get("page.4") as $page4){
             
    $player->sendMessage($page2);
             
    $event->setCancelled();
            }
          }elseif(
    $command[1] === "5" or $command[1] === "five") {
             foreach(
    $this->helpcfg->get("page.5") as $page5){
             
    $player->sendMessage($page2);
             
    $event->setCancelled();
            }
          }elseif(
    $command[1] === "6" or $command[1] === "six") {
             foreach(
    $this->helpcfg->get("page.6") as $page6){
             
    $player->sendMessage($page2);
             
    $event->setCancelled();
            }
          }elseif(
    $command[1] === "7" or $command[1] === "seven") {
             foreach(
    $this->helpcfg->get("page.7") as $page7){
             
    $player->sendMessage($page2);
             
    $event->setCancelled();
            }
          }elseif(
    $command[1] === "8" or $command[1] === "eight") {
             foreach(
    $this->helpcfg->get("page.8") as $page8){
             
    $player->sendMessage($page2);
             
    $event->setCancelled();
            }
          }elseif(
    $command[1] === "9" or $command[1] === "nine") {
             foreach(
    $this->helpcfg->get("page.9") as $page9){
             
    $player->sendMessage($page2);
             
    $event->setCancelled();
            }
          }elseif(
    $command[1] === "10" or $command[1] === "ten") {
             foreach(
    $this->helpcfg->get("page.10") as $page10){
             
    $player->sendMessage($page10);
             
    $event->setCancelled();
         }
      }
    }

    It does work all the way till /help 10, just that it spams the console with...

    Notice: Undefined offset: 1 in /home/testify/plugins/RumBenFX_v1/src/RumBenCoreaft/Main.php on line 1108 (The line containing elseif($command[1] === "x"))
  2. Blabla
    Offline

    Blabla Active Member

    Joined:
    Sep 19, 2015
    Posts:
    82
    Minecraft User:
    The_Beast_Inside
    Well first of all I think you're missing one } at the end. Secondly, I think you forgot to change the message that will be sent to the player on help 3-9. Hope that works :)
  3. Primus
    Offline

    Primus Notable Member

    Joined:
    Apr 7, 2015
    Posts:
    1,470
    Minecraft User:
    PrimusLV
    First, thats one hell of a dynamic code. Second, I don't see "elseif($command[1] === "x")" anywhere?

    PHP:
    var_dump($command);
    To see what the variable contains.
    Last edited: Jun 22, 2016
  4. Muqsit
    Offline

    Muqsit Active Member

    Joined:
    Sep 9, 2015
    Posts:
    175
    Minecraft User:
    Muqsit
    What I meant was...
    Let x be any number from 1 to 10.
  5. CraftYourBukkit
    Offline

    CraftYourBukkit Notable Member Plugin Developer

    Joined:
    Jan 20, 2015
    Posts:
    1,022
    Plugins:
    2
    Minecraft User:
    CraftYourBukkit
    PHP:
    if($command[1] > && $command[1] < 11){
    $stuff $this->helpcfg->get("page." $command[1]);
    foreach(
    $stuff as $p){
    // Do something
    }
    }
    And about the error: $command[number here] is not set, means the command is only /help
    Muqsit likes this.
  6. Muqsit
    Offline

    Muqsit Active Member

    Joined:
    Sep 9, 2015
    Posts:
    175
    Minecraft User:
    Muqsit
    Hmm makes sense
  7. Legoboy0215
    Offline

    Legoboy0215 Notable Member

    Joined:
    Nov 1, 2014
    Posts:
    1,724
    Minecraft User:
    Legoboy0215
    Seriously? that is what a LOOP is for. If I want to add 1 more page, you'll need to add more code. Compared to the mathod Craft showed you, it is very static.
    Vaivez66 likes this.
  8. Muqsit
    Offline

    Muqsit Active Member

    Joined:
    Sep 9, 2015
    Posts:
    175
    Minecraft User:
    Muqsit
    Okay well, whatever you say, I'm just a student...

    I've made the plugin, here it is, but I'm still getting some console errors, though the plugin works well and the messages are delivered on point... and at this point, I think it's... PocketMine's fault?

    GitHub: https://github.com/Muqsit/Help

    Attached Files:

  9. CraftYourBukkit
    Offline

    CraftYourBukkit Notable Member Plugin Developer

    Joined:
    Jan 20, 2015
    Posts:
    1,022
    Plugins:
    2
    Minecraft User:
    CraftYourBukkit
    Please post the code and errors here. It's not our job to read through your code, try it out and find the errors.
  10. Muqsit
    Offline

    Muqsit Active Member

    Joined:
    Sep 9, 2015
    Posts:
    175
    Minecraft User:
    Muqsit
    PHP:
    <?php

    namespace Muqsit;

    use 
    pocketmine\plugin\PluginBase;
    use 
    pocketmine\event\Listener;
    use 
    pocketmine\event\player\PlayerCommandPreprocessEvent;
    use 
    pocketmine\utils\Config;

    class 
    Main extends PluginBase implements Listener{

    public function 
    onEnable(){
    if(!
    file_exists($this->getDataFolder() . "help.yml")) {
    @
    mkdir($this->getDataFolder());
    file_put_contents($this->getDataFolder() . "help.yml",$this->getResource("help.yml"));
    }
    $this->saveDefaultConfig();
    $this->getServer()->getPluginManager()->registerEvents($this$this);
    }

    public function 
    PlayerCommandPreprocessEvent(PlayerCommandPreprocessEvent $event){
    $this->helpcfg = new Config($this->getDataFolder()."help.yml"Config::YAML);
    $command explode(" "strtolower($event->getMessage()));
    $player $event->getPlayer();
    $protect $this->helpcfg->get("pages");

    if(
    $command[0] === "/help" or $command[0] === "/?"){
    if(
    $command[1] > && $command[1] < $protect 1){
    $messages $this->helpcfg->get("page." $command[1]);
    foreach(
    $messages as $msges){
    $player->sendMessage($msges);
    $event->setCancelled();
    }
    }elseif(
    $command[1] < or $command[1] > $protect){
    $helpInitial $this->helpcfg->get("page.1");
    foreach(
    $helpInitial as $helpInit){
    $player->sendMessage($helpInit);
    $event->setCancelled();
    }
    }
    }
    }
    }
    Gives undefined offset 1 on the line containing $command[1]
  11. CraftYourBukkit
    Offline

    CraftYourBukkit Notable Member Plugin Developer

    Joined:
    Jan 20, 2015
    Posts:
    1,022
    Plugins:
    2
    Minecraft User:
    CraftYourBukkit
    Check if it is set with isset() or !empty().
    Muqsit likes this.

Share This Page

Advertisement