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 $player, Tile $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 $player, Tile $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)); } }
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.
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;
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
Maybe make another variable with something like isActive default to false and after the last one set it as true.
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());
Actually, you don't even need a switch statement or an if statement(besides testing if 11 seconds has passed) for this one PHP: <?phpclass Spinner extends PluginTask { public $seconds = 0; public function __construct(PluginBase $plugin,Player $player, Tile $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.
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.