Skip to content

Commit

Permalink
added section generation
Browse files Browse the repository at this point in the history
  • Loading branch information
LudoCrypt committed Jul 23, 2023
1 parent 2f9a499 commit 684bc0e
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 65 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -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
137 changes: 84 additions & 53 deletions src/main/java/net/ludocrypt/limlib/api/world/NbtPlacerUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -150,70 +151,100 @@ public NbtPlacerUtil generateNbt(ChunkRegion region, BlockPos at, TriConsumer<Bl
return this;
}

public NbtPlacerUtil spawnEntities(ChunkRegion region, BlockPos pos, BlockRotation rotation, BlockMirror mirror) {
this.entities.forEach((nbtElement) -> {
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<BlockPos, BlockState, NbtCompound> 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<BlockState, NbtCompound> 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<Entity> 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<Entity> 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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ public void generateMaze(BlockPos pos, long seed, MazeCreator<M> 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)) {
Expand All @@ -82,14 +82,6 @@ public HashMap<BlockPos, M> 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;
Expand Down

0 comments on commit 684bc0e

Please sign in to comment.