diff --git a/gradle.properties b/gradle.properties index d488a2b..297ee76 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,6 +1,6 @@ org.gradle.jvmargs = -Xmx1G org.gradle.parallel = true -version = 8.1.5 +version = 8.1.6 maven_group = net.ludocrypt archives_base_name = limlib \ No newline at end of file diff --git a/src/main/java/net/ludocrypt/limlib/api/world/NbtPlacerUtil.java b/src/main/java/net/ludocrypt/limlib/api/world/NbtPlacerUtil.java index d81d8bd..44c1e75 100644 --- a/src/main/java/net/ludocrypt/limlib/api/world/NbtPlacerUtil.java +++ b/src/main/java/net/ludocrypt/limlib/api/world/NbtPlacerUtil.java @@ -18,6 +18,7 @@ import net.minecraft.entity.decoration.AbstractDecorationEntity; import net.minecraft.nbt.NbtCompound; import net.minecraft.nbt.NbtDouble; +import net.minecraft.nbt.NbtElement; import net.minecraft.nbt.NbtFloat; import net.minecraft.nbt.NbtHelper; import net.minecraft.nbt.NbtInt; @@ -150,70 +151,100 @@ public NbtPlacerUtil generateNbt(ChunkRegion region, BlockPos at, TriConsumer { - NbtCompound entityCompound = (NbtCompound) nbtElement; - NbtList nbtPos = entityCompound.getList("pos", 6); - Vec3d realPosition = mirror(rotate(new Vec3d(nbtPos.getDouble(0), nbtPos.getDouble(1), nbtPos.getDouble(2)), rotation), mirror).subtract(Vec3d.of(lowestPos)).add(Vec3d.of(pos)); - - NbtCompound nbt = entityCompound.getCompound("nbt").copy(); - nbt.remove("Pos"); - nbt.remove("UUID"); - - NbtList posList = new NbtList(); - posList.add(NbtDouble.of(realPosition.x)); - posList.add(NbtDouble.of(realPosition.y)); - posList.add(NbtDouble.of(realPosition.z)); - nbt.put("Pos", posList); - - NbtList rotationList = new NbtList(); - NbtList entityRotationList = nbt.getList("Rotation", 5); - float yawRotation = applyMirror(applyRotation(entityRotationList.getFloat(0), rotation), mirror); - rotationList.add(NbtFloat.of(yawRotation)); - rotationList.add(NbtFloat.of(entityRotationList.getFloat(1))); - nbt.remove("Rotation"); - nbt.put("Rotation", rotationList); - - if (nbt.contains("facing")) { - Direction dir = mirror(rotation.rotate(Direction.fromHorizontal(nbt.getByte("facing"))), mirror); - nbt.remove("facing"); - nbt.putByte("facing", (byte) dir.getHorizontal()); + public NbtPlacerUtil generateNbt(ChunkRegion region, BlockPos offset, BlockPos from, BlockPos to, TriConsumer consumer) { + for (int xi = from.getX(); xi <= to.getX(); xi++) { + for (int yi = from.getY(); yi <= to.getY(); yi++) { + for (int zi = from.getZ(); zi <= to.getZ(); zi++) { + BlockPos currentPos = new BlockPos(xi, yi, zi); + Pair pair = this.positions.get(currentPos.subtract(from).add(offset)); + BlockState state = pair.getFirst(); + NbtCompound nbt = pair.getSecond(); + consumer.accept(currentPos, state == null ? Blocks.BARRIER.getDefaultState() : state, nbt); + } } + } + return this; + } - if (nbt.contains("TileX", 3) && nbt.contains("TileY", 3) && nbt.contains("TileZ", 3)) { - nbt.remove("TileX"); - nbt.remove("TileY"); - nbt.remove("TileZ"); - nbt.putInt("TileX", MathHelper.floor(realPosition.x)); - nbt.putInt("TileY", MathHelper.floor(realPosition.y)); - nbt.putInt("TileZ", MathHelper.floor(realPosition.z)); - } + public NbtPlacerUtil spawnEntities(ChunkRegion region, BlockPos pos, BlockRotation rotation, BlockMirror mirror) { + return spawnEntities(region, BlockPos.ORIGIN, pos, pos.add(this.sizeX, this.sizeY, this.sizeZ), rotation, mirror); + } + + public NbtPlacerUtil spawnEntities(ChunkRegion region, BlockPos offset, BlockPos from, BlockPos to, BlockRotation rotation, BlockMirror mirror) { + this.entities.forEach((nbtElement) -> spawnEntity(nbtElement, region, offset, from, to, rotation, mirror)); + return this; + } - Optional optionalEntity = getEntity(region, nbt); + public NbtPlacerUtil spawnEntity(NbtElement nbtElement, ChunkRegion region, BlockPos offset, BlockPos from, BlockPos to, BlockRotation rotation, BlockMirror mirror) { + NbtCompound entityCompound = (NbtCompound) nbtElement; + NbtList nbtPos = entityCompound.getList("pos", 6); + Vec3d relativeLocation = mirror(rotate(new Vec3d(nbtPos.getDouble(0), nbtPos.getDouble(1), nbtPos.getDouble(2)), rotation), mirror).subtract(Vec3d.of(lowestPos)); + Vec3d realPosition = relativeLocation.add(Vec3d.of(from)); - if (optionalEntity.isPresent()) { - Entity entity = optionalEntity.get(); - entity.refreshPositionAndAngles(realPosition.x, realPosition.y, realPosition.z, yawRotation, entity.getPitch()); + BlockPos min = offset; + BlockPos max = to.subtract(from).add(offset); - if (entity instanceof AbstractDecorationEntity deco) { - double newX = realPosition.getX() - (deco.getWidthPixels() % 32 == 0 ? 0.5 : 0.0) * deco.getHorizontalFacing().rotateYCounterclockwise().getOffsetX(); - double newY = realPosition.getY() - (deco.getHeightPixels() % 32 == 0 ? 0.5 : 0.0); - double newZ = realPosition.getZ() - (deco.getWidthPixels() % 32 == 0 ? 0.5 : 0.0) * deco.getHorizontalFacing().rotateYCounterclockwise().getOffsetZ(); + if (!((relativeLocation.getX() < max.getX() && relativeLocation.getX() >= min.getX()) && (relativeLocation.getY() < max.getY() && relativeLocation.getY() >= min.getY()) + && (relativeLocation.getZ() < max.getZ() && relativeLocation.getZ() >= min.getZ()))) { + return this; + } - newX += deco.getHorizontalFacing().getOffsetX() * 0.46875D; - newZ += deco.getHorizontalFacing().getOffsetZ() * 0.46875D; + NbtCompound nbt = entityCompound.getCompound("nbt").copy(); + nbt.remove("Pos"); + nbt.remove("UUID"); + + NbtList posList = new NbtList(); + posList.add(NbtDouble.of(realPosition.x)); + posList.add(NbtDouble.of(realPosition.y)); + posList.add(NbtDouble.of(realPosition.z)); + nbt.put("Pos", posList); + + NbtList rotationList = new NbtList(); + NbtList entityRotationList = nbt.getList("Rotation", 5); + float yawRotation = applyMirror(applyRotation(entityRotationList.getFloat(0), rotation), mirror); + rotationList.add(NbtFloat.of(yawRotation)); + rotationList.add(NbtFloat.of(entityRotationList.getFloat(1))); + nbt.remove("Rotation"); + nbt.put("Rotation", rotationList); + + if (nbt.contains("facing")) { + Direction dir = mirror(rotation.rotate(Direction.fromHorizontal(nbt.getByte("facing"))), mirror); + nbt.remove("facing"); + nbt.putByte("facing", (byte) dir.getHorizontal()); + } - newX -= 0.5; - newY -= 0.5; - newZ -= 0.5; + if (nbt.contains("TileX", 3) && nbt.contains("TileY", 3) && nbt.contains("TileZ", 3)) { + nbt.remove("TileX"); + nbt.remove("TileY"); + nbt.remove("TileZ"); + nbt.putInt("TileX", MathHelper.floor(realPosition.x)); + nbt.putInt("TileY", MathHelper.floor(realPosition.y)); + nbt.putInt("TileZ", MathHelper.floor(realPosition.z)); + } - deco.setPosition(newX, newY, newZ); - } + Optional optionalEntity = getEntity(region, nbt); - region.spawnEntity(entity); + if (optionalEntity.isPresent()) { + Entity entity = optionalEntity.get(); + entity.refreshPositionAndAngles(realPosition.x, realPosition.y, realPosition.z, yawRotation, entity.getPitch()); + + if (entity instanceof AbstractDecorationEntity deco) { + double newX = realPosition.getX() - (deco.getWidthPixels() % 32 == 0 ? 0.5 : 0.0) * deco.getHorizontalFacing().rotateYCounterclockwise().getOffsetX(); + double newY = realPosition.getY() - (deco.getHeightPixels() % 32 == 0 ? 0.5 : 0.0); + double newZ = realPosition.getZ() - (deco.getWidthPixels() % 32 == 0 ? 0.5 : 0.0) * deco.getHorizontalFacing().rotateYCounterclockwise().getOffsetZ(); + + newX += deco.getHorizontalFacing().getOffsetX() * 0.46875D; + newZ += deco.getHorizontalFacing().getOffsetZ() * 0.46875D; + + newX -= 0.5; + newY -= 0.5; + newZ -= 0.5; + + deco.setPosition(newX, newY, newZ); } - }); + region.spawnEntity(entity); + } return this; } diff --git a/src/main/java/net/ludocrypt/limlib/api/world/chunk/AbstractNbtChunkGenerator.java b/src/main/java/net/ludocrypt/limlib/api/world/chunk/AbstractNbtChunkGenerator.java index c7a30b4..a056a9c 100644 --- a/src/main/java/net/ludocrypt/limlib/api/world/chunk/AbstractNbtChunkGenerator.java +++ b/src/main/java/net/ludocrypt/limlib/api/world/chunk/AbstractNbtChunkGenerator.java @@ -55,7 +55,13 @@ public void generateNbt(ChunkRegion region, BlockPos at, String id, BlockMirror } public void generateNbt(ChunkRegion region, BlockPos at, String id, BlockRotation rotation, BlockMirror mirror) { - loadedStructures.get(id).manipulate(rotation, mirror).generateNbt(region, at, (pos, state, nbt) -> this.modifyStructure(region, pos, state, nbt)).spawnEntities(region, at, rotation, mirror); + NbtPlacerUtil structure = loadedStructures.get(id); + + if (structure == null) { + throw new NullPointerException("Attempted to load undefined structure \'" + id + "\'"); + } + + structure.manipulate(rotation, mirror).generateNbt(region, at, (pos, state, nbt) -> this.modifyStructure(region, pos, state, nbt)).spawnEntities(region, at, rotation, mirror); } @SuppressWarnings("deprecation") diff --git a/src/main/java/net/ludocrypt/limlib/api/world/maze/RectangularMazeGenerator.java b/src/main/java/net/ludocrypt/limlib/api/world/maze/RectangularMazeGenerator.java index 5ca7b2d..78ca9a7 100644 --- a/src/main/java/net/ludocrypt/limlib/api/world/maze/RectangularMazeGenerator.java +++ b/src/main/java/net/ludocrypt/limlib/api/world/maze/RectangularMazeGenerator.java @@ -55,8 +55,8 @@ public void generateMaze(BlockPos pos, long seed, MazeCreator mazeCreator, Ce for (int x = 0; x < 16; x++) { for (int y = 0; y < 16; y++) { BlockPos inPos = pos.add(x, 0, y); - if (mod(inPos.getX(), thickness) == 0 && mod(inPos.getZ(), thickness) == 0) { - BlockPos mazePos = new BlockPos(inPos.getX() - mod(inPos.getX(), (width * thickness)), 0, inPos.getZ() - mod(inPos.getZ(), (height * thickness))); + if (Math.floorMod(inPos.getX(), thickness) == 0 && Math.floorMod(inPos.getZ(), thickness) == 0) { + BlockPos mazePos = new BlockPos(inPos.getX() - Math.floorMod(inPos.getX(), (width * thickness)), 0, inPos.getZ() - Math.floorMod(inPos.getZ(), (height * thickness))); M maze; if (this.mazes.containsKey(mazePos)) { @@ -82,14 +82,6 @@ public HashMap getMazes() { return mazes; } - protected int mod(int x, int n) { - int r = x % n; - if (r < 0) { - r += n; - } - return r; - } - protected long blockSeed(long x, long y, long z) { long l = (x * 3129871) ^ z * 116129781L ^ y; l = l * l * 42317861L + l * 11L;