/
BlockShortGrassTFC.java
172 lines (151 loc) · 6.02 KB
/
BlockShortGrassTFC.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/*
* Work under Copyright. Licensed under the EUPL.
* See the project README.md and LICENSE.txt for more information.
*/
package net.dries007.tfc.objects.blocks.plants;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import javax.annotation.ParametersAreNonnullByDefault;
import net.minecraft.block.Block;
import net.minecraft.block.state.BlockStateContainer;
import net.minecraft.block.state.IBlockState;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Items;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.NonNullList;
import net.minecraft.util.math.AxisAlignedBB;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.RayTraceResult;
import net.minecraft.world.EnumSkyBlock;
import net.minecraft.world.IBlockAccess;
import net.minecraft.world.World;
import net.minecraftforge.common.IShearable;
import net.dries007.tfc.api.types.Plant;
import net.dries007.tfc.objects.items.ItemsTFC;
import net.dries007.tfc.world.classic.ClimateTFC;
@ParametersAreNonnullByDefault
public class BlockShortGrassTFC extends BlockPlantTFC implements IShearable
{
private static final AxisAlignedBB GRASS_AABB = new AxisAlignedBB(0.125D, 0.0D, 0.125D, 0.875D, 1.0D, 0.875D);
private static final AxisAlignedBB SHORTER_GRASS_AABB = new AxisAlignedBB(0.125D, 0.0D, 0.125D, 0.875D, 0.5D, 0.875D);
private static final AxisAlignedBB SHORT_GRASS_AABB = new AxisAlignedBB(0.125D, 0.0D, 0.125D, 0.875D, 0.75D, 0.875D);
private static final AxisAlignedBB SHORTEST_GRASS_AABB = new AxisAlignedBB(0.125D, 0.0D, 0.125D, 0.875D, 0.25D, 0.875D);
private static final Map<Plant, BlockShortGrassTFC> MAP = new HashMap<>();
public static BlockShortGrassTFC get(Plant plant)
{
return BlockShortGrassTFC.MAP.get(plant);
}
public BlockShortGrassTFC(Plant plant)
{
super(plant);
if (MAP.put(plant, this) != null) throw new IllegalStateException("There can only be one.");
}
@Override
public void harvestBlock(World worldIn, EntityPlayer player, BlockPos pos, IBlockState state, @Nullable TileEntity te, ItemStack stack)
{
if (!worldIn.isRemote && stack.getItem() == Items.SHEARS)
{
spawnAsEntity(worldIn, pos, new ItemStack(this, 1));
}
else if (!worldIn.isRemote && stack.getItem().getHarvestLevel(stack, "knife", player, state) != -1)
{
spawnAsEntity(worldIn, pos, new ItemStack(ItemsTFC.STRAW, 1));
}
super.harvestBlock(worldIn, player, pos, state, te, stack);
}
@Override
@Nonnull
public Block.EnumOffsetType getOffsetType()
{
return Block.EnumOffsetType.XZ;
}
@Override
public void updateTick(World worldIn, BlockPos pos, IBlockState state, Random rand)
{
if (!worldIn.isAreaLoaded(pos, 1)) return;
if (plant.isValidGrowthTemp(ClimateTFC.getHeightAdjustedTemp(worldIn, pos)) && plant.isValidSunlight(Math.subtractExact(worldIn.getLightFor(EnumSkyBlock.SKY, pos), worldIn.getSkylightSubtracted())))
{
int j = state.getValue(AGE);
if (rand.nextDouble() < getGrowthRate(worldIn, pos) && net.minecraftforge.common.ForgeHooks.onCropsGrowPre(worldIn, pos.up(), state, true))
{
if (j < 3)
{
worldIn.setBlockState(pos, state.withProperty(AGE, j + 1));
}
net.minecraftforge.common.ForgeHooks.onCropsGrowPost(worldIn, pos, state, worldIn.getBlockState(pos));
}
}
else if (!plant.isValidGrowthTemp(ClimateTFC.getHeightAdjustedTemp(worldIn, pos)) || !plant.isValidSunlight(worldIn.getLightFor(EnumSkyBlock.SKY, pos)))
{
int j = state.getValue(AGE);
if (rand.nextDouble() < getGrowthRate(worldIn, pos) && net.minecraftforge.common.ForgeHooks.onCropsGrowPre(worldIn, pos, state, true))
{
if (j > 0)
{
worldIn.setBlockState(pos, state.withProperty(AGE, j - 1));
}
else
{
worldIn.setBlockToAir(pos);
}
net.minecraftforge.common.ForgeHooks.onCropsGrowPost(worldIn, pos, state, worldIn.getBlockState(pos));
}
}
checkAndDropBlock(worldIn, pos, state);
}
@Override
@Nonnull
public AxisAlignedBB getBoundingBox(IBlockState state, IBlockAccess source, BlockPos pos)
{
switch (state.getValue(AGE))
{
case 0:
return SHORTEST_GRASS_AABB.offset(state.getOffset(source, pos));
case 1:
return SHORTER_GRASS_AABB.offset(state.getOffset(source, pos));
case 2:
return SHORT_GRASS_AABB.offset(state.getOffset(source, pos));
default:
return GRASS_AABB.offset(state.getOffset(source, pos));
}
}
@Override
@Nonnull
protected BlockStateContainer createPlantBlockState()
{
return new BlockStateContainer(this, AGE, growthStageProperty, DAYPERIOD);
}
@Override
public int quantityDroppedWithBonus(int fortune, Random random)
{
return 1 + random.nextInt(fortune * 2 + 1);
}
@SuppressWarnings("deprecation")
@Override
@Nonnull
public ItemStack getItem(World worldIn, BlockPos pos, IBlockState state)
{
return new ItemStack(this, 1);
}
@Override
@Nonnull
public ItemStack getPickBlock(IBlockState state, RayTraceResult target, World world, BlockPos pos, EntityPlayer player)
{
return new ItemStack(this, 1);
}
@Override
public boolean isShearable(ItemStack item, IBlockAccess world, BlockPos pos)
{
return true;
}
@Override
@Nonnull
public NonNullList<ItemStack> onSheared(ItemStack item, IBlockAccess world, BlockPos pos, int fortune)
{
return NonNullList.withSize(1, new ItemStack(this, 1));
}
}