Skip to content

Commit

Permalink
Cursed earth is mostly working.
Browse files Browse the repository at this point in the history
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 <jayaramv2@hexaware.com>
  • Loading branch information
smilodon79 committed Feb 25, 2021
1 parent dfa1313 commit 6a5d5c7
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 2 deletions.
87 changes: 85 additions & 2 deletions src/main/java/azzy/fabric/lookingglass/block/CursedEarthBlock.java
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"variants": {
"": {
"model": "lookingglass:block/polished_whitestone"
}
}
}

0 comments on commit 6a5d5c7

Please sign in to comment.