Skip to content

Commit

Permalink
The implementation of grass growth has been moved to a separate class
Browse files Browse the repository at this point in the history
  • Loading branch information
OldSerpskiStalker committed Dec 18, 2024
1 parent 91f1ab1 commit 0ddf616
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 76 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,81 +50,6 @@ public OnEventSandBox()
instanceExists = true;
}

//todo: доделать замену низкой травы на высокую и повысить время и шанс, чтобы у нас все не заросло за минуту игрового времени
// убрать это
private static final Random random = new Random();

private static int tickCounter = 0;

@SubscribeEvent
public static void onWorldTick_0(TickEvent.WorldTickEvent event)
{
//-' не трогаем сервер и тип мира конец края
if (event.phase == TickEvent.Phase.END || event.world.isRemote)
{
return;
}

if (++tickCounter < 20)
{
return;
}

tickCounter = 0;

World world = event.world;

for (EntityPlayer player : world.playerEntities)
{
BlockPos playerPos = player.getPosition();
int x = random.nextInt(16) + playerPos.getX() - 8;
int z = random.nextInt(16) + playerPos.getZ() - 8;
int y = world.getHeight(x, z) - 1;

BlockPos groundPos = new BlockPos(x, y, z);
Block block = world.getBlockState(groundPos).getBlock();

if (block == Blocks.GRASS)
{
BlockPos abovePos = groundPos.up();

IBlockState stateAbove = world.getBlockState(abovePos);

if (stateAbove.getBlock() == Blocks.TALLGRASS)
{
if (stateAbove.getValue(BlockTallGrass.TYPE) == BlockTallGrass.EnumType.GRASS)
{
BlockPos upperPos = abovePos.up();

if (world.isAirBlock(upperPos) && random.nextInt(100) < 95)
{
IBlockState doubleTallGrassLower = Blocks.DOUBLE_PLANT.getDefaultState()
.withProperty(BlockDoublePlant.VARIANT, BlockDoublePlant.EnumPlantType.GRASS)
.withProperty(BlockDoublePlant.HALF, BlockDoublePlant.EnumBlockHalf.LOWER);

IBlockState doubleTallGrassUpper = Blocks.DOUBLE_PLANT.getDefaultState()
.withProperty(BlockDoublePlant.VARIANT, BlockDoublePlant.EnumPlantType.GRASS)
.withProperty(BlockDoublePlant.HALF, BlockDoublePlant.EnumBlockHalf.UPPER);

world.setBlockState(abovePos, doubleTallGrassLower, 3);
world.setBlockState(upperPos, doubleTallGrassUpper, 3);
}
}
}
else if (world.isAirBlock(abovePos))
{
if (random.nextInt(100) < 95)
{
IBlockState smallGrassState = Blocks.TALLGRASS.getDefaultState()
.withProperty(BlockTallGrass.TYPE, BlockTallGrass.EnumType.GRASS);

world.setBlockState(abovePos, smallGrassState, 3);
}
}
}
}
}

//@SubscribeEvent
//public static void onFogDensity(EntityViewRenderEvent.FogDensity event) {
// Задание плотности тумана
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ public final class RegisterGameplayClass
OnEventComplexityBiomes.class,
OnEventDropHeadMob.class,
OnEventNickNameEntity.class,
OnEventUpdateFire.class
OnEventUpdateFire.class,
OnEventOvergrowingGrass.class
};

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
package org.imesense.dynamicspawncontrol.event;

import net.minecraft.block.Block;
import net.minecraft.block.BlockDoublePlant;
import net.minecraft.block.BlockTallGrass;
import net.minecraft.block.state.IBlockState;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Blocks;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import net.minecraftforge.fml.common.gameevent.TickEvent;
import org.imesense.dynamicspawncontrol.DynamicSpawnControlStructure;
import org.imesense.dynamicspawncontrol.core.field.UniqueField;
import org.imesense.dynamicspawncontrol.core.logfile.Log;
import org.imesense.dynamicspawncontrol.core.util.CodeGeneric;

/**
*
*/
@Mod.EventBusSubscriber(modid = DynamicSpawnControlStructure.STRUCT_INFO_MOD.MOD_ID)
public final class OnEventOvergrowingGrass
{
/**
*
*/
private static volatile int _TICK_COUNTER = 0;

/**
*
*/
private static boolean instanceExists = false;

/**
*
*/
public OnEventOvergrowingGrass()
{
CodeGeneric.printInitClassToLog(this.getClass());

if (instanceExists)
{
Log.writeDataToLogFile(2, String.format("An instance of [%s] already exists!", this.getClass().getSimpleName()));
throw new RuntimeException();
}

instanceExists = true;
}

/**
*
* @param worldTickEvent
*/
@SubscribeEvent
public static void onWorldTick_0(TickEvent.WorldTickEvent worldTickEvent)
{
if (worldTickEvent.phase == TickEvent.Phase.END || worldTickEvent.world.isRemote)
{
return;
}

if (++_TICK_COUNTER < 20)
{
return;
}

_TICK_COUNTER = 0;

World world = worldTickEvent.world;

for (EntityPlayer player : world.playerEntities)
{
BlockPos playerPos = player.getPosition();

int x = UniqueField.RANDOM.nextInt(16) + playerPos.getX() - 8;
int z = UniqueField.RANDOM.nextInt(16) + playerPos.getZ() - 8;

int y = world.getHeight(x, z) - 1;

BlockPos groundPos = new BlockPos(x, y, z);
Block block = world.getBlockState(groundPos).getBlock();

if (block == Blocks.GRASS)
{
BlockPos abovePos = groundPos.up();

IBlockState stateAbove = world.getBlockState(abovePos);

if (stateAbove.getBlock() == Blocks.TALLGRASS)
{
if (stateAbove.getValue(BlockTallGrass.TYPE) == BlockTallGrass.EnumType.GRASS)
{
BlockPos upperPos = abovePos.up();

if (world.isAirBlock(upperPos) && UniqueField.RANDOM.nextInt(100) < 95)
{
IBlockState doubleTallGrassLower = Blocks.DOUBLE_PLANT.getDefaultState()
.withProperty(BlockDoublePlant.VARIANT, BlockDoublePlant.EnumPlantType.GRASS)
.withProperty(BlockDoublePlant.HALF, BlockDoublePlant.EnumBlockHalf.LOWER);

IBlockState doubleTallGrassUpper = Blocks.DOUBLE_PLANT.getDefaultState()
.withProperty(BlockDoublePlant.VARIANT, BlockDoublePlant.EnumPlantType.GRASS)
.withProperty(BlockDoublePlant.HALF, BlockDoublePlant.EnumBlockHalf.UPPER);

world.setBlockState(abovePos, doubleTallGrassLower, 3);
world.setBlockState(upperPos, doubleTallGrassUpper, 3);
}
}
}
else if (world.isAirBlock(abovePos))
{
if (UniqueField.RANDOM.nextInt(100) < 95)
{
IBlockState smallGrassState = Blocks.TALLGRASS.getDefaultState()
.withProperty(BlockTallGrass.TYPE, BlockTallGrass.EnumType.GRASS);

world.setBlockState(abovePos, smallGrassState, 3);
}
}
}
}
}
}

0 comments on commit 0ddf616

Please sign in to comment.