Advertisement
Guest User

Untitled

a guest
Nov 19th, 2016
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.71 KB | None | 0 0
  1. package com.nathan72419.SingularityUtil.TileEntity;
  2.  
  3. import com.nathan72419.SingularityUtil.misc.recipe.MixerRecipesManager;
  4.  
  5. import cofh.api.energy.EnergyStorage;
  6. import cofh.api.energy.IEnergyReceiver;
  7. import cofh.core.util.fluid.FluidTankAdv;
  8. import cpw.mods.fml.relauncher.Side;
  9. import cpw.mods.fml.relauncher.SideOnly;
  10. import net.minecraft.entity.player.EntityPlayer;
  11. import net.minecraft.inventory.ISidedInventory;
  12. import net.minecraft.item.ItemStack;
  13. import net.minecraft.nbt.NBTTagCompound;
  14. import net.minecraft.nbt.NBTTagList;
  15. import net.minecraft.tileentity.TileEntity;
  16. import net.minecraftforge.common.util.ForgeDirection;
  17. import net.minecraftforge.fluids.Fluid;
  18. import net.minecraftforge.fluids.FluidContainerRegistry;
  19. import net.minecraftforge.fluids.FluidStack;
  20. import net.minecraftforge.fluids.FluidTankInfo;
  21. import net.minecraftforge.fluids.IFluidHandler;
  22. import tconstruct.TConstruct;
  23.  
  24. public class TileEntityMixer extends TileEntity implements ISidedInventory, IFluidHandler, IEnergyReceiver{
  25. private ItemStack[] MixerItemStacks = new ItemStack [1];
  26. private static final int[] slotsTop = new int[] {0};
  27. private static final int[] slotsBottom = new int[] {2, 1};
  28. private static final int[] slotsSides = new int[] {1};
  29. public int mixingTime;
  30. public int currentMixingTime;
  31. private String name;
  32. public FluidStack inputFluid;
  33. public FluidStack outputFluid;
  34. FluidTankAdv inputTank = new FluidTankAdv(FluidContainerRegistry.BUCKET_VOLUME * 4);
  35. FluidTankAdv outputTank = new FluidTankAdv(FluidContainerRegistry.BUCKET_VOLUME * 4);
  36. EnergyStorage Battery = new EnergyStorage(10000);
  37.  
  38.  
  39. @Override
  40. public int getSizeInventory() {
  41. return this.MixerItemStacks.length;
  42. }
  43.  
  44. @Override
  45. public ItemStack getStackInSlot(int var) {
  46. return this.MixerItemStacks[var];
  47. }
  48.  
  49. @Override
  50. public ItemStack decrStackSize(int var1, int var2) {
  51. if (this.MixerItemStacks[var1] != null){
  52. ItemStack stack;
  53. if (this.MixerItemStacks[var1].stackSize <= var2){
  54. stack = this.MixerItemStacks[var1];
  55. this.MixerItemStacks[var1] = null;
  56. return stack;
  57. }
  58. else{
  59. stack = this.MixerItemStacks[var1].splitStack(var2);
  60. if(this.MixerItemStacks[var1].stackSize == 0){
  61. this.MixerItemStacks[var1] = null;
  62. }
  63. return stack;
  64. }
  65. }
  66. else {
  67. return null;
  68. }
  69. }
  70.  
  71. public void naming(String name)
  72. {
  73. this.name = name;
  74. }
  75. @Override
  76. public ItemStack getStackInSlotOnClosing(int var1) {
  77. if (this.MixerItemStacks[var1] != null)
  78. {
  79. ItemStack stack = this.MixerItemStacks[var1];
  80. this.MixerItemStacks[var1] = null;
  81. return stack;
  82. }
  83. else {
  84. return null;
  85. }
  86. }
  87.  
  88. @Override
  89. public void setInventorySlotContents(int p_70299_1_, ItemStack p_70299_2_) {
  90.  
  91. }
  92.  
  93. @Override
  94. public String getInventoryName() {
  95. return this.hasCustomInventoryName() ? this.name : "container.mixer";
  96. }
  97.  
  98. @Override
  99. public boolean hasCustomInventoryName() {
  100. return this.name != null && this.name.length() > 0;
  101. }
  102.  
  103. @Override
  104. public int getInventoryStackLimit() {
  105. return 64;
  106. }
  107.  
  108. @Override
  109. public boolean isUseableByPlayer(EntityPlayer p_70300_1_) {
  110. return this.worldObj.getTileEntity(this.xCoord, this.yCoord, this.zCoord) != this ? false : p_70300_1_.getDistanceSq((double)this.xCoord + 0.5D, (double)this.yCoord + 0.5D, (double)this.zCoord + 0.5D) <= 64.0D;
  111. }
  112. public boolean IsInputFluid(Fluid fluid){
  113. return MixerRecipesManager.isFluidValid(fluid);
  114. }
  115. public boolean isMixing() {
  116. return inputTank.getFluidAmount() > TConstruct.oreLiquidValue;
  117. }
  118.  
  119. public void readfromNBT(NBTTagCompound nbt){
  120. super.readFromNBT(nbt);
  121. this.MixerItemStacks = new ItemStack[this.getSizeInventory()];
  122. NBTTagList nbttaglist = nbt.getTagList("Items", 10);
  123. for (int i = 0; i < nbttaglist.tagCount(); ++i)
  124. {
  125. NBTTagCompound nbttagcompound1 = nbttaglist.getCompoundTagAt(i);
  126. byte b0 = nbttagcompound1.getByte("Slot");
  127.  
  128. if (b0 >= 0 && b0 < this.MixerItemStacks.length)
  129. {
  130. this.MixerItemStacks[b0] = ItemStack.loadItemStackFromNBT(nbttagcompound1);
  131. }
  132. }
  133. if (nbt.hasKey("CustomName", 8)){
  134. this.name = nbt.getString("CustomName");
  135. }
  136. this.mixingTime = nbt.getShort("MixingTime");
  137. inputTank.readFromNBT(nbt.getCompoundTag("InputTank"));
  138. outputTank.readFromNBT(nbt.getCompoundTag("OutputTank"));
  139.  
  140. }
  141. public void writeToNBT(NBTTagCompound nbt){
  142. super.writeToNBT(nbt);
  143. nbt.setShort("MixingTime", (short)this.mixingTime);
  144. NBTTagList nbttaglist = new NBTTagList();
  145.  
  146. for (int i = 0; i < this.MixerItemStacks.length; ++i)
  147. {
  148. if (this.MixerItemStacks[i] != null)
  149. {
  150. NBTTagCompound nbttagcompound1 = new NBTTagCompound();
  151. nbttagcompound1.setByte("Slot", (byte)i);
  152. this.MixerItemStacks[i].writeToNBT(nbttagcompound1);
  153. nbttaglist.appendTag(nbttagcompound1);
  154. }
  155. }
  156.  
  157. nbt.setTag("Items", nbttaglist);
  158.  
  159. if (this.hasCustomInventoryName())
  160. {
  161. nbt.setString("CustomName", this.name);
  162. }
  163. nbt.setTag("InputTank", inputTank.writeToNBT(new NBTTagCompound()));
  164. nbt.setTag("OutputTank", outputTank.writeToNBT(new NBTTagCompound()));
  165. }
  166. public void updateEntity(){
  167. boolean isMixing = inputTank.getFluidAmount() > TConstruct.oreLiquidValue;
  168. boolean flag = false;
  169.  
  170. if(!this.worldObj.isRemote)
  171. {
  172. if (this.MixerItemStacks[0] != null && this.inputTank.getFluid() != null && this.Battery.getEnergyStored() >= 15)
  173. {
  174. if(this.canMix())
  175. {
  176. if (inputTank.getFluidAmount() >= TConstruct.oreLiquidValue)
  177. {
  178. flag = true;
  179. }
  180. }
  181. if (this.isMixing() && this.canMix())
  182. {
  183. ++this.mixingTime;
  184. if (this.mixingTime == 200){
  185. this.mixingTime = 0;
  186. this.Mix();
  187. flag = true;
  188. }
  189. }
  190. else
  191. {
  192. this.mixingTime = 0;
  193. }
  194. }
  195. if (isMixing != (inputTank.getFluidAmount() >= TConstruct.oreLiquidValue) && this.Battery.getEnergyStored() >= 15){
  196. flag = true;
  197. }
  198. }
  199. if(flag)
  200. {
  201. this.markDirty();
  202. }
  203. }
  204.  
  205.  
  206.  
  207. public void Mix() {
  208. if (this.canMix())
  209. {
  210. FluidStack output = MixerRecipesManager.mixing().getOutput(this.MixerItemStacks[0], this.inputTank.getFluid());
  211. int OutputAmount = 0;
  212. int InputAmount = this.inputTank.getFluidAmount();
  213. if (this.outputTank.getFluidAmount() ==0)
  214. {
  215. this.outputTank.setFluid(output);
  216. if (this.outputTank.getFluidAmount() < output.amount)
  217. {
  218. ++ OutputAmount;
  219. this.outputTank.fill(new FluidStack(output, OutputAmount), true);
  220. }
  221.  
  222. }
  223. else if (this.outputTank.getFluid().getFluid() == output.getFluid()){
  224. OutputAmount += output.amount;
  225. this.outputTank.fill(new FluidStack(output, OutputAmount), true);
  226. }
  227. --InputAmount;
  228. this.Battery.extractEnergy(15, false);
  229. if (InputAmount <= 0)
  230. {
  231. this.inputTank.setFluid(null);
  232. }
  233. this.inputTank.drain(InputAmount, true);
  234. }
  235. }
  236.  
  237. private boolean canMix()
  238. {
  239. if(this.MixerItemStacks[0] == null || this.inputFluid == null)
  240. {
  241. return false;
  242. }
  243. else
  244. {
  245. FluidStack fluid = MixerRecipesManager.mixing().getOutput(this.MixerItemStacks[0], this.inputTank.getFluid());
  246. if (fluid == null){ return false; }
  247. if (this.outputTank.getFluid() == null) {return true;}
  248. if (!this.outputTank.getFluid().isFluidEqual(fluid)) {return false;}
  249. else{return false;}
  250. }
  251. }
  252. @Override
  253. public void openInventory() {}
  254.  
  255. @Override
  256. public void closeInventory() {}
  257.  
  258. @Override
  259. public boolean isItemValidForSlot(int slot, ItemStack stack) {
  260.  
  261. return MixerRecipesManager.isItemValid(stack);
  262. }
  263. // SidedInventory Method
  264. @Override
  265. public int[] getAccessibleSlotsFromSide(int something) {
  266.  
  267. return something == 0 ? slotsBottom : (something == 1 ? slotsTop : slotsSides);
  268. }
  269.  
  270. @Override
  271. public boolean canInsertItem(int p_102007_1_, ItemStack stack, int direction) {
  272.  
  273. return this.isItemValidForSlot(p_102007_1_, stack);
  274. }
  275.  
  276. @Override
  277. public boolean canExtractItem(int p_102008_1_, ItemStack p_102008_2_, int p_102008_3_) {
  278.  
  279. return false;
  280. }
  281. //Client Side
  282. @SideOnly(Side.CLIENT)
  283. public int getMixingProgress(int part)
  284. {
  285. return this.mixingTime*part/200;
  286. }
  287.  
  288. //FluidHandler
  289. @Override
  290. public int fill(ForgeDirection from, FluidStack resource, boolean doFill) {
  291. if (IsInputFluid(resource.getFluid()) && (from != ForgeDirection.DOWN || from != ForgeDirection.DOWN)) {
  292. return inputTank.fill(resource, doFill);
  293. }
  294. return 0;
  295. }
  296.  
  297. @Override
  298. public FluidStack drain(ForgeDirection from, FluidStack resource, boolean doDrain) {
  299. return null;
  300. }
  301.  
  302. @Override
  303. public FluidStack drain(ForgeDirection from, int maxDrain, boolean doDrain) {
  304. return from == ForgeDirection.DOWN ? new FluidStack(this.inputFluid.getFluid(), maxDrain) : null;
  305. }
  306.  
  307. @Override
  308. public boolean canFill(ForgeDirection from, Fluid fluid) {
  309. return true;
  310. }
  311.  
  312. @Override
  313. public boolean canDrain(ForgeDirection from, Fluid fluid) {
  314. return true;
  315. }
  316.  
  317. @Override
  318. public FluidTankInfo[] getTankInfo(ForgeDirection from) {
  319. return new FluidTankInfo[] { inputTank.getInfo(), outputTank.getInfo() };
  320. }
  321.  
  322. @Override
  323. public boolean canConnectEnergy(ForgeDirection from) {
  324. return true;
  325. }
  326.  
  327. @Override
  328. public int receiveEnergy(ForgeDirection from, int maxReceive, boolean simulate) {
  329. return this.Battery.receiveEnergy(maxReceive, false);
  330. }
  331.  
  332. @Override
  333. public int getEnergyStored(ForgeDirection from) {
  334. return this.Battery.getEnergyStored();
  335. }
  336.  
  337. @Override
  338. public int getMaxEnergyStored(ForgeDirection from) {
  339.  
  340. return 120000;
  341. }
  342.  
  343. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement