Solved Timers

Discussion in 'Plugin Development' started by fatpigsarefat, Jan 31, 2016.

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

    fatpigsarefat

    Ok, so I have this problem.
    In my onEnable() I have a repeating timer, which checks if a player is wearing armor with a certain lore and if they are, gives the a status/potion effect. The problem is, if I'm wearing a leather chestplate without wearing boots, it goes and checks if I have boots on but it doesn't bother checking is I have a chestplate on. If I am wearing boots on it will also check if I'm wearing a chestplate. Here is my code:

    Code:
        @Override
        public void onEnable() {
            Bukkit.getServer().getPluginManager().registerEvents(this, this);
            this.getServer().getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
                
                @Override
                  public void run() {
                      for (Player player : Bukkit.getServer().getOnlinePlayers()) {
                         
                          if (!player.getInventory().getBoots().equals(Material.AIR)) {
    
                              ItemStack ruggedboots = new ItemStack(player.getInventory().getBoots());
                                List ruggedbootslore = new ArrayList();
                                ruggedbootslore.add("A pair of old rugged boots");
                                ruggedbootslore.add(ChatColor.RED + "");
                                ruggedbootslore.add(ChatColor.GRAY + "Speed V");
                               
                               
                              if (ruggedboots.getItemMeta().getLore().equals(ruggedbootslore)) {
                                  player.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, 30, 5));
                              } else {
                                  player.removePotionEffect(PotionEffectType.SPEED);
                              }
                             
                          }
                         
                          if (!player.getInventory().getChestplate().equals(Material.AIR)) {
    
                              ItemStack cushionedchest = new ItemStack(player.getInventory().getChestplate());
                                List cushionedchestl = new ArrayList();
                                cushionedchestl.add("A vest with a few pillows tied to it");
                                cushionedchestl.add(ChatColor.RED + "");
                                cushionedchestl.add(ChatColor.GRAY + "Resistance III");
                                 
                              if (cushionedchest.getItemMeta().getLore().equals(cushionedchestl)) {
                                  player.addPotionEffect(new PotionEffect(PotionEffectType.DAMAGE_RESISTANCE, 30, 2));
                              }                         
                             
                          }
                      }
                  }
                }, 1, 1);
        }
    I am fairly new to coding and I'm probably missing something REALLY obvious here.

    Probably not the best way, but I fixed it myself with two different timers:

    Code:
        @Override
        public void onEnable() {
            Bukkit.getServer().getPluginManager().registerEvents(this, this);
            this.getServer().getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
                
                @Override
                  public void run() {
                      for (Player player : Bukkit.getServer().getOnlinePlayers()) {
                         
                            List cushionedchestl = new ArrayList();
                            cushionedchestl.add("A vest with a few pillows tied to it");
                            cushionedchestl.add(ChatColor.RED + "");
                            cushionedchestl.add(ChatColor.GRAY + "Resistance III");
    
                              if (player.getInventory().getChestplate() != null) {
                                  if (player.getInventory().getChestplate().getItemMeta().getLore().equals(cushionedchestl)) {
                                      player.addPotionEffect(new PotionEffect(PotionEffectType.DAMAGE_RESISTANCE, 30, 2));
                                  }
                              }
    
                          }
                
                         
                      }
                     
                 
                }, 0L, 1L);
           
            this.getServer().getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
                
                @Override
                  public void run() {
                      for (Player player : Bukkit.getServer().getOnlinePlayers()) {
    
                            List ruggedbootslore = new ArrayList();
                            ruggedbootslore.add("A pair of old rugged boots");
                            ruggedbootslore.add(ChatColor.RED + "");
                            ruggedbootslore.add(ChatColor.GRAY + "Speed IV");
    
                            if (player.getInventory().getBoots() != null) {
                                  if (player.getInventory().getBoots().getItemMeta().getLore().equals(ruggedbootslore)) {
                                    player.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, 30, 3));
                                  }
                            }
    
                          }
                
                         
                      }
                     
                 
                }, 0L, 1L);
        }
        
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 10, 2016
Thread Status:
Not open for further replies.

Share This Page