Skip to content

Commit

Permalink
Added BonemealableBlock to branch and root
Browse files Browse the repository at this point in the history
For mod compatibility
  • Loading branch information
supermassimo committed Dec 9, 2024
1 parent 6d58ac8 commit f91d33c
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.ferreusveritas.dynamictrees.block.BlockWithDynamicHardness;
import com.ferreusveritas.dynamictrees.block.leaves.DynamicLeavesBlock;
import com.ferreusveritas.dynamictrees.block.leaves.LeavesProperties;
import com.ferreusveritas.dynamictrees.block.rooty.RootyBlock;
import com.ferreusveritas.dynamictrees.data.provider.DTLootTableProvider;
import com.ferreusveritas.dynamictrees.entity.FallingTreeEntity;
import com.ferreusveritas.dynamictrees.entity.FallingTreeEntity.DestroyType;
Expand All @@ -24,6 +25,7 @@
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.tags.BlockTags;
import net.minecraft.util.Mth;
import net.minecraft.util.RandomSource;
Expand All @@ -32,18 +34,15 @@
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items;
import net.minecraft.world.item.enchantment.EnchantmentHelper;
import net.minecraft.world.item.enchantment.Enchantments;
import net.minecraft.world.level.BlockAndTintGetter;
import net.minecraft.world.level.BlockGetter;
import net.minecraft.world.level.Explosion;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.LevelAccessor;
import net.minecraft.world.level.*;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.block.BonemealableBlock;
import net.minecraft.world.level.block.RenderShape;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.properties.BlockStateProperties;
import net.minecraft.world.level.material.FluidState;
import net.minecraft.world.level.material.PushReaction;
import net.minecraft.world.level.pathfinder.PathComputationType;
Expand All @@ -52,21 +51,17 @@
import net.minecraft.world.phys.BlockHitResult;
import net.minecraft.world.phys.HitResult;
import net.minecraft.world.phys.Vec3;
import net.minecraft.world.phys.shapes.VoxelShape;
import net.minecraftforge.common.ForgeMod;
import net.minecraftforge.common.ToolActions;
import org.antlr.runtime.tree.Tree;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.*;
import java.util.stream.Collectors;

@SuppressWarnings("deprecation")
public abstract class BranchBlock extends BlockWithDynamicHardness implements TreePart, FutureBreakable {
public abstract class BranchBlock extends BlockWithDynamicHardness implements TreePart, FutureBreakable, BonemealableBlock {

public static final int MAX_RADIUS = 8;
public static final String NAME_SUFFIX = "_branch";
Expand Down Expand Up @@ -244,6 +239,36 @@ public BlockState getStateForDecay (BlockState state, LevelAccessor level, Block
return Blocks.AIR.defaultBlockState();
}

/**
* The following 3 methods are overridden by {@link #use(BlockState, Level, BlockPos, Player, InteractionHand, BlockHitResult)}
* and they are not normally called. However, they are here for mod compatibility.
*/
@Override
public boolean isValidBonemealTarget(LevelReader pLevel, BlockPos pPos, BlockState pState, boolean pIsClient){
if (!(pLevel instanceof Level level)) return false;
BlockPos rootPos = TreeHelper.findRootNode(level, pPos);
if (rootPos == BlockPos.ZERO) return false;
BlockState rootState = pLevel.getBlockState(rootPos);
RootyBlock root = TreeHelper.getRooty(rootState);
if (root == null) return false;

return root.isValidBonemealTarget(pLevel, rootPos, rootState, pIsClient);
}
@Override
public boolean isBonemealSuccess(Level pLevel, RandomSource pRandom, BlockPos pPos, BlockState pState){
return true;
}
@Override
public void performBonemeal(ServerLevel pLevel, RandomSource pRandom, BlockPos pPos, BlockState pState){
BlockPos rootPos = TreeHelper.findRootNode(pLevel, pPos);
if (rootPos == BlockPos.ZERO) return;
BlockState rootState = pLevel.getBlockState(rootPos);
RootyBlock root = TreeHelper.getRooty(rootState);
if (root == null) return;

root.performBonemeal(pLevel, pRandom, rootPos, rootState);
}

///////////////////////////////////////////
// RENDERING
///////////////////////////////////////////
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,15 @@
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items;
import net.minecraft.world.level.BlockAndTintGetter;
import net.minecraft.world.level.BlockGetter;
import net.minecraft.world.level.Explosion;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.LevelAccessor;
import net.minecraft.world.level.LevelReader;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.BonemealableBlock;
import net.minecraft.world.level.block.EntityBlock;
import net.minecraft.world.level.block.SoundType;
import net.minecraft.world.level.block.entity.BlockEntity;
Expand Down Expand Up @@ -73,7 +75,7 @@
* @author ferreusveritas
*/
@SuppressWarnings("deprecation")
public class RootyBlock extends BlockWithDynamicHardness implements TreePart, EntityBlock {
public class RootyBlock extends BlockWithDynamicHardness implements TreePart, EntityBlock, BonemealableBlock {

public static RootyBlockDecayer rootyBlockDecayer = null;

Expand Down Expand Up @@ -347,6 +349,29 @@ public void onBlockExploded(BlockState state, Level level, BlockPos pos, Explosi
*/
public int updateRadius(LevelAccessor level, BlockState state, BlockPos pos, int flags, boolean force){ return getRadius(state); }

/**
* The following 3 methods are overridden by {@link #use(BlockState, Level, BlockPos, Player, InteractionHand, BlockHitResult)}
* and they are not normally called. However, they are here for mod compatibility.
*/
@Override
public boolean isValidBonemealTarget(LevelReader pLevel, BlockPos pPos, BlockState pState, boolean pIsClient){
if (pLevel instanceof Level level)
return getSpecies(pState, level, pPos).canBoneMealTree();
return false;
}
@Override
public boolean isBonemealSuccess(Level pLevel, RandomSource pRandom, BlockPos pPos, BlockState pState){
return true;
}
@Override
public void performBonemeal(ServerLevel pLevel, RandomSource pRandom, BlockPos pPos, BlockState pState){
Species species = getSpecies(pState, pLevel, pPos);
if (species.isValid()){
species.applySubstance(pLevel, pPos, pPos, null, null, new ItemStack(Items.BONE_MEAL));
}

}

///////////////////////////////////////////
// TREE STUFF
///////////////////////////////////////////
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1906,7 +1906,7 @@ public SubstanceEffect getSubstanceEffect(ItemStack itemStack) {
* @param itemStack The itemstack to be used.
* @return true if item was used, false otherwise
*/
public boolean applySubstance(Level level, BlockPos rootPos, BlockPos hitPos, Player player, InteractionHand hand, ItemStack itemStack) {
public boolean applySubstance(Level level, BlockPos rootPos, BlockPos hitPos, @Nullable Player player, @Nullable InteractionHand hand, ItemStack itemStack) {
final SubstanceEffect effect = getSubstanceEffect(itemStack);

if (effect != null) {
Expand Down

0 comments on commit f91d33c

Please sign in to comment.