Skip to content

Commit

Permalink
Big Maze refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
LudoCrypt committed Nov 24, 2023
1 parent e496bdf commit b527557
Show file tree
Hide file tree
Showing 16 changed files with 639 additions and 393 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 = 10.0.2
version = 11.0.0
maven_group = net.ludocrypt
archives_base_name = limlib
158 changes: 158 additions & 0 deletions src/main/java/net/ludocrypt/limlib/api/world/Manipulation.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
package net.ludocrypt.limlib.api.world;

import com.mojang.serialization.Codec;

import net.minecraft.util.BlockMirror;
import net.minecraft.util.BlockRotation;
import net.minecraft.util.StringIdentifiable;
import net.minecraft.util.random.RandomGenerator;

public enum Manipulation implements StringIdentifiable {

NONE("none", BlockRotation.NONE, BlockMirror.NONE),
CLOCKWISE_90("clockwise_90", BlockRotation.CLOCKWISE_90, BlockMirror.NONE),
CLOCKWISE_180("180", BlockRotation.CLOCKWISE_180, BlockMirror.NONE),
COUNTERCLOCKWISE_90("counterclockwise_90", BlockRotation.COUNTERCLOCKWISE_90, BlockMirror.NONE),
FRONT_BACK("front_back", BlockRotation.NONE, BlockMirror.FRONT_BACK),
LEFT_RIGHT("left_right", BlockRotation.NONE, BlockMirror.LEFT_RIGHT),
TOP_LEFT_BOTTOM_RIGHT("top_left_bottom_right", BlockRotation.COUNTERCLOCKWISE_90, BlockMirror.LEFT_RIGHT),
TOP_RIGHT_BOTTOM_LEFT("top_right_bottom_left", BlockRotation.CLOCKWISE_90, BlockMirror.LEFT_RIGHT);

public static final Codec<Manipulation> CODEC = StringIdentifiable.createCodec(Manipulation::values);
final String id;
final BlockRotation rotation;
final BlockMirror mirror;

Manipulation(String id, BlockRotation rotation, BlockMirror mirror) {
this.id = id;
this.rotation = rotation;
this.mirror = mirror;
}

public BlockRotation getRotation() {
return rotation;
}

public BlockMirror getMirror() {
return mirror;
}

@Override
public String asString() {
return id;
}

public static Manipulation random(RandomGenerator random) {
return Manipulation.values()[random.nextInt(8)];
}

public static Manipulation of(BlockRotation rotation) {
return of(rotation, BlockMirror.NONE);
}

public static Manipulation of(BlockMirror mirror) {
return of(BlockRotation.NONE, mirror);
}

public static Manipulation of(BlockRotation rotation, BlockMirror mirror) {
return switch (rotation) {
case NONE -> (switch (mirror) {
case NONE -> NONE;
case FRONT_BACK -> FRONT_BACK;
case LEFT_RIGHT -> LEFT_RIGHT;
});
case CLOCKWISE_180 -> (switch (mirror) {
case NONE -> CLOCKWISE_180;
case FRONT_BACK -> LEFT_RIGHT;
case LEFT_RIGHT -> FRONT_BACK;
});
case CLOCKWISE_90 -> (switch (mirror) {
case NONE -> CLOCKWISE_90;
case FRONT_BACK -> TOP_LEFT_BOTTOM_RIGHT;
case LEFT_RIGHT -> TOP_RIGHT_BOTTOM_LEFT;
});
case COUNTERCLOCKWISE_90 -> (switch (mirror) {
case NONE -> COUNTERCLOCKWISE_90;
case FRONT_BACK -> TOP_RIGHT_BOTTOM_LEFT;
case LEFT_RIGHT -> TOP_LEFT_BOTTOM_RIGHT;
});
};
}

public Manipulation rotate(BlockRotation rotation) {
return switch (rotation) {
case NONE -> this;
case CLOCKWISE_180 -> (switch (this) {
case NONE -> CLOCKWISE_180;
case FRONT_BACK -> LEFT_RIGHT;
case LEFT_RIGHT -> FRONT_BACK;
case CLOCKWISE_180 -> NONE;
case CLOCKWISE_90 -> COUNTERCLOCKWISE_90;
case COUNTERCLOCKWISE_90 -> CLOCKWISE_90;
case TOP_LEFT_BOTTOM_RIGHT -> TOP_RIGHT_BOTTOM_LEFT;
case TOP_RIGHT_BOTTOM_LEFT -> TOP_LEFT_BOTTOM_RIGHT;
});
case CLOCKWISE_90 -> (switch (this) {
case NONE -> CLOCKWISE_90;
case FRONT_BACK -> TOP_RIGHT_BOTTOM_LEFT;
case LEFT_RIGHT -> TOP_LEFT_BOTTOM_RIGHT;
case CLOCKWISE_180 -> COUNTERCLOCKWISE_90;
case CLOCKWISE_90 -> CLOCKWISE_180;
case COUNTERCLOCKWISE_90 -> NONE;
case TOP_LEFT_BOTTOM_RIGHT -> FRONT_BACK;
case TOP_RIGHT_BOTTOM_LEFT -> LEFT_RIGHT;
});
case COUNTERCLOCKWISE_90 -> (switch (this) {
case NONE -> COUNTERCLOCKWISE_90;
case FRONT_BACK -> TOP_LEFT_BOTTOM_RIGHT;
case LEFT_RIGHT -> TOP_RIGHT_BOTTOM_LEFT;
case CLOCKWISE_180 -> CLOCKWISE_90;
case CLOCKWISE_90 -> NONE;
case COUNTERCLOCKWISE_90 -> CLOCKWISE_180;
case TOP_LEFT_BOTTOM_RIGHT -> LEFT_RIGHT;
case TOP_RIGHT_BOTTOM_LEFT -> FRONT_BACK;
});
};
}

public Manipulation mirror(BlockMirror mirror) {
return switch (mirror) {
case NONE -> this;
case FRONT_BACK -> (switch (this) {
case NONE -> FRONT_BACK;
case FRONT_BACK -> NONE;
case LEFT_RIGHT -> CLOCKWISE_180;
case CLOCKWISE_180 -> LEFT_RIGHT;
case CLOCKWISE_90 -> TOP_LEFT_BOTTOM_RIGHT;
case COUNTERCLOCKWISE_90 -> TOP_RIGHT_BOTTOM_LEFT;
case TOP_LEFT_BOTTOM_RIGHT -> CLOCKWISE_90;
case TOP_RIGHT_BOTTOM_LEFT -> COUNTERCLOCKWISE_90;
});
case LEFT_RIGHT -> (switch (this) {
case NONE -> LEFT_RIGHT;
case FRONT_BACK -> CLOCKWISE_180;
case LEFT_RIGHT -> NONE;
case CLOCKWISE_180 -> FRONT_BACK;
case CLOCKWISE_90 -> TOP_RIGHT_BOTTOM_LEFT;
case COUNTERCLOCKWISE_90 -> TOP_LEFT_BOTTOM_RIGHT;
case TOP_LEFT_BOTTOM_RIGHT -> COUNTERCLOCKWISE_90;
case TOP_RIGHT_BOTTOM_LEFT -> CLOCKWISE_90;
});
};
}

public Manipulation manipulate(Manipulation manipulation) {
return this.rotate(manipulation.rotation).mirror(manipulation.mirror);
}

public static Manipulation[] rotations() {
return new Manipulation[] { Manipulation.NONE, Manipulation.CLOCKWISE_90, Manipulation.CLOCKWISE_180,
Manipulation.COUNTERCLOCKWISE_90 };
}

public static Manipulation[] mirrors() {
return new Manipulation[] { Manipulation.NONE, Manipulation.FRONT_BACK, Manipulation.LEFT_RIGHT,
Manipulation.TOP_LEFT_BOTTOM_RIGHT, Manipulation.TOP_RIGHT_BOTTOM_LEFT };
}

}
38 changes: 22 additions & 16 deletions src/main/java/net/ludocrypt/limlib/api/world/NbtPlacerUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public NbtPlacerUtil(NbtCompound storedNbt, HashMap<BlockPos, Pair<BlockState, O
this(storedNbt, positions, entities, lowestPos, sizePos.getX(), sizePos.getY(), sizePos.getZ());
}

public NbtPlacerUtil manipulate(BlockRotation rotation, BlockMirror mirror) {
public NbtPlacerUtil manipulate(Manipulation manipulation) {
NbtList paletteList = storedNbt.getList("palette", 10);
HashMap<Integer, BlockState> palette = new HashMap<Integer, BlockState>(paletteList.size());
List<NbtCompound> paletteCompoundList = paletteList
Expand All @@ -79,13 +79,15 @@ public NbtPlacerUtil manipulate(BlockRotation rotation, BlockMirror mirror) {
.put(i,
NbtHelper
.toBlockState(Registries.BLOCK.asLookup(), paletteCompoundList.get(i))
.rotate(rotation)
.mirror(mirror));
.rotate(manipulation.getRotation())
.mirror(manipulation.getMirror()));
}

NbtList sizeList = storedNbt.getList("size", 3);
BlockPos sizeVectorRotated = NbtPlacerUtil
.mirror(new BlockPos(sizeList.getInt(0), sizeList.getInt(1), sizeList.getInt(2)).rotate(rotation), mirror);
.mirror(
new BlockPos(sizeList.getInt(0), sizeList.getInt(1), sizeList.getInt(2)).rotate(manipulation.getRotation()),
manipulation.getMirror());
BlockPos sizeVector = new BlockPos(Math.abs(sizeVectorRotated.getX()), Math.abs(sizeVectorRotated.getY()),
Math.abs(sizeVectorRotated.getZ()));
NbtList positionsList = storedNbt.getList("blocks", 10);
Expand All @@ -97,8 +99,10 @@ public NbtPlacerUtil manipulate(BlockRotation rotation, BlockMirror mirror) {
.map(element -> (NbtCompound) element)
.map((nbtCompound) -> Pair
.of(NbtPlacerUtil
.mirror(new BlockPos(nbtCompound.getList("pos", 3).getInt(0), nbtCompound.getList("pos", 3).getInt(1),
nbtCompound.getList("pos", 3).getInt(2)).rotate(rotation), mirror),
.mirror(
new BlockPos(nbtCompound.getList("pos", 3).getInt(0), nbtCompound.getList("pos", 3).getInt(1),
nbtCompound.getList("pos", 3).getInt(2)).rotate(manipulation.getRotation()),
manipulation.getMirror()),
Pair
.of(palette.get(nbtCompound.getInt("state")),
nbtCompound.contains("nbt", NbtElement.COMPOUND_TYPE)
Expand All @@ -112,7 +116,7 @@ public NbtPlacerUtil manipulate(BlockRotation rotation, BlockMirror mirror) {
.forEach(
(pair) -> positions.put(pair.getFirst().subtract(positionsPairList.get(0).getFirst()), pair.getSecond()));
return new NbtPlacerUtil(storedNbt, positions, storedNbt.getList("entities", 10),
transformSize(sizeVector, rotation, mirror), sizeVector);
transformSize(sizeVector, manipulation.getRotation(), manipulation.getMirror()), sizeVector);
}

public static NbtPlacerUtil load(Identifier id, ResourceManager manager) {
Expand Down Expand Up @@ -234,23 +238,23 @@ public NbtPlacerUtil generateNbt(ChunkRegion region, Vec3i offset, BlockPos from
return this;
}

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 pos, Manipulation manipulation) {
return spawnEntities(region, BlockPos.ORIGIN, pos, pos.add(this.sizeX, this.sizeY, this.sizeZ), manipulation);
}

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));
Manipulation manipulation) {
this.entities.forEach((nbtElement) -> spawnEntity(nbtElement, region, offset, from, to, manipulation));
return this;
}

public NbtPlacerUtil spawnEntity(NbtElement nbtElement, ChunkRegion region, BlockPos offset, BlockPos from, BlockPos to,
BlockRotation rotation, BlockMirror mirror) {
Manipulation manipulation) {
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));
rotate(new Vec3d(nbtPos.getDouble(0), nbtPos.getDouble(1), nbtPos.getDouble(2)), manipulation.getRotation()),
manipulation.getMirror()).subtract(Vec3d.of(lowestPos));
Vec3d realPosition = relativeLocation.add(Vec3d.of(from.subtract(offset)));
BlockPos min = offset;
BlockPos max = to.subtract(from).add(offset);
Expand All @@ -271,14 +275,16 @@ public NbtPlacerUtil spawnEntity(NbtElement nbtElement, ChunkRegion region, Bloc
nbt.put("Pos", posList);
NbtList rotationList = new NbtList();
NbtList entityRotationList = nbt.getList("Rotation", 5);
float yawRotation = applyMirror(applyRotation(entityRotationList.getFloat(0), rotation), mirror);
float yawRotation = applyMirror(applyRotation(entityRotationList.getFloat(0), manipulation.getRotation()),
manipulation.getMirror());
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);
Direction dir = mirror(manipulation.getRotation().rotate(Direction.fromHorizontal(nbt.getByte("facing"))),
manipulation.getMirror());
nbt.remove("facing");
nbt.putByte("facing", (byte) dir.getHorizontal());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import net.ludocrypt.limlib.api.world.FunctionMap;
import net.ludocrypt.limlib.api.world.LimlibHelper;
import net.ludocrypt.limlib.api.world.Manipulation;
import net.ludocrypt.limlib.api.world.NbtGroup;
import net.ludocrypt.limlib.api.world.NbtPlacerUtil;
import net.minecraft.block.Block;
Expand All @@ -14,8 +15,6 @@
import net.minecraft.loot.LootTables;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.resource.ResourceManager;
import net.minecraft.util.BlockMirror;
import net.minecraft.util.BlockRotation;
import net.minecraft.util.Identifier;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.ChunkRegion;
Expand All @@ -39,41 +38,37 @@ public AbstractNbtChunkGenerator(BiomeSource biomeSource, NbtGroup nbtGroup,
}

public void generateNbt(ChunkRegion region, BlockPos at, Identifier id) {
generateNbt(region, at, id, BlockRotation.NONE, BlockMirror.NONE);
generateNbt(region, at, id, Manipulation.NONE);
}

public void generateNbt(ChunkRegion region, BlockPos at, Identifier id, BlockRotation rotation) {
generateNbt(region, at, id, rotation, BlockMirror.NONE);
}

public void generateNbt(ChunkRegion region, BlockPos at, Identifier id, BlockMirror mirror) {
generateNbt(region, at, id, BlockRotation.NONE, mirror);
}

public void generateNbt(ChunkRegion region, BlockPos at, Identifier id, BlockRotation rotation, BlockMirror mirror) {
public void generateNbt(ChunkRegion region, BlockPos at, Identifier id, Manipulation manipulation) {

try {
structures
.eval(id, region.getServer().getResourceManager())
.manipulate(rotation, mirror)
.manipulate(manipulation)
.generateNbt(region, at, (pos, state, nbt) -> this.modifyStructure(region, pos, state, nbt))
.spawnEntities(region, at, rotation, mirror);
.spawnEntities(region, at, manipulation);
} catch (Exception e) {
e.printStackTrace();
throw new NullPointerException("Attempted to load undefined structure \'" + id + "\'");
}

}

public void generateNbt(ChunkRegion region, BlockPos offset, BlockPos from, BlockPos to, Identifier id) {
generateNbt(region, offset, from, to, id, Manipulation.NONE);
}

public void generateNbt(ChunkRegion region, BlockPos offset, BlockPos from, BlockPos to, Identifier id,
BlockRotation rotation, BlockMirror mirror) {
Manipulation manipulation) {

try {
structures
.eval(id, region.getServer().getResourceManager())
.manipulate(rotation, mirror)
.manipulate(manipulation)
.generateNbt(region, offset, from, to, (pos, state, nbt) -> this.modifyStructure(region, pos, state, nbt))
.spawnEntities(region, offset, from, to, rotation, mirror);
.spawnEntities(region, offset, from, to, manipulation);
} catch (Exception e) {
e.printStackTrace();
throw new NullPointerException("Attempted to load undefined structure \'" + id + "\'");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,6 @@
import net.minecraft.world.gen.chunk.ChunkGenerator;
import net.minecraft.world.gen.chunk.VerticalBlockSample;

/**
* A simplification of {@link ChunkGenerator}
*/
public abstract class LiminalChunkGenerator extends ChunkGenerator {

public LiminalChunkGenerator(BiomeSource biomeSource) {
Expand All @@ -53,19 +50,21 @@ public void populateEntities(ChunkRegion region) {
@Override
public CompletableFuture<Chunk> populateNoise(Executor executor, Blender blender, RandomState randomState,
StructureManager structureManager, Chunk chunk) {
throw new UnsupportedOperationException("populateNoise should never be called in LiminalChunkGenerator");
throw new UnsupportedOperationException("Vanilla populateNoise should never be called in LiminalChunkGenerator");
}

/**
* How many chunks (distance) around the currently generating chunk from the
* populateNoise method should be allowed to generate into.
* The number of neighboring chunks which can be accessed for block placement. A
* value of 0 means that only this chunk is accessible. A positive value means
* that the given amount of neighbors are accessible in each direction. A
* negative value means that this region shouldn't be used for block placement.
*/
public abstract int getChunkDistance();
public abstract int getPlacementRadius();

/**
* An extention of the base populateNoise method but with more variables. Use
* ChunkRegion as opposed to world when setting blocks, as it allows you to
* extend through multiple chunks in {@link getChunkDistance} away.
* extend through multiple chunks in {@link getPlacementRadius} away.
*/
public abstract CompletableFuture<Chunk> populateNoise(ChunkRegion chunkRegion, ChunkStatus targetStatus,
Executor executor, ServerWorld world, ChunkGenerator generator,
Expand Down
Loading

0 comments on commit b527557

Please sign in to comment.