Solved Getting players in area and sending message

Discussion in 'Spigot Plugin Development' started by Wilsgaard, Jul 2, 2018.

  1. Hello! I'm trying to create a rolling command which is very simple, though I ran into an issue regarding sending the message in an area/radius instead of sending it to only that player. After searching around for some while I got nowhere, so I just decided to post here.

    Note! I'm quite new to Java so please don't go all hammering on me, though I would love advice, thanks!

    Here is the code: https://hastebin.com/savurojahu.java
     
  2. Since you asked for advice, what I normally do is keep my onCommand in my main class (which extends JavaPlugin), and once I discern which command I'm running (normally with a switch-case statement, or for extra fancy commands a splash of regex), I call a static method in another class. This allows me to avoid having to implement CommandExecutor everywhere, and having to register these. Just one less place to run into problems.

    As for your problem, there are a number of solutions. If you're doing a cuboid area, it's actually super easy. Simply grab two points and loop through all players in the targeted world. if there location is within your region, send the message (hint, you'll want to take advantage of Math.min() and Math.max()).
    If you want to get a region around a specific location, I find it easiest to use the getRelative method.
    Code (Java):
    Location point1 = player.getLocation().getBlock().getRelative(radius, radius, radius).;
    Location point2 = player.getLocation().getBlock().getRelative(-1 * radius, -1 * radius, -1 * radius);
    For spherical areas, its a little more complicated. You can find light weight solutions around on the forums with a little searching, but a more simplistic (with a mild performance hit) is to to call
    Code (Java):
    if (centerLocation.distance(player.getLocation()) <= radius) {}
    For even more complicated areas, Id recommend using the WorldGuard API.
     
    • Like Like x 1
  3. Thank you, I'll be giving it a shot and I'll try to get used to your way of doing the CommandExecutor part.
     
  4. Circular areas are the easiest and most light weight here.

    1. Get all players in the current world (you can optimize this with a little more complicated code, but that's for later).
    2. Compare the distance of each player to your location using the pythagorean theorem.
    3. If that distance is smaller than or equal to your radius, send the message to them.
     
    • Agree Agree x 1
  5. Click every link in @finnbon's signature. Just do it.
     
  6. That's really of no use here.
     
  7. Cool! I'll try out most of the methods here. Appreciate the quick responses guys. <3
     
  8. My apologies, I just got excited. It was an unprofessional comment.
    The logic I had going through my mind was the various uses of those tutorials, which perhaps not useful for this specific question, but if he continues to look into location-based calculations, they're incredibly helpful, even outside of spigot development in general. They're just good to know.
    (Also in-depth tutorials just make me happy)
     
    • Friendly Friendly x 1
  9. Thanks a lot for the fact you appreciate my tutorials! :D
     
    • Friendly Friendly x 1