Paper: Location changes

Created on 24 Jul 2018  路  4Comments  路  Source: PaperMC/Paper

I'm always frustrated when I have to do:
Location start = new Location(Bukkit.getWorld("oof"), 10, 10, 10); Location editableClone = start.clone(); for (int i = 0; i < 100; i++) { editableClone.setX(start.getX() + i); editableClone.setY(start.getY() + i); editableClone.setZ(start.getZ() + i); }

I want to have a location builder:
for (int i = 0; i < 100; i++) { start.clone().setX(start.getX() + i).setY(start.getY() + i).setZ(start.getZ() + i); }

accepted feature

Most helpful comment

Your original snippet:

Location start = new Location(Bukkit.getWorld("oof"), 10, 10, 10);
Location editableClone = start.clone();
for (int i = 0; i < 100; i++) {
    editableClone.setX(start.getX() + i);
    editableClone.setY(start.getY() + i);
    editableClone.setZ(start.getZ() + i);
}

Can be made simpler, like so:

Location start = new Location(Bukkit.getWorld("oof"), 10, 10, 10);
for (int i = 0; i < 100; i++) {
    start.add(i, i, i);
    //Logic
    start.subtract(i, i, i);
}

All 4 comments

I'm not very important in this project, but I don't see why there should be a location object with a builder pattern.
This could be easily accomplished with a location "wrapper" with the needed Location builder.

Your original snippet:

Location start = new Location(Bukkit.getWorld("oof"), 10, 10, 10);
Location editableClone = start.clone();
for (int i = 0; i < 100; i++) {
    editableClone.setX(start.getX() + i);
    editableClone.setY(start.getY() + i);
    editableClone.setZ(start.getZ() + i);
}

Can be made simpler, like so:

Location start = new Location(Bukkit.getWorld("oof"), 10, 10, 10);
for (int i = 0; i < 100; i++) {
    start.add(i, i, i);
    //Logic
    start.subtract(i, i, i);
}

What would be a cool API addition would be a imutable location/vector which supports add and subtract operations by returning a clone.

as said, there is solutions to what you are trying to do here.

What we could do though is a .set(x,y,z) that returns self, and .set(start, x, y, z) that is a shortcut to start.x + x, y, z etc.

So I will consider this accepted and we can add a few more util methods.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

MarkElf picture MarkElf  路  3Comments

Marlej-dev picture Marlej-dev  路  3Comments

successed picture successed  路  3Comments

James94665 picture James94665  路  3Comments

dbkynd picture dbkynd  路  3Comments