here is my code PHP: class VoteCrateTask extends Task{ private $player; private $block; public $seconds; public function __construct(Player $player, Main $plugin, Block $block) { $this->player = $player; $this->plugin = $plugin; $this->block = $block; $this->seconds = 0; } public function onRun(int $currentTick):void { $this->player->sendMessage("hi..."); $timer = $this->seconds--; if ($timer === 5) { $this->closeChest($this->block); } } public function closeChest($block): void{ $pk = new BlockEventPacket; $pk->x = (int)$block->x; $pk->y = (int)$block->y; $pk->z = (int)$block->z; $pk->eventType = 1; $pk->eventData = 0; foreach($this->plugin->getServer()->getOnlinePlayers() as $user) $user->dataPacket($pk); }
you set seconds equal to '0' and you are subtracting, when you should be adding or set seconds equal to 5 for it to work
PHP: class VoteCrateTask extends Task{ private $player; private $block; public $seconds; public function __construct(Player $player, Main $plugin, Block $block) { $this->player = $player; $this->plugin = $plugin; $this->block = $block; $this->seconds = 5; } public function onRun(int $currentTick):void { $timer = $this->seconds; $this->player->sendMessage("hi..."); $timer--; if ($timer === 5) { $this->closeChest($this->block); } } public function closeChest($block): void{ $pk = new BlockEventPacket; $pk->x = (int)$block->x; $pk->y = (int)$block->y; $pk->z = (int)$block->z; $pk->eventType = 1; $pk->eventData = 0; foreach($this->plugin->getServer()->getOnlinePlayers() as $user) $user->dataPacket($pk); }
PHP: $pk = new BlockEventPacket; if ($pk->eventData == 0) { $pk->x = (int)$block->x; $pk->y = (int)$block->y; $pk->z = (int)$block->z; $pk->eventType = 1; $pk->eventData = 1; foreach($this->plugin->getServer()->getOnlinePlayers() as $user) $user->dataPacket($pk); $this->plugin->getScheduler()->scheduleDelayedTask(new VoteCrateTask($player, $this->plugin, $block), 5); } else { $player->sendMessage("§8[§6!§8]§c This crate is already in use..."); }
Exactly it must be a REPEATING task not DELAYED. PHP: $pk = new BlockEventPacket; if ($pk->eventData == 0) { $pk->x = (int)$block->x; $pk->y = (int)$block->y; $pk->z = (int)$block->z; $pk->eventType = 1; $pk->eventData = 1; foreach($this->plugin->getServer()->getOnlinePlayers() as $user) $user->dataPacket($pk); $this->plugin->getScheduler()->scheduleRepeatingTask(new VoteCrateTask($player, $this->plugin, $block), 5); } else { $player->sendMessage("§8[§6!§8]§c This crate is already in use..."); }
You have the task set at '5' PHP: $this->plugin->getScheduler()->scheduleRepeatingTask(new VoteCrateTask($player, $this->plugin, $block), 5); when it should be 20(20 because tasks are in ticks and 20 ticks equals 1 second) PHP: $this->plugin->getScheduler()->scheduleRepeatingTask(new VoteCrateTask($player, $this->plugin, $block), 20);
yeah, what i'm talking about is crate isn't closing and it's just spamming my chat with the message "hi...", it's only supposed to do that one time and it's when opening the crate
PHP: class VoteCrateTask extends Task{ private $player; private $block; public $seconds; public function __construct(Player $player, Main $plugin, Block $block) { $this->player = $player; $this->plugin = $plugin; $this->block = $block; $this->seconds = 5; } public function onRun(int $currentTick):void { $timer = $this->seconds--; if ($timer === 5) { $this->player->sendMessage("hi..."); } if ($timer === 0) { $this->closeChest($this->block); } } public function closeChest($block): void{ $pk = new BlockEventPacket; $pk->x = (int)$block->x; $pk->y = (int)$block->y; $pk->z = (int)$block->z; $pk->eventType = 1; $pk->eventData = 0; foreach($this->plugin->getServer()->getOnlinePlayers() as $user) $user->dataPacket($pk); } try this not sure, will stop spam and maybe fix closing
I just made it using another method and it fixed the message now i'm fixing the crate closing PHP: public function onRun(int $currentTick):void { switch ($this->spins) { case 0: $this->player->sendMessage("opening a crate :)"); break; case 1: $this->closeChest($this->block); break; } $this->spins++; $scheduler = $this->plugin->getScheduler(); if($this->spins < 1) $scheduler->scheduleDelayedTask(new VoteCrateTask($this->player, $this->plugin, $this->block, $this->spins), 20); }
still not working, current code: PHP: class VoteCrateTask extends Task{ private $player; private $block; public $seconds; public function __construct(Player $player, Main $plugin, Block $block) { $this->player = $player; $this->plugin = $plugin; $this->block = $block; $this->seconds = 5; } public function onRun(int $currentTick):void { $timer = $this->seconds; $this->player->sendMessage("hi..."); $timer--; if ($timer === 5) { $this->closeChest($this->block); } } public function closeChest($block): void{ $pk = new BlockEventPacket; $pk->x = (int)$block->x; $pk->y = (int)$block->y; $pk->z = (int)$block->z; $pk->eventType = 1; $pk->eventData = 0; foreach($this->plugin->getServer()->getOnlinePlayers() as $user) $user->dataPacket($pk); }