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

Solved How can I delay the use of items?

Discussion in 'Plugin Help' started by MinekCz, Jul 28, 2020.

  1. Diduhless

    Diduhless Baby Zombie

    Messages:
    199
    GitHub:
    Diduhless
    How is exactly making a repeating task better than only checking two intervals outside of a loop?
     
  2. GodWeedZao

    GodWeedZao Zombie Pigman

    Messages:
    401
    GitHub:
    godweedzao
    so if its not better, why pmmp Team create (Tasks) ?! :p
     
  3. GiantQuartz

    GiantQuartz Spider

    Messages:
    7
    @GodWeedZao
    You're using a sledgehammer to crack a nut.

    @MinekCz
    The most efficient method to solve this problem would be to check that the time interval elapsed since the last use of the item is bigger than 3 seconds.

    I made a simple example for you.
    PHP:
    private $timesSinceLastToolUsage = [];

        public function 
    onBreakWithItem(BlockBreakEvent $event) {
            
    $player $event->getPlayer();

            if(!
    $event->getItem() instanceof Tool) { // making sure the item used is a tool
                
    return;
            }

            
    $name $player->getName();
            
    $timeSinceLastToolUsage $this->timesSinceLastToolUsage[$name] ?? null;
            if(
    $timeSinceLastToolUsage !== null and (microtime(true) - $timeSinceLastToolUsage) < 3) { // if the player has used a tool before and 3 seconds haven't passed, the event is cancelled
                
    $player->sendMessage("3 seconds haven't passed since the last time you used a tool to break a block!");
                
    $event->setCancelled();
            } else {
    // if the 3 seconds have passed or it's the first time a player uses a tool, the player can use the tool and the time will be registered
                
    $timeSinceLastToolUsage[$name] = microtime(true);
            }
        }
     
    Last edited: Aug 1, 2020
    Diduhless likes this.
  4. hexmor

    hexmor Baby Zombie

    Messages:
    110
    GitHub:
    h3xmor
    you are right . tasks will make server laggy . dont use them if you can .
     
    Diduhless likes this.
  5. hexmor

    hexmor Baby Zombie

    Messages:
    110
    GitHub:
    h3xmor
    repeating tasks could be used to do job on time whitout any events .
     
  6. MinekCz

    MinekCz Spider Jockey

    Messages:
    29
    Thx this method is the best
     
  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.