Advertisement
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Solved how to disable 1 minute of pvp ?

Comments in 'Plugin Development' started by Ritch, Jan 27, 2016.

  1. Ritch
    Offline

    Ritch Active Member

    Joined:
    Oct 4, 2015
    Posts:
    184
    Minecraft User:
    anonymous
    Hi, guys how To disable 1 minute of pvp for a WORLD.
  2. xBeastMode
    Offline

    xBeastMode Active Member

    Joined:
    Nov 27, 2015
    Posts:
    292
    This is a quick way of doing it (not recommended). I would use a scheduledDelayedTask instead.
    PHP:
    private $time;
    public function 
    onEnable(){
    $this->time time();
    }
    public function 
    onHit(EntityDamageEvent $e){
    $p $e->getEntity();
    if(
    $p instanceof Player){
    $level $p->getLevel()->getName();
    if(
    $level === "your world name" && (time()-$this->time 60)){
    $e->setCancelled();
    }
    }
    }
    Ritch and Skullex like this.
  3. Ritch
    Offline

    Ritch Active Member

    Joined:
    Oct 4, 2015
    Posts:
    184
    Minecraft User:
    anonymous
    Thank <3
  4. Tim // robske Büba
    Offline

    Tim // robske Büba Notable Member

    Joined:
    Feb 26, 2014
    Posts:
    606
    Minecraft User:
    robske_110
    Or just look in worldprotect!
  5. Ritch
    Offline

    Ritch Active Member

    Joined:
    Oct 4, 2015
    Posts:
    184
    Minecraft User:
    anonymous
    No sorry I have code xD why ? I go look code in Worldprotect ? Thank And more You not help me :/
  6. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Depending on your purpose, this method might not be so bad.
  7. AndrewBit
    Offline

    AndrewBit Notable Member

    Joined:
    Jun 18, 2015
    Posts:
    435
    Minecraft User:
    AndrewBit4
    I think that the best way is using a repeating task.
    PHP:
    class YourPluginClass extends PluginBase implements Listener{

    private 
    cancelPvP true;

    // You should schedule your task.

    # /**
    #  * @param Server $server
    #  */
    # $server->getScheduler()->scheduleRepeatingTask(new TimerTask($this),20); # 20 = ticks before execute the task content.

    public function OnEnable()
    {
    $this->getServer()->getPluginManager()->registerEvents($this,$this);
    }

    public function 
    cancelPvP()
    {
    $this->cancelPvP false;
    }

    public function 
    OnBattle(EntityDamageEvent $event)
    {
    if(
    $event->getPlayer()->getLevel()->getName() == "LevelName"){
    if(
    $this->cancelPvP)
    {
    $event->setCancelled();
    }
    }
    # I'm using two if() instead an "and" because it's faster, at least here.
    }

    }
    Your task should be:
    PHP:
    class TimerTask extends PluginTask{

    private 
    $timer 60;

    private 
    $owner;

    public function 
    __construct(YourPluginClass $owner)
    {
    $this->owner $owner;
    parent::__construct($owner);
    }

    private function 
    getPlugin()
    {
    return 
    $this->owner;
    }

    public function 
    OnRun($currentTick)
    {
    $timer -= 1;
    if(
    $timer == 0){
    $this->getPlugin()->cancelPvP();
    $this->cancel();
    }
    }
    }
  8. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Could you elaborate on this? Any tests or theories that prove this?

    And why use a repeating task? Why not just schedule for 1200 ticks and run it once?
    AndrewBit likes this.
  9. AndrewBit
    Offline

    AndrewBit Notable Member

    Joined:
    Jun 18, 2015
    Posts:
    435
    Minecraft User:
    AndrewBit4
    If you try to attack anybody outside of that world, also cancelPvP will be checked. It's totally unnecesary.
    With my method, cancelPvP will be checked only if you're on a certain world.

    That's my theory.
  10. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Actually, no. The second operand would not be resolved if the first operand resolves to false. (Same applies to or; if the first operand resolves to true, the second isn't run at all) So they are actually the same, the only difference only going to the PHP infrastructure (`and` vs an extra block nesting)
    Also, in that case (premature optimization! :eek:), $this->cancelPvP should be checked first as it has a higher probability to resolve false, and it is definitely much faster to check $this->cancelPvP than `$event->getPlayer()->getLevel()->getName() == "LevelName"` (so assuming both have same probability to be false or true, let's check the fast one first before checking the slow one). The $event one requires the call to 3 methods, each method involving resolving a class property of those scopes, then a == check and instantiating of a new string, which is definitely takes many times more than the "optimization" you did.
    But anyway, this is premature optimization, and it really shouldn't be considered when you write code, otherwise you would be distracted and forget the things that really matter. For more, please Google the disadvantages of premature optimization.

    ALso check edited post above.
    AndrewBit likes this.
  11. CrazedMiner
    Offline

    CrazedMiner Notable Member Plugin Developer

    Joined:
    Jan 31, 2015
    Posts:
    348
    Plugins:
    2
    Minecraft User:
    CrazedMinerYT
    Personally, I would handle it like this:
    PHP:
    public $this->disabled = [];

    public function 
    onDamage(EntityDamageEvent $event) {
        if((
    $player $event->getEntity()) instanceof Player) {
            if(isset(
    $this->disabled[$player->getLevel()->getName()]) {
                if(
    floor(microtome(true) - $this->disabled[$player->getLevel()->getName()]) <= 60) {
                    
    // Cancel the event when a world has pvp disabled
                    
    return $event->setCancelled();
                }
            }
            
    // After successfully being damaged add the level name to the array of disabled worlds
            
    $this->disabled[$player->getLevel()->getName() = microtime(true);
        }
    }
    And if you're worried about performance why use a task for something as simple as this in the first place?
    Last edited: Jan 28, 2016
    PEMapModder likes this.
  12. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    @AndrewBit just to be obsessive, I made a test to compare the difference between if(a){ if(b){ } } and if(a and b){ }
    https://3v4l.org/MGUXF
    CrazedMiner likes this.
  13. CrazedMiner
    Offline

    CrazedMiner Notable Member Plugin Developer

    Joined:
    Jan 31, 2015
    Posts:
    348
    Plugins:
    2
    Minecraft User:
    CrazedMinerYT
    Care to also calculate the added time to a tick each time the task is executed as well?
  14. Ritch
    Offline

    Ritch Active Member

    Joined:
    Oct 4, 2015
    Posts:
    184
    Minecraft User:
    anonymous
    [SOLVED]
    CrazedMiner likes this.
  15. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    That could be inaccurate, depending on the server's current state. Scheduling a task can affect other things in terms of timings. Even a delayed-but-not-running task can cause slight lag, but that could not be measured as it is usually considered as "negligible".
    But say, if you want to update 1000 blocks, and you create 1000 tasks thinking that you can spread out the changes and lag the server less, the 1000 tasks will definitely cause something more than negligible lag.
    Ritch and CrazedMiner like this.

Share This Page

Advertisement