Skip to content

Commit

Permalink
Change spawn platform shape and generation; Fix structure placement
Browse files Browse the repository at this point in the history
  • Loading branch information
jsorrell committed Apr 29, 2022
1 parent 8da35ca commit 7ea7530
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 95 deletions.
Binary file modified screenshots/spawn_platform.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@
import com.mojang.serialization.codecs.RecordCodecBuilder;
import it.unimi.dsi.fastutil.ints.IntArraySet;
import it.unimi.dsi.fastutil.objects.ObjectArraySet;
import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.block.Blocks;
import net.minecraft.block.LeavesBlock;
import net.minecraft.structure.PoolStructurePiece;
import net.minecraft.structure.StructurePiece;
import net.minecraft.structure.StructurePieceType;
Expand All @@ -27,7 +23,9 @@
import net.minecraft.util.registry.Registry;
import net.minecraft.util.registry.RegistryEntry;
import net.minecraft.util.registry.RegistryEntryList;
import net.minecraft.world.*;
import net.minecraft.world.ChunkRegion;
import net.minecraft.world.HeightLimitView;
import net.minecraft.world.StructureWorldAccess;
import net.minecraft.world.biome.Biome;
import net.minecraft.world.biome.source.BiomeAccess;
import net.minecraft.world.biome.source.BiomeSource;
Expand Down Expand Up @@ -106,28 +104,6 @@ public ChunkGenerator withSeed(long seed) {

@Override
public void buildSurface(ChunkRegion region, StructureAccessor structures, Chunk chunk) {
if (region.getDimension().isNatural()) {
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,
new BlockBox(
chunk.getPos().getStartX(),
chunk.getBottomY(),
chunk.getPos().getStartZ(),
chunk.getPos().getStartX() + 15,
chunk.getTopY(),
chunk.getPos().getStartZ() + 15));
}
}
}

@Override
Expand Down Expand Up @@ -271,61 +247,4 @@ public void generateFeatures(StructureWorldAccess world, Chunk chunk, StructureA
@Override
public void populateEntities(ChunkRegion region) {
}

protected static void placeBlock(
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);
if (box.contains(blockPos)) {
world.setBlockState(blockPos, block, Block.NOTIFY_LISTENERS);
}
}

protected static void fillBlocks(
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++) {
placeBlock(world, block, referencePos, x, y, z, box);
}
}
}
}

protected static void generateSpawnPlatform(
ServerWorldAccess world, BlockPos spawnpoint, BlockBox bounds) {
BlockState leavesBlockState = Blocks.OAK_LEAVES.getDefaultState().with(LeavesBlock.DISTANCE, 1);
fillBlocks(
world, Blocks.GRASS_BLOCK.getDefaultState(), spawnpoint, -2, -1, -7, 2, -1, 2, bounds);
placeBlock(world, Blocks.MYCELIUM.getDefaultState(), spawnpoint, 0, -1, 0, bounds);
placeBlock(
world, Blocks.CRIMSON_NYLIUM.getDefaultState(), spawnpoint, -1, -1, 1, bounds);
placeBlock(
world, Blocks.WARPED_NYLIUM.getDefaultState(), spawnpoint, 1, -1, 1, bounds);
placeBlock(world, Blocks.DIRT.getDefaultState(), spawnpoint, 0, -1, -5, bounds);
fillBlocks(world, leavesBlockState, spawnpoint, -2, 3, -7, 2, 4, -3, bounds);
fillBlocks(world, leavesBlockState, spawnpoint, -1, 5, -5, 1, 6, -5, bounds);
fillBlocks(world, leavesBlockState, spawnpoint, 0, 5, -6, 0, 6, -4, bounds);
placeBlock(world, leavesBlockState, spawnpoint, -1, 5, -4, bounds);
placeBlock(world, leavesBlockState, spawnpoint, -1, 5, -6, bounds);
placeBlock(world, Blocks.AIR.getDefaultState(), spawnpoint, -2, 4, -3, bounds);
placeBlock(world, Blocks.AIR.getDefaultState(), spawnpoint, -2, 3, -7, bounds);
fillBlocks(
world, Blocks.OAK_LOG.getDefaultState(), spawnpoint, 0, 0, -5, 0, 5, -5, bounds);
}
}
29 changes: 18 additions & 11 deletions src/main/java/com/jsorrell/skyblock/gen/SkyBlockStructures.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,19 @@
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.block.entity.MobSpawnerBlockEntity;
import net.minecraft.entity.EntityType;
import net.minecraft.structure.Structure;
import net.minecraft.structure.StructurePiece;
import net.minecraft.structure.StructurePlacementData;
import net.minecraft.util.BlockMirror;
import net.minecraft.util.BlockRotation;
import net.minecraft.util.Identifier;
import net.minecraft.util.math.BlockBox;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Direction;
import net.minecraft.world.ServerWorldAccess;

import javax.annotation.Nullable;
import java.util.Objects;
import java.util.Random;

public class SkyBlockStructures {
Expand All @@ -25,8 +30,11 @@ protected static abstract class SkyBlockStructure {
protected BlockRotation rotation;
protected BlockMirror mirror;

public SkyBlockStructure(Direction orientation, BlockBox boundingBox) {
public SkyBlockStructure(@Nullable Direction orientation, BlockBox boundingBox) {
this.facing = orientation;
if (this.facing == null) {
this.facing = Direction.SOUTH;
}
this.boundingBox = boundingBox;
if (orientation == null) {
this.rotation = BlockRotation.NONE;
Expand Down Expand Up @@ -62,9 +70,6 @@ public Direction getFacing() {
}

protected int applyXTransform(int x, int z) {
if (this.facing == null) {
return x;
}
switch (this.facing) {
case NORTH, SOUTH -> {
return this.boundingBox.getMinX() + x;
Expand All @@ -80,16 +85,10 @@ protected int applyXTransform(int x, int z) {
}

protected int applyYTransform(int y) {
if (this.facing == null) {
return y;
}
return y + this.boundingBox.getMinY();
}

protected int applyZTransform(int x, int z) {
if (this.facing == null) {
return z;
}
switch (this.facing) {
case NORTH -> {
return this.boundingBox.getMaxZ() - z;
Expand Down Expand Up @@ -213,7 +212,7 @@ public void generate(ServerWorldAccess world, BlockBox bounds, Random random) {

public static class SilverfishSpawnerStructure extends SpawnerStructure {
public SilverfishSpawnerStructure(StructurePiece piece) {
super(piece, new BlockPos(5, 3, 5), EntityType.SILVERFISH);
super(piece, new BlockPos(5, 3, 6), EntityType.SILVERFISH);
}
}

Expand All @@ -222,4 +221,12 @@ public MagmaCubeSpawner(StructurePiece piece) {
super(piece, new BlockPos(11, 7, 19), EntityType.MAGMA_CUBE);
}
}

public record SpawnPlatform(BlockPos worldSpawn) {
public void generate(ServerWorldAccess world, Random random) {
Structure structure = Objects.requireNonNull(world.getServer()).getStructureManager().getStructure(new Identifier("skyblock", "spawn_platform")).orElseThrow();
BlockPos structureOrigin = worldSpawn.subtract(new BlockPos(4, 1, 1));
structure.place(world, structureOrigin, worldSpawn, new StructurePlacementData(), random, Block.NOTIFY_LISTENERS);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.jsorrell.skyblock.mixin;

import com.jsorrell.skyblock.gen.SkyBlockChunkGenerator;
import com.jsorrell.skyblock.gen.SkyBlockStructures;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.level.ServerWorldProperties;
import org.objectweb.asm.Opcodes;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import org.spongepowered.asm.mixin.injection.callback.LocalCapture;

@Mixin(MinecraftServer.class)
public class MinecraftServerMixin {

@Inject(
method = "setupSpawn",
locals = LocalCapture.CAPTURE_FAILHARD,
at =
@At(
value = "JUMP",
opcode = Opcodes.IFEQ,
ordinal = 1,
shift = At.Shift.BEFORE))
private static void generateSpawnPlatform(ServerWorld world, ServerWorldProperties worldProperties, boolean bonusChest, boolean debugWorld, CallbackInfo ci) {
if (world.getChunkManager().getChunkGenerator() instanceof SkyBlockChunkGenerator) {
BlockPos worldSpawn = new BlockPos(worldProperties.getSpawnX(), worldProperties.getSpawnY(), worldProperties.getSpawnZ());
System.out.println(worldSpawn);
new SkyBlockStructures.SpawnPlatform(worldSpawn).generate(world, world.random);
}
}
}
Binary file not shown.
1 change: 1 addition & 0 deletions src/main/resources/skyblock.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"LavaFluidMixin",
"LightningEntityMixin",
"LivingEntityMixin",
"MinecraftServerMixin",
"RamImpactTaskMixin",
"SaplingBlockMixin",
"SinglePoolElementAccessor",
Expand Down

0 comments on commit 7ea7530

Please sign in to comment.