From 6a5d5c7b61adf458ef5a1ef747ff630955173045 Mon Sep 17 00:00:00 2001 From: smilodon79 Date: Wed, 24 Feb 2021 19:58:48 -0500 Subject: [PATCH] Cursed earth is mostly working. 1. If it's on fire, it turns back into sand, gravel or dirt 2. If it's neighbors are on fire, it catches on fire pretty quickly 3. If it's sufficiently dark, and the neighbor is grass, dirt or path, it turns them into cursed earth. TODO: 1. Cursed earth does not spawn mobs yet. 2. I might need to performance tune the cursed earth mechanics to make it faster by moving the random checks outside the 3 triple for loops (the loops must not be too expensive since they only do -1, 0, 1. But still...) Signed-off-by: smilodon79 --- .../lookingglass/block/CursedEarthBlock.java | 87 ++++++++++++++++++- .../blockstates/cursed_earth.json | 7 ++ 2 files changed, 92 insertions(+), 2 deletions(-) create mode 100644 src/main/resources/assets/lookingglass/blockstates/cursed_earth.json diff --git a/src/main/java/azzy/fabric/lookingglass/block/CursedEarthBlock.java b/src/main/java/azzy/fabric/lookingglass/block/CursedEarthBlock.java index b3a6940..ca59638 100644 --- a/src/main/java/azzy/fabric/lookingglass/block/CursedEarthBlock.java +++ b/src/main/java/azzy/fabric/lookingglass/block/CursedEarthBlock.java @@ -1,7 +1,10 @@ package azzy.fabric.lookingglass.block; import net.fabricmc.fabric.api.object.builder.v1.block.FabricBlockSettings; +import net.minecraft.block.Block; import net.minecraft.block.BlockState; +import net.minecraft.block.Blocks; +import net.minecraft.block.Material; import net.minecraft.server.world.ServerWorld; import net.minecraft.util.math.BlockPos; @@ -15,11 +18,91 @@ public CursedEarthBlock(FabricBlockSettings settings) { @Override public void randomTick(BlockState state, ServerWorld world, BlockPos pos, Random random) { if (!world.isClient) - doTick(world, pos, true); + doTick(world, pos, random); } - protected void doTick(ServerWorld world, BlockPos pos, boolean fastSpreading) { + protected void doTick(ServerWorld world, BlockPos pos, Random random) { int light = world.getLightLevel(pos.up()); System.out.println("Reached here...: '" + light + "'."); + + // Light level 14 is torch. 15 is fire, lava, campfire, etc. + if (light >= 14) { + BlockState blockState = world.getBlockState(pos.up()); + boolean onFire = (blockState.getMaterial() == Material.FIRE); + + if (onFire) { + // If the block catches on fire, it turns to dirt, gravel or sand. + turnCursedEarthToSlag(world, pos, random); + + // Spread the fire to all nearby blocks. + for (int x = -1; x < 2; x++) { + for (int y = -1; y < 2; y++) { + for (int z = -1; z < 2; z++) { + BlockPos tmpPos = pos.add(x, y, z); + // If the nearby block is a cursed earth block + if (world.getBlockState(tmpPos).getBlock() == LookingGlassBlocks.CURSED_EARTH_BLOCK) { + if (random.nextInt(5) == 0) { + // 1 in 5 chance to catch on fire. + world.setBlockState(tmpPos.up(), Blocks.FIRE.getDefaultState()); + } + } + } + } + } + } + + for (int x = -1; x < 2; x++) { + for (int y = -1; y < 2; y++) { + for (int z = -1; z < 2; z++) { + BlockPos tmpPos = pos.add(x, y, z); + // If the nearby block is on fire + if (world.getBlockState(tmpPos.up()).getMaterial() == Material.FIRE) { + if (random.nextInt(5) == 0) { + // 1 in 5 chance to catch self fire. + world.setBlockState(pos.up(), Blocks.FIRE.getDefaultState()); + } + } + } + } + } + } else if (light <= 7) { + for (int x = -1; x < 2; x++) { + for (int y = -1; y < 2; y++) { + for (int z = -1; z < 2; z++) { + BlockPos tmpPos = pos.add(x, y, z); + // If the nearby block is on fire + Block neighbor = world.getBlockState(tmpPos).getBlock(); + if ((neighbor == Blocks.DIRT) || (neighbor == Blocks.GRASS_BLOCK) || (neighbor == Blocks.GRASS_PATH)) { + if (random.nextInt(50) == 0) { + // 1 in 50 chance to turn neighbor into cursed earth. + world.setBlockState(tmpPos, LookingGlassBlocks.CURSED_EARTH_BLOCK.getDefaultState()); + } + } + } + } + } + } + + // TODO 4: Spawn random mobs as required + + } + + private void turnCursedEarthToSlag(ServerWorld world, BlockPos pos, Random random) { + int replaceFlag = random.nextInt(10); + + switch (replaceFlag) { + case 0: + // Replace to dirt. + world.setBlockState(pos, Blocks.DIRT.getDefaultState()); + return; + case 1: + // Replace to gravel. + world.setBlockState(pos, Blocks.GRAVEL.getDefaultState()); + return; + case 2: + // Replace to sand. + world.setBlockState(pos, Blocks.SAND.getDefaultState()); + return; + } } } \ No newline at end of file diff --git a/src/main/resources/assets/lookingglass/blockstates/cursed_earth.json b/src/main/resources/assets/lookingglass/blockstates/cursed_earth.json new file mode 100644 index 0000000..8aabe22 --- /dev/null +++ b/src/main/resources/assets/lookingglass/blockstates/cursed_earth.json @@ -0,0 +1,7 @@ +{ + "variants": { + "": { + "model": "lookingglass:block/polished_whitestone" + } + } +} \ No newline at end of file