Guest User

Tile entity and base

a guest
Dec 30th, 2016
313
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.72 KB | None | 0 0
  1. package com.theundertaker11.GeneticsReborn.blocks.cellanalyser;
  2.  
  3. import com.theundertaker11.GeneticsReborn.tile.GRTileEntityBasicEnergyReceiver;
  4.  
  5. import cofh.api.energy.EnergyStorage;
  6. import cofh.api.energy.IEnergyReceiver;
  7. import cofh.api.energy.IEnergyStorage;
  8. import net.minecraft.block.state.IBlockState;
  9. import net.minecraft.entity.player.EntityPlayer;
  10. import net.minecraft.init.Items;
  11. import net.minecraft.item.ItemStack;
  12. import net.minecraft.nbt.NBTTagCompound;
  13. import net.minecraft.tileentity.TileEntity;
  14. import net.minecraft.util.EnumFacing;
  15. import net.minecraft.util.ITickable;
  16. import net.minecraftforge.common.capabilities.Capability;
  17. import net.minecraftforge.items.CapabilityItemHandler;
  18. import net.minecraftforge.items.IItemHandler;
  19. import net.minecraftforge.items.ItemStackHandler;
  20.  
  21. public class GRTileEntityCellAnalyser extends GRTileEntityBasicEnergyReceiver implements ITickable{
  22.  
  23.  
  24. /**The number of ticks the current item has been cooking*/
  25. private int energyUsed;
  26. /**The number of ticks required to cook an item*/
  27. private static final short ENERGY_NEEDED = 5000;
  28.  
  29. private boolean cachedisActive = false;
  30.  
  31. public GRTileEntityCellAnalyser(){
  32. super();
  33. }
  34.  
  35. // - see if the fuel has run out, and if so turn the furnace "off" and slowly uncook the current item (if any)
  36. // - see if any of the items have finished smelting
  37. // It runs both on the server and the client.
  38. @Override
  39. public void update()
  40. {
  41. // If there is nothing to smelt or there is no room in the output, reset energyUsed and return
  42. if (canSmelt())
  43. {
  44. useEnergy();
  45.  
  46. // If there is enough energy, keep cooking the item
  47. if (this.getEnergyStored(null) > (20*(this.overclockers+1)))
  48. {
  49. energyUsed = energyUsed+(20+((this.overclockers+1)*10));
  50. this.energy = (20*this.overclockers);
  51. }
  52. // Just in case
  53. if (energyUsed < 0) energyUsed = 0;
  54.  
  55. // If energyUsed has reached maxCookTime smelt the item and reset energyUsed
  56. if (energyUsed >= ENERGY_NEEDED) {
  57. smeltItem();
  58. energyUsed = 0;
  59. }
  60. }
  61. else energyUsed = 0;
  62. // when it turns on, force the block to re-render, otherwise the change in state will not be visible.
  63. // The block update (for renderer) is only required on client side
  64. if (cachedisActive != this.isActive)
  65. {
  66. cachedisActive = this.isActive;
  67. if (worldObj.isRemote)
  68. {
  69. IBlockState iblockstate = this.worldObj.getBlockState(this.getPos());
  70. final int FLAGS = 3; // I'm not sure what these flags do, exactly.
  71. worldObj.notifyBlockUpdate(pos, iblockstate, iblockstate, FLAGS);
  72. }
  73. }
  74. }
  75.  
  76. //TODO This will be used to do the % chance for getting certain genes, for testing it just returns diamond
  77. public static ItemStack getSmeltingResultForItem(ItemStack stack)
  78. {
  79. return new ItemStack(Items.DIAMOND);
  80. }
  81.  
  82. /**
  83. * for each fuel slot: decreases the burn time, checks if burnTimeRemaining = 0 and tries to consume a new piece of fuel if one is available
  84. * return the number of fuel slots which are burning
  85. */
  86. private void useEnergy()
  87. {
  88. boolean inventoryChanged = false;
  89. this.energy = this.energy-(20*this.overclockers);
  90. markDirty();
  91. }
  92.  
  93. /**
  94. * Check if any of the input items are smeltable and there is sufficient space in the output slots
  95. * @return true if smelting is possible
  96. */
  97. private boolean canSmelt() {return smeltItem(false);}
  98.  
  99. /**
  100. * Smelt an input item into an output slot, if possible
  101. */
  102. private void smeltItem() {smeltItem(true);}
  103.  
  104. /**
  105. * checks that there is an item to be smelted in one of the input slots and that there is room for the result in the output slots
  106. * If desired, performs the smelt
  107. * @param performSmelt if true, perform the smelt. if false, check whether smelting is possible, but don't change the inventory
  108. * @return false if no items can be smelted, true otherwise
  109. */
  110. private boolean smeltItem(boolean performSmelt)
  111. {
  112. ItemStack result = null;
  113. IItemHandler inventory = this.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, null);
  114. IItemHandler inventoryoutput = this.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, EnumFacing.DOWN);
  115.  
  116. // Sees if the input slot is smeltable and if result fits into an output slot (stacking if possible)
  117. if (inventory != null&&inventory.getStackInSlot(0)!=null)
  118. {
  119. result = getSmeltingResultForItem(inventory.getStackInSlot(0));
  120. if (result != null)
  121. {
  122. //Trys to insert into output slot
  123. ItemStack inputSlotStack = inventory.getStackInSlot(0);
  124. ItemStack outputSlotStack = inventoryoutput.getStackInSlot(0);
  125. if (outputSlotStack == null)
  126. {
  127. if(inventoryoutput.insertItem(0, result, !performSmelt)==null)
  128. {
  129. inventory.extractItem(0, 1, !performSmelt);
  130. markDirty();
  131. return true;
  132. }
  133. }else
  134. {
  135. if(inventoryoutput.insertItem(0, result, true)!=null)
  136. {
  137. return false;
  138. }
  139. else
  140. {
  141. inventoryoutput.insertItem(0, result, !performSmelt);
  142. inventory.extractItem(0, 1, !performSmelt);
  143. markDirty();
  144. return true;
  145. }
  146. }
  147. }
  148. }
  149. return false;
  150. }
  151.  
  152. public double percComplete()
  153. {
  154. double perc = 0;
  155. perc = (this.energyUsed/this.ENERGY_NEEDED);
  156. return perc;
  157. }
  158.  
  159. public double fractionOfEnergyRemaining()
  160. {
  161. double frac = 0;
  162. frac = (this.energy/this.getMaxEnergyStored(null));
  163. return frac;
  164. }
  165.  
  166. @Override
  167. public NBTTagCompound writeToNBT(NBTTagCompound compound)
  168. {
  169. super.writeToNBT(compound);
  170. return compound;
  171.  
  172. }
  173. @Override
  174. public void readFromNBT(NBTTagCompound compound)
  175. {
  176. super.readFromNBT(compound);
  177. }
  178.  
  179. private static final byte ENERGY_USED_FIELD_ID = 0;
  180. private static final byte ENERGY_STORED_FIELD_ID = 1;
  181. private static final byte NUMBER_OF_FIELDS = 2;
  182.  
  183. //@Override
  184. public int getField(int id) {
  185. if (id == ENERGY_USED_FIELD_ID) return energyUsed;
  186. if (id == ENERGY_STORED_FIELD_ID) return this.getEnergyStored(null);
  187. System.err.println("Invalid field ID in TileInventorySmelting.getField:" + id);
  188. return 0;
  189. }
  190.  
  191. //@Override
  192. public void setField(int id, int value)
  193. {
  194. if (id == ENERGY_USED_FIELD_ID) {
  195. energyUsed = (short)value;
  196. } else if (id == ENERGY_STORED_FIELD_ID){
  197.  
  198. }else {
  199. System.err.println("Invalid field ID in TileInventorySmelting.setField:" + id);
  200. }
  201. }
  202.  
  203. //@Override
  204. public int getFieldCount() {
  205. return NUMBER_OF_FIELDS;
  206. }
  207.  
  208. }
  209.  
  210. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////START BASE CLASS OF BELOW CLASS//////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  211. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  212. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  213. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  214. package com.theundertaker11.GeneticsReborn.blocks.cellanalyser;
  215.  
  216. import com.theundertaker11.GeneticsReborn.tile.GRTileEntityBasicEnergyReceiver;
  217.  
  218. import cofh.api.energy.EnergyStorage;
  219. import cofh.api.energy.IEnergyReceiver;
  220. import cofh.api.energy.IEnergyStorage;
  221. import net.minecraft.block.state.IBlockState;
  222. import net.minecraft.entity.player.EntityPlayer;
  223. import net.minecraft.init.Items;
  224. import net.minecraft.item.ItemStack;
  225. import net.minecraft.nbt.NBTTagCompound;
  226. import net.minecraft.tileentity.TileEntity;
  227. import net.minecraft.util.EnumFacing;
  228. import net.minecraft.util.ITickable;
  229. import net.minecraftforge.common.capabilities.Capability;
  230. import net.minecraftforge.items.CapabilityItemHandler;
  231. import net.minecraftforge.items.IItemHandler;
  232. import net.minecraftforge.items.ItemStackHandler;
  233.  
  234. public class GRTileEntityCellAnalyser extends GRTileEntityBasicEnergyReceiver implements ITickable{
  235.  
  236.  
  237. /**The number of ticks the current item has been cooking*/
  238. private int energyUsed;
  239. /**The number of ticks required to cook an item*/
  240. private static final short ENERGY_NEEDED = 5000;
  241.  
  242. private boolean cachedisActive = false;
  243.  
  244. public GRTileEntityCellAnalyser(){
  245. super();
  246. }
  247.  
  248. // - see if the fuel has run out, and if so turn the furnace "off" and slowly uncook the current item (if any)
  249. // - see if any of the items have finished smelting
  250. // It runs both on the server and the client.
  251. @Override
  252. public void update()
  253. {
  254. // If there is nothing to smelt or there is no room in the output, reset energyUsed and return
  255. if (canSmelt())
  256. {
  257. useEnergy();
  258.  
  259. // If there is enough energy, keep cooking the item
  260. if (this.getEnergyStored(null) > (20*(this.overclockers+1)))
  261. {
  262. energyUsed = energyUsed+(20+((this.overclockers+1)*10));
  263. this.energy = (20*this.overclockers);
  264. }
  265. // Just in case
  266. if (energyUsed < 0) energyUsed = 0;
  267.  
  268. // If energyUsed has reached maxCookTime smelt the item and reset energyUsed
  269. if (energyUsed >= ENERGY_NEEDED) {
  270. smeltItem();
  271. energyUsed = 0;
  272. }
  273. }
  274. else energyUsed = 0;
  275. // when it turns on, force the block to re-render, otherwise the change in state will not be visible.
  276. // The block update (for renderer) is only required on client side
  277. if (cachedisActive != this.isActive)
  278. {
  279. cachedisActive = this.isActive;
  280. if (worldObj.isRemote)
  281. {
  282. IBlockState iblockstate = this.worldObj.getBlockState(this.getPos());
  283. final int FLAGS = 3; // I'm not sure what these flags do, exactly.
  284. worldObj.notifyBlockUpdate(pos, iblockstate, iblockstate, FLAGS);
  285. }
  286. }
  287. }
  288.  
  289. //TODO This will be used to do the % chance for getting certain genes, for testing it just returns diamond
  290. public static ItemStack getSmeltingResultForItem(ItemStack stack)
  291. {
  292. return new ItemStack(Items.DIAMOND);
  293. }
  294.  
  295. /**
  296. * for each fuel slot: decreases the burn time, checks if burnTimeRemaining = 0 and tries to consume a new piece of fuel if one is available
  297. * return the number of fuel slots which are burning
  298. */
  299. private void useEnergy()
  300. {
  301. boolean inventoryChanged = false;
  302. this.energy = this.energy-(20*this.overclockers);
  303. markDirty();
  304. }
  305.  
  306. /**
  307. * Check if any of the input items are smeltable and there is sufficient space in the output slots
  308. * @return true if smelting is possible
  309. */
  310. private boolean canSmelt() {return smeltItem(false);}
  311.  
  312. /**
  313. * Smelt an input item into an output slot, if possible
  314. */
  315. private void smeltItem() {smeltItem(true);}
  316.  
  317. /**
  318. * checks that there is an item to be smelted in one of the input slots and that there is room for the result in the output slots
  319. * If desired, performs the smelt
  320. * @param performSmelt if true, perform the smelt. if false, check whether smelting is possible, but don't change the inventory
  321. * @return false if no items can be smelted, true otherwise
  322. */
  323. private boolean smeltItem(boolean performSmelt)
  324. {
  325. ItemStack result = null;
  326. IItemHandler inventory = this.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, null);
  327. IItemHandler inventoryoutput = this.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, EnumFacing.DOWN);
  328.  
  329. // Sees if the input slot is smeltable and if result fits into an output slot (stacking if possible)
  330. if (inventory != null&&inventory.getStackInSlot(0)!=null)
  331. {
  332. result = getSmeltingResultForItem(inventory.getStackInSlot(0));
  333. if (result != null)
  334. {
  335. //Trys to insert into output slot
  336. ItemStack inputSlotStack = inventory.getStackInSlot(0);
  337. ItemStack outputSlotStack = inventoryoutput.getStackInSlot(0);
  338. if (outputSlotStack == null)
  339. {
  340. if(inventoryoutput.insertItem(0, result, !performSmelt)==null)
  341. {
  342. inventory.extractItem(0, 1, !performSmelt);
  343. markDirty();
  344. return true;
  345. }
  346. }else
  347. {
  348. if(inventoryoutput.insertItem(0, result, true)!=null)
  349. {
  350. return false;
  351. }
  352. else
  353. {
  354. inventoryoutput.insertItem(0, result, !performSmelt);
  355. inventory.extractItem(0, 1, !performSmelt);
  356. markDirty();
  357. return true;
  358. }
  359. }
  360. }
  361. }
  362. return false;
  363. }
  364.  
  365. public double percComplete()
  366. {
  367. double perc = 0;
  368. perc = (this.energyUsed/this.ENERGY_NEEDED);
  369. return perc;
  370. }
  371.  
  372. public double fractionOfEnergyRemaining()
  373. {
  374. double frac = 0;
  375. frac = (this.energy/this.getMaxEnergyStored(null));
  376. return frac;
  377. }
  378.  
  379. @Override
  380. public NBTTagCompound writeToNBT(NBTTagCompound compound)
  381. {
  382. super.writeToNBT(compound);
  383. return compound;
  384.  
  385. }
  386. @Override
  387. public void readFromNBT(NBTTagCompound compound)
  388. {
  389. super.readFromNBT(compound);
  390. }
  391.  
  392. private static final byte ENERGY_USED_FIELD_ID = 0;
  393. private static final byte ENERGY_STORED_FIELD_ID = 1;
  394. private static final byte NUMBER_OF_FIELDS = 2;
  395.  
  396. //@Override
  397. public int getField(int id) {
  398. if (id == ENERGY_USED_FIELD_ID) return energyUsed;
  399. if (id == ENERGY_STORED_FIELD_ID) return this.getEnergyStored(null);
  400. System.err.println("Invalid field ID in TileInventorySmelting.getField:" + id);
  401. return 0;
  402. }
  403.  
  404. //@Override
  405. public void setField(int id, int value)
  406. {
  407. if (id == ENERGY_USED_FIELD_ID) {
  408. energyUsed = (short)value;
  409. } else if (id == ENERGY_STORED_FIELD_ID){
  410.  
  411. }else {
  412. System.err.println("Invalid field ID in TileInventorySmelting.setField:" + id);
  413. }
  414. }
  415.  
  416. //@Override
  417. public int getFieldCount() {
  418. return NUMBER_OF_FIELDS;
  419. }
  420.  
  421. }
Advertisement
Add Comment
Please, Sign In to add comment