Skip to content

Commit

Permalink
Fix errors with stronghold portal trying to generate too early
Browse files Browse the repository at this point in the history
  • Loading branch information
jsorrell committed Feb 24, 2021
1 parent e8b0d5c commit 8436818
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 72 deletions.
8 changes: 4 additions & 4 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@
org.gradle.jvmargs=-Xmx1G

# Mod Properties
mod_version = 2.1.3
mod_version = 2.1.4
maven_group = carpet-extension
archives_base_name = skyblock

# Fabric Properties
# see https://modmuss50.me/fabric.html
minecraft_version=1.16.5
yarn_mappings=1.16.5+build.4
loader_version=0.11.1
fabric_api_version=0.30.3+1.16
yarn_mappings=1.16.5+build.5
loader_version=0.11.2
fabric_api_version=0.31.0+1.16

# see https://masa.dy.fi/maven/carpet/fabric-carpet/
carpet_core_version=1.16.5-1.4.27+v210217
184 changes: 116 additions & 68 deletions src/main/java/skyblock/SkyblockChunkGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,7 @@
import net.minecraft.structure.StructurePiece;
import net.minecraft.structure.StructurePieceType;
import net.minecraft.util.Identifier;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.ChunkPos;
import net.minecraft.util.math.ChunkSectionPos;
import net.minecraft.util.math.Direction;
import net.minecraft.util.math.*;
import net.minecraft.util.registry.Registry;
import net.minecraft.world.*;
import net.minecraft.world.biome.Biome;
Expand All @@ -37,6 +34,7 @@
import javax.annotation.Nullable;
import java.util.Arrays;
import java.util.List;
import java.util.Random;
import java.util.function.Supplier;

// Normally, this will always fail validation and give warning on load.
Expand All @@ -45,7 +43,6 @@
public class SkyblockChunkGenerator extends ChunkGenerator {
private final long seed;
private final Supplier<ChunkGeneratorSettings> settings;
// private final ChunkGeneratorType chunkGeneratorType;

// We reference this because SurfaceChunkGenerator final and so can't subclass it
private final NoiseChunkGenerator reference;
Expand All @@ -57,7 +54,7 @@ public class SkyblockChunkGenerator extends ChunkGenerator {
).apply(instance, instance.stable(SkyblockChunkGenerator::new)));

public SkyblockChunkGenerator(long seed, BiomeSource biomeSource, Supplier<ChunkGeneratorSettings> settings) {
super(biomeSource, settings.get().getStructuresConfig());
super(biomeSource, biomeSource, settings.get().getStructuresConfig(), seed);
this.seed = seed;
this.settings = settings;
this.reference = new NoiseChunkGenerator(biomeSource, seed, settings);
Expand Down Expand Up @@ -90,7 +87,7 @@ public void buildSurface(ChunkRegion region, Chunk chunk) {
BlockPos spawn = new BlockPos(region.getLevelProperties().getSpawnX(), region.getLevelProperties().getSpawnY(), region.getLevelProperties().getSpawnZ());
if (chunk.getPos().getStartX() <= spawn.getX() && spawn.getX() <= chunk.getPos().getEndX()
&& chunk.getPos().getStartZ() <= spawn.getZ() && spawn.getZ() <= chunk.getPos().getEndZ()) {
generateSpawnPlatform(region, spawn);
generateSpawnPlatformInBox(region, spawn, new BlockBox(chunk.getPos().getStartX(), 0, chunk.getPos().getStartZ(), chunk.getPos().getStartX() + 15, region.getHeight(), chunk.getPos().getStartZ() + 15));
}
}
}
Expand Down Expand Up @@ -137,13 +134,19 @@ public void carve(long seed, BiomeAccess access, Chunk chunk, GenerationStep.Car
@Override
public void generateFeatures(ChunkRegion region, StructureAccessor accessor) {
ChunkPos chunkPos = new ChunkPos(region.getCenterChunkX(), region.getCenterChunkZ());
BlockPos pos = new BlockPos(chunkPos.getStartX() + 8, 0, chunkPos.getStartZ() + 8);
BlockPos pos = new BlockPos(chunkPos.getStartX(), 0, chunkPos.getStartZ());

accessor.getStructuresWithChildren(ChunkSectionPos.from(pos), Registry.STRUCTURE_FEATURE.get(new Identifier("minecraft:stronghold"))).forEach((structureStart) -> {
for (StructurePiece piece : structureStart.getChildren()) {
if (piece.getType() == StructurePieceType.STRONGHOLD_PORTAL_ROOM) {
BlockPos portalPos = new BlockPos(piece.getBoundingBox().minX, piece.getBoundingBox().minY, piece.getBoundingBox().minZ);
generateStrongholdPortal(region, portalPos, this.seed);
if (piece.intersectsChunk(chunkPos, 0)) {
BlockBox box = new BlockBox(chunkPos.getStartX(), 0, chunkPos.getStartZ(), chunkPos.getStartX() + 15, region.getHeight(), chunkPos.getStartZ() + 15);

ChunkRandom random = new ChunkRandom();
random.setCarverSeed(seed, region.getCenterChunkX(), region.getCenterChunkZ());
generateStrongholdPortalInBox(region, portalPos, random, piece.getFacing(), box);
}
}
}
});
Expand All @@ -153,95 +156,140 @@ public void generateFeatures(ChunkRegion region, StructureAccessor accessor) {
public void populateEntities(ChunkRegion region) {
}

protected static void placeRelativeBlock(WorldAccess world, BlockState block, BlockPos referencePos, int x, int y, int z) {
protected static void placeRelativeBlockInBox(WorldAccess world, BlockState block, BlockPos referencePos, int x, int y, int z, BlockBox box) {
BlockPos blockPos = new BlockPos(referencePos.getX() + x, referencePos.getY() + y, referencePos.getZ() + z);
world.setBlockState(blockPos, block, 2);
if (box.contains(blockPos)) {
world.setBlockState(blockPos, block, 2);
}
}

protected static void fillRelativeBlock(WorldAccess world, BlockState block, BlockPos referencePos, int startX, int startY, int startZ, int endX, int endY, int endZ) {
protected static void fillRelativeBlockInBox(WorldAccess world, BlockState block, BlockPos referencePos, int startX, int startY, int startZ, int endX, int endY, int endZ, BlockBox box) {
for (int x = startX; x <= endX; x++) {
for (int y = startY; y <= endY; y++) {
for (int z = startZ; z <= endZ; z++) {
placeRelativeBlock(world, block, referencePos, x, y, z);
placeRelativeBlockInBox(world, block, referencePos, x, y, z, box);
}
}
}
}

private static void generateStrongholdPortal(ServerWorldAccess world, BlockPos pos, long seed) {
private static void generateStrongholdPortalInBox(ServerWorldAccess world, BlockPos pos, Random random, Direction facing, BlockBox box) {
boolean completePortal = true;
boolean[] eyes = new boolean[12];

ChunkRandom random = new ChunkRandom(seed);

for (int i = 0; i < eyes.length; ++i) {
eyes[i] = random.nextFloat() > 0.9F;
completePortal &= eyes[i];
}

BlockState northPortalFrame = Blocks.END_PORTAL_FRAME.getDefaultState().with(EndPortalFrameBlock.FACING, Direction.NORTH);
BlockState southPortalFrame = Blocks.END_PORTAL_FRAME.getDefaultState().with(EndPortalFrameBlock.FACING, Direction.SOUTH);
BlockState eastPortalFrame = Blocks.END_PORTAL_FRAME.getDefaultState().with(EndPortalFrameBlock.FACING, Direction.EAST);
BlockState westPortalFrame = Blocks.END_PORTAL_FRAME.getDefaultState().with(EndPortalFrameBlock.FACING, Direction.WEST);

placeRelativeBlock(world, southPortalFrame.with(EndPortalFrameBlock.EYE, eyes[0]), pos, 4, 3, 8);
placeRelativeBlock(world, southPortalFrame.with(EndPortalFrameBlock.EYE, eyes[1]), pos, 5, 3, 8);
placeRelativeBlock(world, southPortalFrame.with(EndPortalFrameBlock.EYE, eyes[2]), pos, 6, 3, 8);
placeRelativeBlock(world, northPortalFrame.with(EndPortalFrameBlock.EYE, eyes[3]), pos, 4, 3, 12);
placeRelativeBlock(world, northPortalFrame.with(EndPortalFrameBlock.EYE, eyes[4]), pos, 5, 3, 12);
placeRelativeBlock(world, northPortalFrame.with(EndPortalFrameBlock.EYE, eyes[6]), pos, 6, 3, 12);
placeRelativeBlock(world, eastPortalFrame.with(EndPortalFrameBlock.EYE, eyes[7]), pos, 3, 3, 9);
placeRelativeBlock(world, eastPortalFrame.with(EndPortalFrameBlock.EYE, eyes[8]), pos, 3, 3, 10);
placeRelativeBlock(world, eastPortalFrame.with(EndPortalFrameBlock.EYE, eyes[9]), pos, 3, 3, 11);
placeRelativeBlock(world, westPortalFrame.with(EndPortalFrameBlock.EYE, eyes[10]), pos, 7, 3, 9);
placeRelativeBlock(world, westPortalFrame.with(EndPortalFrameBlock.EYE, eyes[11]), pos, 7, 3, 10);
placeRelativeBlock(world, westPortalFrame.with(EndPortalFrameBlock.EYE, eyes[11]), pos, 7, 3, 11);
if (completePortal) {
BlockState endPortal = Blocks.END_PORTAL.getDefaultState();
fillRelativeBlock(world, endPortal, pos, 4, 3, 9, 6, 3, 11);
}

int spawnerPositionOption = random.nextInt(4);
int x, y = 4, z;
switch (spawnerPositionOption) {
case 0:
x = 5;
z = 6;
BlockState nearPortalFrame = Blocks.END_PORTAL_FRAME.getDefaultState().with(EndPortalFrameBlock.FACING, facing);
BlockState farPortalFrame = Blocks.END_PORTAL_FRAME.getDefaultState().with(EndPortalFrameBlock.FACING, facing.getOpposite());
BlockState rightPortalFrame = Blocks.END_PORTAL_FRAME.getDefaultState().with(EndPortalFrameBlock.FACING, facing.rotateYCounterclockwise());
BlockState leftPortalFrame = Blocks.END_PORTAL_FRAME.getDefaultState().with(EndPortalFrameBlock.FACING, facing.rotateYClockwise());

BlockPos spawnerPos;

switch (facing) {
case EAST:
placeRelativeBlockInBox(world, nearPortalFrame.with(EndPortalFrameBlock.EYE, eyes[0]), pos, 8, 3, 6, box);
placeRelativeBlockInBox(world, nearPortalFrame.with(EndPortalFrameBlock.EYE, eyes[1]), pos, 8, 3, 5, box);
placeRelativeBlockInBox(world, nearPortalFrame.with(EndPortalFrameBlock.EYE, eyes[2]), pos, 8, 3, 4, box);
placeRelativeBlockInBox(world, farPortalFrame.with(EndPortalFrameBlock.EYE, eyes[3]), pos, 12, 3, 6, box);
placeRelativeBlockInBox(world, farPortalFrame.with(EndPortalFrameBlock.EYE, eyes[4]), pos, 12, 3, 5, box);
placeRelativeBlockInBox(world, farPortalFrame.with(EndPortalFrameBlock.EYE, eyes[5]), pos, 12, 3, 4, box);
placeRelativeBlockInBox(world, rightPortalFrame.with(EndPortalFrameBlock.EYE, eyes[6]), pos, 9, 3, 7, box);
placeRelativeBlockInBox(world, rightPortalFrame.with(EndPortalFrameBlock.EYE, eyes[7]), pos, 10, 3, 7, box);
placeRelativeBlockInBox(world, rightPortalFrame.with(EndPortalFrameBlock.EYE, eyes[8]), pos, 11, 3, 7, box);
placeRelativeBlockInBox(world, leftPortalFrame.with(EndPortalFrameBlock.EYE, eyes[9]), pos, 9, 3, 3, box);
placeRelativeBlockInBox(world, leftPortalFrame.with(EndPortalFrameBlock.EYE, eyes[10]), pos, 10, 3, 3, box);
placeRelativeBlockInBox(world, leftPortalFrame.with(EndPortalFrameBlock.EYE, eyes[11]), pos, 11, 3, 3, box);
if (completePortal) {
BlockState endPortal = Blocks.END_PORTAL.getDefaultState();
fillRelativeBlockInBox(world, endPortal, pos, 9, 4, 9, 11, 6, 11, box);
}
spawnerPos = pos.add(6, 3, 5);
break;
case 1:
x = 1;
z = 10;

case NORTH:
placeRelativeBlockInBox(world, nearPortalFrame.with(EndPortalFrameBlock.EYE, eyes[0]), pos, 6, 3, 7, box);
placeRelativeBlockInBox(world, nearPortalFrame.with(EndPortalFrameBlock.EYE, eyes[1]), pos, 5, 3, 7, box);
placeRelativeBlockInBox(world, nearPortalFrame.with(EndPortalFrameBlock.EYE, eyes[2]), pos, 4, 3, 7, box);
placeRelativeBlockInBox(world, farPortalFrame.with(EndPortalFrameBlock.EYE, eyes[3]), pos, 6, 3, 3, box);
placeRelativeBlockInBox(world, farPortalFrame.with(EndPortalFrameBlock.EYE, eyes[4]), pos, 5, 3, 3, box);
placeRelativeBlockInBox(world, farPortalFrame.with(EndPortalFrameBlock.EYE, eyes[5]), pos, 4, 3, 3, box);
placeRelativeBlockInBox(world, rightPortalFrame.with(EndPortalFrameBlock.EYE, eyes[6]), pos, 7, 3, 6, box);
placeRelativeBlockInBox(world, rightPortalFrame.with(EndPortalFrameBlock.EYE, eyes[7]), pos, 7, 3, 5, box);
placeRelativeBlockInBox(world, rightPortalFrame.with(EndPortalFrameBlock.EYE, eyes[8]), pos, 7, 3, 4, box);
placeRelativeBlockInBox(world, leftPortalFrame.with(EndPortalFrameBlock.EYE, eyes[9]), pos, 3, 3, 6, box);
placeRelativeBlockInBox(world, leftPortalFrame.with(EndPortalFrameBlock.EYE, eyes[10]), pos, 3, 3, 5, box);
placeRelativeBlockInBox(world, leftPortalFrame.with(EndPortalFrameBlock.EYE, eyes[11]), pos, 3, 3, 4, box);
if (completePortal) {
BlockState endPortal = Blocks.END_PORTAL.getDefaultState();
fillRelativeBlockInBox(world, endPortal, pos, 4, 3, 4, 6, 3, 6, box);
}
spawnerPos = pos.add(5, 3, 9);
break;
case 2:
x = 5;
z = 14;

case WEST:
placeRelativeBlockInBox(world, nearPortalFrame.with(EndPortalFrameBlock.EYE, eyes[0]), pos, 7, 3, 4, box);
placeRelativeBlockInBox(world, nearPortalFrame.with(EndPortalFrameBlock.EYE, eyes[1]), pos, 7, 3, 5, box);
placeRelativeBlockInBox(world, nearPortalFrame.with(EndPortalFrameBlock.EYE, eyes[2]), pos, 7, 3, 6, box);
placeRelativeBlockInBox(world, farPortalFrame.with(EndPortalFrameBlock.EYE, eyes[3]), pos, 3, 3, 4, box);
placeRelativeBlockInBox(world, farPortalFrame.with(EndPortalFrameBlock.EYE, eyes[4]), pos, 3, 3, 5, box);
placeRelativeBlockInBox(world, farPortalFrame.with(EndPortalFrameBlock.EYE, eyes[5]), pos, 3, 3, 6, box);
placeRelativeBlockInBox(world, rightPortalFrame.with(EndPortalFrameBlock.EYE, eyes[6]), pos, 6, 3, 3, box);
placeRelativeBlockInBox(world, rightPortalFrame.with(EndPortalFrameBlock.EYE, eyes[7]), pos, 5, 3, 3, box);
placeRelativeBlockInBox(world, rightPortalFrame.with(EndPortalFrameBlock.EYE, eyes[8]), pos, 4, 3, 3, box);
placeRelativeBlockInBox(world, leftPortalFrame.with(EndPortalFrameBlock.EYE, eyes[9]), pos, 6, 3, 7, box);
placeRelativeBlockInBox(world, leftPortalFrame.with(EndPortalFrameBlock.EYE, eyes[10]), pos, 5, 3, 7, box);
placeRelativeBlockInBox(world, leftPortalFrame.with(EndPortalFrameBlock.EYE, eyes[11]), pos, 4, 3, 7, box);
if (completePortal) {
BlockState endPortal = Blocks.END_PORTAL.getDefaultState();
fillRelativeBlockInBox(world, endPortal, pos, 4, 3, 4, 6, 3, 6, box);
}
spawnerPos = pos.add(9, 3, 5);
break;
default:
x = 9;
z = 10;

default: // SOUTH
placeRelativeBlockInBox(world, nearPortalFrame.with(EndPortalFrameBlock.EYE, eyes[0]), pos, 4, 3, 8, box);
placeRelativeBlockInBox(world, nearPortalFrame.with(EndPortalFrameBlock.EYE, eyes[1]), pos, 5, 3, 8, box);
placeRelativeBlockInBox(world, nearPortalFrame.with(EndPortalFrameBlock.EYE, eyes[2]), pos, 6, 3, 8, box);
placeRelativeBlockInBox(world, farPortalFrame.with(EndPortalFrameBlock.EYE, eyes[3]), pos, 4, 3, 12, box);
placeRelativeBlockInBox(world, farPortalFrame.with(EndPortalFrameBlock.EYE, eyes[4]), pos, 5, 3, 12, box);
placeRelativeBlockInBox(world, farPortalFrame.with(EndPortalFrameBlock.EYE, eyes[5]), pos, 6, 3, 12, box);
placeRelativeBlockInBox(world, rightPortalFrame.with(EndPortalFrameBlock.EYE, eyes[6]), pos, 3, 3, 9, box);
placeRelativeBlockInBox(world, rightPortalFrame.with(EndPortalFrameBlock.EYE, eyes[7]), pos, 3, 3, 10, box);
placeRelativeBlockInBox(world, rightPortalFrame.with(EndPortalFrameBlock.EYE, eyes[8]), pos, 3, 3, 11, box);
placeRelativeBlockInBox(world, leftPortalFrame.with(EndPortalFrameBlock.EYE, eyes[9]), pos, 7, 3, 9, box);
placeRelativeBlockInBox(world, leftPortalFrame.with(EndPortalFrameBlock.EYE, eyes[10]), pos, 7, 3, 10, box);
placeRelativeBlockInBox(world, leftPortalFrame.with(EndPortalFrameBlock.EYE, eyes[11]), pos, 7, 3, 11, box);
if (completePortal) {
BlockState endPortal = Blocks.END_PORTAL.getDefaultState();
fillRelativeBlockInBox(world, endPortal, pos, 4, 3, 9, 6, 3, 11, box);
}
spawnerPos = pos.add(5, 3, 6);
}

BlockPos spawnerPos = pos.add(x, y, z);
world.setBlockState(spawnerPos, Blocks.SPAWNER.getDefaultState(), 2);
BlockEntity spawnerEntity = world.getBlockEntity(spawnerPos);
if (spawnerEntity instanceof MobSpawnerBlockEntity) {
((MobSpawnerBlockEntity) spawnerEntity).getLogic().setEntityId(EntityType.SILVERFISH);
}
}

protected static void generateSpawnPlatform(ServerWorldAccess world, BlockPos spawnpoint) {
fillRelativeBlock(world, Blocks.GRASS_BLOCK.getDefaultState(), spawnpoint, -2, -1, -7, 2, -1, 2);
placeRelativeBlock(world, Blocks.MYCELIUM.getDefaultState(), spawnpoint, 0, -1, 0);
placeRelativeBlock(world, Blocks.CRIMSON_NYLIUM.getDefaultState(), spawnpoint, -1, -1, 1);
placeRelativeBlock(world, Blocks.WARPED_NYLIUM.getDefaultState(), spawnpoint, 1, -1, 1);
placeRelativeBlock(world, Blocks.DIRT.getDefaultState(), spawnpoint, 0, -1, -5);
fillRelativeBlock(world, Blocks.OAK_LEAVES.getDefaultState(), spawnpoint, -2, 3, -7, 2, 4, -3);
fillRelativeBlock(world, Blocks.OAK_LEAVES.getDefaultState(), spawnpoint, -1, 5, -5, 1, 6, -5);
fillRelativeBlock(world, Blocks.OAK_LEAVES.getDefaultState(), spawnpoint, 0, 5, -6, 0, 6, -4);
placeRelativeBlock(world, Blocks.OAK_LEAVES.getDefaultState(), spawnpoint, -1, 5, -4);
placeRelativeBlock(world, Blocks.OAK_LEAVES.getDefaultState(), spawnpoint, -1, 5, -6);
placeRelativeBlock(world, Blocks.AIR.getDefaultState(), spawnpoint, -2, 4, -3);
placeRelativeBlock(world, Blocks.AIR.getDefaultState(), spawnpoint, -2, 3, -7);
fillRelativeBlock(world, Blocks.OAK_LOG.getDefaultState(), spawnpoint, 0, 0, -5, 0, 5, -5);
protected static void generateSpawnPlatformInBox(ServerWorldAccess world, BlockPos spawnpoint, BlockBox box) {
fillRelativeBlockInBox(world, Blocks.GRASS_BLOCK.getDefaultState(), spawnpoint, -2, -1, -7, 2, -1, 2, box);
placeRelativeBlockInBox(world, Blocks.MYCELIUM.getDefaultState(), spawnpoint, 0, -1, 0, box);
placeRelativeBlockInBox(world, Blocks.CRIMSON_NYLIUM.getDefaultState(), spawnpoint, -1, -1, 1, box);
placeRelativeBlockInBox(world, Blocks.WARPED_NYLIUM.getDefaultState(), spawnpoint, 1, -1, 1, box);
placeRelativeBlockInBox(world, Blocks.DIRT.getDefaultState(), spawnpoint, 0, -1, -5, box);
fillRelativeBlockInBox(world, Blocks.OAK_LEAVES.getDefaultState(), spawnpoint, -2, 3, -7, 2, 4, -3, box);
fillRelativeBlockInBox(world, Blocks.OAK_LEAVES.getDefaultState(), spawnpoint, -1, 5, -5, 1, 6, -5, box);
fillRelativeBlockInBox(world, Blocks.OAK_LEAVES.getDefaultState(), spawnpoint, 0, 5, -6, 0, 6, -4, box);
placeRelativeBlockInBox(world, Blocks.OAK_LEAVES.getDefaultState(), spawnpoint, -1, 5, -4, box);
placeRelativeBlockInBox(world, Blocks.OAK_LEAVES.getDefaultState(), spawnpoint, -1, 5, -6, box);
placeRelativeBlockInBox(world, Blocks.AIR.getDefaultState(), spawnpoint, -2, 4, -3, box);
placeRelativeBlockInBox(world, Blocks.AIR.getDefaultState(), spawnpoint, -2, 3, -7, box);
fillRelativeBlockInBox(world, Blocks.OAK_LOG.getDefaultState(), spawnpoint, 0, 0, -5, 0, 5, -5, box);
}
}

0 comments on commit 8436818

Please sign in to comment.