I created a plugin. When you right click a terracotta block, it opens an interface. I wanted to add a cooldown so it only opens once every 5 seconds. How can I do that? Here's the code: Code: public function onInteract(PlayerInteractEvent $event) { $p = $event->getPlayer(); $level = $p->getLevel(); $block = $event->getBlock(); $chance = rand(1,20); $item = $p->getInventory()->getItemInHand(); if($block->getId() === 235){ if($item->getId() == 264){ $this->GrinderTopaz($p); } else { $p = $event->getPlayer(); $p->sendPopup("§l§d»§r §cVous n'avez pas d'item à broyer!"); } } } public function GrinderTopaz($p){ $api = $this->getServer()->getPluginManager()->getPlugin("FormAPI"); $form = $api->createSimpleForm(function (Player $p, int $data = null){ $result = $data; $chance = rand(1,20); if($result === null){ return true; } switch ($result) { case 0: switch(mt_rand(1, 20)) { case 1: $p->getInventory()->removeItem(Item::get(264, 0, 1)); $p->getInventory()->addItem(Item::get(422, 0, 1)); $p->sendPopup("§l§d»§r §2Item déchiqueté!"); break; case 2: $p->getInventory()->removeItem(Item::get(264, 0, 1)); $p->sendPopup("§l§d»§r §4Vous n'avez rien reçu ._."); break; case 3: $p->getInventory()->removeItem(Item::get(264, 0, 1)); $p->sendPopup("§l§d»§r §4Vous n'avez rien reçu ._."); break; case 4: $p->getInventory()->removeItem(Item::get(264, 0, 1)); $p->sendPopup("§l§d»§r §4Vous n'avez rien reçu ._."); break; case 5: $p->getInventory()->removeItem(Item::get(264, 0, 1)); $p->sendPopup("§l§d»§r §4Vous n'avez rien reçu ._."); break; case 6: $p->getInventory()->removeItem(Item::get(264, 0, 1)); $p->sendPopup("§l§d»§r §4Vous n'avez rien reçu ._."); break; case 7: $p->getInventory()->removeItem(Item::get(264, 0, 1)); $p->sendPopup("§l§d»§r §4Vous n'avez rien reçu ._."); break; case 8: $p->getInventory()->removeItem(Item::get(264, 0, 1)); $p->sendPopup("§l§d»§r §4Vous n'avez rien reçu ._."); break; case 9: $p->getInventory()->removeItem(Item::get(264, 0, 1)); $p->sendPopup("§l§d»§r §4Vous n'avez rien reçu ._."); break; case 10: $p->getInventory()->removeItem(Item::get(264, 0, 1)); $p->sendPopup("§l§d»§r §4Vous n'avez rien reçu ._."); break; case 11: $p->getInventory()->removeItem(Item::get(264, 0, 1)); $p->sendPopup("§l§d»§r §4Vous n'avez rien reçu ._."); break; case 12: $p->getInventory()->removeItem(Item::get(264, 0, 1)); $p->sendPopup("§l§d»§r §4Vous n'avez rien reçu ._."); break; } break; case 1: $p->sendPopup("§l§d»§r §cAction annulée!"); break; } }); $form->setTitle("§e- §7Grinder §e-"); $form->setContent("§7 Souhaitez-vous broyer votre §ediamant §7?\n§eRappel: §7 Vous avez §f1§7 chance sur §f12§7 de récupérer de la poudre de prismarine!"); $form->addButton("§aOui"); $form->addButton("§cNon"); $form->sendToPlayer($p); return $form; } } Yes, I registerd the events. Sorry for my bad english, i'm french.
Save the latest time in seconds (with microtime(true) in a variable assigned to the player and make a simple subtraction
This is what I came up with, but it dosen't seems to work. Code: class Main extends PluginBase implements Listener{ public function onEnable() { $this->getServer()->getPluginManager()->registerEvents($this, $this); } public function onLoad() { $this->getLogger()->info(TextFormat::BLUE . "Loading..."); } private $cooldown = array(); public function onInteract(PlayerInteractEvent $event) : void{ $p = $event->getPlayer(); $level = $p->getLevel(); $block = $event->getBlock(); $chance = rand(1,20); $item = $p->getInventory()->getItemInHand(); if($block->getId() === 235){ if($item->getId() == 458){ if(!isset($this->cooldown[$p->getName()])) $this->cooldown[$p->getName()] = 0; if($this->cooldown[$p->getName()] <= time()){ $this->GrinderTopaz($p); $this->cooldown[$p->getName()] = time() + 5; } else { $p->sendPopup("§l§d»§r §cMerci d'attendre avant de réutiliser le grinder."); } $this->GrinderTopaz($p); } else { $p = $event->getPlayer(); $p->sendPopup("§l§d»§r §cVous n'avez pas d'item à broyer!"); } } }
This is how I did it with my previous plugin: I made an array(?) like this: PHP: public $cooldown = []; In your case, you want the cooldown to be for the block, so you want to check if they are in the array like this: PHP: if(!isset($this->cooldown[$player->getName()])){ //do actions //this checks to see if the player is NOT in the array} If they aren't in the array, as shown above, you want to set them into the array like so: PHP: $this->cooldown[$player->getName()] = time() + (integer in seconds remove the parenthesis); You also want to include your actions within there. If they are in the array, you want to check if the time is up already like this: PHP: else{ if(time() < $this->cooldown[$player->getName()]){ //deny the actions } else { unset($this->cooldown[$player->getName()]); //do same actions as if they are in the array }} Even though this is messy, hope it helped. P.S: I would recommend using TextFormat instead of the color symbols, it saves time and energy
Here's what I have done, it still dosen't works. Code: public function onInteract(PlayerInteractEvent $event) : void{ $p = $event->getPlayer(); $level = $p->getLevel(); $block = $event->getBlock(); $chance = rand(1,20); $item = $p->getInventory()->getItemInHand(); if($block->getId() === 235){ if($item->getId() == 458){ if(!isset($this->cooldown[$p->getName()])) { $this->GrinderTopaz($p); $this->cooldown[$p->getName()] = 0 + 5; } else{ if(time() < $this->cooldown[$p->getName()]){ $p->sendPopup("§l§d»§r §cMerci d'attendre avant de réutiliser le grinder."); } else { unset($this->cooldown[$p->getName()]); $this->GrinderTopaz($p); } } } } }
Can you tell me how it's not working? Is it not doing the function, is there an error, etc. Edit: Instead of using $this->cooldown[$p->getName()] = 0 + 5; Use: $this->cooldown[$p->getName()] = time() + 5;
PHP: public function onInteract(PlayerInteractEvent $event) : void{ $p = $event->getPlayer(); $level = $p->getLevel(); $block = $event->getBlock(); $chance = rand(1,20); $item = $p->getInventory()->getItemInHand(); if($block->getId() === 235){ if($item->getId() == 458){ if(!isset($this->cooldown[$p->getName()])) { $this->GrinderTopaz($p); $this->cooldown[$p->getName()] = time() + 5; } else{ if(time() < $this->cooldown[$p->getName()]){ $p->sendPopup("§l§d»§r §cMerci d'attendre avant de réutiliser le grinder."); } else { unset($this->cooldown[$p->getName()]); $this->GrinderTopaz($p); $this->cooldown[$p->getName()] = time() + 5; } } } }}