Advertisement
Guest User

Untitled

a guest
May 8th, 2015
314
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.92 KB | None | 0 0
  1. package growthcraft.block;
  2.  
  3. import growthcraft.Growthcraft;
  4. import growthcraft.GrowthcraftBlocks;
  5.  
  6. import java.util.Random;
  7.  
  8. import net.minecraft.block.Block;
  9. import net.minecraft.block.BlockPistonBase;
  10. import net.minecraft.block.IGrowable;
  11. import net.minecraft.block.material.Material;
  12. import net.minecraft.block.properties.IProperty;
  13. import net.minecraft.block.properties.PropertyDirection;
  14. import net.minecraft.block.properties.PropertyInteger;
  15. import net.minecraft.block.state.BlockState;
  16. import net.minecraft.block.state.IBlockState;
  17. import net.minecraft.entity.EntityLivingBase;
  18. import net.minecraft.entity.player.EntityPlayer;
  19. import net.minecraft.init.Blocks;
  20. import net.minecraft.init.Items;
  21. import net.minecraft.item.Item;
  22. import net.minecraft.item.ItemStack;
  23. import net.minecraft.util.AxisAlignedBB;
  24. import net.minecraft.util.BlockPos;
  25. import net.minecraft.util.EnumFacing;
  26. import net.minecraft.util.EnumWorldBlockLayer;
  27. import net.minecraft.world.IBlockAccess;
  28. import net.minecraft.world.World;
  29. import net.minecraftforge.fml.relauncher.Side;
  30. import net.minecraftforge.fml.relauncher.SideOnly;
  31.  
  32. public class BlockApple extends Block implements IGrowable
  33. {
  34. //CONSTANTS
  35. private final int GROWTH = 5;
  36.  
  37. //PROPERTIES
  38. public static final PropertyInteger AGE = PropertyInteger.create("age", 0, 2);
  39. public static final PropertyDirection FACING = PropertyDirection.create("facing");
  40.  
  41. public BlockApple(String name)
  42. {
  43. super(Material.plants);
  44.  
  45. this.setUnlocalizedName(Growthcraft.MODID + "." + name);
  46.  
  47. this.setHardness(0.2F);
  48. this.setResistance(5.0F);
  49. this.setStepSound(soundTypeWood);
  50. this.setCreativeTab(null);
  51. this.setTickRandomly(true);
  52.  
  53. this.setDefaultState(this.blockState.getBaseState().withProperty(FACING, EnumFacing.NORTH).withProperty(AGE, Integer.valueOf(0)));
  54. }
  55.  
  56. /************
  57. * BLOCK UPDATE
  58. ************/
  59. @Override
  60. public void updateTick(World worldIn, BlockPos pos, IBlockState state, Random rand)
  61. {
  62. if (!this.canBlockStay(worldIn, pos, state))
  63. {
  64. this.dropBlock(worldIn, pos, state);
  65. }
  66. else if (worldIn.rand.nextInt(this.GROWTH) == 0)
  67. {
  68. int currentAge = ((Integer)state.getValue(AGE)).intValue();
  69.  
  70. if (currentAge < 2)
  71. {
  72. worldIn.setBlockState(pos, state.withProperty(AGE, Integer.valueOf(currentAge + 1)), 2);
  73. }
  74. }
  75. }
  76.  
  77. private boolean canBlockStay(World worldIn, BlockPos pos, IBlockState state)
  78. {
  79. pos = pos.offset(((EnumFacing)state.getValue(FACING)));
  80. IBlockState iblockstate1 = worldIn.getBlockState(pos);
  81. if (iblockstate1.getBlock() == GrowthcraftBlocks.appleLeaves)
  82. {
  83. return (Boolean) iblockstate1.getValue(BlockAppleLeaves.DECAYABLE);
  84. }
  85. else
  86. {
  87. return false;
  88. }
  89. }
  90.  
  91. /************
  92. * TRIGGERS
  93. ************/
  94. @Override
  95. public void onBlockPlacedBy(World worldIn, BlockPos pos, IBlockState state, EntityLivingBase placer, ItemStack stack)
  96. {
  97. EnumFacing placerFacing = BlockPistonBase.getFacingFromEntity(worldIn, pos, placer);
  98.  
  99. if (placerFacing == EnumFacing.UP)
  100. {
  101. EnumFacing enumfacing = EnumFacing.fromAngle((double)placer.rotationYaw);
  102. worldIn.setBlockState(pos, state.withProperty(FACING, enumfacing), 2);
  103. }
  104. else
  105. {
  106. worldIn.setBlockState(pos, state.withProperty(FACING, placerFacing), 2);
  107. }
  108.  
  109. }
  110.  
  111. @Override
  112. public IBlockState onBlockPlaced(World worldIn, BlockPos pos, EnumFacing facing, float hitX, float hitY, float hitZ, int meta, EntityLivingBase placer)
  113. {
  114. //if (!facing.getAxis().isHorizontal())
  115. //{
  116. // facing = EnumFacing.NORTH;
  117. //}
  118. if (facing == EnumFacing.UP)
  119. {
  120. facing = EnumFacing.NORTH;
  121. }
  122.  
  123. return this.getDefaultState().withProperty(FACING, facing).withProperty(AGE, Integer.valueOf(0));
  124. }
  125.  
  126. @Override
  127. public void onNeighborBlockChange(World worldIn, BlockPos pos, IBlockState state, Block neighborBlock)
  128. {
  129. if (!this.canBlockStay(worldIn, pos, state))
  130. {
  131. this.dropBlock(worldIn, pos, state);
  132. }
  133. }
  134.  
  135. private void dropBlock(World worldIn, BlockPos pos, IBlockState state)
  136. {
  137. worldIn.setBlockState(pos, Blocks.air.getDefaultState(), 3);
  138. this.dropBlockAsItem(worldIn, pos, state, 0);
  139. }
  140.  
  141. /************
  142. * BLOCK META/STATE
  143. ************/
  144.  
  145. /*
  146. * IF bits 0x4 and 0x8 is equal to 3
  147. -> apple is downward
  148. check bits 0x1 and 0x2 for stage
  149. 0 = stage 0
  150. 1 = stage 2
  151. 2 = stage 3
  152. ELSE
  153. -> apple is sidewards
  154. check bits 0x1 and 0x2 for orientation
  155. 0 = attached to the north
  156. 1 = attached to the east
  157. 2 = attached to the south
  158. 3 = attached to the west
  159. check bits 0x4 and 0x8 for stage
  160. 0 = stage 0
  161. 1 = stage 1
  162. 2 = stage 2
  163.  
  164. *
  165. */
  166.  
  167. @Override
  168. public IBlockState getStateFromMeta(int meta)
  169. {
  170. EnumFacing enumfacing = EnumFacing.getHorizontal(meta);
  171. int i = (meta >> 2);
  172.  
  173. if (i == 3)
  174. {
  175. enumfacing = EnumFacing.DOWN;
  176. i = (meta & 3);
  177. }
  178.  
  179. return this.getDefaultState().withProperty(FACING, enumfacing).withProperty(AGE, Integer.valueOf(i));
  180. }
  181.  
  182. @Override
  183. public int getMetaFromState(IBlockState state)
  184. {
  185. byte b0 = 0;
  186. EnumFacing enumfacing = (EnumFacing)state.getValue(FACING);
  187. int age = ((Integer)state.getValue(AGE)).intValue();
  188. int i;
  189.  
  190. if (enumfacing == EnumFacing.DOWN)
  191. {
  192. i = b0 | age;
  193. i |= 12;
  194. }
  195. else
  196. {
  197. i = b0 | (enumfacing).getHorizontalIndex();
  198. i |= age << 2;
  199. }
  200.  
  201. return i;
  202. }
  203.  
  204. @Override
  205. protected BlockState createBlockState()
  206. {
  207. return new BlockState(this, new IProperty[] {FACING, AGE});
  208. }
  209.  
  210. /************
  211. * STUFF
  212. ************/
  213. @Override
  214. @SideOnly(Side.CLIENT)
  215. public Item getItem(World worldIn, BlockPos pos)
  216. {
  217. return Items.apple;
  218. }
  219.  
  220. @Override
  221. public boolean canGrow(World worldIn, BlockPos pos, IBlockState state, boolean isClient)
  222. {
  223. return ((Integer)state.getValue(AGE)).intValue() < 2;
  224. }
  225.  
  226. @Override public boolean canUseBonemeal(World worldIn, Random rand, BlockPos pos, IBlockState state) { return true; }
  227.  
  228. @Override
  229. public void grow(World worldIn, Random rand, BlockPos pos, IBlockState state)
  230. {
  231. worldIn.setBlockState(pos, state.withProperty(AGE, Integer.valueOf(((Integer)state.getValue(AGE)).intValue() + 1)), 2);
  232. }
  233.  
  234. @Override public boolean canSilkHarvest(World world, BlockPos pos, IBlockState state, EntityPlayer player) { return false; }
  235.  
  236. /************
  237. * DROPS
  238. ************/
  239. @Override
  240. public Item getItemDropped(IBlockState state, Random rand, int fortune)
  241. {
  242. return ((Integer)state.getValue(AGE)).intValue() == 2 ? Items.apple : null;
  243. }
  244.  
  245. @Override
  246. public int quantityDropped(Random random)
  247. {
  248. return 1;
  249. }
  250.  
  251. @Override
  252. public void dropBlockAsItemWithChance(World worldIn, BlockPos pos, IBlockState state, float chance, int fortune)
  253. {
  254. super.dropBlockAsItemWithChance(worldIn, pos, state, chance, 0);
  255. }
  256.  
  257. /************
  258. * BOUNDING BOX
  259. ************/
  260. @Override
  261. public AxisAlignedBB getCollisionBoundingBox(World worldIn, BlockPos pos, IBlockState state)
  262. {
  263. //this.setBlockBoundsBasedOnState(worldIn, pos);
  264. //return super.getCollisionBoundingBox(worldIn, pos, state);
  265. return null;
  266. }
  267.  
  268. @Override
  269. @SideOnly(Side.CLIENT)
  270. public AxisAlignedBB getSelectedBoundingBox(World worldIn, BlockPos pos)
  271. {
  272. this.setBlockBoundsBasedOnState(worldIn, pos);
  273. return super.getSelectedBoundingBox(worldIn, pos);
  274. }
  275.  
  276. @Override
  277. public void setBlockBoundsBasedOnState(IBlockAccess worldIn, BlockPos pos)
  278. {
  279. IBlockState iblockstate = worldIn.getBlockState(pos);
  280. EnumFacing enumfacing = (EnumFacing)iblockstate.getValue(FACING);
  281. int age = ((Integer)iblockstate.getValue(AGE)).intValue();
  282. float f;
  283.  
  284. if (age == 0)
  285. {
  286. f = 0.015625F;
  287.  
  288. switch (BlockApple.SwitchEnumFacing.FACING_LOOKUP[enumfacing.ordinal()])
  289. {
  290. case 1:
  291. this.setBlockBounds(0.0F, 0.0F, 1.0F - f, 1.0F, 1.0F, 1.0F);
  292. break;
  293. case 2:
  294. this.setBlockBounds(0.0F, 0.0F, 0.0F, 1.0F, 1.0F, f);
  295. break;
  296. case 3:
  297. this.setBlockBounds(0.0F, 0.0F, 0.0F, f, 1.0F, 1.0F);
  298. break;
  299. case 4:
  300. this.setBlockBounds(1.0F - f, 0.0F, 0.0F, 1.0F, 1.0F, 1.0F);
  301. break;
  302. case 5:
  303. this.setBlockBounds(0.0F, 0.0F, 0.0F, 1.0F, f, 1.0F);
  304. }
  305. }
  306. else
  307. {
  308. f = 0.125F + 0.03125F * (age + 1);
  309.  
  310. switch (BlockApple.SwitchEnumFacing.FACING_LOOKUP[enumfacing.ordinal()])
  311. {
  312. case 1:
  313. this.setBlockBounds(0.5F - f, 0.5F - f, 1.0F - f, 0.5F + f, 0.5F + f, 1.0F);
  314. break;
  315. case 2:
  316. this.setBlockBounds(0.5F - f, 0.5F - f, 0.0F, 0.5F + f, 0.5F + f, f);
  317. break;
  318. case 3:
  319. this.setBlockBounds(0.0F, 0.5F - f, 0.5F - f, f, 0.5F + f, 0.5F + f);
  320. break;
  321. case 4:
  322. this.setBlockBounds(1.0F - f, 0.5F - f, 0.5F - f, 1.0F, 0.5F + f, 0.5F + f);
  323. break;
  324. case 5:
  325. this.setBlockBounds(0.5F - f, 0.0F, 0.5F - f, 0.5F + f, f, 0.5F + f);
  326. }
  327. }
  328. }
  329.  
  330. static final class SwitchEnumFacing
  331. {
  332. static final int[] FACING_LOOKUP = new int[EnumFacing.values().length];
  333. private static final String __OBFID = "CL_00002130";
  334.  
  335. static
  336. {
  337. try
  338. {
  339. FACING_LOOKUP[EnumFacing.SOUTH.ordinal()] = 1;
  340. }
  341. catch (NoSuchFieldError var5)
  342. {
  343. ;
  344. }
  345.  
  346. try
  347. {
  348. FACING_LOOKUP[EnumFacing.NORTH.ordinal()] = 2;
  349. }
  350. catch (NoSuchFieldError var4)
  351. {
  352. ;
  353. }
  354.  
  355. try
  356. {
  357. FACING_LOOKUP[EnumFacing.WEST.ordinal()] = 3;
  358. }
  359. catch (NoSuchFieldError var3)
  360. {
  361. ;
  362. }
  363.  
  364. try
  365. {
  366. FACING_LOOKUP[EnumFacing.EAST.ordinal()] = 4;
  367. }
  368. catch (NoSuchFieldError var2)
  369. {
  370. ;
  371. }
  372.  
  373. try
  374. {
  375. FACING_LOOKUP[EnumFacing.DOWN.ordinal()] = 5;
  376. }
  377. catch (NoSuchFieldError var1)
  378. {
  379. ;
  380. }
  381. }
  382. }
  383.  
  384. /************
  385. * RENDERING
  386. ************/
  387. @Override public boolean isFullCube() { return false; }
  388. @Override public boolean isOpaqueCube() { return false; }
  389.  
  390. @Override
  391. @SideOnly(Side.CLIENT)
  392. public EnumWorldBlockLayer getBlockLayer()
  393. {
  394. return EnumWorldBlockLayer.CUTOUT;
  395. }
  396.  
  397. //@Override
  398. //@SideOnly(Side.CLIENT)
  399. //public Block.EnumOffsetType getOffsetType()
  400. //{
  401. // return Block.EnumOffsetType.XYZ;
  402. //}
  403. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement