Files
src
build.gradle
gradle.properties
settings.gradle
Code
7 254
src/main/java/com/kodari/ruineddeep/RuinedDeepMod.java
package com.kodari.ruineddeep;
import java.util.Set;
import java.util.List;
import java.util.Random;
import net.minecraft.block.Blocks;
import net.minecraft.block.BlockState;
import net.minecraft.block.PillarBlock;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerChunkEvents;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerTickEvents;
import net.minecraft.registry.RegistryKey;
import net.minecraft.registry.RegistryKeys;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.util.Identifier;
import net.minecraft.util.math.Direction;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.Heightmap;
import net.minecraft.world.World;
import net.minecraft.world.chunk.WorldChunk;
public final class RuinedDeepMod implements ModInitializer {
private static final RegistryKey<World> RUINED_DEEP = RegistryKey.of(
RegistryKeys.WORLD,
Identifier.of("ruineddeep", "ruined_deep")
);
@Override
public void onInitialize() {
ServerTickEvents.START_WORLD_TICK.register(RuinedDeepMod::handleVoidFall);
ServerChunkEvents.CHUNK_LOAD.register(RuinedDeepMod::convertSurfaceGrass);
}
private static void convertSurfaceGrass(ServerWorld world, WorldChunk chunk) {
if (!RUINED_DEEP.equals(world.getRegistryKey())) {
return;
}
int startX = chunk.getPos().getStartX();
int startZ = chunk.getPos().getStartZ();
for (int x = startX; x < startX + 16; x++) {
for (int z = startZ; z < startZ + 16; z++) {
int surfaceY = world.getTopY(Heightmap.Type.WORLD_SURFACE, x, z) - 1;
BlockPos surfacePos = new BlockPos(x, surfaceY, z);
if (world.getBlockState(surfacePos).isOf(Blocks.GRASS_BLOCK)) {
world.setBlockState(surfacePos, Blocks.MYCELIUM.getDefaultState());
}
}
}
addDeadTree(world, startX, startZ);
}
private static void addDeadTree(ServerWorld world, int startX, int startZ) {
Random random = new Random((long) startX * 341873128712L + (long) startZ * 132897987541L);
if (random.nextBoolean()) {
return;
}
int x = startX + 4 + random.nextInt(8);
int z = startZ + 4 + random.nextInt(8);
if (Math.abs(x) < 10 && Math.abs(z) < 10) {
return;
}
int surfaceY = world.getTopY(Heightmap.Type.WORLD_SURFACE, x, z);
if (!world.getBlockState(new BlockPos(x, surfaceY - 1, z)).isOf(Blocks.MYCELIUM)) {
return;
}
int height = 5 + random.nextInt(3);
for (int offset = 0; offset < height + 2; offset++) {
if (!world.getBlockState(new BlockPos(x, surfaceY + offset, z)).isOf(Blocks.AIR)) {
return;
}
}
BlockState verticalLog = Blocks.STRIPPED_DARK_OAK_LOG.getDefaultState().with(PillarBlock.AXIS, Direction.Axis.Y);
BlockState xLog = verticalLog.with(PillarBlock.AXIS, Direction.Axis.X);
BlockState zLog = verticalLog.with(PillarBlock.AXIS, Direction.Axis.Z);
for (int offset = 0; offset < height; offset++) {
world.setBlockState(new BlockPos(x, surfaceY + offset, z), verticalLog);
}
int branchY = surfaceY + height - 3;
int xDirection = random.nextBoolean() ? 1 : -1;
int zDirection = random.nextBoolean() ? 1 : -1;
placeDeadLog(world, new BlockPos(x + xDirection, branchY, z), xLog);
placeDeadLog(world, new BlockPos(x + xDirection * 2, branchY + 1, z), verticalLog);
placeDeadLog(world, new BlockPos(x, branchY + 1, z + zDirection), zLog);
placeDeadLog(world, new BlockPos(x, branchY + 2, z + zDirection * 2), verticalLog);
}
private static void placeDeadLog(ServerWorld world, BlockPos pos, BlockState state) {
if (world.getBlockState(pos).isOf(Blocks.AIR)) {
world.setBlockState(pos, state);
}
}
private static void handleVoidFall(ServerWorld world) {
boolean fallingFromEnd = World.END.equals(world.getRegistryKey());
boolean fallingFromRuinedDeep = RUINED_DEEP.equals(world.getRegistryKey());
if (!fallingFromEnd && !fallingFromRuinedDeep) {
return;
}
MinecraftServer server = world.getServer();
ServerWorld destination = server.getWorld(fallingFromEnd ? RUINED_DEEP : World.END);
if (destination == null) {
return;
}
for (ServerPlayerEntity player : List.copyOf(world.getPlayers())) {
double fallThreshold = fallingFromEnd ? -32.0 : -60.0;
if (player.getY() > fallThreshold) {
continue;
}
int x = 0;
int z = 0;
int safeY = destination.getTopY(Heightmap.Type.WORLD_SURFACE, x, z) + 1;
if (fallingFromEnd) {
int platformY = Math.max(safeY - 1, 64);
for (int platformX = -2; platformX <= 2; platformX++) {
for (int platformZ = -2; platformZ <= 2; platformZ++) {
destination.setBlockState(
new BlockPos(platformX, platformY, platformZ),
Blocks.DEEPSLATE_TILES.getDefaultState()
);
}
}
safeY = platformY + 1;
}
player.teleport(
destination,
x + 0.5,
safeY,
z + 0.5,
Set.of(),
player.getYaw(),
player.getPitch(),
true
);
}
}
}