delay between each loop

Discussion in 'Plugin Development' started by EnchantedMiners, Sep 7, 2015.

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

    EnchantedMiners

    I tried

    Code:
                        for(double i = b.getY(); i < b.getY()+5.0; i = i+0.1){
                            long time = System.currentTimeMillis();
                           
                            Location newloc = new Location(b.getWorld(), b.getX()+0.5, i, b.getZ()+0.5);
                            ParticleEffect.SLIME.display(0, 0, 0, (float)0, 5, newloc, 100);
                           
                            long wait = time + 50 - System.currentTimeMillis();
                            if (wait > 0)
                                try {
                                    Thread.sleep(wait);
                                } catch (InterruptedException e) {
                                    e.printStackTrace();
                                }
                        }
    But Thread.sleep(wait);
    makes the entire server lagg for a while....
    and i sure don't want that.
    is there any other option to go with ?
     
  2. Offline

    Hawktasard

  3. Offline

    EnchantedMiners

  4. Offline

    Oxyorum

    @EnchantedMiners Nononono. Don't use Thread.sleep(). Also, @Hawktasard is correct. You will want to have a look at the repeating task examples in the page he linked.
     
  5. Offline

    Hawktasard

  6. Offline

    EnchantedMiners

    well i know how to use schedulers i just though there was a way of doing this on a for loop since its way cleaner and easier but only i would need a delay between each result... so there is no way of doing it without putting the thread to sleep ?
     
  7. Offline

    Oxyorum

    @EnchantedMiners You do not want to put the thread to sleep, since that would mean the entire server would hang for the amount of time you specified (you'd be putting the main server thread to sleep). All you would need to do if you used a scheduler is create a task and have your loop running inside the run() method of that task. Simplicity itself.

    Note: If my explanation on Thread.sleep() is wrong, call me out.
     
  8. Offline

    Petersoj

    You can try using Thread.sleep() in a class OTHER than the main one (the one that extends "JavaPlugin")
     
  9. Offline

    EnchantedMiners

    Yes you're right about the Thread.sleep(); but ok so what i am trying to do there is make a particle animation so every lets say 3 milisec will addd 0.5 to the Y location and spawn the particle there, i tried doing with the scheduler its not spawning particles here is my code
    Code:
        double y;
     
        public void check(){
            List<Teams> teams = data.getAllteams();
         
            for(Teams team : teams){
                Location loc1 = methods.stringToLocation(team.getCuboid_loc1());
                Location loc2 = methods.stringToLocation(team.getCuboid_loc2());
                Cuboid cuboid = new Cuboid(loc1, loc2);
             
                for(Block b : cuboid){
                    if(b.getType() == Material.EMERALD_ORE){
                        y = b.getY();
                        double newy = y + 0.1;
                     
                        Location newloc = new Location(b.getWorld(), b.getX()+0.5, newy, b.getZ()+0.5);
                        ParticleEffect.SLIME.display(0, 0, 0, (float)0, 5, newloc, 100);
                     
                        Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, new Runnable(){
                            @Override
                            public void run() {
                                y = b.getY();
                            }
                        }, 60L);
                    }
                }
            }
        }
    That check();
    is running on a class implementing Runnable and its being called when the plugin is enabled aka onEnable
    Code:
    Bukkit.getScheduler().scheduleSyncRepeatingTask(instance, new RegionEffectsCheck(instance), 0L, 3L);
    How exactly... uhmmm i guess im using Main class when i call the runnable class from onEnable??

    EDIT: Just noticed my runnable class make no sense since it will run every 3 miliseconds and every time is called it will run the delayed task which won't work... any fix ?
     
  10. Offline

    Oxyorum

    @EnchantedMiners Okay. Is that check() method being called in the run() method of your RegionEffectsCheck task? Also, I recommend that the RegionEffectsCheck extends BukkitRunnable rather than implement Runnable (it would work either way, but just a recommendation).

    Edit: The scheduler tasks like scheduleSyncRepeatingTask() take time in ticks not milliseconds.
     
  11. Offline

    EnchantedMiners

    yes is on the run() and thanks for the tip i will change Runnable to BukkitRunnable :)
     
  12. Offline

    Oxyorum

    @EnchantedMiners Look at my edit in my previous post. Also, it would have been nice to say you were using a llibrary. :p

    I would recommend adding some debugging text to help you identify the problem.
     
  13. Offline

    Hawktasard

    @Petersoj
    New object does not mean new thread.
    This will still freeze the server:
    Code:java
    1. public class MyClass extends JavaPlugin
    2. {
    3. @Override
    4. public void onEnable()
    5. {
    6. new AnotherClass();
    7. }
    8. }
    9.  
    10. public class AnotherClass
    11. {
    12.  
    13. public AnotherClass()
    14. {
    15. Thread.sleep(1500);
    16. }
    17.  
    18. }
     
  14. Offline

    EnchantedMiners

    @Hawktasard @Oxyorum @Petersoj i got it fixed with the shedulers it was a pain in the ass thinking how to but got it working xD...
    here is the code if someone in the future needs it to idk whatever xD
    Thanks alot guys!
    Code:
    package com.dragonshardnetwork.schedulers;
    
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    
    import org.bukkit.Location;
    import org.bukkit.Material;
    import org.bukkit.block.Block;
    
    import com.dragonshardnetwork.PAConfig;
    import com.dragonshardnetwork.PAMMO;
    import com.dragonshardnetwork.utilities.Methods;
    import com.dragonshardnetwork.utilities.ParticleEffect;
    import com.dragonshardnetwork.utilities.region.Cuboid;
    import com.dragonshardnetwork.utilities.teams.Teams;
    
    public class RegionEffectsCheck implements Runnable{
    
        PAMMO plugin;
        PAConfig data;
        Methods methods;
       
        public RegionEffectsCheck(PAMMO plugin){
            this.plugin = plugin;
            this.data = plugin.getData();
            this.methods = plugin.getMethods();
        }
       
        @Override
        public void run() {
            check();
        }
       
        HashMap<Block, Double> bprogress = new HashMap<Block, Double>();
       
        public void check(){
            List<Teams> teams = data.getAllteams();
           
            for(Teams team : teams){
                Location loc1 = methods.stringToLocation(team.getCuboid_loc1());
                Location loc2 = methods.stringToLocation(team.getCuboid_loc2());
                Cuboid cuboid = new Cuboid(loc1, loc2);
               
                for(Block b : cuboid){
                    if(team.getName().equalsIgnoreCase("fireflies")){
                        if(b.getType() == Material.EMERALD_ORE){
                            if(!bprogress.containsKey(b))
                                bprogress.put(b, 0.0);
                            Map<Block, Double> y = new HashMap<Block, Double>();
                            y.put(b, b.getY()+bprogress.get(b)+1);
                           
                            Location newloc = new Location(b.getWorld(), b.getX()+0.5, y.get(b), b.getZ()+0.5);
                            if(y.get(b) >= b.getY()+5.0){
                                y.put(b, b.getY()+1.0);
                                bprogress.put(b, 0.0);
                            }
                            ParticleEffect.SLIME.display(0, 0, 0, (float)0, 20, newloc, 100);
                            bprogress.put(b, bprogress.get(b)+0.2);
                        }
                    } else if(team.getName().equalsIgnoreCase("bandits")){
                        if(b.getType() == Material.REDSTONE_ORE){
                            if(!bprogress.containsKey(b))
                                bprogress.put(b, 0.0);
                            Map<Block, Double> y = new HashMap<Block, Double>();
                            y.put(b, b.getY()+bprogress.get(b)+1.0);
                           
                            Location newloc = new Location(b.getWorld(), b.getX()+0.5, y.get(b), b.getZ()+0.5);
                            if(y.get(b) >= b.getY()+5.0){
                                y.put(b, b.getY()+1.0);
                                bprogress.put(b, 0.0);
                            }
                           
                            ParticleEffect.REDSTONE.display(0, 0, 0, (float)0, 20, newloc, 100);
                            bprogress.put(b, bprogress.get(b)+0.2);
                        }
                    }
                }
            }
        }
    }
    
     
    Hawktasard likes this.
  15. Offline

    mythbusterma

    Nope.

    As a rule, there is no reason to ever sleep the thread when you're doing Bukkit programming, as you're never responsible for your own scheduling.
     
Thread Status:
Not open for further replies.

Share This Page