Skip to content

Commit

Permalink
Add more bonemeal options, such as bone meal grass spread
Browse files Browse the repository at this point in the history
  • Loading branch information
KnightMiner committed Jul 18, 2019
1 parent 49165ab commit 69d6a21
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 12 deletions.
18 changes: 15 additions & 3 deletions src/main/java/knightminer/inspirations/common/Config.java
Expand Up @@ -233,7 +233,6 @@ public class Config {
// tweaks
public static boolean enablePigDesaddle = true;
public static boolean enableFittedCarpets = true;
public static boolean enableExtraBonemeal = true;
public static boolean betterFlowerPot = true;
public static boolean flowerPotComparator = true;
public static boolean coloredEnchantedRibbons = true;
Expand All @@ -253,6 +252,11 @@ public class Config {
public static boolean enableMoreSeeds = true;
public static boolean addGrassDrops = true;
public static boolean nerfCarrotPotatoDrops = true;
// bonemeal
public static boolean bonemealMushrooms = true;
public static boolean bonemealDeadBush = true;
public static boolean bonemealGrassSpread = true;
public static boolean bonemealMyceliumSpread = true;

public static String[] flowerOverrides = {
"biomesoplenty:flower_0->7",
Expand All @@ -279,7 +283,7 @@ public class Config {
* @param event PreInit event from main mod class
*/
public static void preInit(FMLPreInitializationEvent event) {
configFile = new Configuration(event.getSuggestedConfigurationFile(), "0.3", false);
configFile = new Configuration(event.getSuggestedConfigurationFile(), "0.4", false);

showAllVariants = configFile.getBoolean("showAllVariants", "general", showAllVariants,
"Shows all variants for dynamically textured blocks, like bookshelves. If false just the first will be shown");
Expand Down Expand Up @@ -469,7 +473,15 @@ public static void preInit(FMLPreInitializationEvent event) {
enableFittedCarpets = configFile.getBoolean("fittedCarpets", "tweaks", enableFittedCarpets, "Carpets fit to stairs. Uses a block override, so disable if another mod replaces carpets");

// bonemeal
enableExtraBonemeal = configFile.getBoolean("extraBonemeal", "tweaks", enableExtraBonemeal, "Bonemeal can be used on mycelium to produce mushrooms and on sand to produce dead bushes");
if (getConfigVersion() < 0.4) {
boolean oldValue = configFile.get("tweaks", "extraBonemeal", true).getBoolean();
bonemealMushrooms = oldValue;
bonemealDeadBush = oldValue;
}
bonemealMushrooms = configFile.getBoolean("mushrooms", "tweaks.bonemeal", bonemealMushrooms, "Bonemeal can be used on mycelium to produce mushrooms");
bonemealDeadBush = configFile.getBoolean("deadBush", "tweaks.bonemeal", bonemealDeadBush, "Bonemeal can be used on sand to produce dead bushes");
bonemealGrassSpread = configFile.getBoolean("grassSpread", "tweaks.bonemeal", bonemealGrassSpread, "Bonemeal can be used on dirt to produce grass if adjecent to grass");
bonemealMyceliumSpread = configFile.getBoolean("myceliumSpread", "tweaks.bonemeal", bonemealMyceliumSpread, "Bonemeal can be used on dirt to produce mycelium if adjecent to mycelium");

// heartroot
enableHeartbeet = configFile.getBoolean("heartbeet", "tweaks", enableHeartbeet, "Enables heartbeets: a rare drop from beetroots which can be eaten to restore a bit of health");
Expand Down
78 changes: 69 additions & 9 deletions src/main/java/knightminer/inspirations/tweaks/TweaksEvents.java
@@ -1,15 +1,15 @@
package knightminer.inspirations.tweaks;

import java.util.List;
import knightminer.inspirations.common.Config;
import knightminer.inspirations.common.network.MilkablePacket;
import knightminer.inspirations.common.network.InspirationsNetwork;
import knightminer.inspirations.common.network.MilkablePacket;
import knightminer.inspirations.library.ItemMetaKey;
import knightminer.inspirations.shared.InspirationsShared;
import knightminer.inspirations.shared.SharedEvents;
import net.minecraft.block.Block;
import net.minecraft.block.BlockBush;
import net.minecraft.block.BlockCrops;
import net.minecraft.block.BlockDirt;
import net.minecraft.block.state.IBlockState;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityLivingBase;
Expand All @@ -22,6 +22,7 @@
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.EnumActionResult;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.SoundCategory;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Vec3d;
Expand All @@ -35,6 +36,8 @@
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import net.minecraftforge.items.ItemHandlerHelper;

import java.util.List;

public class TweaksEvents {

@SubscribeEvent
Expand Down Expand Up @@ -62,7 +65,7 @@ public static void unsaddlePig(EntityInteract event) {

@SubscribeEvent
public static void extraBonemeal(BonemealEvent event) {
if(!Config.enableExtraBonemeal) {
if(!Config.bonemealMushrooms && !Config.bonemealDeadBush && !Config.bonemealGrassSpread && !Config.bonemealMyceliumSpread) {
return;
}

Expand All @@ -73,19 +76,30 @@ public static void extraBonemeal(BonemealEvent event) {
}

BlockPos pos = event.getPos();
Block block = world.getBlockState(pos).getBlock();
boolean isMycelium = block == Blocks.MYCELIUM;
IBlockState state = world.getBlockState(pos);
Block block = state.getBlock();
// block must be mycelium for mushrooms or sand for dead bushes
if(!isMycelium && block != Blocks.SAND) {
return;
if((Config.bonemealMushrooms && block == Blocks.MYCELIUM) || (Config.bonemealDeadBush && block == Blocks.SAND)) {
bonemealPlants(block, world, pos);
event.setResult(Result.ALLOW);
}
// block must be dirt for grass/mycelium spread
else if((Config.bonemealGrassSpread || Config.bonemealMyceliumSpread) && block == Blocks.DIRT && state.getValue(BlockDirt.VARIANT) == BlockDirt.DirtType.DIRT) {
if (bonemealDirt(world, pos)) {
event.setResult(Result.ALLOW);
}
}
}

/** Called when using bonemeal on mycelium or sand to produce a plant */
private static void bonemealPlants(Block base, World world, BlockPos pos) {
// this is mostly copied from grass block code, so its a bit weird
BlockPos up = pos.up();
BlockBush bush = Blocks.DEADBUSH;
IBlockState state = bush.getDefaultState();

// 128 chances, this affects how far blocks are spread
boolean isMycelium = base == Blocks.MYCELIUM;
for (int i = 0; i < 128; ++i) {
BlockPos next = up;
int j = 0;
Expand Down Expand Up @@ -114,15 +128,61 @@ public static void extraBonemeal(BonemealEvent event) {
next = next.add(world.rand.nextInt(3) - 1, (world.rand.nextInt(3) - 1) * world.rand.nextInt(3) / 2, world.rand.nextInt(3) - 1);

// if the new position is invalid, this cycle is done
if (world.getBlockState(next.down()).getBlock() != block|| world.getBlockState(next).isNormalCube()) {
if (world.getBlockState(next.down()).getBlock() != base || world.getBlockState(next).isNormalCube()) {
break;
}

++j;
}
}
}

/** Called when using bonemeal on a dirt block to spread grass */
private static boolean bonemealDirt(World world, BlockPos pos) {
if(world.getLightFromNeighbors(pos.up()) < 9) {
return false;
}

// first, get a count of grass and mycelium on all sides
int grass = 0;
int mycelium = 0;
for (EnumFacing side : EnumFacing.HORIZONTALS) {
BlockPos offset = pos.offset(side);
IBlockState state = world.getBlockState(offset);
Block block = state.getBlock();

// hill logic: go up for dirt, down for air
if (block.isAir(state, world, pos)) {
state = world.getBlockState(offset.down());
block = state.getBlock();
}
else if (block != Blocks.GRASS && block != Blocks.MYCELIUM) {
state = world.getBlockState(offset.up());
block = state.getBlock();
}

// increment if the state is grass/mycelium
if (Config.bonemealGrassSpread && block == Blocks.GRASS) {
grass++;
}
else if (Config.bonemealMyceliumSpread && block == Blocks.MYCELIUM) {
mycelium++;
}
}

// no results? exit
if (grass == 0 && mycelium == 0) {
return false;
}

// chance gets higher the more blocks of the type surround
if (world.rand.nextInt(5) > (Math.max(grass, mycelium) - 1)) {
return true;
}

event.setResult(Result.ALLOW);
// place block based on which has more, grass wins ties
world.setBlockState(pos, grass >= mycelium ? Blocks.GRASS.getDefaultState() : Blocks.MYCELIUM.getDefaultState());
return true;
}

@SubscribeEvent
Expand Down

0 comments on commit 69d6a21

Please sign in to comment.