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

How to repeat a Piece of code as times as i want?

Comments in 'Plugin Development' started by TheKillerTacos, Apr 13, 2016.

  1. TheKillerTacos
    Offline

    TheKillerTacos Active Member

    Joined:
    Jan 21, 2016
    Posts:
    76
    Minecraft User:
    TheKillerTacos
    How can I repeat a Piece of code as many times I want?
    for example:
    I want to repeat this piece of code about 100 times (I know it will spam console, just an example)
    PHP:
    $this->getLogger()->info(TextFormat::GREEN "Something");
    without copy-pasting it around 100 times (a lot of work mostly if the code i want to Repeat is way long)

    I tried googling how to loop code in php as times as i want, and related searches, but couldnt find it and was busy at the moment doing Real-Life Stuff (jk :( )

    So if you can help me out, ill be thanked! (Thanked? sounds like Tanked)
  2. Vaivez66
    Offline

    Vaivez66 Notable Member Plugin Developer

    Joined:
    Jun 10, 2015
    Posts:
    402
    Plugins:
    2
  3. TheKillerTacos
    Offline

    TheKillerTacos Active Member

    Joined:
    Jan 21, 2016
    Posts:
    76
    Minecraft User:
    TheKillerTacos
    Sorry, but this is only for number/text related stuff as i see, i want to Repeat a code, the example i gave is just a little code, and is not even related to the code i want to Loop/Repeat, so, Any other solution?
  4. basprohop
    Offline

    basprohop Active Member Plugin Developer

    Joined:
    Aug 8, 2015
    Posts:
    128
    Plugins:
    3
    Minecraft User:
    basprohop
    while loop

    Code:
    $i = 0;
    while ($i < 100) {
      //Your code
     $i++;
    }
    
    applqpak likes this.
  5. applqpak
    Offline

    applqpak Active Member Plugin Developer

    Joined:
    Dec 16, 2015
    Posts:
    284
    Plugins:
    1
    Minecraft User:
    applqpak
    PHP:
    for($i 0$i <= 10$i++)
    {

      
    // CODE HERE

    }
    Example:
    PHP:
    for($i 0$i <= 10$i++)
    {

      echo 
    "Hi";

    }
    NOTES:

    1. There is probably a better way...

    3. Change the 10 in $i <= 10; to whatever number you want, and whatever number you set it to the code will be executed that many times,
    Vaivez66 likes this.
  6. applqpak
    Offline

    applqpak Active Member Plugin Developer

    Joined:
    Dec 16, 2015
    Posts:
    284
    Plugins:
    1
    Minecraft User:
    applqpak
    Why code tags? Why not PHP tags? :'(
  7. basprohop
    Offline

    basprohop Active Member Plugin Developer

    Joined:
    Aug 8, 2015
    Posts:
    128
    Plugins:
    3
    Minecraft User:
    basprohop
    Too mainstream :D
    applqpak likes this.
  8. xBeastMode
    Offline

    xBeastMode Active Member

    Joined:
    Nov 27, 2015
    Posts:
    292
    Bad idea.

    PHP:
    public function repeatingFunc($times 100){
    for(
    $i 0$i $times+1; ++$i){
    //do something here

    if($skip)continue;//use this is you want to skip something

    break;//use this in case you want to terminate the loop
    }
    }
    [/QUOTE]
  9. basprohop
    Offline

    basprohop Active Member Plugin Developer

    Joined:
    Aug 8, 2015
    Posts:
    128
    Plugins:
    3
    Minecraft User:
    basprohop
    [/QUOTE]

    Lel, why is it a bad idea. He merely asked how to repeat code never gave any specific use scenarios
    applqpak likes this.
  10. TheKillerTacos
    Offline

    TheKillerTacos Active Member

    Joined:
    Jan 21, 2016
    Posts:
    76
    Minecraft User:
    TheKillerTacos
    Im wanting this for Skyblock generation without involving Chunks, so i want to setblock() 100 times, without Copy-Pasting it, but also i want to do it by 50 times more or less, so thats the main reason of the Thread
  11. TheKillerTacos
    Offline

    TheKillerTacos Active Member

    Joined:
    Jan 21, 2016
    Posts:
    76
    Minecraft User:
    TheKillerTacos
    so i tried this and:
    both give me this error:
    Simple Code:
    PHP:
    if($command->getName() == "pop"){
                
    $times $this->getConfig()->get("times"); // Times is 10
                
    for($i 0$i <= $times$i++)
                    {
                    
    $player $event->getPlayer();
                    
    $player->sendPopup("Hi!");

                    }
            }
    Last edited: Apr 14, 2016
  12. xBeastMode
    Offline

    xBeastMode Active Member

    Joined:
    Nov 27, 2015
    Posts:
    292
    Show full event
    applqpak likes this.
  13. TheKillerTacos
    Offline

    TheKillerTacos Active Member

    Joined:
    Jan 21, 2016
    Posts:
    76
    Minecraft User:
    TheKillerTacos
    Here is the whole plugin:
    PHP:
    <?phpnamespace Popper;

    use 
    pocketmine\Player;
    use 
    pocketmine\level\Location;
    use 
    pocketmine\utils\Config;
    use 
    pocketmine\utils\TextFormat;
    use 
    pocketmine\event\player\PlayerJoinEvent;
    use 
    pocketmine\event\Listener;
    use 
    pocketmine\command\CommandSender;
    use 
    pocketmine\command\Command;
    use 
    pocketmine\plugin\PluginBase as Base;


    class 
    Main extends Base implements Listener{
     public function 
    onEnable(){$this->saveDefaultConfig();$this->getServer()->getPluginManager()->registerEvents($this$this);$this->getLogger()->info(TextFormat::GREEN "Popper Loaded! [TKRT]");
     }
     public function 
    onDisable(){$this->getLogger()->info(TextFormat::RED "Popper Unloaded! Server Crashed? or Stopped?");
     }
     

     public function 
    onPlayerJoinEvent(PlayerJoinEvent $event){$player $event->getPlayer();$this->getServer()->broadcastMessage(TextFormat::GREEN "Welcome " .TextFormat::YELLOW .TextFormat::BOLD .$player->getName().TextFormat::RESET.TextFormat::GREEN" to the server");$player->sendPopup("Hi!");
     return;
     }
     
     public function 
    onCommand(CommandSender $senderCommand $command$label, array $args){
     if(
    $command->getName() == "pop"){$times $this->getConfig()->get("IslandsPerWorld");
     for(
    $i 0$i <= $times$i++)
     {
    $player $event->getPlayer();$player->sendPopup("Hi!");

     }
     }
     
     }
     }
    And my repo at Github: https://github.com/XenoNova/Popper-Dev

    PS: Updated the code
    Last edited: Apr 14, 2016
  14. xBeastMode
    Offline

    xBeastMode Active Member

    Joined:
    Nov 27, 2015
    Posts:
    292
    That's not the same code you showed above.
    applqpak likes this.
  15. TheKillerTacos
    Offline

    TheKillerTacos Active Member

    Joined:
    Jan 21, 2016
    Posts:
    76
    Minecraft User:
    TheKillerTacos
    changed the command pop to loop since it was making conflicts with another plugin, so there!
    PS: Thats the old code, here the Actual code, sine i used the Github one that i havent updated it:
    PHP:
    <?php

    namespace Popper;

    use 
    pocketmine\Player;
    use 
    pocketmine\level\Location;
    use 
    pocketmine\utils\Config;
    use 
    pocketmine\utils\TextFormat;
    use 
    pocketmine\event\player\PlayerJoinEvent;
    use 
    pocketmine\event\Listener;
    use 
    pocketmine\command\CommandSender;
    use 
    pocketmine\command\Command;
    use 
    pocketmine\plugin\PluginBase as Base;


    class 
    Main extends Base implements Listener{
        public function 
    onEnable(){
            
    $this->saveDefaultConfig();
            
    $this->getServer()->getPluginManager()->registerEvents($this$this);
            
    $this->getLogger()->info(TextFormat::GREEN "Popper Loaded! [TKRT]");
        }
        public function 
    onDisable(){
            
    $this->getLogger()->info(TextFormat::RED "Popper Unloaded! Server Crashed? or Stopped?");
        }
     

        public function 
    onPlayerJoinEvent(PlayerJoinEvent $event){
            
    $player $event->getPlayer();
            
    $this->getServer()->broadcastMessage(TextFormat::GREEN "Welcome " .TextFormat::YELLOW .TextFormat::BOLD .$player->getName().TextFormat::RESET.TextFormat::GREEN" to the server");
            
    $player->sendPopup("Hi!");
            return;
            }
         
        public function 
    onCommand(CommandSender $senderCommand $command$label, array $args){
            if(
    $command->getName() == "pop"){
                
    $times $this->getConfig()->get("Times"); //Times is 10
                
    for($i 0$i <= $times$i++)
                    {
                    
    $player $event->getPlayer();
                    
    $player->sendPopup("Hi!");

                    }
            }

            }
        }
  16. xBeastMode
    Offline

    xBeastMode Active Member

    Joined:
    Nov 27, 2015
    Posts:
    292
    What's with $event->getPlayer()? .-.
  17. TheKillerTacos
    Offline

    TheKillerTacos Active Member

    Joined:
    Jan 21, 2016
    Posts:
    76
    Minecraft User:
    TheKillerTacos
    it obtains player/senders name to send a little popup, the popup is located at the hotbar but the code gives the error
  18. Primus
    Offline

    Primus Notable Member

    Joined:
    Apr 7, 2015
    Posts:
    1,470
    Minecraft User:
    PrimusLV
    There is nothing wrong with it.

    Show your config file. You have to remember that yaml file requires special formatting.
    applqpak likes this.
  19. TheKillerTacos
    Offline

    TheKillerTacos Active Member

    Joined:
    Jan 21, 2016
    Posts:
    76
    Minecraft User:
    TheKillerTacos
    here it is:
    Code:
    #Config File for Popper! [TKRT]
    Times: 10
    barely nothing
  20. basprohop
    Offline

    basprohop Active Member Plugin Developer

    Joined:
    Aug 8, 2015
    Posts:
    128
    Plugins:
    3
    Minecraft User:
    basprohop
    You can't call $event->getPlayer() in onCommand function. B/c $event does not exist in its scope

Share This Page

Advertisement