Solved Scheduler in multiple class

Discussion in 'Spigot Plugin Development' started by Froyzz, May 27, 2016.

  1. Hi guys!
    How can i recover a scheduler into an event class in my main class ?
    Because i need to take the id of the scheduler for make a time in my config, like this:
    Code (Text):
    public void onDisable(){
            if (plugin.getServer().getScheduler().isCurrentlyRunning(1)){
            long i = System.currentTimeMillis();
            try {
                Config.setTime(i);
                Config.save();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            }
            }
        }
    I did this on my event class :
    Code (Text):
    Integer TaskID =  Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {    
    i can't use static, i don't know why:
    Code (Text):
        @EventHandler
        public void onmort(EntityDeathEvent event) throws IOException{
           
            Entity p = event.getEntity();
            if (p instanceof PigZombie){
                if (p.isCustomNameVisible()){
                    if (p.getWorld().getName().equals("world")){
                        long iaabc = System.currentTimeMillis();
                        Config.setTimeHero(iaabc);
                        Config.save();
                        if (this.plugin.getConfig().getLong("Time") != 0){
                            Bukkit.broadcastMessage("There is a reload so this is this step");
                            long time = this.plugin.getConfig().getLong("Time");
                            long tempshero = this.plugin.getConfig().getLong("TempsHero");
                             long tth = time-tempshero;
                             long temps = 86400000-tth;
                             long finaltime = temps*1000/20;                                                
                               
                              Integer TaskID =  Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {                                
                                 
                                    @Override
                                    public void run() {                      

                                         
                                Random r = new Random();
                                int aléatoire = r.nextInt(4);                      
                                switch(aléatoire){
                                case 0:
                                    Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "mm mobs spawn Herobrine 1 world,5286,81,3775");                      
                                break;

                                case 1:
                                    Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "mm mobs spawn Herobrine 1 world,5286,81,3778");
                                break;
                               
                                case 2:
                                    Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "mm mobs spawn Herobrine 1 world,5289,81,3778");
                                break;
                               
                                case 3:
                                    Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "mm mobs spawn Herobrine 1 world,5289,81,3775");
                                   
                                break;
                               
                                case 4:
                                    Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "mm mobs spawn Herobrine 1 world,5289,81,3775");
                                   
                                break;
                               
                                default:;
                                break;      
                                }
                               
                                    }
                                 }, finaltime);
                             Config.setTime(0);
                        } else {
                            Bukkit.broadcastMessage("00");
                        Integer TaskID =  Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {                          
                                   
                            @Override
                            public void run() {                      

                                 
                        Random r = new Random();
                        int aléatoire = r.nextInt(4);                      
                        switch(aléatoire){
                        case 0:
                            Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "mm mobs spawn Herobrine 1 world,5286,81,3775");                      
                        break;

                        case 1:
                            Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "mm mobs spawn Herobrine 1 world,5286,81,3778");
                        break;
                       
                        case 2:
                            Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "mm mobs spawn Herobrine 1 world,5289,81,3778");
                        break;
                       
                        case 3:
                            Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "mm mobs spawn Herobrine 1 world,5289,81,3775");
                           
                        break;
                       
                        case 4:
                            Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "mm mobs spawn Herobrine 1 world,5289,81,3775");
                           
                        break;
                       
                        default:;
                        break;      
                        }
                       
                            }
                         }, 1728000L);
                         }
                        }
                        }
                                           
                    }
        }
    But the scheduler 1 doesn't exit cause i can't take a scheduler from my class event. ( this code is in my Main class)
    May you help me please :(
     
  2. Learn basic java... Scope, specifically. If you store the BukkitRunnable object in your main class, anything with a reference to your main class can use it.

    You are declaring taskID as a local variable (if you do not know what that is, just stop programming, go find a textbook, and start reading), it simply ceases to exist as soon as the scope (the block of code containing it) is exited.

    Static is really not the best way to do this either.

    That code is pure pain to look at. You should properly indent each line, and put the opening brackets on the line below the method, if or loop (it is much easier to read), It is impossible to even tell if half the code is inside the runnable or how many other blocks it is inside.
     
    • Winner Winner x 1
  3. Also, "instanceof" very slow method in java.
    Use:
    Code (Java):

    if (p.getType() == EntityType.PIG_ZOMBIE)
    {
     
     
  4. Thank for your answer, yes my code is very bad at looking, i should pay attention to it, and i know what is a local variable, but with static i can use it through my event class to my main, but it seems to doesn't work. I was watching tutorial for scope but it just using static. How can i store the bukkitrunnable in my main class without using static ?
     
  5. Nice, i didn't know thank you.
     
  6. Make a nonstatic variable, and pass the instance of your main class to each major subclass: new SomethingClass(this), and store the passed main class in a variable. Honestly static is easier, but if you do not fully understand it, it can cause problems.