1. The forums will be archived and moved to a read only mode in about 2 weeks (mid march).

Solved code keeps repeating

Discussion in 'Development' started by Teamblocket, Jul 8, 2017.

  1. Teamblocket

    Teamblocket Zombie

    Messages:
    301
    GitHub:
    teamblocket
    hey, so i have this plugin that spins stuff in a chest and it gives you the items etc but it repeats the code everytime i only want it to repeat once

    task class
    PHP:
    class Spinner extends PluginTask{

        public 
    $seconds 0;
        
        public function 
    __construct(PluginBase $plugin,Player $playerTile $tile){
            
    parent::__construct($plugin);
            
    $this->plugin $plugin;
            
    $this->player $player;
            
    $this->tile $tile;
        }

          public function 
    onRun($tick){

              
    $this->tile->getInventory()->setItem(13$this->plugin->setWin());

              if(
    $this->seconds == 1){
                  
    $this->tile->getInventory()->setItem(13$this->plugin->setWin());
              }

              if(
    $this->seconds == 2){
                  
    $this->tile->getInventory()->setItem(13$this->plugin->setWin());
              }

              if(
    $this->seconds == 3){
                  
    $this->tile->getInventory()->setItem(13$this->plugin->setWin());
              }

              if(
    $this->seconds == 4){
                  
    $this->tile->getInventory()->setItem(13$this->plugin->setWin());
              }

              if(
    $this->seconds ==  5){
                  
    $this->tile->getInventory()->setItem(13$this->plugin->setWin());
              }

              if(
    $this->seconds == 6){
                  
    $this->tile->getInventory()->setItem(13$this->plugin->setWin());
              }

              if(
    $this->seconds == 7){
                  
    $this->tile->getInventory()->setItem(13$this->plugin->setWin());
              }

              if(
    $this->seconds == 8){
                  
    $this->tile->getInventory()->setItem(13$this->plugin->setWin());
              }

              if(
    $this->seconds == 9){
                  
    $this->tile->getInventory()->setItem(13$this->plugin->setWin());
              }

              if(
    $this->seconds == 10){
                  
    $this->tile->getInventory()->setItem(13$this->plugin->setWin());
              }

              if(
    $this->seconds == 11){
                  
    $this->tile->getInventory()->setItem(13$this->plugin->setWin());
                  
    $id $this->getTaskId();
                  
    $this->plugin->getServer()->getScheduler()->cancelTask($id);
                  
    $this->plugin->pickWin($this->player$this->tile);
              }
              
    $this->seconds++;
          }
    }  
    pickWin() function
    PHP:
        public function pickWin(Player $playerTile $tile){
            if(
    $tile->getInventory()->getItem(13)->getCustomName() == "test"){
                
    $cmds = ["setuperm ".$player->getName()." test.command.*""ppreload"];
                foreach(
    $cmds as $cmd){
                      
    $this->getServer()->dispatchCommand(new ConsoleCommandSender(), $cmd);
                }
                
    $this->getServer()->broadcastMessage("test");
                
    $player->sendMessage("test ");
                
    $player->removeWindow($this->crateChest($player));

            }
        }
     
  2. Keith

    Keith Spider Jockey

    Messages:
    42
    GitHub:
    k3ithos
    Why are you setting $this->seconds within the task? It won't save...
     
  3. Teamblocket

    Teamblocket Zombie

    Messages:
    301
    GitHub:
    teamblocket
    so what do i do?
     
  4. Eduardo

    Eduardo Baby Zombie

    Messages:
    100
    GitHub:
    xBeastMode
    Seeing all those "if" is giving me cancer.
    Paste the part where you schedule the task.

    If you're talking about `$this->seconds++`, it's actually incrementing the number by 1, not setting it.
     
  5. Teamblocket

    Teamblocket Zombie

    Messages:
    301
    GitHub:
    teamblocket
    anyone?..
     
  6. Teamblocket

    Teamblocket Zombie

    Messages:
    301
    GitHub:
    teamblocket
    $this->getServer()->getScheduler()->scheduleRepeatingTask(new Spinner($this, $player, $tile),20);
     
  7. xZeroMCPE

    xZeroMCPE Witch

    Messages:
    67
    GitHub:
    xZeroMCPE
    Here and use case instead of IF. Case's is easy to read... instead of having 100+ if statements...

    Code:
            
    
    $this->seconds++;
    
    switch($this->seconds){
    
                case 1:
                case 2:
                case 3:
                case 4:
                case 5:
                case 6:
                case 7:
                case 8:
                case 9:
                case 10:
                    $this->tile->getInventory()->setItem(13, $this->plugin->setWin());
                    break;
    
                case 11:
                    $this->plugin->pickWin($this->player, $this->tile);  
                    $this->tile->getInventory()->setItem(13, $this->plugin->setWin());
                    $this->plugin->getServer()->getScheduler()->cancelTask($this->getTaskID());
                    break;
            }
    Change the seconds public variable to = 0;
     
    Teamblocket and Derpific like this.
  8. Teamblocket

    Teamblocket Zombie

    Messages:
    301
    GitHub:
    teamblocket
    thanks
     
  9. Teamblocket

    Teamblocket Zombie

    Messages:
    301
    GitHub:
    teamblocket
    the code still repeats :/ but the task does stop
     
  10. xZeroMCPE

    xZeroMCPE Witch

    Messages:
    67
    GitHub:
    xZeroMCPE
    Further, explain?
     
  11. Teamblocket

    Teamblocket Zombie

    Messages:
    301
    GitHub:
    teamblocket
    the last wins just repeats , at least 10 times so if the win is a stack of obsidian you would get about triple of that
     
  12. xZeroMCPE

    xZeroMCPE Witch

    Messages:
    67
    GitHub:
    xZeroMCPE
    Maybe make another variable with something like isActive default to false and after the last one set it as true.
     
    InspectorGadget likes this.
  13. Awzaw

    Awzaw Zombie Pigman Poggit Admin

    Messages:
    726
    GitHub:
    awzaw
    Why not use the PurePerms API?
     
    Last edited: Jul 9, 2017
  14. Teamblocket

    Teamblocket Zombie

    Messages:
    301
    GitHub:
    teamblocket
    never thought of that... , changing it now
     
  15. Teamblocket

    Teamblocket Zombie

    Messages:
    301
    GitHub:
    teamblocket
    when i try to remove the chest GUI noting happens , mind checking it for me? , this code is at the end of the switch the last case
    PHP:
    $this->player->removeWindow($this->tile->getInventory());
     
  16. Thunder33345

    Thunder33345 Moderator Staff Member

    Messages:
    2,137
    GitHub:
    Thunder33345
    this is kinda bad
    why not just if $seconds <= 10?
     
    Irish likes this.
  17. Irish

    Irish Baby Zombie

    Messages:
    156
    GitHub:
    irishpacks
    Actually, you don't even need a switch statement or an if statement(besides testing if 11 seconds has passed) for this one :p

    PHP:
    <?php


    class Spinner extends PluginTask {

        public 
    $seconds 0;

        public function 
    __construct(PluginBase $plugin,Player $playerTile $tile) {
            
    parent::__construct($plugin);
            
    $this->plugin $plugin;
            
    $this->player $player;
            
    $this->tile $tile;
        }

        public function 
    onRun($tick) {
            
    $this->tile->getInventory()->setItem(13$this->plugin->setWin());
            if(
    $this->seconds == 11){;
                
    $id $this->getTaskId();
                
    $this->plugin->getServer()->getScheduler()->cancelTask($id);
                
    $this->plugin->pickWin($this->player$this->tile);
            }
            
    $this->seconds++;
        }

    }  
    If I understand your issue correctly, you are doing a sort of lottery plugin, correct?

    Show me your setWin function if you could.
     
  18. xZeroMCPE

    xZeroMCPE Witch

    Messages:
    67
    GitHub:
    xZeroMCPE
    I used the case statement for a reason (he could easy edit)... What if he wanted to add different things to them?

    My way serves a purpose. But it's still possible with your method too.
     
    Last edited: Jul 11, 2017
  19. Thunder33345

    Thunder33345 Moderator Staff Member

    Messages:
    2,137
    GitHub:
    Thunder33345
    yeah but there's like better ways to do that without adding 1 to 10 what if you want 100 of these?
     
  20. xZeroMCPE

    xZeroMCPE Witch

    Messages:
    67
    GitHub:
    xZeroMCPE
    Then Irish method would fit it.; with some edit.
     
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.