Solved Repeating a task.

Discussion in 'Plugin Help/Development/Requests' started by oceantheskatr, Apr 20, 2015.

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

    oceantheskatr

    Hi, I'm having a bit of trouble with my code. I'm making a plugin that will rename the compass every 10 ticks with the format "Closest player is: player, who is # blocks away".

    I have successfully set this up so that it gets the closest player and the distance, and even point the compass towards them. The problem I am now having is that it'll only run through the code once, even though it's in a repeating task. It seems that it will only modify compasses without any item meta. Once it shows closest player the first time, it stops changing the name or updating the other player's location on the compass.

    I was hoping to get some assistance in fixing this, I'm sure it's something silly that I missed! :)

    [​IMG]

    Code:
    package me.themineshack;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.entity.Player;
    import org.bukkit.event.Listener;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class PlayerCompass extends JavaPlugin implements Listener {
    
        @Override
        public void onEnable() {
            Bukkit.getServer().getPluginManager().registerEvents(this, this);
            this.getServer().getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
                public void run() {
                    for (Player player : Bukkit.getServer().getOnlinePlayers()) { // Checking all players on server
                        ItemStack item = new ItemStack(Material.COMPASS);
                        if (player.getInventory().containsAtLeast(item, 1)){ // If player inv has at least one compass.
                            Player result = null;
                            double lastDistance = Double.MAX_VALUE;
                            for(Player p : player.getWorld().getPlayers()) { // If player is equal to nearby players
                               if(player == p)
                                   continue;
                            
                               double distance = player.getLocation().distance(p.getLocation());
                               if(distance < lastDistance) {
                                   lastDistance = distance; // Distance of other player closest to player
                                   result = p; // Other player's name
                                  
                               }
                            }
                            if (result != null) {
                                final Player res = result;
                                final int lastD = (int)lastDistance;
                                player.setCompassTarget(res.getLocation());
                              
                                final ItemStack[] stacks = player.getInventory().getContents();
                                 for(ItemStack stack : stacks) { // Search inventory
                                       if(stack == null )
                                           continue;
                                       if(stack.getType() == Material.COMPASS) { // If inventory slot has a compass
                                          ItemMeta meta = stack.getItemMeta();
                                          String title = ChatColor.WHITE + "Closest player is: " + ChatColor.RED + res.getName() + ChatColor.WHITE + ", who is "  + ChatColor.RED + lastD + ChatColor.WHITE + " blocks away.";
                                          meta.setDisplayName(title);
                                          stack.setItemMeta(meta); // Set custom name
                                       }
                                   }
                                
                               // player.sendMessage("Closest player is: " + ChatColor.RED + res.getName() + ChatColor.WHITE + ", who is "  + ChatColor.RED + lastD + ChatColor.WHITE + " blocks away.");
                            } else {
                                player.sendMessage(ChatColor.RED + "No players found.");
                            }
                        }
                    }
                }
            }, 20L, 10L); // Repeat every half second
        }
      
        @Override
        public void onDisable() {
        }
    }
    Tagging the great @I Al Istannen due to previous posts :)
     
  2. Offline

    I Al Istannen

    @oceantheskatr Late reply, but:
    You check if the inventory contains an ItemStack! That includes item Meta. Your new ItemStack(Material.Compass) has not the same itemMeta as an renamed Compass, because the displayname changed! Use inventory.contains(Material.Compass) instead. Should fix it ;)

    And you could use a BukkitRunnable. Not sure if it makes a difference, a Runnable is a java Interface, but you can cancel the BukkitRunnable via super.cancel(). Quite neat sometimes ;)
    I think you start a BukkitRunnable via: runnableName.runWhatEverYouWant()
     
  3. Offline

    oceantheskatr

  4. Offline

    I Al Istannen

Thread Status:
Not open for further replies.

Share This Page