Util Relative Coordinates

Discussion in 'Resources' started by JBoss925, Jun 23, 2016.

?

Is this at all useful?

  1. Ye

  2. nah

Results are only viewable after voting.
Thread Status:
Not open for further replies.
  1. Offline

    JBoss925

    Hey everyone, I made this and I thought it was pretty nifty, so I figured I'd share it.

    It is a Relative Location class which holds x, y, and z values relative to an origin.

    There are static methods in the class that you can call in order to manipulate regions such as getting relative coordinates of a list of locations relative to an origin and vice versa.

    I'm using it to store material type data with relative coordinates so that I can later translate them back into absolute location coordinates to be used in a game I'm making. It essentially lets me store chunks of locations and data about them such that they can be moved.

    You could also store data of players relative to each other and then restore there relative positions later or something like that (might add some implementation of that).

    I don't know what else it could really be used for, maybe a chess mini game (checking where pieces are relative to each other).

    I'm thinking about making a RelativeBlock object which keeps material and other data about blocks while using relative coordinates. If you'd like to see that I'll try to get around to programming it.

    Anyway, here's the code. Hopefully it's useful, probably not though.


    Code (open)

    Code:java
    1.  
    2. package your.package.here;
    3.  
    4. import org.bukkit.Location;
    5. import java.util.ArrayList;
    6. import java.util.List;
    7.  
    8. /**
    9. * Created by JBoss925 on 6/23/16.
    10. */
    11. public class RelativeCoordinates {
    12.  
    13. public int x;
    14. public int y;
    15. public int z;
    16.  
    17.  
    18. public RelativeCoordinates(int x, int y, int z) {
    19. this.x = x;
    20. this.y = y;
    21. this.z = z;
    22. }
    23.  
    24. public static List<Location> getAbsoluteLocations(List<RelativeCoordinates> list, Location origin){
    25. List<Location> returnList = new ArrayList<Location>();
    26. for(RelativeCoordinates r : list){
    27. double tempBlockX = (double) origin.getBlockX() + r.getX();
    28. double tempBlockY = (double) origin.getBlockY() + r.getY();
    29. double tempBlockZ = (double) origin.getBlockZ() + r.getZ();
    30. Location temploc = new Location(origin.getWorld(), tempBlockX, tempBlockY, tempBlockZ);
    31. returnList.add(temploc);
    32. }
    33. return returnList;
    34. }
    35.  
    36. public static List<RelativeCoordinates> getRelativeCoordinates(List<Location> locs, Location origin){
    37. List<RelativeCoordinates> relativeCoords = new ArrayList<RelativeCoordinates>();
    38. for(Location loc : locs){
    39. int tempRelX = loc.getBlockX() - origin.getBlockX();
    40. int tempRelY = loc.getBlockY() - origin.getBlockY();
    41. int tempRelZ = loc.getBlockZ() - origin.getBlockZ();
    42. RelativeCoordinates relComplete = new RelativeCoordinates(tempRelX, tempRelY, tempRelZ);
    43. relativeCoords.add(relComplete);
    44. }
    45. return relativeCoords;
    46. }
    47.  
    48. public static List<Location> getAbsoluteLocationsWithinTwoLocations(Location loc1, Location loc2){
    49. double startx = (double) Math.min(loc1.getBlockX(), loc2.getBlockX());
    50. double endx = (double) Math.max(loc1.getBlockX(), loc2.getBlockX());
    51. double starty = (double) Math.min(loc1.getBlockY(), loc2.getBlockY());
    52. double endy = (double) Math.max(loc1.getBlockY(), loc2.getBlockY());
    53. double startz = (double) Math.min(loc1.getBlockZ(), loc2.getBlockZ());
    54. double endz = (double) Math.max(loc1.getBlockZ(), loc2.getBlockZ());
    55. List<Location> locs = new ArrayList<Location>();
    56. for(double x = startx; x<= endx; x++){
    57. for(double y = starty; y<= endy; y++){
    58. for(double z = startz; z<= endz; z++){
    59. locs.add(new Location(loc1.getWorld(), x, y, z));
    60. }
    61. }
    62. }
    63. return locs;
    64. }
    65.  
    66. public static Location getOriginWithinTwoLocations(Location loc1, Location loc2){
    67. double middlex = (double) Math.min(loc1.getBlockX(), loc2.getBlockX()) + (Math.max(loc1.getBlockX(), loc2.getBlockX()) - Math.min(loc1.getBlockX(), loc2.getBlockX()));
    68. double middley = (double) Math.min(loc1.getBlockY(), loc2.getBlockY()) + (Math.max(loc1.getBlockY(), loc2.getBlockY()) - Math.min(loc1.getBlockY(), loc2.getBlockY()));
    69. double middlez = (double) Math.min(loc1.getBlockZ(), loc2.getBlockZ()) + (Math.max(loc1.getBlockZ(), loc2.getBlockZ()) - Math.min(loc1.getBlockZ(), loc2.getBlockZ()));
    70. return new Location(loc1.getWorld(), middlex, middley, middlez);
    71. }
    72.  
    73.  
    74. public int getX() {
    75. return x;
    76. }
    77.  
    78. public int getY() {
    79. return y;
    80. }
    81.  
    82. public int getZ() {
    83. return z;
    84. }
    85.  
    86. public void setX(int x) {
    87. this.x = x;
    88. }
    89.  
    90. public void setY(int y) {
    91. this.y = y;
    92. }
    93.  
    94. public void setZ(int z) {
    95. this.z = z;
    96. }
    97.  
    98. }
    99.  

     
  2. Offline

    MisterErwin

    @JBoss925 Why are the fields public and Getter/Setter functions exists?

    Why are you forcing to use Lists via the parameters? Why not Iterables so Sets work too?

    Why can't I see any javadocs or comments?
     
    bwfcwalshy likes this.
  3. Offline

    JBoss925

    As I progress further into the plugin I'll update it.

    I'll make some changes to see if I can make this thing into a real Util.
     
  4. Offline

    ChipDev

    I like it, although it is just simple math and you dont have comments :l
     
Thread Status:
Not open for further replies.

Share This Page