Solved /near with 75 blocks range?

Discussion in 'Plugin Development' started by BeastCraft3, Nov 23, 2014.

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

    BeastCraft3

    Hellau, how do make a simple /near command with 75 blocks range? this is how far I've come:
    Code:java
    1. package com.Beast.KeptenCustom;
    2.  
    3. import java.util.ArrayList;
    4. import java.util.List;
    5.  
    6. import org.bukkit.Bukkit;
    7. import org.bukkit.command.Command;
    8. import org.bukkit.command.CommandExecutor;
    9. import org.bukkit.command.CommandSender;
    10. import org.bukkit.entity.Player;
    11.  
    12. public class Near implements CommandExecutor {
    13.  
    14. List<String> distance = new ArrayList<String>();
    15.  
    16. @Override
    17. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    18. if(!(sender instanceof Player)) {
    19. sender.sendMessage("Only players can use this command");
    20. return true;
    21. }
    22. if(cmd.getName().equalsIgnoreCase("near")) {
    23. Player p = (Player) sender;
    24. for (Player target : Bukkit.getOnlinePlayers()) {
    25. if (!target.getName().equalsIgnoreCase(p.getName()) &&
    26. p.getWorld().getName().equalsIgnoreCase(target.getWorld().getName())) {
    27.  
    28. }
    29. }
    30. }
    31. return false;
    32. }
    33.  
    34. }
    35.  
     
  2. Offline

    mrCookieSlime

    BeastCraft3
    Use the method player.getNearbyEntities(75D, 75D, 75D) and then loop through them and check whether they are a Player or not.
     
  3. Offline

    BeastCraft3

    mrCookieSlime
    what does "D" means and how do I check if the nearby entities is a human or not?
     
  4. Offline

    mrCookieSlime

    BeastCraft3
    D stands for Double.

    and simply use if (entity instanceof Player)
     
  5. Offline

    BeastCraft3

    mrCookieSlime
    ok thanks, do I need to do a != null check on the nearby entities? at least it stand I have to ;3
     
  6. Offline

    mrCookieSlime

    BeastCraft3
    It's always good to use as many null checks as possible. However Entities arent that often null, but better add one just to be extra safe.
     
  7. Offline

    BeastCraft3

    mrCookieSlime
    ok ;) I don't think I've ever been getting so good awnsers before :D.
    btw, instead of (entity instanceof Player) could I use (target instanceof Player) ??
     
  8. Offline

    mrCookieSlime

    BeastCraft3
    You could. After all it doesn't matter how you call your Variables.
     
  9. Offline

    BeastCraft3

    mrCookieSlime
    ok, one last thing:
    how would I make it display all the nearby players?
    i thought it was p.sendMessage("Nearby players: " + target);
    but then I remembered that target isn't a string.
    Should I save the nearby players in a list or a hashset maybe?
     
  10. Offline

    Skionz

    target.getName();
     
  11. Offline

    mrCookieSlime

    BeastCraft3
    Yep, you could store their names in a list and then print the list out in a readable format.
     
  12. Offline

    BeastCraft3

    Will this work?

    Code:java
    1. @Override
    2. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    3. if(!(sender instanceof Player)) {
    4. sender.sendMessage("Only players can use this command");
    5. return true;
    6. }
    7. if(cmd.getName().equalsIgnoreCase("near")) {
    8. Player p = (Player) sender;
    9. for (Player target : Bukkit.getOnlinePlayers()) {
    10. if (!target.getName().equalsIgnoreCase(p.getName()) &&
    11. p.getWorld().getName().equalsIgnoreCase(target.getWorld().getName())) {
    12. if(p.hasPermission("KeptenCustom.Near.75")) {
    13. if(p.getNearbyEntities(75D, 75D, 75D) != null) {
    14. p.sendMessage("§cTheres no players in the range of 75 blocks!");
    15. } else {
    16. if(target instanceof Player) {
    17. p.sendMessage("Nearby players: " + target.getName());
    18. }
    19. }
    20. }
    21.  
    22. }
    23. }
    24. }
    25. return false;
    26. }


    mrCookieSlime Skionz
    sorry for double posting, I forgot to tahg you :p

    <Edit by mrCookieSlime: Merged Posts. Please dont double post. There is an Edit-Button right next to the Date.>
     
  13. Offline

    mrCookieSlime

    BeastCraft3
    No. p.getNearbyEntities() returns a list of all nearby entities. Make your loop using that.
    for (Entity nearby: p.getNearbyEntities(75D, 75D, 75D))
     
  14. Offline

    BeastCraft3

    mrCookieSlime
    Anything wrong now?
    Code:java
    1. @Override
    2. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    3. if(!(sender instanceof Player)) {
    4. sender.sendMessage("Only players can use this command");
    5. return true;
    6. }
    7. if(cmd.getName().equalsIgnoreCase("near")) {
    8. Player p = (Player) sender;
    9. for (Player target : Bukkit.getOnlinePlayers()) {
    10. if (!target.getName().equalsIgnoreCase(p.getName()) &&
    11. p.getWorld().getName().equalsIgnoreCase(target.getWorld().getName())) {
    12. if(p.hasPermission("KeptenCustom.Near.75")) {
    13. for (Entity nearby: p.getNearbyEntities(75D, 75D, 75D));
    14. if(target instanceof Player) {
    15. p.sendMessage("Nearby players: " + target.getDisplayName());
    16. }
    17. }
    18.  
    19. }
    20. }
    21. }
    22. return false;
    23. }
     
  15. Offline

    mrCookieSlime

    Yes. Why do you have a for loop looping through all online Players?
    The only loop you want to have here is the Entity loop.
     
  16. Offline

    WampyCakes

    Here I have remade it to work, I didn't test it but I am pretty sure it'll work.

    Code:java
    1. package com.Beast.KeptenCustom;
    2.  
    3. import org.bukkit.ChatColor;
    4. import org.bukkit.command.Command;
    5. import org.bukkit.command.CommandSender;
    6. import org.bukkit.entity.Entity;
    7. import org.bukkit.entity.Player;
    8.  
    9. public class Near{
    10.  
    11. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
    12. if(!(sender instanceof Player)) {
    13. sender.sendMessage("Only players can use this command");
    14. return false;
    15. }else if(sender instanceof Player && commandLabel.equalsIgnoreCase("near")) {
    16. Player player = (Player) sender;
    17. if(player.hasPermission("KeptenCustom.Near.75")) {
    18. for(Entity e : player.getNearbyEntities(75, 75, 75)){
    19. if(e instanceof Player){
    20. player.sendMessage(ChatColor.BLUE + "Nearby players within 75 blocks: " + e);
    21. }else if(!(e instanceof Player))
    22. return false;
    23.  
    24. }
    25. }
    26. }
    27. return false;
    28. }
    29.  
    30. }
    31.  
    32.  
    33.  
    34.  


    Also I am assuming you already have near command and your permissions setup correctly in the plugin.yml.
    Feel free to change what it spits out for text on lines 13, and 20!
    Hope this helps.
     
  17. Offline

    BeastCraft3

    mrCookieSlime
    when I do
    Entity e = (Entity) sender;
    if(e instanceof Player) {
    like this?
     
  18. Offline

    mrCookieSlime

    WampyCakes
    Won't work. Entities are not Strings. You cannot send an entitiy through the chat. Well at least not without any big stamps...

    BeastCraft3
    Nope. I am talking about the for-loop.

     
  19. Offline

    BeastCraft3

    mrCookieSlime
    I can't have nearby as an instance: nearby cannot be resolved to a variable
     
  20. Offline

    mrCookieSlime

    BeastCraft3
    Yes. Take a look at the Code I sent you.
     
  21. Offline

    BeastCraft3

    mrCookieSlime
    I can't have nearby as an instance: nearby cannot be resolved to a variable
     
  22. Offline

    mrCookieSlime

  23. Offline

    BeastCraft3

    mrCookieSlime
    fine now? btw, I had to cast nearby to a player ;3
    Code:java
    1. @Override
    2. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    3. if(!(sender instanceof Player)) {
    4. sender.sendMessage("Only players can use this command");
    5. return true;
    6. }
    7. if(cmd.getName().equalsIgnoreCase("near")) {
    8. Player p = (Player) sender;
    9. if(p.hasPermission("KeptenCustom.Near.75")) {
    10. for (Entity nearby : p.getNearbyEntities(75D, 75D, 75D)) {
    11. if(nearby instanceof Player) {
    12. p.sendMessage("Nearby players: " + ((Player) nearby).getDisplayName());
    13. }
    14. }
    15. }
    16. }
    17. return false;
    18. }
     
  24. Offline

    mrCookieSlime

    BeastCraft3
    Yep. That would work. However it would spam your Chat quite a bit as it sends for every nearby Player a new message.

    I suggest you to store their Names in an ArrayList.
    And then do the following after the for-loop:
    Code:java
    1. String players = "";
    2. for (String player: list) {
    3. if (list.indexOf(player) < list.size() - 1) players = players + player + ", ";
    4. else players = players + player
    5. }
    6. p.sendMessage("Nearby Players: " + players);
     
  25. Offline

    BeastCraft3

    mrCookieSlime
    List<String> list = new ArrayList<String>(); ?
    like that ^ it's suppost to be a string right?
     
  26. Offline

    mrCookieSlime

    BeastCraft3
    Yes and then add the displayname of the player to this list where you currently send the message.
     
  27. Offline

    BeastCraft3

    mrCookieSlime
    is it okey now?
    Code:java
    1. @Override
    2. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    3. if(!(sender instanceof Player)) {
    4. sender.sendMessage("Only players can use this command");
    5. return true;
    6. }
    7. if(cmd.getName().equalsIgnoreCase("near")) {
    8. Player p = (Player) sender;
    9. if(p.hasPermission("KeptenCustom.Near.75")) {
    10. for (Entity nearby : p.getNearbyEntities(75D, 75D, 75D)) {
    11. if(nearby instanceof Player) {
    12. String players = "";
    13. for (String player: list) {
    14. if (list.indexOf(player) < list.size() - 1) players = players + player + ", ";
    15. else players = players + player;
    16. }
    17. list.add(((Player) nearby).getDisplayName());
    18. p.sendMessage("Nearby Players: " + players);
    19. }
    20. }
    21. }
    22. }
    23. return false;
    24. }
     
  28. Offline

    mrCookieSlime

    BeastCraft3
    Almost.
    As I said, the inner for loop needs to be after the first for loop not inside.
     
  29. Offline

    BeastCraft3

    mrCookieSlime
    like this? with after the first for loop I think you ment after the } right?
    Code:java
    1. package com.Beast.KeptenCustom;
    2.  
    3. import java.util.ArrayList;
    4. import java.util.List;
    5.  
    6. import org.bukkit.command.Command;
    7. import org.bukkit.command.CommandExecutor;
    8. import org.bukkit.command.CommandSender;
    9. import org.bukkit.entity.Entity;
    10. import org.bukkit.entity.Player;
    11.  
    12. public class Near implements CommandExecutor {
    13.  
    14. List<String> list = new ArrayList<String>();
    15.  
    16. public Main plugin;
    17. public Near(Main instance) {
    18. plugin = instance;
    19. }
    20.  
    21. @Override
    22. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    23. if(!(sender instanceof Player)) {
    24. sender.sendMessage("Only players can use this command");
    25. return true;
    26. }
    27. String players = "";
    28. if(cmd.getName().equalsIgnoreCase("near")) {
    29. Player p = (Player) sender;
    30. if(p.hasPermission("KeptenCustom.Near.75")) {
    31. for (Entity nearby : p.getNearbyEntities(75D, 75D, 75D)) {
    32. if(nearby instanceof Player) {
    33. list.add(((Player) nearby).getDisplayName());
    34. p.sendMessage("Nearby Players: " + players);
    35. }
    36. }
    37. for (String player: list) {
    38. if (list.indexOf(player) < list.size() - 1) players = players + player + ", ";
    39. else players = players + player;
    40. }
    41. }
    42. }
    43. return false;
    44. }
    45.  
    46. }
    47.  
     
  30. Op
    String concatenation is bad (e.g. see here). You should use a StringBuilder. Apache also offers a method called StringUtils#join().
     
Thread Status:
Not open for further replies.

Share This Page