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 (Move your mouse to reveal the content) Code (open) Code (close) Code:java package your.package.here; import org.bukkit.Location;import java.util.ArrayList;import java.util.List; /*** Created by JBoss925 on 6/23/16.*/public class RelativeCoordinates { public int x; public int y; public int z; public RelativeCoordinates(int x, int y, int z) { this.x = x; this.y = y; this.z = z; } public static List<Location> getAbsoluteLocations(List<RelativeCoordinates> list, Location origin){ List<Location> returnList = new ArrayList<Location>(); for(RelativeCoordinates r : list){ double tempBlockX = (double) origin.getBlockX() + r.getX(); double tempBlockY = (double) origin.getBlockY() + r.getY(); double tempBlockZ = (double) origin.getBlockZ() + r.getZ(); Location temploc = new Location(origin.getWorld(), tempBlockX, tempBlockY, tempBlockZ); returnList.add(temploc); } return returnList; } public static List<RelativeCoordinates> getRelativeCoordinates(List<Location> locs, Location origin){ List<RelativeCoordinates> relativeCoords = new ArrayList<RelativeCoordinates>(); for(Location loc : locs){ int tempRelX = loc.getBlockX() - origin.getBlockX(); int tempRelY = loc.getBlockY() - origin.getBlockY(); int tempRelZ = loc.getBlockZ() - origin.getBlockZ(); RelativeCoordinates relComplete = new RelativeCoordinates(tempRelX, tempRelY, tempRelZ); relativeCoords.add(relComplete); } return relativeCoords; } public static List<Location> getAbsoluteLocationsWithinTwoLocations(Location loc1, Location loc2){ double startx = (double) Math.min(loc1.getBlockX(), loc2.getBlockX()); double endx = (double) Math.max(loc1.getBlockX(), loc2.getBlockX()); double starty = (double) Math.min(loc1.getBlockY(), loc2.getBlockY()); double endy = (double) Math.max(loc1.getBlockY(), loc2.getBlockY()); double startz = (double) Math.min(loc1.getBlockZ(), loc2.getBlockZ()); double endz = (double) Math.max(loc1.getBlockZ(), loc2.getBlockZ()); List<Location> locs = new ArrayList<Location>(); for(double x = startx; x<= endx; x++){ for(double y = starty; y<= endy; y++){ for(double z = startz; z<= endz; z++){ locs.add(new Location(loc1.getWorld(), x, y, z)); } } } return locs; } public static Location getOriginWithinTwoLocations(Location loc1, Location loc2){ double middlex = (double) Math.min(loc1.getBlockX(), loc2.getBlockX()) + (Math.max(loc1.getBlockX(), loc2.getBlockX()) - Math.min(loc1.getBlockX(), loc2.getBlockX())); double middley = (double) Math.min(loc1.getBlockY(), loc2.getBlockY()) + (Math.max(loc1.getBlockY(), loc2.getBlockY()) - Math.min(loc1.getBlockY(), loc2.getBlockY())); double middlez = (double) Math.min(loc1.getBlockZ(), loc2.getBlockZ()) + (Math.max(loc1.getBlockZ(), loc2.getBlockZ()) - Math.min(loc1.getBlockZ(), loc2.getBlockZ())); return new Location(loc1.getWorld(), middlex, middley, middlez); } public int getX() { return x; } public int getY() { return y; } public int getZ() { return z; } public void setX(int x) { this.x = x; } public void setY(int y) { this.y = y; } public void setZ(int z) { this.z = z; } }
@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?
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.