How to get location in a sphere instead of a square?

Discussion in 'Plugin Development' started by PimpDuck, Sep 6, 2013.

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

    PimpDuck

    Okay, so my plugin is like the essentials /near but the distance is editable in the config. My problem I'm having is this. Lets say the distance is set to 100. Well, if I go north, east, south, or west past 100 blocks it doesn't let me see any further (Like it's suppose to) but lets say I go diagonal from the player. Once I do this even though I have it set to 100 it lets me see up to like 160??? I think this is because it's getting the location in a square. How would I make it get the location in a sphere so this doesn't happen? My code is all here: https://github.com/PimpDuck/DCNear

    Please help, and thanks :)

    chasechocolate It seems like you're the only person experienced enough and knows how to do this... Could you help? :/ This is the last bug that needs fixed then the plugins done :)

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 4, 2016
  2. Offline

    kreashenz

    A friend made me this method.
    Code:java
    1. private static List<Location> circle(Location loc, int radius, int height, boolean hollow, boolean sphere, int plusY){
    2. List<Location> circleblocks = new ArrayList<Location>();
    3. int cx = loc.getBlockX();
    4. int cy = loc.getBlockY();
    5. int cz = loc.getBlockZ();
    6.  
    7. for(int x = cx - radius; x <= cx + radius; x++){
    8. for (int z = cz - radius; z <= cz + radius; z++){
    9. for(int y = (sphere ? cy - radius : cy); y < (sphere ? cy + radius : cy + height); y++){
    10. double dist = (cx - x) * (cx - x) + (cz - z) * (cz - z) + (sphere ? (cy - y) * (cy - y) : 0);
    11.  
    12. if(dist < radius * radius && !(hollow && dist < (radius - 1) * (radius - 1))){
    13. Location l = new Location(loc.getWorld(), x, y + plusY, z);
    14. circleblocks.add(l);
    15. }
    16. }
    17. }
    18. }
    19.  
    20. return circleblocks;
    21. }


    PimpDuck
     
  3. Offline

    PimpDuck

    kreashenz So how exactly would this work with my code? All my code is here: https://github.com/PimpDuck/DCNear If you helped it would mean ALOT. I mean, this is the last bug on my plugin then I'm done :)
     
  4. Offline

    kreashenz

    Just try..

    // In the command
    circle(player.getLocation(), 50, 20, false, true, 0);
     
  5. Offline

    PimpDuck

    kreashenz I'm not sure what you mean by "in the command" Can you be a little more specific?
     
  6. Offline

    kreashenz

    PimpDuck

    // command name
    // check if the sender is a player
    // player has permission
    circle(player.getLocation(), 50, 20, false, true, 0);
    }
    }
    }
     
  7. Offline

    PimpDuck

    kreashenz I added
    Code:
      private static List<Location> circle(Location loc, int radius, int height, boolean hollow, boolean sphere, int plusY){
                List<Location> circleblocks = new ArrayList<Location>();
                int cx = loc.getBlockX();
                int cy = loc.getBlockY();
                int cz = loc.getBlockZ();
     
                for(int x = cx - radius; x <= cx + radius; x++){
                    for (int z = cz - radius; z <= cz + radius; z++){
                        for(int y = (sphere ? cy - radius : cy); y < (sphere ? cy + radius : cy + height); y++){
                            double dist = (cx - x) * (cx - x) + (cz - z) * (cz - z) + (sphere ? (cy - y) * (cy - y) : 0);
     
                            if(dist < radius * radius && !(hollow && dist < (radius - 1) * (radius - 1))){
                                Location l = new Location(loc.getWorld(), x, y + plusY, z);
                                circleblocks.add(l);
                            }
                        }
                    }
                }
     
                return circleblocks;
            }
    to my class. Now it looks like
    Code:java
    1. package me.PimpDuck.DCNear;
    2.  
    3.  
    4.  
    5. import java.util.*;
    6.  
    7. import org.bukkit.ChatColor;
    8. import org.bukkit.Location;
    9. import org.bukkit.command.Command;
    10. import org.bukkit.command.CommandExecutor;
    11. import org.bukkit.command.CommandSender;
    12. import org.bukkit.entity.Entity;
    13. import org.bukkit.entity.Player;
    14.  
    15. public class CommandClass implements CommandExecutor {
    16.  
    17. List<String> nearby = new ArrayList<String>();
    18.  
    19. private Main plugin;
    20.  
    21. public CommandClass(Main instance)
    22. {
    23. this.plugin = instance;
    24. }
    25.  
    26. private static List<Location> circle(Location loc, int radius, int height, boolean hollow, boolean sphere, int plusY){
    27. List<Location> circleblocks = new ArrayList<Location>();
    28. int cx = loc.getBlockX();
    29. int cy = loc.getBlockY();
    30. int cz = loc.getBlockZ();
    31.  
    32. for(int x = cx - radius; x <= cx + radius; x++){
    33. for (int z = cz - radius; z <= cz + radius; z++){
    34. for(int y = (sphere ? cy - radius : cy); y < (sphere ? cy + radius : cy + height); y++){
    35. double dist = (cx - x) * (cx - x) + (cz - z) * (cz - z) + (sphere ? (cy - y) * (cy - y) : 0);
    36.  
    37. if(dist < radius * radius && !(hollow && dist < (radius - 1) * (radius - 1))){
    38. Location l = new Location(loc.getWorld(), x, y + plusY, z);
    39. circleblocks.add(l);
    40. }
    41. }
    42. }
    43. }
    44.  
    45. return circleblocks;
    46. }
    47.  
    48. @Override
    49. public boolean onCommand(CommandSender sender, Command cmd, String command, String[] args) {
    50. if(cmd.getName().equalsIgnoreCase("near")){
    51. if ((sender instanceof Player)) {
    52. Player player = (Player) sender;
    53. double range = 0;
    54. if (player.hasPermission("dcnear.god") || player.hasPermission("dcnear.ub3r") || player.hasPermission("dcnear.legend") || player.hasPermission("dcnear.super") || player.hasPermission("dcnear.*")) {
    55.  
    56. if (args.length > 1) {
    57. player.sendMessage(ChatColor.DARK_BLUE + "[DCNear] " + ChatColor.DARK_RED + "Too many arguments!");
    58. return false;
    59. }
    60. if (args.length == 1 && player.hasPermission("dcnear.*")) {
    61. try {
    62. range = Double.parseDouble(args[0]);
    63. } catch (NumberFormatException nfe) {
    64. player.sendMessage(ChatColor.DARK_BLUE + "[DCNear] " + ChatColor.DARK_RED + "Argument must be a number!");
    65. return false;
    66. }
    67. }
    68. if (args.length == 0 && player.hasPermission("dcnear.god")) {
    69. range = this.plugin.getConfig().getDouble("god");
    70. }
    71. if(args.length == 0 && player.hasPermission("dcnear.ub3r")){
    72. range = this.plugin.getConfig().getDouble("ub3r");
    73. }
    74. if(args.length == 0 && player.hasPermission("dcnear.legend")){
    75. range = this.plugin.getConfig().getDouble("legend");
    76. }
    77. if(args.length == 0 && player.hasPermission("dcnear.super")){
    78. range = this.plugin.getConfig().getDouble("super");
    79. }
    80.  
    81. if (range != 0 ) {
    82. Location start_loc = player.getLocation();
    83. StringBuilder sb = new StringBuilder();
    84. for (Entity nearbyEntity : player.getNearbyEntities(range, range, range)) {
    85. if (nearbyEntity instanceof Player) {
    86. nearby.add(((Player) nearbyEntity).getName());
    87. Location end_loc = nearbyEntity.getLocation();
    88. int distance = (int) start_loc.distance(end_loc);
    89. sb.append(((Player) nearbyEntity).getName()).append(" (").append(ChatColor.DARK_RED).append(distance).append("m").append(ChatColor.WHITE).append("), ");
    90. }
    91. }
    92. String message;
    93. if (sb.length() > 0) {
    94. message = sb.toString().substring(0, (sb.length() - 2));
    95. } else {
    96. message = "none";
    97. }
    98. player.sendMessage(ChatColor.GOLD + "Players nearby: " + ChatColor.WHITE + message);
    99. }
    100. } else {
    101. sender.sendMessage(ChatColor.DARK_BLUE + "[DCNear] " + ChatColor.DARK_RED + "You do not have permission use this command!");
    102. return false;
    103. }
    104. } else {
    105. sender.sendMessage(ChatColor.DARK_BLUE + "[DCNear] " + ChatColor.DARK_RED + "Only in game players can use this command!");
    106. return false;
    107. }
    108. return true;
    109. }
    110. return false;
    111. }
    112. }


    So looking at this code, where would I put that statement without causing errors and so everything else still works fluently.
     
  8. Offline

    chasechocolate

  9. Offline

    PimpDuck

    chasechocolate Do you see any way of fixing it? :/ Like I said, I'm pretty much done except this :)
     
  10. Offline

    chasechocolate

    PimpDuck I'm just gonna submit a pull request to the repo, and hopefully you will be able to see/understand what I am doing :)
     
  11. Offline

    PimpDuck

    chasechocolate Oh that would be great and mean alot man :)

    chasechocolate Thanks man, it works. I owe you alot. You're a great guy and deserve your staff position! :)

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 4, 2016
  12. Offline

    kreashenz

    chasechocolate Wow! Haha, my friend must have gotten it from you. Thanks for that, it helped me a lot in a plugin :)
     
    chasechocolate likes this.
  13. Offline

    Musaddict

    Sorry for taking so long to reply to you PimpDuck. I replied to you in your other thread. It was a simple edit:


    You originally had this:
    Code:java
    1. for (Entity nearbyEntity : player.getNearbyEntities(range, range, range)) {
    2. if (nearbyEntity instanceof Player) {
    3. Location end_loc = nearbyEntity.getLocation();
    4. int distance = (int) start_loc.distance(end_loc);
    5.  
    6. if(distance <= range){
    7. sb.append(((Player) nearbyEntity).getName()).append("(").append(ChatColor.DARK_RED).append(distance).append("m").append(ChatColor.WHITE).append("), ");
    8. }
    9. }
    10. }


    And it could have been changed simply to this:

    Code:java
    1. for (Player nearbyPlayer : player.getServer().getOnlinePlayers()) {
    2. Location end_loc = nearbyPlayer.getLocation();
    3. int distance = (int) player.getLocation().distance(end_loc);
    4.  
    5. if(distance <= range){
    6. sb.append(((Player) nearbyEntity).getName()).append("(").append(ChatColor.DARK_RED).append(distance).append("m").append(ChatColor.WHITE).append("), ");
    7. }
    8. }
     
    PimpDuck likes this.
Thread Status:
Not open for further replies.

Share This Page