[Help]Delay between lines of code.

Discussion in 'Plugin Development' started by alex123099, May 11, 2013.

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

    alex123099

    I have this code:

    Code:
    int mp = caster.getFoodLevel();
    if(mp>=ARMAGEDDON_MP){
    Location loc = caster.getLocation();
    int cx = loc.getBlockX();
           int cy = loc.getBlockY();
           int cz = loc.getBlockZ();
           World w = loc.getWorld();
           int rSquared = radius * radius;
           
           for (int x = cx - radius; x <= cx +radius; x++) {
                   for (int z = cz - radius; z <= cz +radius; z++) {
                           if ((cx - x) * (cx -x) + (cz - z) * (cz - z) <= rSquared && (cx - x) * (cx -x) + (cz - z) * (cz - z) > 4 /*2 squared*/) {
                               final Location l = new Location(w, x, cy, z);
               if(l.getBlock().getType().toString().equalsIgnoreCase("air"))
               l.getBlock().setType(Material.FIRE);
                           }
                   }
           }
    caster.sendMessage(ChatColor.RED + "Burn Baby Burn!");
    caster.setFoodLevel(mp-ARMAGEDDON_MP);
    w.playSound(loc, Sound.EXPLODE, 70, 0);
    }else
    caster.sendMessage(ChatColor.RED + "Not enough mana!");
    
    What it does is summon a circle of fire around the player. The problem is that it spawns the circle instantly, but what I want is for each block of fire to appear in serial, as in one after the other with a little delay in between them. In other words, I want first block to be lit on fire, then second one, then third one and so forth.

    I'm pretty new to scheduler programming so I am not sure how this should be done properly, so any help would be much appreciated.
     
  2. Offline

    Barinade

    Scheduler
     
  3. Offline

    alex123099

    I realize I need to use scheduler, but I am not sure how.
     
  4. Offline

    Compressions

  5. Offline

    alex123099

    I understand the concept of scheduling, I am using it in my code at specific parts, I just don't understand how I will turn the code I have now into what I want it to do o.o I've tried scheduling the code that lights up a fire at a certain location, but it still spawns the whole thing at once, so I think I'm doing something wrong 0.0
     
  6. Offline

    Sagacious_Zed Bukkit Docs

    alex123099
    Whatever you want to run latter. you put inside an anonymous bukkit runnable if you wish then invoke the the method to run the task latter..
     
  7. Offline

    alex123099

    I've tried doing:
    Code:
    for (int x = cx - radius; x <= cx +radius; x++) {
                    for (int z = cz - radius; z <= cz +radius; z++) {
                        if ((cx - x) * (cx -x) + (cz - z) * (cz - z) <= rSquared && (cx - x) * (cx -x) + (cz - z) * (cz - z) > 4 /*2 squared*/) {
                       final Location l = new Location(w, x, cy, z);
       if(l.getBlock().getType().toString().equalsIgnoreCase("air"))
                           caster.getServer().getScheduler().runTaskLater(plugin, new Runnable(){
                           public void run(){
                           l.getBlock().setType(Material.FIRE);
                           }
                           }, 5L);
                       
       
                        }
                    }
           }
    
    But the result is still the same, the fire shows up instantly and not block by block
     
  8. Offline

    alex123099

  9. Offline

    sirrus86

    Your method is doing a search of all applicable blocks, then making a change to all of them at once. If you're looking to have the ring of fire sort of spin around the user, you may need some trigonometry (which I suck at, sorry).

    I can however give an example of how to use a scheduler to do incremental calculations (which you'd need for this ultimately):
    Code:
    private int step = 0;
     
    private BukkitRunnable task = new BukkitRunnable() {
        @Override
        public void run() {
            if (step > 0) {
                // function involving "step"
                step --;
            }
            else {
                this.cancel();
            }
        }
    }
     
    public void doStep() {
        step = 30;
        task.runTaskTimer(plugin, 0L, 20L);
    }
    Of course there are probably better ways, this is just the way I'm comfortable with.
     
Thread Status:
Not open for further replies.

Share This Page