Allow querying blocks by plant type (will reduce a lot of code later on)

This commit is contained in:
Cheeserolls 2015-06-11 14:43:57 +01:00
parent 495442c03f
commit cd1421805a
9 changed files with 130 additions and 43 deletions

View file

@ -0,0 +1,18 @@
/*******************************************************************************
* Copyright 2014, the Biomes O' Plenty Team
*
* This work is licensed under a Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International Public License.
*
* To view a copy of this license, visit http://creativecommons.org/licenses/by-nc-nd/4.0/.
******************************************************************************/
package biomesoplenty.api.block;
import net.minecraft.util.BlockPos;
import net.minecraft.world.IBlockAccess;
import net.minecraftforge.common.EnumPlantType;
public interface ISustainsPlantType
{
public boolean canSustainPlantType(IBlockAccess world, BlockPos pos, EnumPlantType plantType);
}

View file

@ -45,6 +45,16 @@ public class BlockBOPBamboo extends BlockBOPDecoration
this.setDefaultState(this.blockState.getBaseState().withProperty(AGE, Integer.valueOf(0)));
}
// bamboo doesn't sustain plants (except more bamboo on top)
@Override
public boolean canSustainPlant(IBlockAccess world, BlockPos pos, EnumFacing direction, net.minecraftforge.common.IPlantable plantable)
{
return direction == EnumFacing.UP && plantable.getPlant(world, pos.offset(direction)).getBlock() == this;
}
@Override
public boolean canBlockStay(World world, BlockPos pos, IBlockState state)
{
@ -142,11 +152,6 @@ public class BlockBOPBamboo extends BlockBOPDecoration
return true;
}
// bamboo doesn't sustain plants (except more bamboo on top)
@Override
public boolean canSustainPlant(IBlockAccess world, BlockPos pos, EnumFacing direction, net.minecraftforge.common.IPlantable plantable)
{
return direction == EnumFacing.UP && plantable.getPlant(world, pos.offset(direction)).getBlock() == this;
}
}

View file

@ -24,13 +24,15 @@ import net.minecraft.util.EnumFacing;
import net.minecraft.util.IStringSerializable;
import net.minecraft.world.IBlockAccess;
import net.minecraft.world.World;
import net.minecraftforge.common.EnumPlantType;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import biomesoplenty.api.block.BOPBlocks;
import biomesoplenty.api.block.IBOPBlock;
import biomesoplenty.api.block.ISustainsPlantType;
import biomesoplenty.common.item.ItemBOPBlock;
public class BlockBOPDirt extends Block implements IBOPBlock
public class BlockBOPDirt extends Block implements IBOPBlock, ISustainsPlantType
{
// TODO: make it ploughable into farmland
@ -96,13 +98,10 @@ public class BlockBOPDirt extends Block implements IBOPBlock
// both variant and coarseness saved in meta, first bit coarseness, other bits variant
return (Boolean.TRUE.equals(state.getValue(COARSE)) ? 8 : 0) | ((BOPDirtType) state.getValue(VARIANT)).ordinal();
}
@Override
public boolean canSustainPlant(IBlockAccess world, BlockPos pos, EnumFacing direction, net.minecraftforge.common.IPlantable plantable)
{
net.minecraftforge.common.EnumPlantType plantType = plantable.getPlantType(world, pos.offset(direction));
public boolean canSustainPlantType(IBlockAccess world, BlockPos pos, EnumPlantType plantType)
{
switch (plantType)
{
// support desert, plains and cave plants
@ -122,6 +121,12 @@ public class BlockBOPDirt extends Block implements IBOPBlock
}
}
@Override
public boolean canSustainPlant(IBlockAccess world, BlockPos pos, EnumFacing direction, net.minecraftforge.common.IPlantable plantable)
{
return this.canSustainPlantType(world, pos, plantable.getPlantType(world, pos.offset(direction)));
}
@Override
@SideOnly(Side.CLIENT)
public void updateTick(World world, BlockPos pos, IBlockState state, Random rand) {

View file

@ -12,6 +12,7 @@ import java.util.HashSet;
import java.util.Set;
import biomesoplenty.api.block.IBOPBlock;
import biomesoplenty.api.block.ISustainsPlantType;
import biomesoplenty.common.item.ItemBOPBlock;
import net.minecraft.block.Block;
import net.minecraft.block.material.Material;
@ -23,7 +24,7 @@ import net.minecraft.util.EnumFacing;
import net.minecraft.world.IBlockAccess;
import net.minecraftforge.common.EnumPlantType;
public class BlockBOPGeneric extends Block implements IBOPBlock
public class BlockBOPGeneric extends Block implements IBOPBlock, ISustainsPlantType
{
// implement IBOPBlock
@ -65,11 +66,16 @@ public class BlockBOPGeneric extends Block implements IBOPBlock
return this;
}
@Override
public boolean canSustainPlantType(IBlockAccess world, BlockPos pos, EnumPlantType plantType)
{
return this.plantTypesICanSustain.contains(plantType);
}
@Override
public boolean canSustainPlant(IBlockAccess world, BlockPos pos, EnumFacing direction, net.minecraftforge.common.IPlantable plantable)
{
EnumPlantType plantType = plantable.getPlantType(world, pos.offset(direction));
return this.plantTypesICanSustain.contains(plantType);
return this.canSustainPlantType(world, pos, plantable.getPlantType(world, pos.offset(direction)));
}
}

View file

@ -12,6 +12,7 @@ import java.util.Random;
import biomesoplenty.api.block.BOPBlocks;
import biomesoplenty.api.block.IBOPBlock;
import biomesoplenty.api.block.ISustainsPlantType;
import biomesoplenty.common.item.ItemBOPBlock;
import net.minecraft.block.Block;
import net.minecraft.block.BlockDirt;
@ -35,10 +36,11 @@ import net.minecraft.util.EnumParticleTypes;
import net.minecraft.util.IStringSerializable;
import net.minecraft.world.IBlockAccess;
import net.minecraft.world.World;
import net.minecraftforge.common.EnumPlantType;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
public class BlockBOPGrass extends BlockGrass implements IBOPBlock
public class BlockBOPGrass extends BlockGrass implements IBOPBlock, ISustainsPlantType
{
// TODO: make it ploughable into farmland
@ -112,14 +114,10 @@ public class BlockBOPGrass extends BlockGrass implements IBOPBlock
}
@Override
public boolean canSustainPlant(IBlockAccess world, BlockPos pos, EnumFacing direction, net.minecraftforge.common.IPlantable plantable)
public boolean canSustainPlantType(IBlockAccess world, BlockPos pos, EnumPlantType plantType)
{
// Workaround for bug in forge - BlockReed calls this function with the wrong block position
if (plantable instanceof BlockReed) {pos = pos.down();}
IBlockState state = world.getBlockState(pos);
net.minecraftforge.common.EnumPlantType plantType = plantable.getPlantType(world, pos.offset(direction));
switch ((BOPGrassType) state.getValue(VARIANT))
{
@ -156,7 +154,17 @@ public class BlockBOPGrass extends BlockGrass implements IBOPBlock
default:
return false;
}
}
@Override
public boolean canSustainPlant(IBlockAccess world, BlockPos pos, EnumFacing direction, net.minecraftforge.common.IPlantable plantable)
{
// Workaround for bug in forge (hopefully temporary) - BlockReed calls this function with the wrong block position
// ...which leads to us getting a state for a block which might not be this one
if (plantable instanceof BlockReed) {pos = pos.down();}
return this.canSustainPlantType(world, pos, plantable.getPlantType(world, pos.offset(direction)));
}

View file

@ -27,11 +27,13 @@ import net.minecraft.util.EnumFacing;
import net.minecraft.util.IStringSerializable;
import net.minecraft.world.IBlockAccess;
import net.minecraft.world.World;
import net.minecraftforge.common.EnumPlantType;
import biomesoplenty.api.block.IBOPBlock;
import biomesoplenty.api.block.ISustainsPlantType;
import biomesoplenty.api.item.BOPItems;
import biomesoplenty.common.item.ItemBOPBlock;
public class BlockBOPMud extends Block implements IBOPBlock
public class BlockBOPMud extends Block implements IBOPBlock, ISustainsPlantType
{
// add properties
@ -148,12 +150,9 @@ public class BlockBOPMud extends Block implements IBOPBlock
return 4;
}
@Override
public boolean canSustainPlant(IBlockAccess world, BlockPos pos, EnumFacing direction, net.minecraftforge.common.IPlantable plantable)
{
net.minecraftforge.common.EnumPlantType plantType = plantable.getPlantType(world, pos.offset(direction));
public boolean canSustainPlantType(IBlockAccess world, BlockPos pos, EnumPlantType plantType)
{
switch (plantType)
{
case Plains:
@ -171,4 +170,11 @@ public class BlockBOPMud extends Block implements IBOPBlock
}
}
@Override
public boolean canSustainPlant(IBlockAccess world, BlockPos pos, EnumFacing direction, net.minecraftforge.common.IPlantable plantable)
{
return this.canSustainPlantType(world, pos, plantable.getPlantType(world, pos.offset(direction)));
}
}

View file

@ -23,10 +23,12 @@ import net.minecraft.util.EnumFacing;
import net.minecraft.util.IStringSerializable;
import net.minecraft.world.IBlockAccess;
import net.minecraft.world.World;
import net.minecraftforge.common.EnumPlantType;
import biomesoplenty.api.block.IBOPBlock;
import biomesoplenty.api.block.ISustainsPlantType;
import biomesoplenty.common.item.ItemBOPBlock;
public class BlockBOPSand extends BlockFalling implements IBOPBlock
public class BlockBOPSand extends BlockFalling implements IBOPBlock, ISustainsPlantType
{
// add properties
@ -118,13 +120,12 @@ public class BlockBOPSand extends BlockFalling implements IBOPBlock
default:
break;
}
}
}
@Override
public boolean canSustainPlant(IBlockAccess world, BlockPos pos, EnumFacing direction, net.minecraftforge.common.IPlantable plantable)
{
net.minecraftforge.common.EnumPlantType plantType = plantable.getPlantType(world, pos.offset(direction));
public boolean canSustainPlantType(IBlockAccess world, BlockPos pos, EnumPlantType plantType)
{
switch (plantType)
{
case Desert:
@ -142,4 +143,11 @@ public class BlockBOPSand extends BlockFalling implements IBOPBlock
}
}
@Override
public boolean canSustainPlant(IBlockAccess world, BlockPos pos, EnumFacing direction, net.minecraftforge.common.IPlantable plantable)
{
return this.canSustainPlantType(world, pos, plantable.getPlantType(world, pos.offset(direction)));
}
}

View file

@ -17,17 +17,20 @@ import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import biomesoplenty.api.block.ISustainsPlantType;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Sets;
import net.minecraft.block.Block;
import net.minecraft.block.BlockLiquid;
import net.minecraft.block.material.Material;
import net.minecraft.block.properties.IProperty;
import net.minecraft.block.state.IBlockState;
import net.minecraft.util.BlockPos;
import net.minecraft.util.EnumFacing;
import net.minecraft.world.World;
import net.minecraftforge.common.IPlantable;
import net.minecraftforge.common.EnumPlantType;
public class BlockQuery
{
@ -91,7 +94,7 @@ public class BlockQuery
public CompoundQueryBuilder withAirAbove() {return this.and(airAbove);}
public CompoundQueryBuilder withLightAtLeast(int a) {return this.and(new BlockPosQueryLightAtLeast(a));}
public CompoundQueryBuilder withLightNoMoreThan(int a) {return this.and(new BlockPosQueryLightNoMoreThan(a));}
public CompoundQueryBuilder sustainsPlant(IPlantable plant) {return this.and(new BlockPosQuerySustainsPlant(plant));}
public CompoundQueryBuilder sustainsPlant(EnumPlantType plantType) {return this.and(new BlockPosQuerySustainsPlantType(plantType));}
public IBlockPosQuery create() {return this.query.instance();}
@ -302,18 +305,46 @@ public class BlockQuery
}
}
// Match blocks which can sustain the given IPlantable block
public static class BlockPosQuerySustainsPlant implements IBlockPosQuery
// Match blocks which can sustain the given forge EnumPlantType plant type
public static class BlockPosQuerySustainsPlantType implements IBlockPosQuery
{
private IPlantable plant;
public BlockPosQuerySustainsPlant(IPlantable plant)
private EnumPlantType plantType;
public BlockPosQuerySustainsPlantType(EnumPlantType plantType)
{
this.plant = plant;
this.plantType = plantType;
}
@Override
public boolean matches(World world, BlockPos pos)
{
return world.getBlockState(pos).getBlock().canSustainPlant(world, pos, EnumFacing.UP, this.plant);
IBlockState state = world.getBlockState(pos);
Block block = state.getBlock();
if (block instanceof ISustainsPlantType)
{
// If there's a function specifically available for it, then use it
return ((ISustainsPlantType)block).canSustainPlantType(world, pos, this.plantType);
}
else
{
// Otherwise fall back to the vanilla code
switch (this.plantType)
{
case Desert: return block == net.minecraft.init.Blocks.sand || block == net.minecraft.init.Blocks.hardened_clay || block == net.minecraft.init.Blocks.stained_hardened_clay || block == net.minecraft.init.Blocks.dirt;
case Nether: return block == net.minecraft.init.Blocks.soul_sand;
case Crop: return block == net.minecraft.init.Blocks.farmland;
case Cave: return block.isSideSolid(world, pos, EnumFacing.UP);
case Plains: return block == net.minecraft.init.Blocks.grass || block == net.minecraft.init.Blocks.dirt || block == net.minecraft.init.Blocks.farmland;
case Water: return block.getMaterial() == Material.water && ((Integer)state.getValue(BlockLiquid.LEVEL)) == 0;
case Beach:
boolean isBeach = block == net.minecraft.init.Blocks.grass || block == net.minecraft.init.Blocks.dirt || block == net.minecraft.init.Blocks.sand;
boolean hasWater = (world.getBlockState(pos.east()).getBlock().getMaterial() == Material.water ||
world.getBlockState(pos.west()).getBlock().getMaterial() == Material.water ||
world.getBlockState(pos.north()).getBlock().getMaterial() == Material.water ||
world.getBlockState(pos.south()).getBlock().getMaterial() == Material.water);
return isBeach && hasWater;
}
return false;
}
}
}

View file

@ -148,6 +148,6 @@ public class BlockStateUtils
return null;
}
}