Skip to content

Commit

Permalink
Fix #9
Browse files Browse the repository at this point in the history
  • Loading branch information
LasmGratel committed Nov 21, 2021
1 parent 73a8ffc commit 67c934d
Show file tree
Hide file tree
Showing 10 changed files with 159 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import com.projecturanus.foodcraft.common.network.registerMessages
import com.projecturanus.foodcraft.common.recipe.RecipeRegistryHandler
import com.projecturanus.foodcraft.fluid.FluidCookingOil
import com.projecturanus.foodcraft.fluid.FluidMilk
import com.projecturanus.foodcraft.worldgen.FruitTreeWorldGen
import com.projecturanus.foodcraft.worldgen.FruitTreeWorldGenHandler
import net.minecraft.util.ResourceLocation
import net.minecraftforge.common.MinecraftForge
import net.minecraftforge.fluids.FluidRegistry
Expand Down Expand Up @@ -43,7 +43,7 @@ object FoodCraftReloaded {
FluidRegistry.addBucketForFluid(FluidCookingOil)
registerMessages()

MinecraftForge.TERRAIN_GEN_BUS.register(FruitTreeWorldGen)
MinecraftForge.TERRAIN_GEN_BUS.register(FruitTreeWorldGenHandler)
}

fun registerTileEntities() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.projecturanus.foodcraft.common.block

import com.projecturanus.foodcraft.common.config.FcConfig
import com.projecturanus.foodcraft.worldgen.WorldGenFruitTree
import net.minecraft.block.*
import net.minecraft.block.BlockSapling.STAGE
import net.minecraft.block.state.BlockStateContainer
Expand All @@ -11,7 +13,6 @@ import net.minecraft.item.ItemStack
import net.minecraft.util.NonNullList
import net.minecraft.util.math.BlockPos
import net.minecraft.world.World
import net.minecraft.world.gen.feature.WorldGenTrees
import net.minecraftforge.event.terraingen.TerrainGen
import net.minecraftforge.fml.relauncher.Side
import net.minecraftforge.fml.relauncher.SideOnly
Expand Down Expand Up @@ -90,13 +91,14 @@ class BlockFruitSapling(val leavesState: IBlockState) : BlockBush(), IGrowable {
override fun getLocalizedName(): String = I18n.format(baseTranslationKey, I18n.format(realTranslationKey))
}

fun generateTree(worldIn: World, pos: BlockPos, state: IBlockState, rand: Random, leavesState: IBlockState, isSapling: Boolean = true) {
fun generateTree(worldIn: World, pos: BlockPos, state: IBlockState, rand: Random, fruitLeafState: IBlockState, isSapling: Boolean = true) {
if (!TerrainGen.saplingGrowTree(worldIn, rand, pos)) return
val logState = Blocks.LOG.defaultState.withProperty(BlockOldLog.VARIANT, BlockPlanks.EnumType.JUNGLE)
val leafState = Blocks.LEAVES.defaultState.withProperty(BlockOldLeaf.VARIANT, BlockPlanks.EnumType.JUNGLE)
val air = Blocks.AIR.defaultState

worldIn.setBlockState(pos, air, 4)
val genTrees = WorldGenTrees(true, 4 + rand.nextInt(7), logState, leavesState, false)
val genTrees = WorldGenFruitTree(4 + rand.nextInt(7), logState, leafState, fruitLeafState, FcConfig.fruitLeavesChance)
if (!genTrees.generate(worldIn, rand, pos) && isSapling) {
worldIn.setBlockState(pos, state, 4)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.projecturanus.foodcraft.common.config;

import com.projecturanus.foodcraft.FoodCraftReloadedKt;
import com.projecturanus.foodcraft.worldgen.FruitTreeWorldGen;
import com.projecturanus.foodcraft.worldgen.FruitTreeWorldGenHandler;
import net.minecraftforge.common.config.Config;
import net.minecraftforge.common.config.ConfigManager;
import net.minecraftforge.fml.client.event.ConfigChangedEvent;
Expand All @@ -19,6 +19,12 @@ public class FcConfig {
@Config.SlidingOption
public static double fruitTreeChance = 0.08;

@Config.Name("Fruit Leaves Generate Chance")
@Config.Comment({"Generate chance for fruit leaves on a fruit tree, default to 0.3 (lower to be more rare)"})
@Config.RangeDouble(min = 0.0, max = 1.0)
@Config.SlidingOption
public static double fruitLeavesChance = 0.3;

@Config.Name("Fruit Tree Biomes Whitelist")
@Config.Comment("Use ; to split, requires full ResourceLocation name")
public static String fruitTreeBiomes = "minecraft:jungle;minecraft:mutated_jungle;minecraft:mutated_jungle_edge";
Expand Down Expand Up @@ -129,7 +135,7 @@ public static class ClientConfig {
public static void onReloadConfig(ConfigChangedEvent event) {
if (event.getModID().equals(FoodCraftReloadedKt.MODID)) {
ConfigManager.sync(FoodCraftReloadedKt.MODID, Config.Type.INSTANCE);
FruitTreeWorldGen.INSTANCE.setBiomeIds(fruitTreeBiomes);
FruitTreeWorldGenHandler.INSTANCE.setBiomeIds(fruitTreeBiomes);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import net.minecraft.item.Item;
import net.minecraftforge.fml.common.registry.GameRegistry;

@SuppressWarnings("unused")
@GameRegistry.ObjectHolder(FoodCraftReloadedKt.MODID)
public class FCRItems {
public static final Item CHOPPING_BOARD = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,19 +140,19 @@ object RegisterHandler {
} to arrayOf("crop$capitalized", "food$capitalized", "seed$capitalized", "listAllveggie")
}
plantFruits.forEach {
val capitalized = it.toString().replace("_", "").capitalize()
val blockCrop = BlockCrop().apply {
setRegistryName(MODID, it.toString())
}
PLANTS += blockCrop
blockQueue += blockCrop
itemQueue += crop(null, blockCrop) {
setRegistryName(MODID, it.toString())
creativeTab = FcTabPlant
translationKey = "$MODID.$it"
CROPS += this@crop
blockCrop.cropItem = this
} to arrayOf("crop$capitalized", "food$capitalized", "seed$capitalized", "listAllfruit")
val capitalized = it.toString().replace("_", "").capitalize()
val blockCrop = BlockCrop().apply {
setRegistryName(MODID, it.toString())
}
PLANTS += blockCrop
blockQueue += blockCrop
itemQueue += crop(null, blockCrop) {
setRegistryName(MODID, it.toString())
creativeTab = FcTabPlant
translationKey = "$MODID.$it"
CROPS += this@crop
blockCrop.cropItem = this
} to arrayOf("crop$capitalized", "food$capitalized", "seed$capitalized", "listAllfruit")
}
saplingFruits.forEach {
val fruit = food(name = it.toString(), healAmount = 1, saturation = 0.6f) {}
Expand All @@ -172,10 +172,10 @@ object RegisterHandler {
LEAVES += blockLeaves
blockQueue += blockSapling
SAPLINGS += blockSapling
itemQueue += ItemBlock(blockLeaves).apply {
itemQueue += ItemBlockTranslation(blockLeaves).apply {
setRegistryName(MODID, "${it}_leaves")
} to arrayOf("treeLeaves")
itemQueue += ItemBlock(blockSapling).apply {
itemQueue += ItemBlockTranslation(blockSapling).apply {
setRegistryName(MODID, "${it}_sapling")
} to arrayOf("treeSapling")
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.projecturanus.foodcraft.common.item

import net.minecraft.block.Block
import net.minecraft.item.ItemBlock
import net.minecraft.item.ItemStack
import net.minecraftforge.fml.relauncher.Side
import net.minecraftforge.fml.relauncher.SideOnly

/**
* Delegate translation to block
*/
class ItemBlockTranslation(block: Block) : ItemBlock(block) {
@SideOnly(Side.CLIENT)
override fun getItemStackDisplayName(stack: ItemStack): String =
block.localizedName
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import net.minecraftforge.fml.common.eventhandler.SubscribeEvent
import net.minecraftforge.fml.common.registry.ForgeRegistries
import net.minecraftforge.registries.ForgeRegistry

object FruitTreeWorldGen {
object FruitTreeWorldGenHandler {
private var biomeIdsInternal = emptyList<Int>()

var biomeIds
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package com.projecturanus.foodcraft.worldgen

import net.minecraft.block.BlockSapling
import net.minecraft.block.material.Material
import net.minecraft.block.state.IBlockState
import net.minecraft.init.Blocks
import net.minecraft.util.EnumFacing
import net.minecraft.util.math.BlockPos
import net.minecraft.util.math.BlockPos.MutableBlockPos
import net.minecraft.world.World
import net.minecraft.world.gen.feature.WorldGenAbstractTree
import java.util.*
import kotlin.math.abs

class WorldGenFruitTree(val minTreeHeight: Int, val wood: IBlockState, val leaves: IBlockState, val fruitLeaves: IBlockState, val fruitChance: Double) : WorldGenAbstractTree(true) {

override fun generate(worldIn: World, rand: Random, position: BlockPos): Boolean {
val i = rand.nextInt(3) + minTreeHeight
var flag = true
return if (position.y >= 1 && position.y + i + 1 <= worldIn.height) {
for (j in position.y..position.y + 1 + i) {
var k = 1
if (j == position.y) {
k = 0
}
if (j >= position.y + 1 + i - 2) {
k = 2
}
val mutablePos = MutableBlockPos()
var l = position.x - k
while (l <= position.x + k && flag) {
var i1 = position.z - k
while (i1 <= position.z + k && flag) {
if (j >= 0 && j < worldIn.height) {
if (!isReplaceable(worldIn, mutablePos.setPos(l, j, i1))) {
flag = false
}
} else {
flag = false
}
++i1
}
++l
}
}
if (!flag) {
false
} else {
var state = worldIn.getBlockState(position.down())
if (state.block.canSustainPlant(
state,
worldIn,
position.down(),
EnumFacing.UP,
Blocks.SAPLING as BlockSapling
) && position.y < worldIn.height - i - 1
) {
state.block.onPlantGrow(state, worldIn, position.down(), position)
for (i3 in position.y - 3 + i..position.y + i) {
val i4 = i3 - (position.y + i)
val j1 = 1 - i4 / 2
for (k1 in position.x - j1..position.x + j1) {
val l1 = k1 - position.x
for (i2 in position.z - j1..position.z + j1) {
val j2 = i2 - position.z
if (abs(l1) != j1 || abs(j2) != j1 || rand.nextInt(2) != 0 && i4 != 0) {
val blockpos = BlockPos(k1, i3, i2)
state = worldIn.getBlockState(blockpos)
if (state.block.isAir(state, worldIn, blockpos) || state.block.isLeaves(
state,
worldIn,
blockpos
) || state.material === Material.VINE
) {
val leaves = if (worldIn.rand.nextDouble() <= fruitChance) {
this.leaves
} else {
this.fruitLeaves
}
setBlockAndNotifyAdequately(worldIn, blockpos, leaves)
}
}
}
}
}
for (j3 in 0 until i) {
val upN = position.up(j3)
state = worldIn.getBlockState(upN)
if (state.block.isAir(state, worldIn, upN) || state.block.isLeaves(
state,
worldIn,
upN
) || state.material === Material.VINE
) {
setBlockAndNotifyAdequately(worldIn, position.up(j3), this.wood)
}
}
true
} else {
false
}
}
} else {
false
}
}
}
3 changes: 2 additions & 1 deletion src/main/resources/assets/foodcraftreloaded/lang/en_us.lang
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,8 @@ item.foodcraftreloaded.diamond_kitchen_knife.name=Diamond Knife
item.foodcraftreloaded.emerald_kitchen_knife.name=Emerald Knife

# Sapling
item.foodcraftreloaded.sapling=%s Sapling
item.foodcraftreloaded.sapling.name=%s Sapling
item.foodcraftreloaded.leaves.name=%s Leaves

# Jam
item.foodcraftreloaded.jam.name=%s Jam
Expand Down
3 changes: 2 additions & 1 deletion src/main/resources/assets/foodcraftreloaded/lang/zh_cn.lang
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,8 @@ item.foodcraftreloaded.diamond_kitchen_knife.name=钻石刀
item.foodcraftreloaded.emerald_kitchen_knife.name=绿宝石刀

# Sapling
item.foodcraftreloaded.sapling=%s树苗
item.foodcraftreloaded.sapling.name=%s树苗
item.foodcraftreloaded.leaves.name=%s树叶

# Jam
item.foodcraftreloaded.jam.name=%s果酱
Expand Down

0 comments on commit 67c934d

Please sign in to comment.