Tenyar97

My_Bed

Jan 1st, 2014
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.25 KB | None | 0 0
  1. package assets.betterdefense.common;
  2.  
  3. import java.util.Iterator;
  4. import java.util.Random;
  5.  
  6. import net.minecraft.block.Block;
  7. import net.minecraft.block.BlockDirectional;
  8. import net.minecraft.block.material.Material;
  9. import net.minecraft.client.renderer.texture.IconRegister;
  10. import net.minecraft.entity.Entity;
  11. import net.minecraft.entity.player.EntityPlayer;
  12. import net.minecraft.entity.player.EnumStatus;
  13. import net.minecraft.item.Item;
  14. import net.minecraft.util.ChunkCoordinates;
  15. import net.minecraft.util.Direction;
  16. import net.minecraft.util.Icon;
  17. import net.minecraft.world.IBlockAccess;
  18. import net.minecraft.world.World;
  19. import net.minecraft.world.biome.BiomeGenBase;
  20. import cpw.mods.fml.relauncher.Side;
  21. import cpw.mods.fml.relauncher.SideOnly;
  22.  
  23. public class My_Bed extends BlockDirectional
  24. {
  25. /** Maps the foot-of-bed block to the head-of-bed block. */
  26. public static final int[][] footBlockToHeadBlockMap = new int[][] {{0, 1}, { -1, 0}, {0, -1}, {1, 0}};
  27. @SideOnly(Side.CLIENT)
  28. private Icon[] field_94472_b;
  29. @SideOnly(Side.CLIENT)
  30. private Icon[] bedSideIcons;
  31. @SideOnly(Side.CLIENT)
  32. private Icon[] bedTopIcons;
  33.  
  34. public My_Bed(int par1)
  35. {
  36. super(par1, Material.grass);
  37. this.setBounds();
  38. }
  39.  
  40. /**
  41. * Called upon block activation (right click on the block.)
  42. */
  43. public boolean onBlockActivated(World par1World, int par2, int par3, int par4, EntityPlayer par5EntityPlayer, int par6, float par7, float par8, float par9)
  44. {
  45. if (par1World.isRemote)
  46. {
  47. return true;
  48. }
  49. else
  50. {
  51. int i1 = par1World.getBlockMetadata(par2, par3, par4);
  52.  
  53. if (!isBlockHeadOfBed(i1))
  54. {
  55. int j1 = getDirection(i1);
  56. par2 += footBlockToHeadBlockMap[j1][0];
  57. par4 += footBlockToHeadBlockMap[j1][1];
  58.  
  59. if (par1World.getBlockId(par2, par3, par4) != this.blockID)
  60. {
  61. return true;
  62. }
  63.  
  64. i1 = par1World.getBlockMetadata(par2, par3, par4);
  65. }
  66.  
  67. if (par1World.provider.canRespawnHere() && par1World.getBiomeGenForCoords(par2, par4) != BiomeGenBase.hell)
  68. {
  69. if (isBedOccupied(i1))
  70. {
  71. EntityPlayer entityplayer1 = null;
  72. Iterator iterator = par1World.playerEntities.iterator();
  73.  
  74. while (iterator.hasNext())
  75. {
  76. EntityPlayer entityplayer2 = (EntityPlayer)iterator.next();
  77.  
  78. if (entityplayer2.isPlayerSleeping())
  79. {
  80. ChunkCoordinates chunkcoordinates = entityplayer2.playerLocation;
  81.  
  82. if (chunkcoordinates.posX == par2 && chunkcoordinates.posY == par3 && chunkcoordinates.posZ == par4)
  83. {
  84. entityplayer1 = entityplayer2;
  85. }
  86. }
  87. }
  88.  
  89. if (entityplayer1 != null)
  90. {
  91. par5EntityPlayer.addChatMessage("tile.bed.occupied");
  92. return true;
  93. }
  94.  
  95. setBedOccupied(par1World, par2, par3, par4, false);
  96. }
  97.  
  98. EnumStatus enumstatus = par5EntityPlayer.sleepInBedAt(par2, par3, par4);
  99.  
  100. if (enumstatus == EnumStatus.OK)
  101. {
  102. setBedOccupied(par1World, par2, par3, par4, true);
  103. return true;
  104. }
  105. else
  106. {
  107. if (enumstatus == EnumStatus.NOT_POSSIBLE_NOW)
  108. {
  109. par5EntityPlayer.addChatMessage("tile.bed.noSleep");
  110. }
  111.  
  112. return true;
  113. }
  114. }
  115. else
  116. {
  117. double d0 = (double)par2 + 0.5D;
  118. double d1 = (double)par3 + 0.5D;
  119. double d2 = (double)par4 + 0.5D;
  120. par1World.setBlockToAir(par2, par3, par4);
  121. int k1 = getDirection(i1);
  122. par2 += footBlockToHeadBlockMap[k1][0];
  123. par4 += footBlockToHeadBlockMap[k1][1];
  124.  
  125. if (par1World.getBlockId(par2, par3, par4) == this.blockID)
  126. {
  127. par1World.setBlockToAir(par2, par3, par4);
  128. d0 = (d0 + (double)par2 + 0.5D) / 2.0D;
  129. d1 = (d1 + (double)par3 + 0.5D) / 2.0D;
  130. d2 = (d2 + (double)par4 + 0.5D) / 2.0D;
  131. }
  132.  
  133. par1World.newExplosion((Entity)null, (double)((float)par2 + 0.5F), (double)((float)par3 + 0.5F), (double)((float)par4 + 0.5F), 5.0F, true, true);
  134. return true;
  135. }
  136. }
  137. }
  138.  
  139. @SideOnly(Side.CLIENT)
  140.  
  141. /**
  142. * From the specified side and block metadata retrieves the blocks texture. Args: side, metadata
  143. */
  144. public Icon getIcon(int par1, int par2)
  145. {
  146. if (par1 == 0)
  147. {
  148. return Block.dirt.getBlockTextureFromSide(par1);
  149. }
  150. else
  151. {
  152. int k = getDirection(par2);
  153. int l = Direction.bedDirection[k][par1];
  154. int i1 = isBlockHeadOfBed(par2) ? 1 : 0;
  155. return (i1 != 1 || l != 2) && (i1 != 0 || l != 3) ? (l != 5 && l != 4 ? this.bedTopIcons[i1] : this.bedSideIcons[i1]) : this.field_94472_b[i1];
  156. }
  157. }
  158.  
  159. @SideOnly(Side.CLIENT)
  160.  
  161. /**
  162. * When this method is called, your block should register all the icons it needs with the given IconRegister. This
  163. * is the only chance you get to register icons.
  164. */
  165. public void registerIcons(IconRegister par1IconRegister)
  166. {
  167. this.bedTopIcons = new Icon[] {par1IconRegister.registerIcon(this.getTextureName() + "_feet_top"), par1IconRegister.registerIcon(this.getTextureName() + "_head_top")};
  168. this.field_94472_b = new Icon[] {par1IconRegister.registerIcon(this.getTextureName() + "_feet_end"), par1IconRegister.registerIcon(this.getTextureName() + "_head_end")};
  169. this.bedSideIcons = new Icon[] {par1IconRegister.registerIcon(this.getTextureName() + "_feet_side"), par1IconRegister.registerIcon(this.getTextureName() + "_head_side")};
  170. }
  171.  
  172. /**
  173. * The type of render function that is called for this block
  174. */
  175. public int getRenderType()
  176. {
  177. return 14;
  178. }
  179.  
  180. /**
  181. * If this block doesn't render as an ordinary block it will return False (examples: signs, buttons, stairs, etc)
  182. */
  183. public boolean renderAsNormalBlock()
  184. {
  185. return false;
  186. }
  187.  
  188. /**
  189. * Is this block (a) opaque and (b) a full 1m cube? This determines whether or not to render the shared face of two
  190. * adjacent blocks and also whether the player can attach torches, redstone wire, etc to this block.
  191. */
  192. public boolean isOpaqueCube()
  193. {
  194. return false;
  195. }
  196.  
  197. /**
  198. * Updates the blocks bounds based on its current state. Args: world, x, y, z
  199. */
  200. public void setBlockBoundsBasedOnState(IBlockAccess par1IBlockAccess, int par2, int par3, int par4)
  201. {
  202. this.setBounds();
  203. }
  204.  
  205. /**
  206. * Lets the block know when one of its neighbor changes. Doesn't know which neighbor changed (coordinates passed are
  207. * their own) Args: x, y, z, neighbor blockID
  208. */
  209. public void onNeighborBlockChange(World par1World, int par2, int par3, int par4, int par5)
  210. {
  211. int i1 = par1World.getBlockMetadata(par2, par3, par4);
  212. int j1 = getDirection(i1);
  213.  
  214. if (isBlockHeadOfBed(i1))
  215. {
  216. if (par1World.getBlockId(par2 - footBlockToHeadBlockMap[j1][0], par3, par4 - footBlockToHeadBlockMap[j1][1]) != this.blockID)
  217. {
  218. par1World.setBlockToAir(par2, par3, par4);
  219. }
  220. }
  221. else if (par1World.getBlockId(par2 + footBlockToHeadBlockMap[j1][0], par3, par4 + footBlockToHeadBlockMap[j1][1]) != this.blockID)
  222. {
  223. par1World.setBlockToAir(par2, par3, par4);
  224.  
  225. if (!par1World.isRemote)
  226. {
  227. this.dropBlockAsItem(par1World, par2, par3, par4, i1, 0);
  228. }
  229. }
  230. }
  231.  
  232. /**
  233. * Returns the ID of the items to drop on destruction.
  234. */
  235. public int idDropped(int par1, Random par2Random, int par3)
  236. {
  237. return isBlockHeadOfBed(par1) ? 0 : Item.bed.itemID;
  238. }
  239.  
  240. /**
  241. * Set the bounds of the bed block.
  242. */
  243. private void setBounds()
  244. {
  245. this.setBlockBounds(0.0F, 0.0F, 0.0F, 1.0F, 0.5625F, 1.0F);
  246. }
  247.  
  248. /**
  249. * Returns whether or not this bed block is the head of the bed.
  250. */
  251. public static boolean isBlockHeadOfBed(int par0)
  252. {
  253. return (par0 & 8) != 0;
  254. }
  255.  
  256. /**
  257. * Return whether or not the bed is occupied.
  258. */
  259. public static boolean isBedOccupied(int par0)
  260. {
  261. return (par0 & 4) != 0;
  262. }
  263.  
  264. /**
  265. * Sets whether or not the bed is occupied.
  266. */
  267. public static void setBedOccupied(World par0World, int par1, int par2, int par3, boolean par4)
  268. {
  269. int l = par0World.getBlockMetadata(par1, par2, par3);
  270.  
  271. if (par4)
  272. {
  273. l |= 4;
  274. }
  275. else
  276. {
  277. l &= -5;
  278. }
  279.  
  280. par0World.setBlockMetadataWithNotify(par1, par2, par3, l, 4);
  281. }
  282.  
  283. /**
  284. * Gets the nearest empty chunk coordinates for the player to wake up from a bed into.
  285. */
  286. public static ChunkCoordinates getNearestEmptyChunkCoordinates(World par0World, int par1, int par2, int par3, int par4)
  287. {
  288. int i1 = par0World.getBlockMetadata(par1, par2, par3);
  289. int j1 = BlockDirectional.getDirection(i1);
  290.  
  291. for (int k1 = 0; k1 <= 1; ++k1)
  292. {
  293. int l1 = par1 - footBlockToHeadBlockMap[j1][0] * k1 - 1;
  294. int i2 = par3 - footBlockToHeadBlockMap[j1][1] * k1 - 1;
  295. int j2 = l1 + 2;
  296. int k2 = i2 + 2;
  297.  
  298. for (int l2 = l1; l2 <= j2; ++l2)
  299. {
  300. for (int i3 = i2; i3 <= k2; ++i3)
  301. {
  302. if (par0World.doesBlockHaveSolidTopSurface(l2, par2 - 1, i3) && !par0World.getBlockMaterial(l2, par2, i3).isOpaque() && !par0World.getBlockMaterial(l2, par2 + 1, i3).isOpaque())
  303. {
  304. if (par4 <= 0)
  305. {
  306. return new ChunkCoordinates(l2, par2, i3);
  307. }
  308.  
  309. --par4;
  310. }
  311. }
  312. }
  313. }
  314.  
  315. return null;
  316. }
  317.  
  318. /**
  319. * Drops the block items with a specified chance of dropping the specified items
  320. */
  321. public void dropBlockAsItemWithChance(World par1World, int par2, int par3, int par4, int par5, float par6, int par7)
  322. {
  323. if (!isBlockHeadOfBed(par5))
  324. {
  325. super.dropBlockAsItemWithChance(par1World, par2, par3, par4, par5, par6, 0);
  326. }
  327. }
  328.  
  329. /**
  330. * Returns the mobility information of the block, 0 = free, 1 = can't push but can move over, 2 = total immobility
  331. * and stop pistons
  332. */
  333. public int getMobilityFlag()
  334. {
  335. return 1;
  336. }
  337.  
  338. @SideOnly(Side.CLIENT)
  339.  
  340. /**
  341. * only called by clickMiddleMouseButton , and passed to inventory.setCurrentItem (along with isCreative)
  342. */
  343. public int idPicked(World par1World, int par2, int par3, int par4)
  344. {
  345. return Item.bed.itemID;
  346. }
  347.  
  348. /**
  349. * Called when the block is attempted to be harvested
  350. */
  351. public void onBlockHarvested(World par1World, int par2, int par3, int par4, int par5, EntityPlayer par6EntityPlayer)
  352. {
  353. if (par6EntityPlayer.capabilities.isCreativeMode && isBlockHeadOfBed(par5))
  354. {
  355. int i1 = getDirection(par5);
  356. par2 -= footBlockToHeadBlockMap[i1][0];
  357. par4 -= footBlockToHeadBlockMap[i1][1];
  358.  
  359. if (par1World.getBlockId(par2, par3, par4) == this.blockID)
  360. {
  361. par1World.setBlockToAir(par2, par3, par4);
  362. }
  363. }
  364. }
  365. }
Advertisement
Add Comment
Please, Sign In to add comment