-
Notifications
You must be signed in to change notification settings - Fork 81
/
BlockSaguaro.java
364 lines (317 loc) · 14.2 KB
/
BlockSaguaro.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
package com.progwml6.natura.overworld.block.saguaro;
import java.util.List;
import java.util.Random;
import javax.annotation.Nullable;
import com.progwml6.natura.library.NaturaRegistry;
import com.progwml6.natura.overworld.NaturaOverworld;
import net.minecraft.block.Block;
import net.minecraft.block.SoundType;
import net.minecraft.block.material.Material;
import net.minecraft.block.properties.IProperty;
import net.minecraft.block.properties.PropertyBool;
import net.minecraft.block.state.BlockStateContainer;
import net.minecraft.block.state.IBlockState;
import net.minecraft.entity.Entity;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.init.Blocks;
import net.minecraft.util.DamageSource;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.Mirror;
import net.minecraft.util.Rotation;
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 net.minecraftforge.common.ForgeHooks;
import net.minecraftforge.common.IPlantable;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
public class BlockSaguaro extends Block implements IPlantable
{
//@formatter:off
/** Whether this cactus connects in the northern direction */
public static final PropertyBool NORTH = PropertyBool.create("north");
/** Whether this cactus connects in the eastern direction */
public static final PropertyBool EAST = PropertyBool.create("east");
/** Whether this cactus connects in the southern direction */
public static final PropertyBool SOUTH = PropertyBool.create("south");
/** Whether this cactus connects in the western direction */
public static final PropertyBool WEST = PropertyBool.create("west");
/** Whether this cactus connects in the upper direction */
public static final PropertyBool UP = PropertyBool.create("up");
/** Whether this cactus connects in the lower direction */
public static final PropertyBool DOWN = PropertyBool.create("down");
public static final AxisAlignedBB BASE_AABB = new AxisAlignedBB(0.125D, 0.0D, 0.125D, 0.875D, 0.875D, 0.875D);
public static final AxisAlignedBB NORMAL_BOUNDING_BOX = new AxisAlignedBB(0.125D, 0.0D, 0.125D, 0.875D, 0.875D, 0.875D);
public static final AxisAlignedBB UP_BOUNDING_BOX = new AxisAlignedBB(0.125D, 0.0D, 0.125D, 0.875D, 1.0D, 0.875D);
// TODO: Redo bounding boxes
/*protected static final AxisAlignedBB[] BOUNDING_BOXES = new AxisAlignedBB[] {
new AxisAlignedBB(0.125D, 0.0D, 0.125D, 0.875D, 0.875D, 0.875D),
new AxisAlignedBB(0.375D, 0.0D, 0.375D, 0.625D, 1.0D, 1.0D),
new AxisAlignedBB(0.0D, 0.0D, 0.375D, 0.625D, 1.0D, 0.625D),
new AxisAlignedBB(0.0D, 0.0D, 0.375D, 0.625D, 1.0D, 1.0D),
new AxisAlignedBB(0.375D, 0.0D, 0.0D, 0.625D, 1.0D, 0.625D),
new AxisAlignedBB(0.375D, 0.0D, 0.0D, 0.625D, 1.0D, 1.0D),
new AxisAlignedBB(0.0D, 0.0D, 0.0D, 0.625D, 1.0D, 0.625D),
new AxisAlignedBB(0.0D, 0.0D, 0.0D, 0.625D, 1.0D, 1.0D),
new AxisAlignedBB(0.375D, 0.0D, 0.375D, 1.0D, 1.0D, 0.625D),
new AxisAlignedBB(0.375D, 0.0D, 0.375D, 1.0D, 1.0D, 1.0D),
new AxisAlignedBB(0.0D, 0.0D, 0.375D, 1.0D, 1.0D, 0.625D),
new AxisAlignedBB(0.0D, 0.0D, 0.375D, 1.0D, 1.0D, 1.0D),
new AxisAlignedBB(0.375D, 0.0D, 0.0D, 1.0D, 1.0D, 0.625D),
new AxisAlignedBB(0.375D, 0.0D, 0.0D, 1.0D, 1.0D, 1.0D),
new AxisAlignedBB(0.0D, 0.0D, 0.0D, 1.0D, 1.0D, 0.625D),
new AxisAlignedBB(0.0D, 0.0D, 0.0D, 1.0D, 1.0D, 1.0D)
};
public static final AxisAlignedBB BASE_AABB = new AxisAlignedBB(0.125D, 0.0D, 0.125D, 0.875D, 0.875D, 0.875D);
public static final AxisAlignedBB SOUTH_AABB = new AxisAlignedBB(0.375D, 0.0D, 0.625D, 0.625D, 1.5D, 1.0D);
public static final AxisAlignedBB WEST_AABB = new AxisAlignedBB(0.0D, 0.0D, 0.375D, 0.375D, 1.5D, 0.625D);
public static final AxisAlignedBB NORTH_AABB = new AxisAlignedBB(0.375D, 0.0D, 0.0D, 0.625D, 1.5D, 0.375D);
public static final AxisAlignedBB EAST_AABB = new AxisAlignedBB(0.625D, 0.0D, 0.375D, 1.0D, 1.5D, 0.625D);
public static final AxisAlignedBB UP_AABB = new AxisAlignedBB(0.625D, 0.0D, 0.375D, 1.0D, 1.5D, 0.625D);
public static final AxisAlignedBB DOWN_AABB = new AxisAlignedBB(0.625D, 0.0D, 0.375D, 1.0D, 1.5D, 0.625D);*/
//@formatter:on
public BlockSaguaro()
{
super(Material.CACTUS);
this.setDefaultState(this.blockState.getBaseState().withProperty(NORTH, false).withProperty(EAST, false).withProperty(SOUTH, false).withProperty(WEST, false).withProperty(UP, false));
this.setSoundType(SoundType.CLOTH);
this.setHardness(0.3f);
this.setTickRandomly(true);
this.setCreativeTab(NaturaRegistry.tabWorld);
}
@Override
public void addCollisionBoxToList(IBlockState state, World worldIn, BlockPos pos, AxisAlignedBB entityBox, List<AxisAlignedBB> collidingBoxes, @Nullable Entity entityIn, boolean p_185477_7_)
{
if (!p_185477_7_)
{
state = state.getActualState(worldIn, pos);
}
addCollisionBoxToList(pos, entityBox, collidingBoxes, BASE_AABB);
// TODO: Redo bounding boxes
/*
if (state.getValue(NORTH).booleanValue())
{
addCollisionBoxToList(pos, entityBox, collidingBoxes, NORTH_AABB);
}
if (state.getValue(EAST).booleanValue())
{
addCollisionBoxToList(pos, entityBox, collidingBoxes, EAST_AABB);
}
if (state.getValue(SOUTH).booleanValue())
{
addCollisionBoxToList(pos, entityBox, collidingBoxes, SOUTH_AABB);
}
if (state.getValue(WEST).booleanValue())
{
addCollisionBoxToList(pos, entityBox, collidingBoxes, WEST_AABB);
}
if (state.getValue(UP).booleanValue())
{
addCollisionBoxToList(pos, entityBox, collidingBoxes, UP_AABB);
}
if (state.getValue(DOWN).booleanValue())
{
addCollisionBoxToList(pos, entityBox, collidingBoxes, DOWN_AABB);
}*/
}
@Override
public AxisAlignedBB getBoundingBox(IBlockState state, IBlockAccess source, BlockPos pos)
{
state = this.getActualState(state, source, pos);
if (state.getValue(UP).booleanValue())
{
return UP_BOUNDING_BOX;
}
else
{
return NORMAL_BOUNDING_BOX;
}
// TODO: Redo bounding boxes
//state = this.getActualState(state, source, pos);
//return BOUNDING_BOXES[getBoundingBoxIdx(state)];
}
// TODO: Redo bounding boxes
/**
* Returns the correct index into boundingBoxes, based on what the cactus is connected to.
*/
/*
private static int getBoundingBoxIdx(IBlockState state)
{
int i = 0;
if (state.getValue(NORTH).booleanValue())
{
i |= 1 << EnumFacing.NORTH.getHorizontalIndex();
}
if (state.getValue(EAST).booleanValue())
{
i |= 1 << EnumFacing.EAST.getHorizontalIndex();
}
if (state.getValue(SOUTH).booleanValue())
{
i |= 1 << EnumFacing.SOUTH.getHorizontalIndex();
}
if (state.getValue(WEST).booleanValue())
{
i |= 1 << EnumFacing.WEST.getHorizontalIndex();
}
if (state.getValue(UP).booleanValue())
{
i |= 1 << 4; // TEMP FIX FOR UP
}
return i;
}*/
/**
* Used to determine ambient occlusion and culling when rebuilding chunks for render
*/
@Override
public boolean isOpaqueCube(IBlockState state)
{
return false;
}
@Override
public boolean isFullCube(IBlockState state)
{
return false;
}
public boolean canConnectTo(IBlockAccess worldIn, BlockPos pos)
{
IBlockState iblockstate = worldIn.getBlockState(pos);
Block block = iblockstate.getBlock();
return block == Blocks.BARRIER ? false : ((!(block instanceof BlockSaguaro) || iblockstate.getMaterial() != this.blockMaterial) ? (iblockstate.getMaterial().isOpaque() && iblockstate.isFullCube() ? iblockstate.getMaterial() != Material.GOURD : false) : true);
}
@Override
@SideOnly(Side.CLIENT)
public boolean shouldSideBeRendered(IBlockState blockState, IBlockAccess blockAccess, BlockPos pos, EnumFacing side)
{
return true;
}
/**
* Convert the BlockState into the correct metadata value
*/
@Override
public int getMetaFromState(IBlockState state)
{
return 0;
}
/**
* Get the actual Block state of this Block at the given position. This applies properties not visible in the
* metadata, such as cactus connections.
*/
@Override
public IBlockState getActualState(IBlockState state, IBlockAccess worldIn, BlockPos pos)
{
return state.withProperty(NORTH, this.canConnectTo(worldIn, pos.north())).withProperty(EAST, this.canConnectTo(worldIn, pos.east())).withProperty(SOUTH, this.canConnectTo(worldIn, pos.south())).withProperty(WEST, this.canConnectTo(worldIn, pos.west())).withProperty(UP, this.canConnectTo(worldIn, pos.up())).withProperty(DOWN, this.canConnectTo(worldIn, pos.down()));
}
/**
* Returns the blockstate with the given rotation from the passed blockstate. If inapplicable, returns the passed
* blockstate.
*/
@Override
public IBlockState withRotation(IBlockState state, Rotation rot)
{
switch (rot)
{
case CLOCKWISE_180:
return state.withProperty(NORTH, state.getValue(SOUTH)).withProperty(EAST, state.getValue(WEST)).withProperty(SOUTH, state.getValue(NORTH)).withProperty(WEST, state.getValue(EAST)).withProperty(UP, state.getValue(UP)).withProperty(DOWN, state.getValue(DOWN));
case COUNTERCLOCKWISE_90:
return state.withProperty(NORTH, state.getValue(EAST)).withProperty(EAST, state.getValue(SOUTH)).withProperty(SOUTH, state.getValue(WEST)).withProperty(WEST, state.getValue(NORTH)).withProperty(UP, state.getValue(UP)).withProperty(DOWN, state.getValue(DOWN));
case CLOCKWISE_90:
return state.withProperty(NORTH, state.getValue(WEST)).withProperty(EAST, state.getValue(NORTH)).withProperty(SOUTH, state.getValue(EAST)).withProperty(WEST, state.getValue(SOUTH)).withProperty(UP, state.getValue(UP)).withProperty(DOWN, state.getValue(DOWN));
default:
return state;
}
}
/**
* Returns the blockstate with the given mirror of the passed blockstate. If inapplicable, returns the passed
* blockstate.
*/
@SuppressWarnings("deprecation")
@Override
public IBlockState withMirror(IBlockState state, Mirror mirrorIn)
{
switch (mirrorIn)
{
case LEFT_RIGHT:
return state.withProperty(NORTH, state.getValue(SOUTH)).withProperty(SOUTH, state.getValue(NORTH)).withProperty(UP, state.getValue(UP)).withProperty(DOWN, state.getValue(DOWN));
case FRONT_BACK:
return state.withProperty(EAST, state.getValue(WEST)).withProperty(WEST, state.getValue(EAST)).withProperty(UP, state.getValue(UP)).withProperty(DOWN, state.getValue(DOWN));
default:
return super.withMirror(state, mirrorIn);
}
}
@Override
protected BlockStateContainer createBlockState()
{
return new BlockStateContainer(this, new IProperty[] { NORTH, EAST, WEST, SOUTH, UP, DOWN });
}
@Override
public void updateTick(World worldIn, BlockPos pos, IBlockState state, Random rand)
{
if (worldIn.getWorldInfo().isRaining() && worldIn.isAirBlock(pos.up()))
{
boolean canGrow = (rand.nextInt(20) == 0);
if (ForgeHooks.onCropsGrowPre(worldIn, pos, state, canGrow))
{
switch (rand.nextInt(4))
{
case 0:
if (worldIn.isAirBlock(pos.north()))
{
worldIn.setBlockState(pos.north(), NaturaOverworld.saguaroFruit.getDefaultState().withProperty(BlockSaguaroFruit.FACING, EnumFacing.SOUTH), 3);
}
break;
case 1:
if (worldIn.isAirBlock(pos.east()))
{
worldIn.setBlockState(pos.east(), NaturaOverworld.saguaroFruit.getDefaultState().withProperty(BlockSaguaroFruit.FACING, EnumFacing.WEST), 3);
}
break;
case 2:
if (worldIn.isAirBlock(pos.south()))
{
worldIn.setBlockState(pos.south(), NaturaOverworld.saguaroFruit.getDefaultState().withProperty(BlockSaguaroFruit.FACING, EnumFacing.NORTH), 3);
}
break;
case 3:
if (worldIn.isAirBlock(pos.west()))
{
worldIn.setBlockState(pos.west(), NaturaOverworld.saguaroFruit.getDefaultState().withProperty(BlockSaguaroFruit.FACING, EnumFacing.EAST), 3);
}
break;
}
ForgeHooks.onCropsGrowPost(worldIn, pos, state, worldIn.getBlockState(pos));
}
}
}
@Override
public boolean canPlaceBlockAt(World worldIn, BlockPos pos)
{
return super.canPlaceBlockAt(worldIn, pos) ? this.canBlockStay(worldIn, pos) : false;
}
public boolean canBlockStay(World worldIn, BlockPos pos)
{
IBlockState state = worldIn.getBlockState(pos.down());
Block block = state.getBlock();
return block == this || block == Blocks.SAND || block == null;
}
@Override
public void onEntityCollidedWithBlock(World worldIn, BlockPos pos, IBlockState state, Entity entityIn)
{
if (!(entityIn instanceof EntityItem))
{
entityIn.attackEntityFrom(DamageSource.CACTUS, 1.0F);
}
}
@Override
public EnumPlantType getPlantType(IBlockAccess world, BlockPos pos)
{
return EnumPlantType.Desert;
}
@Override
public IBlockState getPlant(IBlockAccess world, BlockPos pos)
{
return getDefaultState();
}
}