Jump to content
  • Home
  • Files
  • Docs
  • Merch
Topics
  • All Content

  • This Topic
  • This Forum

  • Advanced Search
  • Existing user? Sign In  

    Sign In



    • Not recommended on shared computers


    • Forgot your password?

  • Sign Up
  • All Activity
  • Home
  • Mod Developer Central
  • Modder Support
  • [SOLVED] [1.14.2] custom tree does not replace sapling
1.13 Update Notes for Mod Creators
Sign in to follow this  
Followers 1
mmyron

[SOLVED] [1.14.2] custom tree does not replace sapling

By mmyron, June 9 in Modder Support

  • Reply to this topic
  • Start new topic

Recommended Posts

mmyron    0

mmyron

mmyron    0

  • Tree Puncher
  • mmyron
  • Members
  • 0
  • 17 posts
Posted June 9

I have a custom tree that is not replacing the sapling when it spawns, and instead forms over the sapling leaving the sapling at the bottom.

 

Code:

Custom Sapling:

package com.mmyron.bettergameplay.block;

import com.mmyron.bettergameplay.block.tree.TallBirchTree;

import net.minecraft.block.SaplingBlock;

public class TallBirchSaplingBlock extends SaplingBlock{

	public TallBirchSaplingBlock(Properties properties) {
		super(new TallBirchTree(), properties);
	}

}

 

Tree:

package com.mmyron.bettergameplay.block.tree;

import java.util.Random;

import com.mmyron.bettergameplay.world.gen.feature.TallBirchTreeFeature;

import net.minecraft.block.trees.Tree;
import net.minecraft.world.gen.feature.AbstractTreeFeature;
import net.minecraft.world.gen.feature.NoFeatureConfig;

public class TallBirchTree extends Tree{
	@Override
	protected AbstractTreeFeature<NoFeatureConfig> getTreeFeature(Random random){
		return new TallBirchTreeFeature(NoFeatureConfig::func_214639_a, true);
	}
}

 

TreeFeature:

package com.mmyron.bettergameplay.world.gen.feature;

import java.util.Random;
import java.util.Set;
import java.util.function.Function;

import com.mmyron.bettergameplay.init.BlockList;
import com.mojang.datafixers.Dynamic;

import net.minecraft.block.BlockState;
import net.minecraft.block.Blocks;
import net.minecraft.block.LogBlock;
import net.minecraft.util.Direction;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.MutableBoundingBox;
import net.minecraft.world.IBlockReader;
import net.minecraft.world.IWorld;
import net.minecraft.world.gen.IWorldGenerationReader;
import net.minecraft.world.gen.feature.AbstractTreeFeature;
import net.minecraft.world.gen.feature.NoFeatureConfig;
import net.minecraftforge.common.IPlantable;

public class TallBirchTreeFeature extends AbstractTreeFeature<NoFeatureConfig>{

	private BlockState blockStateWood = Blocks.OAK_LOG.getDefaultState();
	private BlockState blockStateLeaves = Blocks.OAK_LEAVES.getDefaultState();
	private final int minTreeHeight = 12;
	
	public TallBirchTreeFeature(Function<Dynamic<?>, ? extends NoFeatureConfig> dynamic, boolean notify) {
		super(dynamic, notify);
		// TODO Auto-generated constructor stub
	}
	
	private void generateLeaves(IWorld parWorld, BlockPos parBlockPos, int height, Random parRandom) {
		for (int foliageY = parBlockPos.getY() - 4 + height; foliageY <= parBlockPos.getY() + height; ++foliageY)
        {
            int foliageLayer = foliageY - (parBlockPos.getY() + height) - 2;
            int foliageLayerRadius = 0 - foliageLayer / 2;

            for (int foliageX = parBlockPos.getX() - foliageLayerRadius; foliageX <= parBlockPos.getX() + foliageLayerRadius; ++foliageX)
            {
                int foliageRelativeX = foliageX - parBlockPos.getX();
                
                for (int foliageZ = parBlockPos.getZ() - foliageLayerRadius; foliageZ <= parBlockPos.getZ() + foliageLayerRadius; ++foliageZ)
                {
                    int foliageRelativeZ = foliageZ - parBlockPos.getZ();

                    // Fill in layer with some randomness
                    if (Math.abs(foliageRelativeX) != foliageLayerRadius || Math.abs(foliageRelativeZ) != foliageLayerRadius || parRandom.nextInt(2) != 0 && foliageLayer != 0)
                    {
                        BlockPos blockPos = new BlockPos(foliageX, foliageY, foliageZ);
                        BlockState state = parWorld.getBlockState(blockPos);

                        if (state.getBlock().isAir(state, parWorld, blockPos) || state.getBlock() == Blocks.OAK_LOG)
                        {
                            this.setBlockState(parWorld, blockPos, blockStateLeaves);
                        }
                    }
                }
            }
        }
	}
	
	private void generateTrunk(IWorld worldIn, BlockPos parBlockPos, int minHeight)
    {
		for(int height = 0; height < minTreeHeight; ++height) {
			BlockPos upN = parBlockPos.up(height);
			BlockState state = worldIn.getBlockState(upN);
			
			if(state.getBlock().isAir(state, worldIn, upN) || state.getBlock() == Blocks.OAK_LEAVES || state.getBlock() == BlockList.Register.TALL_BIRCH_SAPLING) {
				this.setBlockState(worldIn, parBlockPos.up(height), blockStateWood.with(LogBlock.AXIS, Direction.Axis.Y));
			}
		}
    }
	
	private boolean isSuitableLocation(IWorldGenerationReader worldIn, BlockPos parBlockPos, int minHeight)
    {
        boolean isSuitableLocation = true;
        
        for (int checkY = parBlockPos.getY(); checkY <= parBlockPos.getY() + 1 + minHeight; ++checkY)
        {
            // Handle increasing space towards top of tree
            int extraSpaceNeeded = 1;
            // Handle base location
            if (checkY == parBlockPos.getY())
            {
                extraSpaceNeeded = 0;
            }             
            // Handle top location
            if (checkY >= parBlockPos.getY() + 1 + minHeight - 2)
            {
                extraSpaceNeeded = 2;
            }

            for (int checkX = parBlockPos.getX() - extraSpaceNeeded; checkX <= parBlockPos.getX() + extraSpaceNeeded && isSuitableLocation; ++checkX)
            {
                for (int checkZ = parBlockPos.getZ() - extraSpaceNeeded; checkZ <= parBlockPos.getZ() + extraSpaceNeeded && isSuitableLocation; ++checkZ)
                {
                    isSuitableLocation = /*isReplaceable(worldIn,blockPos.setPos(checkX, checkY, checkZ))*/ true;
                }
            }
        }
        
        return isSuitableLocation;
    }
	
	@Override
	protected boolean place(Set<BlockPos> changedBlocks, IWorldGenerationReader worldIn, Random random, BlockPos pos, MutableBoundingBox p_208519_5_) {
		int minHeight = random.nextInt(3) + minTreeHeight;
        
        if (pos.getY() >= 1 && pos.getY() + minHeight + 1 <= 256)
        {
            if (!isSuitableLocation(worldIn, pos, minHeight))
            {
                return false;
            }
            else
            {
                BlockState state = ((IBlockReader) worldIn).getBlockState(pos.down());

                if (state.getBlock().canSustainPlant(state, (IBlockReader) worldIn, pos.down(), Direction.UP, (IPlantable) BlockList.Register.TALL_BIRCH_SAPLING) && pos.getY() < 256 - minHeight - 1)
                {
                    state.getBlock().onPlantGrow(state, (IWorld) worldIn, pos.down(), pos);
                    generateLeaves((IWorld) worldIn, pos, minHeight, random);
                    generateTrunk((IWorld) worldIn, pos, minHeight);
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }
        else
        {
            return false;
        }
	}

}

 

  • Quote

Share this post


Link to post
Share on other sites

Wolftopia    0

Wolftopia

Wolftopia    0

  • Tree Puncher
  • Wolftopia
  • Members
  • 0
  • 18 posts
Posted June 10 (edited)

I have had this problem too. I've tried a lot of things, and for some reason it seems that the sapling can't be replaced by other blocks even if canBeReplacedByLogs returns true.

 

Edit: I did some more testing and found something sort of weird.

 

When I add

	@Override
	public void grow(IWorld worldIn, BlockPos pos, BlockState state, Random rand) {
		super.grow(worldIn, pos, state, rand);
		worldIn.setBlockState(pos, RevampBlocks.Register.MAPLE_LOG.getDefaultState().with(BlockMapleLog.SAP, true), 1|2);
	}

to my custom sapling block class, it replaces the sapling with a log but doesn't grow the tree. However, when I have

	@Override
	public void grow(IWorld worldIn, BlockPos pos, BlockState state, Random rand) {
		worldIn.setBlockState(pos, RevampBlocks.Register.MAPLE_LOG.getDefaultState().with(BlockMapleLog.SAP, true), 1|2);
		super.grow(worldIn, pos, state, rand);
	}

instead, it grows the tree but doesn't replace the sapling.

Edited June 10 by Wolftopia
  • Quote

Share this post


Link to post
Share on other sites

Wolftopia    0

Wolftopia

Wolftopia    0

  • Tree Puncher
  • Wolftopia
  • Members
  • 0
  • 18 posts
Posted June 10

I found a solution: you can use the BirchTreeFeature code as a template.

  • Quote

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Insert image from URL
×
  • Desktop
  • Tablet
  • Phone
Sign in to follow this  
Followers 1
Go To Topic Listing



  • Recently Browsing

    No registered users viewing this page.

  • Posts

    • Oliviafrostpaw
      [1.14.4] Injecting into Existing Loot Tables, Blocks

      By Oliviafrostpaw · Posted 10 minutes ago

      After some poking, the function is not firing whatsoever
    • RaphGamingz
      [1.14.4] Dimensions

      By RaphGamingz · Posted 1 hour ago

      Anyone?
    • RaphGamingz
      [1.14.4] Injecting into Existing Loot Tables, Blocks

      By RaphGamingz · Posted 1 hour ago

      Are you sure this is correct, shouldn’t it be  new ResourceLocation(“minecraft”,”grass”) And is the function firing?
    • mervinrasquinha
      Introducing Stonecage

      By mervinrasquinha · Posted 3 hours ago

      If you’re interested in a modpack with a little bit of tech, a little bit of magic, and a lot of adventure, I would love for you to try out my modpack Stonecage. Stonecage takes mods you might have seen before (and many you might not have), and adds a twist to them to make them fresh again. The main gimmick of the pack is that it makes stone unmineable, meaning you’ll have to explore caves and dungeons the way they were intended, and find solutions to problems you never knew existed. It aims to be a hardcore pack that’s more fair than most, and leaves you to seek combat and adventure on your own terms. While you may start out weak, you’ll find magical artifacts known as curios in your adventures that can be crafted into powerful baubles that will help you in combat and elsewhere. You’ll also find pieces of ancient machines that with research can be deciphered and put back together, bringing industry back to a world that has long been without it. The pack is heavily centered around researching these lost machines, and the research table will guide you through the modpack while giving you more freedom than a quest book would. With a small, curated list of mods, most computers will likely be able to run it, and the many, many lines of scripting keep these mods integrated into what feels like a cohesive whole. If this sounds like fun to you, I encourage you to check it out. If you do, I hope you have as much fun playing it as I did creating it.
    • Darth_Cobalt
      Forge 1.14.4 crashes.

      By Darth_Cobalt · Posted 4 hours ago

      Hi I just downloaded forge and every time i try to load it, it crashes. I have tried 1.14.4 - 28.1.0 and 1.14.4 - 28.1.106. It crashes with both versions. I couldn't figure out how to upload the crash files, could somebody explain how I can do that?
  • Topics

    • Oliviafrostpaw
      7
      [1.14.4] Injecting into Existing Loot Tables, Blocks

      By Oliviafrostpaw
      Started December 8

    • RaphGamingz
      1
      [1.14.4] Dimensions

      By RaphGamingz
      Started Yesterday at 07:45 AM

    • mervinrasquinha
      0
      Introducing Stonecage

      By mervinrasquinha
      Started 3 hours ago

    • Darth_Cobalt
      0
      Forge 1.14.4 crashes.

      By Darth_Cobalt
      Started 4 hours ago

    • leonardsores
      0
      Fun mod interactions

      By leonardsores
      Started 4 hours ago

  • Who's Online (See full list)

    • xerca
    • Oliviafrostpaw
    • Redstoneguy129
    • Raelsyl
  • All Activity
  • Home
  • Mod Developer Central
  • Modder Support
  • [SOLVED] [1.14.2] custom tree does not replace sapling
  • Theme
  • Contact Us
  • Discord

Copyright © 2019 ForgeDevelopment LLC · Ads by Curse Powered by Invision Community