How can i stop.

Discussion in 'Plugin Development' started by NoSpanMan, Oct 9, 2015.

Thread Status:
Not open for further replies.
  1. Offline

    NoSpanMan

    How can i stop this from outside (simple)?
    Code:
      public void captureTimer1() {
         new BukkitRunnable() {
           
           public void run() {
             if (captureTime1 != -1) {
               if(captureTime1 != 0) {
                   
                 captureTime1--;
               } else {
                 
                 for(Player p : Bukkit.getOnlinePlayers()){
                 
                   sendMessage(DB + "Point 1 is captured");
                   
                 cancel();
                   }
                 }
               }
             }
           }.runTaskTimer(this, 0, 40); // 20L = 1sec
       }
    
     
  2. Offline

    Langsdorf

    Just this:
    Code:
        public static BukkitTask task;
       
        public static void a() {
             task = new BukkitRunnable() {
                public void run() {
                   
                }
            }.runTaskTimer(this, 0, 20);
           
            if (task != null) {
                task.cancel();
            }
        }
     
  3. Offline

    NoSpanMan

    @Langsdorf I dont get it? But i mean from outside this timer what i have in the captureTimer1(); example captureTimer1().cancel();
     
  4. Offline

    Langsdorf

    Code:
        public static int captureTime1 = 20;
        public static BukkitTask task;
        public static void createTask() {
            task = new BukkitRunnable() {
                @Override
                public void run() {
                    if (captureTime1 != 0) {
                        captureTime1 -= 1;
                    } else {
                        cancel();
                    }
                }
            }.runTaskTimer(LGlad.pl, 0, 20);
        }
      
        public static void canceltask() {
            if (task != null) {
                task.cancel();
            }
        }
    or


    Code:
    public static int captureTime1 = 20;
    
      
        public static BukkitTask createTask() {
            return new BukkitRunnable() {
                @Override
                public void run() {
                    if (captureTime1 != 0) {
                        captureTime1 -= 1;
                    } else {
                        cancel();
                    }
                }
            }.runTaskTimer(LGlad.pl, 0, 20);
        }
    
       public static void canceltask() {
            createTask().cancel();
        }
    
    
     
  5. @Langsdorf Stop the static abuse and please stop spoonfeeding. People generally copy and do not learn from spoonfeeding.
     
    MarinD99 and Langsdorf like this.
  6. Offline

    NoSpanMan

    @Langsdorf But what happens here can you explain? But thanks im gonna try with my code.


    @bwfcwalshy Without (spoonfeeding) you don't get it ever. So stop with those commends.
     
  7. Offline

    Binner_Done

    @NoSpanMan You explain it, spoonfeeding is literally giving them what you need so in a maths test you ask ma'am for help and she would help you work it out yourself instead of telling you the answer. I'm with @bwfcwalshy on this one
     
    Zombie_Striker likes this.
  8. Offline

    Langsdorf

    here
    Code:
     public static BukkitTask createTask() {
            return new BukkitRunnable() {
                @Override
                public void run() {
                    if (captureTime1 != 0) {
                        captureTime1 -= 1;
                    } else {
                        cancel();
                    }
                }
            }.runTaskTimer(LGlad.pl, 0, 20);
        }
    I created a method that return the BukkitTask, using "return new BukkitRunnable........runTaskTimer(LGlad.pl, 0, 20);"

    "LGlad.pl" is my plugin, "0" is the delay and "20" is the period.

    ---

    Here

    Code:
    public static BukkitTask task;
        public static void createTask() {
            task = new BukkitRunnable() {
                @Override
                public void run() {
                    if (captureTime1 != 0) {
                        captureTime1 -= 1;
                    } else {
                        cancel();
                    }
                }
            }.runTaskTimer(LGlad.pl, 0, 20);
        }
    
        public static void canceltask() {
            if (task != null) {
                task.cancel();
            }
        }
    
    I created a variable BukkitTask with "task" name

    Here:
    Code:
    task = new BukkitRunnable() {
                @Override
                public void run() {
                    if (captureTime1 != 0) {
                        captureTime1 -= 1;
                    } else {
                        cancel();
                    }
                }
            }.runTaskTimer(LGlad.pl, 0, 20);
    
    I set the variable "task" to "new BukkitRunnable() {.......runTaskTimer(LGlad.pl, 0, 20);"

    Now, here:
    Code:
    public static void canceltask() {
            if (task != null) {
                task.cancel();
            }
        }
    
    I checked if the variable " task" is null and I canceled it.
     
  9. Offline

    mythbusterma

    @NoSpanMan

    Usually it's easiest just to have it take care of itself, because most of the time the runnable can check whether it needs to stop running, and, assuming you're using BukkitRunnables like you should, you can just invoke BukkitRunnable#cancel() inside the declaration of the runnable itself.

    Also, @Langsdorf doesn't really know what he's talking about, there's absolutely no reason those fields should be public or static, or even why you need to store a reference to the BukkitTask at all (most of the time).

    If you truly can't figure out if the runnable should be run again inside the runnable, then storing a reference is fine, but you should store it in a private, non-static field.

    Disregarding how remarkably ignorant this comment is, I look forward to many another thread from you essentially asking other people to write your code for you.
     
  10. Offline

    Scimiguy

    And that is one major reason we don't "spoonfeed"
    Langsdorf is a really awful example of spoonfeeding, and does it on almost every post he makes
     
    mythbusterma and Zombie_Striker like this.
Thread Status:
Not open for further replies.

Share This Page