Help with some code? :/

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??? Someone told me " it seems as if it is done by increasing the bounding box of the player by that size. If you want it with diagonals, then check if the distance is < 100 before appending it to the StringBuilder." but I don't understand why he said distance is < 100 if it's editable in the config? Would I have to check if the distance is < getConfig().getDouble("god"){ } ? My code is all here: https://github.com/PimpDuck/DCNear

    Can anyone help?

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

    ZeusAllMighty11

    Please keep bumping to a minimum time of once every 24 hours.

    Player's only have a certain visible distance, and due to that you can't do ridiculous numbers.
    Are you sure you're counting diagonals correctly? Remember that locations are a double, so the exact possible diagonal will not be accurate to your's, as it literally uses decimals.
     
  3. Offline

    Musaddict

    You're searching for players within a cube of 100x100x100, instead of within a sphere with a radius of 100. Because of this, you could be standing at 0, 0, 0, and they could be standing at 100, 100, 100, and the hypotenuse between those two points would be 173.2.

    I believe there is a distance function built into Bukkit that will allow you to simply call it. Something like:

    player.getDistance(getNearbyEntities().getlocation())

    Or something like that.

    Edit: Additionally, instead of searching through all entities within the limited view distance of the player, you could simply use a for() to cycle through all the online players, get their location, and check if the player's location is less than 100 away from that online player.
     
  4. Offline

    PimpDuck

    Wouldn't this cause me to re-write the entire plugin?

    Musaddict I forgot to tag you^

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

    Musaddict

    Not necesarily, you as far as I can see you would just need to rewrite your for():
    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. }


    just use the for() to call a list of online players, and see if the distance between the player location and the online player's location is within "range". If so, then do stuff.

    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. }
     
Thread Status:
Not open for further replies.

Share This Page