Guest User

Untitled

a guest
May 18th, 2015
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.74 KB | None | 0 0
  1. package com.looke81.biowarfare.tileentity;
  2.  
  3. import net.minecraft.item.ItemStack;
  4. import net.minecraft.nbt.NBTBase;
  5. import net.minecraft.nbt.NBTTagByte;
  6. import net.minecraft.nbt.NBTTagCompound;
  7. import net.minecraft.nbt.NBTTagList;
  8. import net.minecraft.server.gui.IUpdatePlayerListBox;
  9. import net.minecraft.util.EnumFacing;
  10. import net.minecraft.util.EnumParticleTypes;
  11. import net.minecraft.world.World;
  12. import cofh.api.energy.EnergyStorage;
  13. import cofh.api.energy.IEnergyReceiver;
  14.  
  15. import com.looke81.biowarfare.blocks.IExpellable;
  16. import com.looke81.biowarfare.blocks.MicrobeExtractor;
  17. import com.looke81.biowarfare.crafting.ExtractorRecipesRegistry;
  18. import com.looke81.biowarfare.crafting.ExtractorRecipesRegistry.ExtractorRecipes;
  19. import com.looke81.biowarfare.init.BioWarfareBlocks;
  20.  
  21. public class TileEntityMicrobeExtractor extends BaseMachine implements IUpdatePlayerListBox, IEnergyReceiver, IExpellable {
  22.  
  23. public int currentProcessTime;
  24. private ItemStack input, output;
  25. public EnergyStorage energyRF;
  26.  
  27. private static final int RF_TICK = 20;
  28. public static final int TOTAL_PROCESS_TIME = 150;
  29. public static final int INPUT_SLOT = 0;
  30. public static final int OUTPUT_SLOT = 1;
  31.  
  32. public TileEntityMicrobeExtractor() {
  33. energyRF = new EnergyStorage(10000);
  34. currentProcessTime = 0;
  35. inventory = new InventoryTile(2);
  36. }
  37.  
  38. @Override
  39. public void update() {
  40. if (!this.hasWorldObj()) return;
  41. World world = this.getWorld();
  42. if (world.isRemote) return;
  43.  
  44. if (currentProcessTime > 0 || canWork()) {
  45. if (currentProcessTime == 0) {
  46. input = inventory.getStackInSlot(INPUT_SLOT);
  47. currentProcessTime = 1;
  48. MicrobeExtractor.setState(world, pos, BioWarfareBlocks.block_MicrobeExtractorActive);
  49. }
  50. if (currentProcessTime > 0 && currentProcessTime < TOTAL_PROCESS_TIME) {
  51. if (inventory.getStackInSlot(INPUT_SLOT) == null || !inventory.getStackInSlot(INPUT_SLOT).isItemEqual(input)) {
  52. doReset();
  53. world.markBlockForUpdate(this.pos);
  54. return;
  55. }
  56. if (energyRF.getEnergyStored() >= RF_TICK ) {
  57. energyRF.modifyEnergyStored(-RF_TICK);
  58. ++currentProcessTime;
  59. }
  60. }
  61. if (currentProcessTime >= TOTAL_PROCESS_TIME) {
  62. inventory.modifyStack(INPUT_SLOT, -1);
  63. if (inventory.getStackInSlot(OUTPUT_SLOT) == null)
  64. inventory.setStackInSlot(output, OUTPUT_SLOT);
  65. else inventory.getStackInSlot(OUTPUT_SLOT).stackSize += output.stackSize;
  66. doReset();
  67. }
  68. world.markBlockForUpdate(this.pos);
  69. }
  70. }
  71.  
  72. public boolean canWork() {
  73. if (inventory.getStackInSlot(INPUT_SLOT) == null) return false;
  74.  
  75. output = ExtractorRecipesRegistry.getOutput(inventory.getStackInSlot(INPUT_SLOT).getItem());
  76.  
  77. return output != null && !(inventory.getStackInSlot(OUTPUT_SLOT) != null &&
  78. (!inventory.getStackInSlot(OUTPUT_SLOT).isItemEqual(output))) &&
  79. !(inventory.getStackInSlot(OUTPUT_SLOT) != null &&
  80. inventory.getStackInSlot(OUTPUT_SLOT).stackSize + output.stackSize >
  81. inventory.getStackInSlot(OUTPUT_SLOT).getMaxStackSize());
  82. }
  83.  
  84. public void doReset() {
  85. currentProcessTime = 0;
  86. input = null;
  87. output = null;
  88. MicrobeExtractor.setState(worldObj, pos, BioWarfareBlocks.block_MicrobeExtractorIdle);
  89. }
  90.  
  91. /*******************************************************************************************************************
  92. ************************************** Energy Functions ***********************************************************
  93. *******************************************************************************************************************/
  94.  
  95. @Override
  96. public int receiveEnergy(EnumFacing from, int maxReceive, boolean simulate) {
  97. int actual = energyRF.receiveEnergy(maxReceive, simulate);
  98. if (actual > 0) this.getWorld().markBlockForUpdate(this.pos);
  99. return actual;
  100. }
  101.  
  102. @Override
  103. public int getEnergyStored(EnumFacing from) {
  104. return energyRF.getEnergyStored();
  105. }
  106.  
  107. @Override
  108. public int getMaxEnergyStored(EnumFacing from) {
  109. return energyRF.getMaxEnergyStored();
  110. }
  111.  
  112. @Override
  113. public boolean canConnectEnergy(EnumFacing from) {
  114. return true;
  115. }
  116.  
  117. @Override
  118. public void spawnActiveParticles(double x, double y, double z) {
  119. worldObj.spawnParticle(EnumParticleTypes.SMOKE_NORMAL, x, y + 0.4, z, 0, 0, 0);
  120. worldObj.spawnParticle(EnumParticleTypes.SMOKE_NORMAL, x, y + 0.4, z, 0, 0, 0);
  121. }
  122.  
  123. /*******************************************************************************************************************
  124. ************************************** Inventory Functions ********************************************************
  125. *******************************************************************************************************************/
  126.  
  127. @Override
  128. public int[] getSlotsForFace(EnumFacing side) {
  129. return new int[] {0,1};
  130. }
  131.  
  132. @Override
  133. public boolean canInsertItem(int index, ItemStack itemStackIn, EnumFacing direction) {
  134. return index == 0;
  135. }
  136.  
  137. @Override
  138. public boolean canExtractItem(int index, ItemStack stack, EnumFacing direction) {
  139. return index == 1;
  140. }
  141.  
  142. @Override
  143. public boolean isItemValidForSlot(int index, ItemStack stack) {
  144. return true;
  145. }
  146.  
  147. @Override
  148. public int getField(int id) {
  149. switch (id) {
  150. case 0:
  151. return currentProcessTime;
  152. default:
  153. return 0;
  154. }
  155.  
  156. }
  157.  
  158. @Override
  159. public void setField(int id, int value) {
  160. switch (id) {
  161. case 0:
  162. currentProcessTime = value;
  163. }
  164. }
  165.  
  166. @Override
  167. public int getFieldCount() {
  168. return 1;
  169. }
  170.  
  171. @Override
  172. public void clear() {
  173. for (int i = 0; i < inventory.getSizeInventory(); i++) {
  174. inventory.setStackInSlot(null, i);
  175. }
  176. }
  177.  
  178. /*******************************************************************************************************************
  179. **************************************** Tile Functions ***********************************************************
  180. *******************************************************************************************************************/
  181.  
  182. @Override
  183. public void readFromNBT(NBTTagCompound tag) {
  184. super.readFromNBT(tag);
  185. energyRF.readFromNBT(tag);
  186. inventory.readFromNBT(tag, this);
  187. NBTTagList itemsTag = tag.getTagList("Stacks", 10);
  188. for (int i = 0; i < itemsTag.tagCount(); i++)
  189. {
  190. NBTTagCompound nbtTagCompound1 = itemsTag.getCompoundTagAt(i);
  191. NBTBase nbt = nbtTagCompound1.getTag("Stack");
  192. int j;
  193. if ((nbt instanceof NBTTagByte)) {
  194. j = nbtTagCompound1.getByte("Stack") & 0xFF;
  195. } else {
  196. j = nbtTagCompound1.getShort("Stack");
  197. }
  198. switch (j) {
  199. case 0:
  200. input = ItemStack.loadItemStackFromNBT(nbtTagCompound1);
  201. break;
  202. case 1:
  203. output = ItemStack.loadItemStackFromNBT(nbtTagCompound1);
  204. break;
  205. }
  206. }
  207. currentProcessTime = tag.getInteger("CurrentProcessTime");
  208. }
  209.  
  210. @Override
  211. public void writeToNBT(NBTTagCompound tag) {
  212. super.writeToNBT(tag);
  213. energyRF.writeToNBT(tag);
  214. inventory.writeToNBT(tag);
  215. NBTTagList nbtTagList = new NBTTagList();
  216. for (int i = 0; i < 2; i++) {
  217. NBTTagCompound nbtTagCompound1 = new NBTTagCompound();
  218. nbtTagCompound1.setShort("Stack", (short)i);
  219. switch (i)
  220. {
  221. case 0:
  222. if (input != null)
  223. input.writeToNBT(nbtTagCompound1);
  224. break;
  225. case 1:
  226. if (output != null)
  227. output.writeToNBT(nbtTagCompound1);
  228. break;
  229. }
  230. nbtTagList.appendTag(nbtTagCompound1);
  231. }
  232. tag.setTag("Stacks", nbtTagList);
  233. tag.setInteger("CurrentProcessTime", currentProcessTime);
  234. }
  235.  
  236. @Override
  237. public String getName() {
  238. // TODO Auto-generated method stub
  239. return null;
  240. }
  241. }
Advertisement
Add Comment
Please, Sign In to add comment