Advertisement
Guest User

Untitled

a guest
Aug 30th, 2016
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.62 KB | None | 0 0
  1. package com.jolteffect.teslaindustryexpansion.blocks.washplant;
  2.  
  3. import javax.annotation.Nonnull;
  4. import javax.annotation.Nullable;
  5. import com.jolteffect.teslaindustryexpansion.container.PowerContainerBase;
  6. import com.jolteffect.teslaindustryexpansion.init.Config;
  7. import com.jolteffect.teslaindustryexpansion.init.ModItems;
  8. import com.jolteffect.teslaindustryexpansion.tileentity.TileEntityBaseMachine;
  9.  
  10. import net.darkhax.tesla.capability.TeslaCapabilities;
  11. import net.minecraft.block.material.Material;
  12. import net.minecraft.block.state.IBlockState;
  13. import net.minecraft.client.gui.Gui;
  14. import net.minecraft.entity.player.EntityPlayer;
  15. import net.minecraft.inventory.Container;
  16. import net.minecraft.item.ItemStack;
  17. import net.minecraft.item.crafting.FurnaceRecipes;
  18. import net.minecraft.nbt.NBTTagCompound;
  19. import net.minecraft.network.NetworkManager;
  20. import net.minecraft.network.Packet;
  21. import net.minecraft.network.play.server.SPacketUpdateTileEntity;
  22. import net.minecraft.tileentity.TileEntity;
  23. import net.minecraft.util.EnumFacing;
  24. import net.minecraft.util.ResourceLocation;
  25. import net.minecraft.util.SoundCategory;
  26. import net.minecraft.util.math.BlockPos;
  27. import net.minecraft.world.IBlockAccess;
  28. import net.minecraft.world.World;
  29. import net.minecraftforge.common.capabilities.Capability;
  30. import net.minecraftforge.fluids.Fluid;
  31. import net.minecraftforge.fluids.FluidStack;
  32. import net.minecraftforge.fluids.FluidTank;
  33. import net.minecraftforge.fluids.FluidTankInfo;
  34. import net.minecraftforge.fluids.capability.CapabilityFluidHandler;
  35. import net.minecraftforge.fluids.capability.IFluidHandler;
  36. import net.minecraftforge.fluids.capability.IFluidTankProperties;
  37. import net.minecraftforge.items.ItemStackHandler;
  38.  
  39. public class TileEntityWashPlant extends TileEntityBaseMachine implements IFluidHandler {
  40.  
  41. protected FluidTank tank = new FluidTank(10000);
  42. private long lastTickFluidLevel = 0;
  43.  
  44. private long lastTickPowerLevel = 0;
  45. public long processTime = Config.machineProcessTime;
  46. private int burnTime = 0;
  47. private int overClockMultiplier = 4;
  48. private int overClockNegative = 8;
  49. private long powerUpgrade= Config.machinePowerUse;
  50. private static long PowerCap = Config.machineCap;
  51. private static int SLOTS = 6; //1 Input 1 Output 4 Upgrade
  52. private static int soundTimer = 0;
  53.  
  54. public TileEntityWashPlant() {
  55. super(new ContainerWashPlant(PowerCap), new ItemStackHandler(6));
  56.  
  57. }
  58.  
  59. @Override
  60. public IFluidTankProperties[] getTankProperties() {
  61.  
  62. return null;
  63.  
  64. }
  65.  
  66. @Override
  67. public int fill(FluidStack resource, boolean doFill) {
  68.  
  69. return tank.fill(resource, doFill);
  70. }
  71.  
  72. @Override
  73. public FluidStack drain(FluidStack resource, boolean doDrain) {
  74.  
  75. return tank.drain(resource, doDrain);
  76. }
  77.  
  78. @Override
  79. public FluidStack drain(int maxDrain, boolean doDrain) {
  80.  
  81. return tank.drain(maxDrain, doDrain);
  82. }
  83.  
  84.  
  85.  
  86. @Override
  87. public void update() {
  88. if(!worldObj.isRemote)
  89. {
  90. //using this to check Client/Server Sync until Gui implemented
  91. System.out.println("TeslaIndustryExpansion NOW PROCESSING. SERVER Fluid amount=" + tank.getFluidAmount());
  92.  
  93. }
  94. if(worldObj.isRemote)
  95. {
  96. //using this to check Client/Server Sync until Gui implemented
  97. System.out.println("TeslaIndustryExpansion NOW PROCESSING. CLIENT Fluid amount=" + tank.getFluidAmount());
  98.  
  99. }
  100.  
  101. checkUpgrade();
  102.  
  103.  
  104. if (canSmelt() && getContainer().consumePower(true))
  105. {
  106. this.setIsRunning(true);
  107. getContainer().setUsePower(powerUpgrade);
  108. getContainer().consumePower(false);
  109. burnTime++;
  110. soundTimer++;
  111.  
  112. if (soundTimer > 10)
  113. {
  114. worldObj.playSound((EntityPlayer) null, (double) pos.getX() + 0.5D, (double) pos.getY() + 0.5D, (double) pos.getZ() + 0.5D,
  115. (net.minecraft.util.SoundEvent) net.minecraft.util.SoundEvent.REGISTRY.getObject(new ResourceLocation(("block.fire.extinguish"))),
  116. SoundCategory.NEUTRAL, 1.0F, 1.0F);
  117. soundTimer =0;
  118. }
  119.  
  120. if (burnTime >= processTime)
  121. {
  122. smeltItem();
  123. burnTime = 0;
  124.  
  125. }
  126.  
  127. this.markDirty();
  128. }
  129. else if (this.getIsRunning())
  130. {
  131. this.setIsRunning(false);
  132.  
  133. }
  134.  
  135. if(inventory.getStackInSlot(0) == null)
  136. {
  137. burnTime = 0;
  138. this.setIsRunning(false);
  139. this.markDirty();
  140. }
  141.  
  142. if (this.getStoredPower() != lastTickPowerLevel) this.markDirty();
  143. if (this.tank.getFluidAmount() != lastTickFluidLevel) this.markDirty();
  144. lastTickPowerLevel = this.getStoredPower();
  145. lastTickFluidLevel = this.tank.getFluidAmount();
  146.  
  147. }
  148.  
  149. @Override
  150. public NBTTagCompound getUpdateTag() {
  151. return writeToNBT(new NBTTagCompound());
  152.  
  153. }
  154.  
  155. @Override
  156. public void readFromNBT(NBTTagCompound compound)
  157. {
  158. super.readFromNBT(compound);
  159. tank.readFromNBT(compound);
  160. if (compound.hasKey("BurnTime")) burnTime = compound.getInteger("BurnTime");
  161.  
  162. }
  163.  
  164. @Override
  165. public NBTTagCompound writeToNBT (NBTTagCompound compound)
  166. {
  167. compound.setInteger("BurnTime", burnTime);
  168. tank.writeToNBT(compound);
  169. return super.writeToNBT(compound);
  170. }
  171.  
  172. @Nullable
  173. @Override
  174. public SPacketUpdateTileEntity getUpdatePacket()
  175. {
  176.  
  177. NBTTagCompound tag = new NBTTagCompound();
  178. writeToNBT(tag);
  179. return new SPacketUpdateTileEntity(getPos(), 0, getUpdateTag());
  180.  
  181. }
  182.  
  183. @Override
  184. public void onDataPacket(NetworkManager net, SPacketUpdateTileEntity pkt)
  185. {
  186. readFromNBT(pkt.getNbtCompound());
  187. }
  188.  
  189.  
  190. @Override
  191. public Gui getClientGuiElement(int id, EntityPlayer player, World worldIn, BlockPos pos) {
  192.  
  193. return new GuiWashPlant(player.inventory, this);
  194. }
  195.  
  196. @Override
  197. public Container getServerGuiElement(int id, EntityPlayer player, World worldIn, BlockPos pos) {
  198.  
  199. return new GuiContainerWashPlant(player.inventory, this);
  200. }
  201.  
  202. @Override
  203. public boolean hasCapability(@Nonnull Capability<?> capability, @Nullable EnumFacing facing) {
  204. if(capability == CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY) {
  205. return true;
  206. }
  207. if (capability == TeslaCapabilities.CAPABILITY_CONSUMER)
  208. {
  209. return true;
  210. }
  211. return super.hasCapability(capability, facing);
  212. }
  213.  
  214. @SuppressWarnings("unchecked")
  215. @Nonnull
  216. @Override
  217. public <T> T getCapability(@Nonnull Capability<T> capability, @Nullable EnumFacing facing) {
  218.  
  219. if(capability == CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY) {
  220. return (T) tank;
  221. }
  222. if (capability == TeslaCapabilities.CAPABILITY_CONSUMER)
  223. {
  224. return (T) containerPower;
  225. }
  226. return super.getCapability(capability, facing);
  227. }
  228.  
  229.  
  230. public boolean containsFluid() {
  231. return tank.getFluid() != null;
  232. }
  233.  
  234.  
  235. private void smeltItem()
  236. {
  237. if (this.worldObj.isRemote) return;
  238.  
  239. ItemStack inputStack = inventory.getStackInSlot(0);
  240. ItemStack outputStack = inventory.getStackInSlot(1);
  241. ItemStack result = FurnaceRecipes.instance().getSmeltingResult(inputStack).copy();
  242.  
  243. inventory.extractItem(0, 1, false);
  244. inventory.insertItem(1, result, false);
  245.  
  246. }
  247.  
  248. private boolean canSmelt()
  249. {
  250. ItemStack inputStack = inventory.getStackInSlot(0);
  251. ItemStack outputStack = inventory.getStackInSlot(1);
  252. ItemStack result;
  253.  
  254. if (inputStack != null && (result = FurnaceRecipes.instance().getSmeltingResult(inputStack)) != null)
  255. {
  256. if (outputStack == null) return true;
  257.  
  258. if (ItemStack.areItemsEqual(result, outputStack) && (outputStack.stackSize + result.stackSize) <= 64)
  259. {
  260. return true;
  261. }
  262. }
  263.  
  264. return false;
  265. }
  266.  
  267. public double getProgress()
  268. {
  269. return (double)burnTime / (double)processTime;
  270. }
  271.  
  272. public ContainerWashPlant getContainer()
  273. {
  274. return (ContainerWashPlant)this.containerPower;
  275. }
  276.  
  277. public FluidTank getTank()
  278. {
  279. return (FluidTank)this.tank;
  280. }
  281.  
  282. private void checkUpgrade() {
  283. ItemStack upgradeSlot1 = inventory.getStackInSlot(2);
  284. ItemStack upgradeSlot2 = inventory.getStackInSlot(3);
  285. ItemStack upgradeSlot3 = inventory.getStackInSlot(4);
  286. ItemStack upgradeSlot4 = inventory.getStackInSlot(5);
  287. processTime = Config.machineProcessTime;
  288. powerUpgrade = Config.machinePowerUse;
  289.  
  290. if(upgradeSlot1 !=null)
  291. {
  292. if(upgradeSlot1.getItem() == ModItems.itemOverclocker)
  293. {
  294. processTime -= upgradeSlot1.stackSize * overClockMultiplier;
  295. powerUpgrade += upgradeSlot1.stackSize * overClockNegative;
  296. }
  297. else if (upgradeSlot1.getItem() == ModItems.itemEfficiency)
  298. {
  299. powerUpgrade -= upgradeSlot1.stackSize * overClockMultiplier;;
  300. }
  301.  
  302. }
  303.  
  304. if(upgradeSlot2 !=null)
  305. {
  306. if(upgradeSlot2.getItem() == ModItems.itemOverclocker)
  307. {
  308. processTime -= upgradeSlot2.stackSize * overClockMultiplier;;
  309. powerUpgrade += upgradeSlot2.stackSize * overClockNegative;
  310. }
  311. else if (upgradeSlot2.getItem() == ModItems.itemEfficiency)
  312. {
  313. powerUpgrade -= upgradeSlot2.stackSize * overClockMultiplier;;
  314. }
  315.  
  316. }
  317. if(upgradeSlot3 !=null)
  318. {
  319. if(upgradeSlot3.getItem() == ModItems.itemOverclocker)
  320. {
  321. processTime -= upgradeSlot3.stackSize * overClockMultiplier;;
  322. powerUpgrade += upgradeSlot3.stackSize * overClockNegative;
  323. }
  324. else if (upgradeSlot3.getItem() == ModItems.itemEfficiency)
  325. {
  326. powerUpgrade -= upgradeSlot3.stackSize * overClockMultiplier;;
  327. }
  328.  
  329. }
  330.  
  331. if(upgradeSlot4 !=null)
  332. {
  333. if(upgradeSlot4.getItem() == ModItems.itemOverclocker)
  334. {
  335. processTime -= upgradeSlot4.stackSize * overClockMultiplier;;
  336. powerUpgrade += upgradeSlot4.stackSize * overClockNegative;
  337. }
  338. else if (upgradeSlot4.getItem() == ModItems.itemEfficiency)
  339. {
  340. powerUpgrade -= upgradeSlot4.stackSize * overClockMultiplier;;
  341. }
  342.  
  343. }
  344. getContainer().setUsePower(powerUpgrade);
  345.  
  346.  
  347. }
  348.  
  349. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement