tcr/common/src/main/java/common/block/foliage/BlockDoublePlant.java

335 lines
11 KiB
Java
Raw Normal View History

2025-05-25 12:13:15 +02:00
package common.block.foliage;
2025-03-11 00:23:54 +01:00
2025-05-25 12:13:15 +02:00
import common.block.Block;
2025-05-25 14:45:04 +02:00
import common.block.Material;
2025-05-25 12:13:15 +02:00
import common.block.SoundType;
2025-05-07 18:19:24 +02:00
import common.entity.npc.EntityNPC;
import common.entity.types.EntityLiving;
import common.init.Blocks;
import common.init.Items;
import common.item.Item;
import common.item.ItemStack;
2025-07-19 23:55:20 +02:00
import common.item.StackSize;
2025-06-22 22:14:22 +02:00
import common.item.tool.ItemShears;
2025-05-07 18:19:24 +02:00
import common.model.Model;
import common.model.ModelProvider;
2025-07-21 19:54:21 +02:00
import common.model.GuiPosition;
2025-06-21 10:31:07 +02:00
import common.properties.Property;
2025-05-07 18:19:24 +02:00
import common.properties.PropertyEnum;
import common.rng.Random;
import common.tileentity.TileEntity;
2025-05-08 12:37:48 +02:00
import common.util.BlockPos;
2025-05-07 18:19:24 +02:00
import common.util.Identifyable;
2025-05-08 12:37:48 +02:00
import common.util.Facing.Axis;
import common.vars.Vars;
2025-05-07 18:19:24 +02:00
import common.world.IWorldAccess;
import common.world.State;
import common.world.World;
2025-05-13 17:02:57 +02:00
import common.world.AWorldServer;
2025-03-11 00:23:54 +01:00
2025-06-27 16:56:08 +02:00
public class BlockDoublePlant extends BlockBush implements IGrowable
2025-03-11 00:23:54 +01:00
{
public static final PropertyEnum<BlockDoublePlant.EnumBlockHalf> HALF = PropertyEnum.<BlockDoublePlant.EnumBlockHalf>create("half", BlockDoublePlant.EnumBlockHalf.class);
2025-06-21 14:04:15 +02:00
public static final BlockDoublePlant[] PLANTS = new BlockDoublePlant[EnumPlantType.values().length];
2025-03-11 00:23:54 +01:00
2025-06-20 23:10:16 +02:00
private final EnumPlantType type;
2025-06-21 18:58:35 +02:00
public static BlockDoublePlant getByType(EnumPlantType type) {
return PLANTS[type.ordinal()];
}
2025-06-20 23:10:16 +02:00
public BlockDoublePlant(EnumPlantType type)
2025-03-11 00:23:54 +01:00
{
2025-05-25 14:45:04 +02:00
super(Material.BUSH);
2025-06-20 23:10:16 +02:00
this.type = type;
2025-06-27 16:56:08 +02:00
this.setDefaultState(this.getBaseState().withProperty(HALF, BlockDoublePlant.EnumBlockHalf.LOWER));
2025-03-11 00:23:54 +01:00
this.setHardness(0.0F);
2025-07-20 14:20:55 +02:00
this.setSound(SoundType.GRASS);
2025-06-22 13:01:09 +02:00
this.setFlammable(60, 100);
2025-06-21 14:04:15 +02:00
PLANTS[type.ordinal()] = this;
2025-06-20 23:10:16 +02:00
}
public EnumPlantType getType() {
return this.type;
2025-03-11 00:23:54 +01:00
}
2025-07-20 14:20:55 +02:00
public void tick(AWorldServer worldIn, BlockPos pos, State state, Random rand)
2025-03-11 00:23:54 +01:00
{
if(Vars.plantDry && worldIn.getTemperatureC(pos) >= 50.0f)
2025-03-11 00:23:54 +01:00
{
boolean upper = state.getValue(HALF) == EnumBlockHalf.UPPER;
if(!upper)
pos = pos.up();
if(upper || worldIn.getState(pos).getBlock() == this)
worldIn.setState(pos, Blocks.air.getState());
pos = pos.down();
if(!upper || worldIn.getState(pos).getBlock() == this)
2025-06-20 23:10:16 +02:00
worldIn.setState(pos, this.type == EnumPlantType.GRASS || worldIn.rand.chance(20) ? Blocks.air.getState() :
Blocks.dead_bush.getState());
2025-03-11 00:23:54 +01:00
return;
}
2025-07-20 14:20:55 +02:00
super.tick(worldIn, pos, state, rand);
2025-03-11 00:23:54 +01:00
}
2025-07-20 14:20:55 +02:00
public void setBlockBounds(IWorldAccess worldIn, BlockPos pos)
2025-03-11 00:23:54 +01:00
{
this.setBlockBounds(0.0F, 0.0F, 0.0F, 1.0F, 1.0F, 1.0F);
}
2025-07-20 14:20:55 +02:00
public boolean canPlace(World worldIn, BlockPos pos)
2025-03-11 00:23:54 +01:00
{
2025-07-20 14:20:55 +02:00
return super.canPlace(worldIn, pos) && worldIn.isAirBlock(pos.up());
2025-03-11 00:23:54 +01:00
}
/**
* Whether this Block can be replaced directly by other blocks (true for e.g. tall grass)
*/
2025-07-20 14:20:55 +02:00
public boolean canReplace(World worldIn, BlockPos pos)
2025-03-11 00:23:54 +01:00
{
State iblockstate = worldIn.getState(pos);
if (iblockstate.getBlock() != this)
{
return true;
}
else
{
2025-06-20 23:10:16 +02:00
return this.type == BlockDoublePlant.EnumPlantType.FERN || this.type == BlockDoublePlant.EnumPlantType.GRASS;
2025-03-11 00:23:54 +01:00
}
}
protected void checkAndDropBlock(World worldIn, BlockPos pos, State state)
{
if (!this.canBlockStay(worldIn, pos, state))
{
boolean flag = state.getValue(HALF) == BlockDoublePlant.EnumBlockHalf.UPPER;
BlockPos blockpos = flag ? pos : pos.up();
BlockPos blockpos1 = flag ? pos.down() : pos;
Block block = (Block)(flag ? this : worldIn.getState(blockpos).getBlock());
Block block1 = (Block)(flag ? worldIn.getState(blockpos1).getBlock() : this);
if (block == this)
{
worldIn.setState(blockpos, Blocks.air.getState(), 2);
}
if (block1 == this)
{
worldIn.setState(blockpos1, Blocks.air.getState(), 3);
if (!flag)
{
2025-07-20 14:20:55 +02:00
this.drop(worldIn, blockpos1, state, 0);
2025-03-11 00:23:54 +01:00
}
}
}
}
public boolean canBlockStay(World worldIn, BlockPos pos, State state)
{
if (state.getValue(HALF) == BlockDoublePlant.EnumBlockHalf.UPPER)
{
return worldIn.getState(pos.down()).getBlock() == this;
}
else
{
State iblockstate = worldIn.getState(pos.up());
return iblockstate.getBlock() == this && super.canBlockStay(worldIn, pos, iblockstate);
}
}
/**
* Get the Item that this Block should drop when harvested.
*/
2025-07-20 14:20:55 +02:00
public Item getDrop(State state, Random rand, int fortune)
2025-03-11 00:23:54 +01:00
{
if (state.getValue(HALF) == BlockDoublePlant.EnumBlockHalf.UPPER)
{
return null;
}
else
{
2025-07-20 14:20:55 +02:00
return this.type == BlockDoublePlant.EnumPlantType.FERN ? null : (this.type == BlockDoublePlant.EnumPlantType.GRASS ? (rand.chance(8) ? Items.wheat_seed : null) : super.getDrop(state, rand, fortune));
2025-03-11 00:23:54 +01:00
}
}
2025-06-20 23:10:16 +02:00
public void placeAt(World worldIn, BlockPos lowerPos, int flags)
2025-03-11 00:23:54 +01:00
{
2025-06-20 23:10:16 +02:00
worldIn.setState(lowerPos, this.getState().withProperty(HALF, BlockDoublePlant.EnumBlockHalf.LOWER), flags);
2025-03-11 00:23:54 +01:00
worldIn.setState(lowerPos.up(), this.getState().withProperty(HALF, BlockDoublePlant.EnumBlockHalf.UPPER), flags);
}
/**
* Called by ItemBlocks after a block is set in the world, to allow post-place logic
*/
2025-07-20 14:20:55 +02:00
public void onPlace(World worldIn, BlockPos pos, State state, EntityLiving placer)
2025-03-11 00:23:54 +01:00
{
worldIn.setState(pos.up(), this.getState().withProperty(HALF, BlockDoublePlant.EnumBlockHalf.UPPER), 2);
}
2025-07-20 14:20:55 +02:00
public void postBroken(World worldIn, EntityNPC player, BlockPos pos, State state, TileEntity te)
2025-03-11 00:23:54 +01:00
{
if (worldIn.client || player.getCurrentEquippedItem() == null || !(player.getCurrentEquippedItem().getItem() instanceof ItemShears) || state.getValue(HALF) != BlockDoublePlant.EnumBlockHalf.LOWER || !this.onHarvest(worldIn, pos, state, player))
{
2025-07-20 14:20:55 +02:00
super.postBroken(worldIn, player, pos, state, te);
2025-03-11 00:23:54 +01:00
}
}
2025-07-20 14:20:55 +02:00
public void preBroken(World worldIn, BlockPos pos, State state, EntityNPC player)
2025-03-11 00:23:54 +01:00
{
if (state.getValue(HALF) == BlockDoublePlant.EnumBlockHalf.UPPER)
{
if (worldIn.getState(pos.down()).getBlock() == this)
{
// if (!player.creative)
// {
State iblockstate = worldIn.getState(pos.down());
2025-06-20 23:10:16 +02:00
if (this.type != BlockDoublePlant.EnumPlantType.FERN && this.type != BlockDoublePlant.EnumPlantType.GRASS)
2025-03-11 00:23:54 +01:00
{
worldIn.destroyBlock(pos.down(), true);
}
else if (!worldIn.client)
{
if (player.getCurrentEquippedItem() != null && player.getCurrentEquippedItem().getItem() instanceof ItemShears)
{
this.onHarvest(worldIn, pos, iblockstate, player);
worldIn.setBlockToAir(pos.down());
}
else
{
worldIn.destroyBlock(pos.down(), true);
}
}
else
{
worldIn.setBlockToAir(pos.down());
}
// }
// else
// {
// worldIn.setBlockToAir(pos.down());
// }
}
}
// else if (player.creative && worldIn.getState(pos.up()).getBlock() == this)
// {
// worldIn.setState(pos.up(), Blocks.air.getState(), 2);
// }
2025-07-20 14:20:55 +02:00
super.preBroken(worldIn, pos, state, player);
2025-03-11 00:23:54 +01:00
}
private boolean onHarvest(World worldIn, BlockPos pos, State state, EntityNPC player)
{
2025-06-20 23:10:16 +02:00
if (this.type != BlockDoublePlant.EnumPlantType.FERN && this.type != BlockDoublePlant.EnumPlantType.GRASS)
2025-03-11 00:23:54 +01:00
{
return false;
}
else
{
2025-07-20 14:20:55 +02:00
dropItem(worldIn, pos, new ItemStack(this.type == BlockDoublePlant.EnumPlantType.GRASS ? Items.tallgrass : Items.fern, 2));
2025-03-11 00:23:54 +01:00
return true;
}
}
/**
* Whether this IGrowable can grow
*/
public boolean canGrow(World worldIn, BlockPos pos, State state, boolean isClient)
{
2025-06-20 23:10:16 +02:00
return this.type != BlockDoublePlant.EnumPlantType.GRASS && this.type != BlockDoublePlant.EnumPlantType.FERN;
2025-03-11 00:23:54 +01:00
}
public boolean canUseBonemeal(World worldIn, Random rand, BlockPos pos, State state)
{
return true;
}
2025-05-13 17:02:57 +02:00
public void grow(AWorldServer worldIn, Random rand, BlockPos pos, State state)
2025-03-11 00:23:54 +01:00
{
2025-07-20 14:20:55 +02:00
dropItem(worldIn, pos, new ItemStack(this.getItem()));
2025-03-11 00:23:54 +01:00
}
2025-06-21 10:31:07 +02:00
protected Property[] getProperties()
2025-03-11 00:23:54 +01:00
{
2025-06-27 16:56:08 +02:00
return new Property[] {HALF};
2025-03-11 00:23:54 +01:00
}
2025-05-04 20:27:55 +02:00
public Model getModel(ModelProvider provider, String name, State state) {
2025-06-20 23:10:16 +02:00
if(this.type == EnumPlantType.SUNFLOWER && state.getValue(HALF) == EnumBlockHalf.UPPER)
return provider.getModel("sunflower_front")
2025-03-11 00:23:54 +01:00
.add(0.8f, 0f, 8f, 15.2f, 8f, 8f).noShade().rotate(8, 8, 8, Axis.Y, 45, true).ns("sunflower_top")
.uv(0, 8, 16, 16).noCull()
.add(8f, 0f, 0.8f, 8f, 8f, 15.2f).noShade().rotate(8, 8, 8, Axis.Y, 45, true).we("sunflower_top")
.uv(0, 8, 16, 16).noCull()
.add(9.6f, -1f, 1f, 9.6f, 15f, 15f).noShade().rotate(8, 8, 8, Axis.Z, 22.5f, true).w("sunflower_back")
.uv(0, 0, 16, 16).noCull().e().uv(0, 0, 16, 16).noCull();
else
2025-06-20 23:10:16 +02:00
return provider.getModel(this.type.getName() + "_" + (state.getValue(HALF) == EnumBlockHalf.UPPER
2025-03-11 00:23:54 +01:00
? "top" : "bottom")).cross();
}
2025-07-17 23:55:47 +02:00
2025-07-21 19:54:21 +02:00
public GuiPosition getItemPosition() {
return GuiPosition.CROSS;
2025-07-17 23:55:47 +02:00
}
2025-07-19 23:55:20 +02:00
public State getItemState() {
return this.getState().withProperty(HALF, EnumBlockHalf.UPPER);
}
2025-06-22 21:45:20 +02:00
2025-07-20 14:20:55 +02:00
public StackSize getMaxAmount() {
2025-07-19 23:55:20 +02:00
return StackSize.S;
2025-06-22 21:45:20 +02:00
}
2025-03-11 00:23:54 +01:00
2025-05-04 20:27:55 +02:00
public static enum EnumBlockHalf implements Identifyable
2025-03-11 00:23:54 +01:00
{
UPPER,
LOWER;
public String toString()
{
return this.getName();
}
public String getName()
{
return this == UPPER ? "upper" : "lower";
}
}
2025-05-04 20:27:55 +02:00
public static enum EnumPlantType implements Identifyable
2025-03-11 00:23:54 +01:00
{
2025-07-07 16:11:16 +02:00
SUNFLOWER("sunflower", "Sonnenblume"),
SYRINGA("syringa", "Flieder"),
GRASS("large_tallgrass", "Hohes Gras"),
FERN("large_fern", "Großer Farn"),
ROSE("rose_bush", "Rosenstrauch"),
PAEONIA("paeonia", "Pfingstrose");
2025-03-11 00:23:54 +01:00
private final String name;
private final String display;
2025-07-07 16:11:16 +02:00
private EnumPlantType(String name, String display)
2025-03-11 00:23:54 +01:00
{
this.name = name;
this.display = display;
}
public String toString()
{
return this.name;
}
public String getName()
{
return this.name;
}
public String getDisplay()
{
return this.display;
}
}
}