Solved Weird problem: code running faster every time...

Discussion in 'Plugin Development' started by CraterHater, Sep 4, 2015.

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

    CraterHater

    So I've got this for a special skill and it runs everytime a bit faster... super weird. this is my code
    Note that int "s" has been declared as
    private int s = 0;
    and this code is run using a simple playerinteractevent.

    here's my code:
    Code:
                      if (Cooldowns.tryCooldown(p, "robin8", 1000)) {
                         s = 0;
                          BukkitScheduler scheduler = Bukkit.getServer().getScheduler();
                          scheduler.scheduleSyncRepeatingTask(this, new Runnable() {
                              @Override
                              public void run() {
                                  if(s != 40){
                                  Vector dir = p.getLocation().getDirection().multiply(1);
                                  Vector vec = new Vector(dir.getX(), 0, dir.getZ());
                                  Block b = p.getLocation().add(vec).getWorld().getHighestBlockAt(p.getLocation().add(vec));
                                  Vector dir1 = p.getLocation().getDirection().multiply(s);
                                  Vector vec1 = new Vector(dir1.getX(), 0, dir1.getZ());
                                  Location loc = b.getLocation().add(vec1);
                                  p.getWorld().playEffect(loc, Effect.STEP_SOUND, Material.STONE);
                                  Entity irongolem = loc.getWorld().spawnEntity(loc, EntityType.ARMOR_STAND);
                                  ArmorStand irongolem1=(ArmorStand)irongolem;
                                  irongolem1.setVisible(false);
                                  if(s != 15){
                                
                                  s++;
                                  p.sendMessage(s + "");
                                  for(Entity e : irongolem1.getNearbyEntities(4, 4, 4)){
                                       if(e instanceof LivingEntity){
                                           if(e != p){
                                           ((LivingEntity) e).addPotionEffect(new PotionEffect(PotionEffectType.CONFUSION, 100, 1, true));
                                           ((LivingEntity) e).addPotionEffect(new PotionEffect(PotionEffectType.SLOW, 100, 1, true));
                                           ((LivingEntity) e).addPotionEffect(new PotionEffect(PotionEffectType.WEAKNESS, 100, 1, true));
                                           ((LivingEntity) e).damage(6D);
                                           }
                                         
                                       }
                                  }
                                       }else{
                                           for(Entity e : p.getNearbyEntities(25, 25, 25)){
                                           if(e instanceof ArmorStand){
                                               e.remove();
                                           s = 40;
                                           }
                                           }
                                         
                                       }
                                  }
                                
                              }
                            
                          }, 0L, 2L);
              
                      }
                  }else{
                      p.sendMessage("§b§2[Spells]:§rYou have " + (Cooldowns.getCooldown(p, "robin8") / 1000) + " seconds left!");
                      p.playSound(p.getLocation(), Sound.NOTE_PLING, 3, 3);
                  }
                
              }
          }
      }
    Anyone?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 11, 2016
  2. @CraterHater
    This is highly inefficient :/
    You're creating a new repeating task each time this part is called, this has 2 disadvantages:
    1) Your issue
    2) Increasing memory usage
     
  3. Offline

    CraterHater

    Auch... that is dumb of me, but how do I prevent that from happening?

    Also this is just one time going trough s = 0; cause you only click once here, so he should only make it once.
     
    Last edited: Sep 4, 2015
  4. @CraterHater
    Create a list, and at this moment add the player to the list. In your on enable set up a task like this, but instead you iterate over all players in the list and do the stuff.
     
    CraterHater likes this.
  5. Offline

    CraterHater

    @megamichiel thanks! that worked, just one thing I know I can't store Players in a list cause it is too inefficient but how would I convert String containing PlayerName to Player?
     
  6. CraterHater likes this.
Thread Status:
Not open for further replies.

Share This Page