Guest User

Untitled

a guest
Jun 11th, 2016
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.86 KB | None | 0 0
  1. public class ModBluestone extends Block {
  2.  
  3. public static final PropertyEnum<ModBluestone.EnumAttachPosition> NORTH = PropertyEnum.<ModBluestone.EnumAttachPosition> create(
  4. "north", ModBluestone.EnumAttachPosition.class);
  5. public static final PropertyEnum<ModBluestone.EnumAttachPosition> EAST = PropertyEnum.<ModBluestone.EnumAttachPosition> create(
  6. "east", ModBluestone.EnumAttachPosition.class);
  7. public static final PropertyEnum<ModBluestone.EnumAttachPosition> SOUTH = PropertyEnum.<ModBluestone.EnumAttachPosition> create(
  8. "south", ModBluestone.EnumAttachPosition.class);
  9. public static final PropertyEnum<ModBluestone.EnumAttachPosition> WEST = PropertyEnum.<ModBluestone.EnumAttachPosition> create(
  10. "west", ModBluestone.EnumAttachPosition.class);
  11. public static final PropertyInteger POWER = PropertyInteger.create("power", 0, 15);
  12. protected static final AxisAlignedBB[] BLUESTONE_WIRE_AABB = new AxisAlignedBB[] {
  13. new AxisAlignedBB(0.1875D, 0.0D, 0.1875D, 0.8125D, 0.0625D, 0.8125D),
  14. new AxisAlignedBB(0.1875D, 0.0D, 0.1875D, 0.8125D, 0.0625D, 1.0D),
  15. new AxisAlignedBB(0.0D, 0.0D, 0.1875D, 0.8125D, 0.0625D, 0.8125D),
  16. new AxisAlignedBB(0.0D, 0.0D, 0.1875D, 0.8125D, 0.0625D, 1.0D),
  17. new AxisAlignedBB(0.1875D, 0.0D, 0.0D, 0.8125D, 0.0625D, 0.8125D),
  18. new AxisAlignedBB(0.1875D, 0.0D, 0.0D, 0.8125D, 0.0625D, 1.0D),
  19. new AxisAlignedBB(0.0D, 0.0D, 0.0D, 0.8125D, 0.0625D, 0.8125D),
  20. new AxisAlignedBB(0.0D, 0.0D, 0.0D, 0.8125D, 0.0625D, 1.0D),
  21. new AxisAlignedBB(0.1875D, 0.0D, 0.1875D, 1.0D, 0.0625D, 0.8125D),
  22. new AxisAlignedBB(0.1875D, 0.0D, 0.1875D, 1.0D, 0.0625D, 1.0D),
  23. new AxisAlignedBB(0.0D, 0.0D, 0.1875D, 1.0D, 0.0625D, 0.8125D),
  24. new AxisAlignedBB(0.0D, 0.0D, 0.1875D, 1.0D, 0.0625D, 1.0D),
  25. new AxisAlignedBB(0.1875D, 0.0D, 0.0D, 1.0D, 0.0625D, 0.8125D),
  26. new AxisAlignedBB(0.1875D, 0.0D, 0.0D, 1.0D, 0.0625D, 1.0D),
  27. new AxisAlignedBB(0.0D, 0.0D, 0.0D, 1.0D, 0.0625D, 0.8125D),
  28. new AxisAlignedBB(0.0D, 0.0D, 0.0D, 1.0D, 0.0625D, 1.0D) };
  29. private boolean canProvidePower = true;
  30. private final Set<BlockPos> blocksNeedingUpdate = Sets.<BlockPos> newHashSet();
  31.  
  32. public ModBluestone() {
  33. super(Material.CIRCUITS);
  34. this.setSoundType(SoundType.STONE);
  35. this.setResistance(1F);
  36. this.setHardness(3F);
  37. this.setDefaultState(this.blockState.getBaseState().withProperty(NORTH, ModBluestone.EnumAttachPosition.NONE)
  38. .withProperty(EAST, ModBluestone.EnumAttachPosition.NONE)
  39. .withProperty(SOUTH, ModBluestone.EnumAttachPosition.NONE)
  40. .withProperty(WEST, ModBluestone.EnumAttachPosition.NONE).withProperty(POWER, Integer.valueOf(0)));
  41. }
  42.  
  43. public AxisAlignedBB getBoundingBox(IBlockState state, IBlockAccess source, BlockPos pos) {
  44. return BLUESTONE_WIRE_AABB[getAABBIndex(state.getActualState(source, pos))];
  45. }
  46.  
  47. private static int getAABBIndex(IBlockState state) {
  48. int i = 0;
  49. boolean flag = state.getValue(NORTH) != ModBluestone.EnumAttachPosition.NONE;
  50. boolean flag1 = state.getValue(EAST) != ModBluestone.EnumAttachPosition.NONE;
  51. boolean flag2 = state.getValue(SOUTH) != ModBluestone.EnumAttachPosition.NONE;
  52. boolean flag3 = state.getValue(WEST) != ModBluestone.EnumAttachPosition.NONE;
  53.  
  54. if (flag || flag2 && !flag && !flag1 && !flag3) {
  55. i |= 1 << EnumFacing.NORTH.getHorizontalIndex();
  56. }
  57.  
  58. if (flag1 || flag3 && !flag && !flag1 && !flag2) {
  59. i |= 1 << EnumFacing.EAST.getHorizontalIndex();
  60. }
  61.  
  62. if (flag2 || flag && !flag1 && !flag2 && !flag3) {
  63. i |= 1 << EnumFacing.SOUTH.getHorizontalIndex();
  64. }
  65.  
  66. if (flag3 || flag1 && !flag && !flag2 && !flag3) {
  67. i |= 1 << EnumFacing.WEST.getHorizontalIndex();
  68. }
  69.  
  70. return i;
  71. }
  72.  
  73. /**
  74. * Get the actual Block state of this Block at the given position. This
  75. * applies properties not visible in the metadata, such as fence
  76. * connections.
  77. */
  78. public IBlockState getActualState(IBlockState state, IBlockAccess worldIn, BlockPos pos) {
  79. state = state.withProperty(WEST, this.getAttachPosition(worldIn, pos, EnumFacing.WEST));
  80. state = state.withProperty(EAST, this.getAttachPosition(worldIn, pos, EnumFacing.EAST));
  81. state = state.withProperty(NORTH, this.getAttachPosition(worldIn, pos, EnumFacing.NORTH));
  82. state = state.withProperty(SOUTH, this.getAttachPosition(worldIn, pos, EnumFacing.SOUTH));
  83. return state;
  84. }
  85.  
  86. private ModBluestone.EnumAttachPosition getAttachPosition(IBlockAccess worldIn, BlockPos pos,
  87. EnumFacing direction) {
  88. BlockPos blockpos = pos.offset(direction);
  89. IBlockState iblockstate = worldIn.getBlockState(pos.offset(direction));
  90.  
  91. if (!canConnectTo(worldIn.getBlockState(blockpos), direction, worldIn, blockpos)
  92. && (iblockstate.isNormalCube() || !canConnectUpwardsTo(worldIn, blockpos.down()))) {
  93. IBlockState iblockstate1 = worldIn.getBlockState(pos.up());
  94.  
  95. if (!iblockstate1.isNormalCube()) {
  96. boolean flag = worldIn.getBlockState(blockpos).isSideSolid(worldIn, blockpos, EnumFacing.UP)
  97. || worldIn.getBlockState(blockpos).getBlock() == Blocks.GLOWSTONE;
  98.  
  99. if (flag && canConnectUpwardsTo(worldIn, blockpos.up())) {
  100. if (iblockstate.isBlockNormalCube()) {
  101. return ModBluestone.EnumAttachPosition.UP;
  102. }
  103.  
  104. return ModBluestone.EnumAttachPosition.SIDE;
  105. }
  106. }
  107.  
  108. return ModBluestone.EnumAttachPosition.NONE;
  109. } else {
  110. return ModBluestone.EnumAttachPosition.SIDE;
  111. }
  112. }
  113.  
  114. @Nullable
  115. public AxisAlignedBB getCollisionBoundingBox(IBlockState blockState, World worldIn, BlockPos pos) {
  116. return NULL_AABB;
  117. }
  118.  
  119. /**
  120. * Used to determine ambient occlusion and culling when rebuilding chunks
  121. * for render
  122. */
  123. public boolean isOpaqueCube(IBlockState state) {
  124. return false;
  125. }
  126.  
  127. public boolean isFullCube(IBlockState state) {
  128. return false;
  129. }
  130.  
  131. public boolean canPlaceBlockAt(World worldIn, BlockPos pos) {
  132. return worldIn.getBlockState(pos.down()).isFullyOpaque()
  133. || worldIn.getBlockState(pos.down()).getBlock() == Blocks.GLOWSTONE;
  134. }
  135.  
  136. private IBlockState updateSurroundingBluestone(World worldIn, BlockPos pos, IBlockState state) {
  137. state = this.calculateCurrentChanges(worldIn, pos, pos, state);
  138. List<BlockPos> list = Lists.newArrayList(this.blocksNeedingUpdate);
  139. this.blocksNeedingUpdate.clear();
  140.  
  141. for (BlockPos blockpos : list) {
  142. worldIn.notifyNeighborsOfStateChange(blockpos, this);
  143. }
  144. return state;
  145. }
  146.  
  147. private IBlockState calculateCurrentChanges(World worldIn, BlockPos pos1, BlockPos pos2, IBlockState state) {
  148. IBlockState iblockstate = state;
  149. int i = ((Integer) state.getValue(POWER)).intValue();
  150. int j = 0;
  151. j = this.getMaxCurrentStrength(worldIn, pos2, j);
  152. this.canProvidePower = false;
  153. int k = worldIn.isBlockIndirectlyGettingPowered(pos1);
  154. this.canProvidePower = true;
  155.  
  156. if (k > 0 && k > j - 1) {
  157. j = k;
  158. }
  159.  
  160. int l = 0;
  161.  
  162. for (EnumFacing enumfacing : EnumFacing.Plane.HORIZONTAL) {
  163. BlockPos blockpos = pos1.offset(enumfacing);
  164. boolean flag = blockpos.getX() != pos2.getX() || blockpos.getZ() != pos2.getZ();
  165.  
  166. if (flag) {
  167. l = this.getMaxCurrentStrength(worldIn, blockpos, l);
  168. }
  169.  
  170. if (worldIn.getBlockState(blockpos).isNormalCube() && !worldIn.getBlockState(pos1.up()).isNormalCube()) {
  171. if (flag && pos1.getY() >= pos2.getY()) {
  172. l = this.getMaxCurrentStrength(worldIn, blockpos.up(), l);
  173. }
  174. } else if (!worldIn.getBlockState(blockpos).isNormalCube() && flag && pos1.getY() <= pos2.getY()) {
  175. l = this.getMaxCurrentStrength(worldIn, blockpos.down(), l);
  176. }
  177. }
  178.  
  179. if (l > j) {
  180. j = l - 1;
  181. } else if (j > 0) {
  182. --j;
  183. } else {
  184. j = 0;
  185. }
  186.  
  187. if (k > j - 1) {
  188. j = k;
  189. }
  190.  
  191. if (i != j) {
  192. state = state.withProperty(POWER, Integer.valueOf(j));
  193.  
  194. if (worldIn.getBlockState(pos1) == iblockstate) {
  195. worldIn.setBlockState(pos1, state, 2);
  196. }
  197.  
  198. this.blocksNeedingUpdate.add(pos1);
  199.  
  200. for (EnumFacing enumfacing1 : EnumFacing.values()) {
  201. this.blocksNeedingUpdate.add(pos1.offset(enumfacing1));
  202. }
  203. }
  204.  
  205. return state;
  206. }
  207.  
  208. /**
  209. * Calls World.notifyNeighborsOfStateChange() for all neighboring blocks,
  210. * but only if the given block is a redstone wire.
  211. */
  212. private void notifyWireNeighborsOfStateChange(World worldIn, BlockPos pos) {
  213. if (worldIn.getBlockState(pos).getBlock() == this) {
  214. worldIn.notifyNeighborsOfStateChange(pos, this);
  215.  
  216. for (EnumFacing enumfacing : EnumFacing.values()) {
  217. worldIn.notifyNeighborsOfStateChange(pos.offset(enumfacing), this);
  218. }
  219. }
  220. }
  221.  
  222. public void onBlockAdded(World worldIn, BlockPos pos, IBlockState state) {
  223. if (!worldIn.isRemote) {
  224. this.updateSurroundingBluestone(worldIn, pos, state);
  225.  
  226. for (EnumFacing enumfacing : EnumFacing.Plane.VERTICAL) {
  227. worldIn.notifyNeighborsOfStateChange(pos.offset(enumfacing), this);
  228. }
  229.  
  230. for (EnumFacing enumfacing1 : EnumFacing.Plane.HORIZONTAL) {
  231. this.notifyWireNeighborsOfStateChange(worldIn, pos.offset(enumfacing1));
  232. }
  233.  
  234. for (EnumFacing enumfacing2 : EnumFacing.Plane.HORIZONTAL) {
  235. BlockPos blockpos = pos.offset(enumfacing2);
  236.  
  237. if (worldIn.getBlockState(blockpos).isNormalCube()) {
  238. this.notifyWireNeighborsOfStateChange(worldIn, blockpos.up());
  239. } else {
  240. this.notifyWireNeighborsOfStateChange(worldIn, blockpos.down());
  241. }
  242. }
  243. }
  244. }
  245.  
  246. public void breakBlock(World worldIn, BlockPos pos, IBlockState state) {
  247. super.breakBlock(worldIn, pos, state);
  248.  
  249. if (!worldIn.isRemote) {
  250. for (EnumFacing enumfacing : EnumFacing.values()) {
  251. worldIn.notifyNeighborsOfStateChange(pos.offset(enumfacing), this);
  252. }
  253.  
  254. this.updateSurroundingBluestone(worldIn, pos, state);
  255.  
  256. for (EnumFacing enumfacing1 : EnumFacing.Plane.HORIZONTAL) {
  257. this.notifyWireNeighborsOfStateChange(worldIn, pos.offset(enumfacing1));
  258. }
  259.  
  260. for (EnumFacing enumfacing2 : EnumFacing.Plane.HORIZONTAL) {
  261. BlockPos blockpos = pos.offset(enumfacing2);
  262.  
  263. if (worldIn.getBlockState(blockpos).isNormalCube()) {
  264. this.notifyWireNeighborsOfStateChange(worldIn, blockpos.up());
  265. } else {
  266. this.notifyWireNeighborsOfStateChange(worldIn, blockpos.down());
  267. }
  268. }
  269. }
  270. }
  271.  
  272. private int getMaxCurrentStrength(World worldIn, BlockPos pos, int strength) {
  273. if (worldIn.getBlockState(pos).getBlock() != this) {
  274. return strength;
  275. } else {
  276. int i = ((Integer) worldIn.getBlockState(pos).getValue(POWER)).intValue();
  277. return i > strength ? i : strength;
  278. }
  279. }
  280.  
  281. /**
  282. * Called when a neighboring block was changed and marks that this state
  283. * should perform any checks during a neighbor change. Cases may include
  284. * when redstone power is updated, cactus blocks popping off due to a
  285. * neighboring solid block, etc.
  286. */
  287. public void neighborChanged(IBlockState state, World worldIn, BlockPos pos, Block blockIn) {
  288. if (!worldIn.isRemote) {
  289. if (this.canPlaceBlockAt(worldIn, pos)) {
  290. this.updateSurroundingBluestone(worldIn, pos, state);
  291. } else {
  292. this.dropBlockAsItem(worldIn, pos, state, 0);
  293. worldIn.setBlockToAir(pos);
  294. }
  295. }
  296. }
  297.  
  298. /**
  299. * Get the Item that this Block should drop when harvested.
  300. */
  301. @Nullable
  302. public Item getItemDropped(IBlockState state, Random rand, int fortune) {
  303. return Item.getItemFromBlock(ModElements.bluestone_wire);
  304. }
  305.  
  306. public int getStrongPower(IBlockState blockState, IBlockAccess blockAccess, BlockPos pos, EnumFacing side) {
  307. return !this.canProvidePower ? 0 : blockState.getWeakPower(blockAccess, pos, side);
  308. }
  309.  
  310. public int getWeakPower(IBlockState blockState, IBlockAccess blockAccess, BlockPos pos, EnumFacing side) {
  311. if (!this.canProvidePower) {
  312. return 0;
  313. } else {
  314. int i = ((Integer) blockState.getValue(POWER)).intValue();
  315.  
  316. if (i == 0) {
  317. return 0;
  318. } else if (side == EnumFacing.UP) {
  319. return i;
  320. } else {
  321. EnumSet<EnumFacing> enumset = EnumSet.<EnumFacing> noneOf(EnumFacing.class);
  322.  
  323. for (EnumFacing enumfacing : EnumFacing.Plane.HORIZONTAL) {
  324. if (this.isPowerSourceAt(blockAccess, pos, enumfacing)) {
  325. enumset.add(enumfacing);
  326. }
  327. }
  328.  
  329. if (side.getAxis().isHorizontal() && enumset.isEmpty()) {
  330. return i;
  331. } else if (enumset.contains(side) && !enumset.contains(side.rotateYCCW())
  332. && !enumset.contains(side.rotateY())) {
  333. return i;
  334. } else {
  335. return 0;
  336. }
  337. }
  338. }
  339. }
  340.  
  341. private boolean isPowerSourceAt(IBlockAccess worldIn, BlockPos pos, EnumFacing side) {
  342. BlockPos blockpos = pos.offset(side);
  343. IBlockState iblockstate = worldIn.getBlockState(blockpos);
  344. boolean flag = iblockstate.isNormalCube();
  345. boolean flag1 = worldIn.getBlockState(pos.up()).isNormalCube();
  346. return !flag1 && flag && canConnectUpwardsTo(worldIn, blockpos.up()) ? true
  347. : (canConnectTo(iblockstate, side, worldIn, pos) ? true
  348. : (iblockstate.getBlock() == Blocks.POWERED_REPEATER
  349. && iblockstate.getValue(BlockRedstoneDiode.FACING) == side ? true
  350. : !flag && canConnectUpwardsTo(worldIn, blockpos.down())));
  351. }
  352.  
  353. protected static boolean canConnectUpwardsTo(IBlockAccess worldIn, BlockPos pos) {
  354. return canConnectTo(worldIn.getBlockState(pos), null, worldIn, pos);
  355. }
  356.  
  357. protected static boolean canConnectTo(IBlockState blockState, @Nullable EnumFacing side, IBlockAccess world,
  358. BlockPos pos) {
  359. Block block = blockState.getBlock();
  360. if (block == ModElements.bluestone_wire) {
  361. return true;
  362. } else if (Blocks.UNPOWERED_REPEATER.isSameDiode(blockState)) {
  363. EnumFacing enumfacing = (EnumFacing) blockState.getValue(BlockRedstoneRepeater.FACING);
  364. return enumfacing == side || enumfacing.getOpposite() == side;
  365. }
  366. if (block == Blocks.REDSTONE_WIRE) {
  367. return false;
  368. } else {
  369. return false;
  370. }
  371. }
  372.  
  373. /**
  374. * Can this block provide power. Only wire currently seems to have this
  375. * change based on its state.
  376. */
  377. public boolean canProvidePower(IBlockState state) {
  378. return this.canProvidePower;
  379. }
  380.  
  381. public ItemStack getItem(World worldIn, BlockPos pos, IBlockState state) {
  382. return new ItemStack(Item.getItemFromBlock(ModElements.bluestone_wire));
  383. }
  384.  
  385. /**
  386. * Convert the given metadata into a BlockState for this Block
  387. */
  388. public IBlockState getStateFromMeta(int meta) {
  389. return this.getDefaultState().withProperty(POWER, Integer.valueOf(meta));
  390. }
  391.  
  392. @SideOnly(Side.CLIENT)
  393. public BlockRenderLayer getBlockLayer() {
  394. return BlockRenderLayer.CUTOUT;
  395. }
  396.  
  397. /**
  398. * Convert the BlockState into the correct metadata value
  399. */
  400. public int getMetaFromState(IBlockState state) {
  401. return ((Integer) state.getValue(POWER)).intValue();
  402. }
  403.  
  404. /**
  405. * Returns the blockstate with the given rotation from the passed
  406. * blockstate. If inapplicable, returns the passed blockstate.
  407. */
  408. public IBlockState withRotation(IBlockState state, Rotation rot) {
  409. switch (rot) {
  410. case CLOCKWISE_180:
  411. return state.withProperty(NORTH, state.getValue(SOUTH)).withProperty(EAST, state.getValue(WEST))
  412. .withProperty(SOUTH, state.getValue(NORTH)).withProperty(WEST, state.getValue(EAST));
  413. case COUNTERCLOCKWISE_90:
  414. return state.withProperty(NORTH, state.getValue(EAST)).withProperty(EAST, state.getValue(SOUTH))
  415. .withProperty(SOUTH, state.getValue(WEST)).withProperty(WEST, state.getValue(NORTH));
  416. case CLOCKWISE_90:
  417. return state.withProperty(NORTH, state.getValue(WEST)).withProperty(EAST, state.getValue(NORTH))
  418. .withProperty(SOUTH, state.getValue(EAST)).withProperty(WEST, state.getValue(SOUTH));
  419. default:
  420. return state;
  421. }
  422. }
  423.  
  424. /**
  425. * Returns the blockstate with the given mirror of the passed blockstate. If
  426. * inapplicable, returns the passed blockstate.
  427. */
  428. public IBlockState withMirror(IBlockState state, Mirror mirrorIn) {
  429. switch (mirrorIn) {
  430. case LEFT_RIGHT:
  431. return state.withProperty(NORTH, state.getValue(SOUTH)).withProperty(SOUTH, state.getValue(NORTH));
  432. case FRONT_BACK:
  433. return state.withProperty(EAST, state.getValue(WEST)).withProperty(WEST, state.getValue(EAST));
  434. default:
  435. return super.withMirror(state, mirrorIn);
  436. }
  437. }
  438.  
  439. protected BlockStateContainer createBlockState() {
  440. return new BlockStateContainer(this, new IProperty[] { NORTH, EAST, SOUTH, WEST, POWER });
  441. }
  442.  
  443. static enum EnumAttachPosition implements IStringSerializable {
  444. UP("up"), SIDE("side"), NONE("none");
  445.  
  446. private final String name;
  447.  
  448. private EnumAttachPosition(String name) {
  449. this.name = name;
  450. }
  451.  
  452. public String toString() {
  453. return this.getName();
  454. }
  455.  
  456. public String getName() {
  457. return this.name;
  458. }
  459. }
  460.  
  461. @SideOnly(Side.CLIENT)
  462. public static int colorMultiplier(int p_176337_0_) {
  463. float f = (float) p_176337_0_ / 15.0F;
  464. float f1 = f * 0.6F + 0.4F;
  465.  
  466. if (p_176337_0_ == 0) {
  467. f1 = 0.3F;
  468. }
  469.  
  470. float f2 = f * f * 0.7F - 0.5F;
  471. float f3 = f * f * 0.6F - 0.7F;
  472.  
  473. if (f2 < 0.0F) {
  474. f2 = 0.0F;
  475. }
  476.  
  477. if (f3 < 0.0F) {
  478. f3 = 0.0F;
  479. }
  480.  
  481. int i = MathHelper.clamp_int((int) (f1 * 255.0F), 0, 255);
  482. int j = MathHelper.clamp_int((int) (f2 * 255.0F), 0, 255);
  483. int k = MathHelper.clamp_int((int) (f3 * 255.0F), 0, 255);
  484. return -16777216 | i << 16 | j << 8 | k;
  485. }
  486.  
  487. }
Advertisement
Add Comment
Please, Sign In to add comment