Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Fix rock blocks affected by gravity sideways and vertical
  • Loading branch information
alcatrazEscapee committed Aug 17, 2018
1 parent dced342 commit 20572cf
Show file tree
Hide file tree
Showing 17 changed files with 327 additions and 175 deletions.
92 changes: 69 additions & 23 deletions src/main/java/net/dries007/tfc/api/ITreeGenerator.java
Expand Up @@ -8,18 +8,71 @@

import java.util.Random;

import net.minecraft.block.Block;
import net.minecraft.block.BlockSapling;
import net.minecraft.block.state.IBlockState;
import net.minecraft.init.Blocks;
import net.minecraft.util.Rotation;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraft.world.gen.structure.template.PlacementSettings;
import net.minecraft.world.gen.structure.template.TemplateManager;
import net.minecraft.world.gen.structure.StructureBoundingBox;
import net.minecraft.world.gen.structure.template.*;

import net.dries007.tfc.api.types.Tree;
import net.dries007.tfc.objects.blocks.wood.BlockLeavesTFC;

public interface ITreeGenerator
{

/**
* This is a copy of the method included in the Template class, with some key differences.
* This will ignore TEs / Entities, and does less checks for bad usage, since it will only be used for tree worldgen
* It will do an additional check that the block is replaceable; important for tree growth; as to not replace other blocks
*
* @param worldIn the world
* @param pos the position
* @param template the template
* @param placementIn the placement settings
*/
static void addStructureToWorld(World worldIn, BlockPos pos, Template template, PlacementSettings placementIn)
{
int flags = 2;
ITemplateProcessor templateProcessor = new BlockRotationProcessor(pos, placementIn);
StructureBoundingBox structureboundingbox = placementIn.getBoundingBox();

for (Template.BlockInfo template$blockinfo : template.blocks)
{
BlockPos blockpos = Template.transformedBlockPos(placementIn, template$blockinfo.pos).add(pos);
Template.BlockInfo template$blockinfo1 = templateProcessor.processBlock(worldIn, blockpos, template$blockinfo);

if (template$blockinfo1 != null)
{
Block block1 = template$blockinfo1.blockState.getBlock();

if ((!placementIn.getIgnoreStructureBlock() || block1 != Blocks.STRUCTURE_BLOCK) && (structureboundingbox == null || structureboundingbox.isVecInside(blockpos)))
{
IBlockState iblockstate = template$blockinfo1.blockState.withMirror(placementIn.getMirror());
IBlockState iblockstate1 = iblockstate.withRotation(placementIn.getRotation());

if (worldIn.getBlockState(blockpos).getMaterial().isReplaceable() || worldIn.getBlockState(blockpos).getBlock() instanceof BlockLeavesTFC)
worldIn.setBlockState(blockpos, iblockstate1, flags);

}
}
}

for (Template.BlockInfo template$blockinfo2 : template.blocks)
{
BlockPos blockpos1 = Template.transformedBlockPos(placementIn, template$blockinfo2.pos).add(pos);

if (structureboundingbox == null || structureboundingbox.isVecInside(blockpos1))
{
worldIn.notifyNeighborsRespectDebug(blockpos1, template$blockinfo2.blockState.getBlock(), false);
}

}
}

/**
* Called to generate a tree. Each Tree must have one of these. Used for world gen and sapling growth
*
Expand All @@ -32,27 +85,34 @@ public interface ITreeGenerator
void generateTree(TemplateManager manager, World world, BlockPos pos, Tree tree, Random rand);

/**
* This only sets the properties used by ITreeGenerator.addStructureToWorld
* @return A default set of placement settings for tree generation
*/

static PlacementSettings getDefaultSettings()
{
return new PlacementSettings()
.setIgnoreEntities(true)
.setIgnoreStructureBlock(false);
return new PlacementSettings().setIgnoreStructureBlock(false);
}

/**
* This only sets the properties used by ITreeGenerator.addStructureToWorld
* @param rand For generating random settings
* @return A set of placement settings with random rotation
*/
static PlacementSettings getRandomSettings(Random rand)
{
return getDefaultSettings()
.setRotation(Rotation.values()[rand.nextInt(Rotation.values().length)]);
return getDefaultSettings().setRotation(Rotation.values()[rand.nextInt(Rotation.values().length)]);
}

static boolean checkGenerationConditions(World world, BlockPos pos, Tree treeType)
/**
* Checks if a tree can be generated. This implementation checks height, radius, and light level
*
* @param world The world
* @param pos The pos of the tree
* @param treeType The tree type (for checking if the tree can generate)
* @return true if the tree can generate.
*/
default boolean canGenerateTree(World world, BlockPos pos, Tree treeType)
{
// Check if ground is flat enough
final int radius = treeType.maxGrowthRadius;
Expand All @@ -79,24 +139,10 @@ static boolean checkGenerationConditions(World world, BlockPos pos, Tree treeTyp

// Check the position for liquids, etc.
if (world.getBlockState(pos).getMaterial().isLiquid() || !world.getBlockState(pos).getMaterial().isReplaceable())
if (!(world.getBlockState(pos) instanceof BlockSapling))
if (!(world.getBlockState(pos).getBlock() instanceof BlockSapling))
return false;

// Check if there is sufficient light level
return world.getLightFromNeighbors(pos) >= 7;
}

/**
* Checks if a tree can be generated. This implementation checks height, radius, and light level
* Suggested use is to use within your implementation of generateTree()
*
* @param world The world
* @param pos The pos of the tree
* @param treeType The tree type (for checking if the tree can generate)
* @return true if the tree can generate.
*/
default boolean canGenerateTree(World world, BlockPos pos, Tree treeType)
{
return checkGenerationConditions(world, pos, treeType);
}
}
52 changes: 36 additions & 16 deletions src/main/java/net/dries007/tfc/api/types/Rock.java
Expand Up @@ -14,6 +14,8 @@
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.registries.IForgeRegistryEntry;

import static net.dries007.tfc.api.types.Rock.FallingBlockType.*;

/**
* todo: document API
*/
Expand Down Expand Up @@ -79,31 +81,42 @@ public RockCategory getRockCategory()

public enum Type
{
RAW(Material.ROCK, false, false),
SMOOTH(Material.ROCK, false, false),
COBBLE(Material.ROCK, true, false),
BRICKS(Material.ROCK, false, false),
SAND(Material.SAND, true, false),
GRAVEL(Material.SAND, true, false),
DIRT(Material.GROUND, false, false),
GRASS(Material.GRASS, false, true),
DRY_GRASS(Material.GRASS, false, true),
CLAY(Material.GRASS, false, false),
CLAY_GRASS(Material.GRASS, false, true),
FARMLAND(Material.GROUND, false, false),
PATH(Material.GROUND, false, false);
RAW(Material.ROCK, NO_FALL, false), // Todo: add collapsing when broken
SMOOTH(Material.ROCK, NO_FALL, false),
COBBLE(Material.ROCK, FALL_HORIZONTAL, false),
BRICKS(Material.ROCK, NO_FALL, false),
SAND(Material.SAND, FALL_HORIZONTAL, false),
GRAVEL(Material.SAND, FALL_HORIZONTAL, false),
DIRT(Material.GROUND, FALL_HORIZONTAL, false),
GRASS(Material.GRASS, FALL_HORIZONTAL, true),
DRY_GRASS(Material.GRASS, FALL_HORIZONTAL, true),
CLAY(Material.GRASS, FALL_VERTICAL, false),
CLAY_GRASS(Material.GRASS, FALL_VERTICAL, true),
FARMLAND(Material.GROUND, FALL_VERTICAL, false),
PATH(Material.GROUND, FALL_VERTICAL, false);

public final Material material;
public final boolean isAffectedByGravity;
public final boolean isGrass;

Type(Material material, boolean isAffectedByGravity, boolean isGrass)
private final FallingBlockType gravType;

Type(Material material, FallingBlockType gravType, boolean isGrass)
{
this.material = material;
this.isAffectedByGravity = isAffectedByGravity;
this.gravType = gravType;
this.isGrass = isGrass;
}

public boolean canFall()
{
return gravType != NO_FALL;
}

public boolean canFallHorizontal()
{
return gravType == FALL_HORIZONTAL;
}

public Type getNonGrassVersion()
{
if (!isGrass) return this;
Expand Down Expand Up @@ -132,4 +145,11 @@ public Type getGrassVersion(Type spreader)
throw new IllegalArgumentException("You cannot get grass from rock types.");
}
}

protected enum FallingBlockType
{
NO_FALL,
FALL_VERTICAL,
FALL_HORIZONTAL
}
}
Expand Up @@ -7,6 +7,8 @@

import java.util.Random;

import javax.annotation.ParametersAreNonnullByDefault;

import net.minecraft.block.Block;
import net.minecraft.block.material.Material;
import net.minecraft.block.properties.PropertyInteger;
Expand All @@ -26,9 +28,13 @@
import net.minecraft.world.World;
import net.minecraftforge.common.IPlantable;

import mcp.MethodsReturnNonnullByDefault;
import net.dries007.tfc.api.types.Rock;
import net.dries007.tfc.util.IFallingBlock;

public class BlockFarmlandTFC extends BlockRockVariant
@MethodsReturnNonnullByDefault
@ParametersAreNonnullByDefault
public class BlockFarmlandTFC extends BlockRockVariantFallable
{
public static final int MAX_MOISTURE = 15;
public static final PropertyInteger MOISTURE = PropertyInteger.create("moisture", 0, MAX_MOISTURE);
Expand All @@ -50,8 +56,8 @@ public class BlockFarmlandTFC extends BlockRockVariant
0xff8f8f8f,
0xff878787,
};
protected static final AxisAlignedBB FARMLAND_AABB = new AxisAlignedBB(0.0D, 0.0D, 0.0D, 1.0D, 0.9375D, 1.0D);
protected static final AxisAlignedBB FLIPPED_AABB = new AxisAlignedBB(0.0D, 0.9375D, 0.0D, 1.0D, 1.0D, 1.0D);
private static final AxisAlignedBB FARMLAND_AABB = new AxisAlignedBB(0.0D, 0.0D, 0.0D, 1.0D, 0.9375D, 1.0D);
private static final AxisAlignedBB FLIPPED_AABB = new AxisAlignedBB(0.0D, 0.9375D, 0.0D, 1.0D, 1.0D, 1.0D);

public BlockFarmlandTFC(Rock.Type type, Rock rock)
{
Expand Down
Expand Up @@ -7,6 +7,8 @@

import java.util.Random;

import javax.annotation.ParametersAreNonnullByDefault;

import net.minecraft.block.state.BlockFaceShape;
import net.minecraft.block.state.IBlockState;
import net.minecraft.item.Item;
Expand All @@ -15,11 +17,14 @@
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.IBlockAccess;

import mcp.MethodsReturnNonnullByDefault;
import net.dries007.tfc.api.types.Rock;

public class BlockPathTFC extends BlockRockVariant
@MethodsReturnNonnullByDefault
@ParametersAreNonnullByDefault
public class BlockPathTFC extends BlockRockVariantFallable
{
protected static final AxisAlignedBB GRASS_PATH_AABB = new AxisAlignedBB(0.0D, 0.0D, 0.0D, 1.0D, 0.9375D, 1.0D);
private static final AxisAlignedBB GRASS_PATH_AABB = new AxisAlignedBB(0.0D, 0.0D, 0.0D, 1.0D, 0.9375D, 1.0D);

public BlockPathTFC(Rock.Type type, Rock rock)
{
Expand Down
@@ -0,0 +1,26 @@
/*
* Work under Copyright. Licensed under the EUPL.
* See the project README.md and LICENSE.txt for more information.
*
*/

package net.dries007.tfc.objects.blocks.stone;

import javax.annotation.ParametersAreNonnullByDefault;

import mcp.MethodsReturnNonnullByDefault;
import net.dries007.tfc.api.types.Rock;
import net.dries007.tfc.util.ICollapsableBlock;

@MethodsReturnNonnullByDefault
@ParametersAreNonnullByDefault
public class BlockRockRaw extends BlockRockVariant implements ICollapsableBlock
{

public BlockRockRaw(Rock.Type type, Rock rock)
{
super(type, rock);
}

//todo: add collapsable mechanics
}

0 comments on commit 20572cf

Please sign in to comment.