-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathGTFOCrop.java
More file actions
171 lines (139 loc) · 5.05 KB
/
GTFOCrop.java
File metadata and controls
171 lines (139 loc) · 5.05 KB
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
package gregtechfoodoption.block;
import gregtechfoodoption.GTFOValues;
import gregtechfoodoption.GregTechFoodOption;
import net.minecraft.block.BlockCrops;
import net.minecraft.block.properties.PropertyInteger;
import net.minecraft.block.state.BlockStateContainer;
import net.minecraft.block.state.IBlockState;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.EnumHand;
import net.minecraft.util.NonNullList;
import net.minecraft.util.math.AxisAlignedBB;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.IBlockAccess;
import net.minecraft.world.World;
import net.minecraftforge.common.EnumPlantType;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public class GTFOCrop extends BlockCrops {
public static final PropertyInteger DEFAULT_AGE = PropertyInteger.create("age", 0, 5);
private static final AxisAlignedBB CROPS_AABB = new AxisAlignedBB(0.0D, 0.0D, 0.0D, 1.0D, 0.25D, 1.0D);
protected ItemStack seed;
protected ItemStack crop;
public static List<GTFOCrop> CROP_BLOCKS = new ArrayList<>();
private final String name;
protected GTFOCrop(String name) {
this.setDefaultState(this.blockState.getBaseState().withProperty(this.getAgeProperty(), 0));
this.setRegistryName(GregTechFoodOption.MODID, "crop_" + name);
CROP_BLOCKS.add(this);
this.name = name;
this.setTranslationKey("gtfo_crop_" + name);
this.setCreativeTab(GTFOValues.TAB_GTFO_CROPS);
}
public static GTFOCrop create(String name) {
return new GTFOCrop(name);
}
public AxisAlignedBB getBoundingBox(IBlockState state, IBlockAccess source, BlockPos pos) {
return CROPS_AABB;
}
@Override
public EnumPlantType getPlantType(IBlockAccess world, BlockPos pos) {
return EnumPlantType.Crop;
}
public int getMaxAge() {
return 5;
}
public void getDrops(NonNullList<ItemStack> drops, IBlockAccess world, BlockPos pos, IBlockState state, int fortune) {
int age = this.getAge(state);
Random rand = world instanceof World ? ((World) world).rand : new Random();
if (age >= this.getMaxAge()) {
if (!seed.isEmpty()) {
ItemStack seedStack = seed.copy();
if (rand.nextInt(9) == 0) seedStack.setCount(seedStack.getCount() + 1);
drops.add(seedStack);
}
int cropCount = 0;
for (int i = 0; i < 3 + fortune; ++i) {
if (rand.nextInt(2 * this.getMaxAge()) <= age) {
cropCount++;
}
}
if (cropCount > 0) {
ItemStack crop = this.crop.copy();
crop.setCount(cropCount);
drops.add(crop);
}
}
}
public Item getSeed() {
return seed.getItem();
}
public Item getCrop() {
return crop.getItem();
}
public ItemStack getCropStack() {
return crop;
}
public void setSeed(ItemStack seed) {
this.seed = seed;
}
public void setCrop(ItemStack crop) {
this.crop = crop;
}
@Override
public int damageDropped(IBlockState state) {
return seed.getItemDamage();
}
// The One Probe needs this!
public ItemStack getItem(World worldIn, BlockPos pos, IBlockState state) {
return this.seed;
}
public ItemStack getSeedStack() {
return this.seed;
}
@Override
public PropertyInteger getAgeProperty() {
return DEFAULT_AGE;
}
protected BlockStateContainer createBlockState() {
return new BlockStateContainer(this, getAgeProperty());
}
public String getName() {
return this.name;
}
@Override
public boolean onBlockActivated(World world, BlockPos pos, IBlockState state, EntityPlayer playerIn, EnumHand hand, EnumFacing facing, float hitX, float hitY, float hitZ) {
Random rand = world instanceof World ? world.rand : new Random();
int age = this.getAge(state);
if (this.isMaxAge(state)) {
List<ItemStack> drops = new ArrayList<>();
if (!seed.isEmpty() && rand.nextInt(9) == 0) {
ItemStack seedStack = seed.copy();
drops.add(seedStack);
}
int cropCount = 0;
for (int i = 0; i < 3; ++i) {
if (rand.nextInt(2 * this.getMaxAge()) <= age) {
cropCount++;
}
}
if (cropCount > 0) {
ItemStack crop = this.crop.copy();
crop.setCount(cropCount);
drops.add(crop);
}
for (ItemStack drop : drops) {
if (!playerIn.addItemStackToInventory(drop)) {
playerIn.dropItem(drop, false);
}
}
world.setBlockState(pos, state.withProperty(getAgeProperty(), 0), 3);
return true;
}
return false;
}
}