Skip to content

Commit

Permalink
refactor: rename Structure#pickStructure to Structure#pick
Browse files Browse the repository at this point in the history
  • Loading branch information
smartcmd committed Jan 14, 2025
1 parent 48bceb2 commit 3a2d0b0
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ Unless otherwise specified, any version comparison below is the comparison of se
- (API) Removed `ScoreboardService#ServerEventListener` as it is not supposed to be touched by plugin.
- (API) Methods `BlockEntityFurnaceBaseComponent#getStoredXP` and `BlockEntityFurnaceBaseComponent#setStoredXP` now
accept `int` instead of `float`.
- (API) Renamed `Structure#pickStructure` to `Structure#pick`.
- Removed useless class `PackageClassLoaderUtils`, dependency `org.reflections.reflections` is also removed.
- Added `-dev` suffix to api version in development build.
- Changed `ContainerActionProcessorHolder` to a final class instead of an interface, because this abstraction is
Expand Down
43 changes: 40 additions & 3 deletions api/src/main/java/org/allaymc/api/utils/Structure.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,28 @@ public record Structure(
private static final int FORMAT_VERSION = 1;
private static final BlockState STRUCTURE_VOID_DEFAULT_STATE = BlockTypes.STRUCTURE_VOID.getDefaultState();

public static Structure pickStructure(Dimension dimension, int x, int y, int z, int sizeX, int sizeY, int sizeZ) {
return pickStructure(dimension, x, y, z, sizeX, sizeY, sizeZ, true);
/**
* @see #pick(Dimension, int, int, int, int, int, int, boolean)
*/
public static Structure pick(Dimension dimension, int x, int y, int z, int sizeX, int sizeY, int sizeZ) {
return pick(dimension, x, y, z, sizeX, sizeY, sizeZ, true);
}

public static Structure pickStructure(Dimension dimension, int x, int y, int z, int sizeX, int sizeY, int sizeZ, boolean saveEntities) {
/**
* Pick a structure from the dimension.
*
* @param dimension the dimension to pick the structure from.
* @param x the x coordinate of the structure.
* @param y the y coordinate of the structure.
* @param z the z coordinate of the structure.
* @param sizeX the size of the structure in x direction.
* @param sizeY the size of the structure in y direction.
* @param sizeZ the size of the structure in z direction.
* @param saveEntities whether to save the entities in the structure.
*
* @return the picked structure.
*/
public static Structure pick(Dimension dimension, int x, int y, int z, int sizeX, int sizeY, int sizeZ, boolean saveEntities) {
var blockStates = new BlockState[2][sizeX][sizeY][sizeZ];
var blockEntities = new HashMap<Vector3ic, NbtMap>();
var entities = new ArrayList<NbtMap>();
Expand Down Expand Up @@ -76,6 +93,13 @@ public static Structure pickStructure(Dimension dimension, int x, int y, int z,
return new Structure(blockStates, blockEntities, entities, sizeX, sizeY, sizeZ, x, y, z);
}

/**
* Load structure data from nbt.
*
* @param nbt the nbt data to load.
*
* @return the loaded structure.
*/
public static Structure formNBT(NbtMap nbt) {
if (nbt.getInt("format_version") != FORMAT_VERSION) {
throw new StructureException("format_version should be " + FORMAT_VERSION);
Expand Down Expand Up @@ -149,6 +173,14 @@ public static Structure formNBT(NbtMap nbt) {
);
}

/**
* Place the structure in the dimension.
*
* @param dimension the dimension to place the structure in.
* @param x the x coordinate to place the structure.
* @param y the y coordinate to place the structure.
* @param z the z coordinate to place the structure.
*/
public void place(Dimension dimension, int x, int y, int z) {
for (int lx = 0; lx < sizeX; lx++) {
for (int ly = 0; ly < sizeY; ly++) {
Expand Down Expand Up @@ -195,6 +227,11 @@ public void place(Dimension dimension, int x, int y, int z) {
}
}

/**
* Save the structure data to nbt.
*
* @return the nbt data of the structure.
*/
public NbtMap toNBT() {
var capacity = sizeX * sizeY * sizeZ;
var layer0 = new Integer[capacity];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public void prepareCommandTree(CommandTree tree) {
context.addError("Invalid size");
return context.fail();
}
var structure = Structure.pickStructure(
var structure = Structure.pick(
player.getDimension(),
(int) start.x, (int) start.y, (int) start.z,
sizeX, sizeY, sizeZ,
Expand Down

0 comments on commit 3a2d0b0

Please sign in to comment.