Skip to content

Commit

Permalink
Fix monster box placement. Fixes #4435
Browse files Browse the repository at this point in the history
  • Loading branch information
quat1024 committed Dec 21, 2023
1 parent 228f8b1 commit a9b3170
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
import net.minecraft.core.Direction;
import net.minecraft.server.level.WorldGenRegion;
import net.minecraft.util.RandomSource;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.chunk.ChunkGenerator;
import net.minecraft.world.level.levelgen.FlatLevelSource;

import org.violetmoon.quark.base.Quark;
import org.violetmoon.quark.base.config.type.DimensionConfig;
import org.violetmoon.quark.base.util.BlockUtils;
import org.violetmoon.quark.base.world.generator.Generator;
Expand All @@ -25,27 +27,32 @@ public void generateChunk(WorldGenRegion world, ChunkGenerator generator, Random
return;

double chance = MonsterBoxModule.chancePerChunk;

while(rand.nextDouble() <= chance) {
BlockPos pos = chunkCorner.offset(rand.nextInt(16), MonsterBoxModule.minY + rand.nextInt(MonsterBoxModule.maxY - MonsterBoxModule.minY + 1), rand.nextInt(16));
if(world.isEmptyBlock(pos)) {
BlockPos testPos = pos;
BlockState testState;
int moves = 0;

do {
testPos = testPos.below();
testState = world.getBlockState(testPos);
moves++;
} while(moves < MonsterBoxModule.searchRange && BlockUtils.isStoneBased(testState, world, testPos) && testPos.getY() >= MonsterBoxModule.minY);

if(testPos.getY() >= MonsterBoxModule.minY && world.isEmptyBlock(testPos.above()) && world.getBlockState(testPos).isFaceSturdy(world, testPos, Direction.UP)) {
world.setBlock(testPos.above(), MonsterBoxModule.monster_box.defaultBlockState(), 0);
while(chance > 0 && rand.nextDouble() <= chance) {
BlockPos.MutableBlockPos pos = chunkCorner.offset(rand.nextInt(16), rand.nextInt(MonsterBoxModule.minY, MonsterBoxModule.maxY), rand.nextInt(16)).mutable();
for(int moves = 0; moves < MonsterBoxModule.searchRange && pos.getY() > MonsterBoxModule.minY; moves++) {
BlockState state = world.getBlockState(pos);
if(canPlaceHere(world, pos, state)) {
world.setBlock(pos, MonsterBoxModule.monster_box.defaultBlockState(), 0);
break;
}

pos = pos.move(0, -1, 0); //down
}

chance -= MonsterBoxModule.chancePerChunk;
chance -= 1;
}
}

private boolean canPlaceHere(WorldGenRegion level, BlockPos.MutableBlockPos pos, BlockState state) {
if(!state.canBeReplaced())
return false;

//Mutable blockpos nudging dance to avoid an allocation, probably not worth it, stupidest thing ever, vibes based optimization
BlockPos below = pos.move(0, -1, 0);
BlockState belowState = level.getBlockState(below);
boolean result = BlockUtils.isStoneBased(belowState, level, below) && belowState.isFaceSturdy(level, below, Direction.UP);

pos.move(0, 1, 0);
return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public class MonsterBoxModule extends ZetaModule {

public static BlockEntityType<MonsterBoxBlockEntity> blockEntityType;

@Config(description = "The chance for the monster box generator to try and place one in a chunk, 1 is 100%\nThis can be higher than 100% if you want multiple per chunk, , 0 is 0%")
@Config(description = "The chance for the monster box generator to try and place one in a chunk. 0 is 0%, 1 is 100%\nThis can be higher than 100% if you want multiple per chunk.")
public static double chancePerChunk = 0.2;

@Config
Expand Down

0 comments on commit a9b3170

Please sign in to comment.