Guest User

Untitled

a guest
Jun 7th, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.07 KB | None | 0 0
  1. package com.thenewjourney.blocks.pervateki;
  2.  
  3. import net.minecraft.nbt.NBTTagCompound;
  4. import net.minecraft.tileentity.TileEntity;
  5.  
  6. /**
  7. * An energy storage which is not limited by the maximum 32 bit integer.
  8. *
  9. * @author CJMinecraft
  10. */
  11. public class ModEnergyStorage {
  12.  
  13. private long energy;
  14. private long capacity;
  15. private long maxReceive;
  16. private long maxExtract;
  17.  
  18. /**
  19. * Initialise an energy storage.
  20. *
  21. * @param capacity The capacity of the energy storage
  22. */
  23. public ModEnergyStorage(long capacity) {
  24. this(capacity, capacity, capacity, 0);
  25. }
  26.  
  27. /**
  28. * Initialise an energy storage.
  29. *
  30. * @param capacity The capacity of the energy storage
  31. * @param maxTransfer The maximum amount of energy which can be received and
  32. * extracted
  33. */
  34. public ModEnergyStorage(long capacity, long maxTransfer) {
  35. this(capacity, maxTransfer, maxTransfer, 0);
  36. }
  37.  
  38. /**
  39. * Initialise an energy storage
  40. *
  41. * @param capacity The capacity of the energy storage
  42. * @param maxReceive The maximum amount of energy which can be received
  43. * @param maxExtract The maximum amount of energy which can be extracted
  44. */
  45. public ModEnergyStorage(long capacity, long maxReceive, long maxExtract) {
  46. this(capacity, maxReceive, maxExtract, 0);
  47. }
  48.  
  49. /**
  50. * Initialise an energy storage
  51. *
  52. * @param capacity The capacity of the energy storage
  53. * @param maxReceive The maximum amount of energy which can be received
  54. * @param maxExtract The maximum amount of energy which can be extracted
  55. * @param energy The energy inside of the energy storage
  56. */
  57. public ModEnergyStorage(long capacity, long maxReceive, long maxExtract, long energy) {
  58. this.capacity = capacity;
  59. this.maxReceive = maxReceive;
  60. this.maxExtract = maxExtract;
  61. this.energy = Math.max(0, Math.min(capacity, energy));
  62. }
  63.  
  64. /**
  65. * Give energy to the energy storage
  66. *
  67. * @param maxReceive The amount of energy to give
  68. * @param simulate Whether it is a simulation - if so, no energy will actually be
  69. * given
  70. * @return The amount of energy which was received
  71. */
  72. public long receiveEnergy(long maxReceive, boolean simulate) {
  73. if (!canReceive())
  74. return 0;
  75.  
  76. long energyReceived = Math.min(this.capacity - this.energy, Math.min(this.maxReceive, maxReceive));
  77. if (!simulate)
  78. this.energy += energyReceived;
  79. return energyReceived;
  80. }
  81.  
  82. /**
  83. * Take energy from the energy storage
  84. *
  85. * @param maxExtract The amount of energy to take
  86. * @param simulate Whether it is a simulation - if so, no energy will actually be
  87. * taken
  88. * @return The amount of energy which was extracted
  89. */
  90. public long extractEnergy(long maxExtract, boolean simulate) {
  91. if (!canExtract())
  92. return 0;
  93.  
  94. long energyExtracted = Math.min(this.energy, Math.min(this.maxExtract, maxExtract));
  95. if (!simulate)
  96. this.energy -= energyExtracted;
  97. return energyExtracted;
  98. }
  99.  
  100. /**
  101. * For use only inside your {@link TileEntity}
  102. *
  103. * @param maxExtract How much you want to extract
  104. * @param simulate Whether it is a simulation
  105. * @return The amount of energy taken
  106. */
  107. public long extractEnergyInternal(long maxExtract, boolean simulate) {
  108. long before = this.maxExtract;
  109. this.maxExtract = Long.MAX_VALUE;
  110.  
  111. long toReturn = this.extractEnergy(maxExtract, simulate);
  112.  
  113. this.maxExtract = before;
  114. return toReturn;
  115. }
  116.  
  117. /**
  118. * For use only inside your {@link TileEntity}
  119. *
  120. * @param maxReceive How much you want to give
  121. * @param simulate Whether it is a simulation
  122. * @return The amount of energy given
  123. */
  124. public long receiveEnergyInternal(long maxReceive, boolean simulate) {
  125. long before = this.maxReceive;
  126. this.maxReceive = Long.MAX_VALUE;
  127.  
  128. long toReturn = this.receiveEnergy(maxReceive, simulate);
  129.  
  130. this.maxReceive = before;
  131. return toReturn;
  132. }
  133.  
  134. /**
  135. * Read and set all values from the data inside the given
  136. * {@link NBTTagCompound}
  137. *
  138. * @param nbt The {@link NBTTagCompound} with all the data
  139. */
  140. public void readFromNBT(NBTTagCompound nbt) {
  141. this.energy = nbt.getInteger("Energy");
  142. this.capacity = nbt.getInteger("Capacity");
  143. this.maxReceive = nbt.getInteger("MaxReceive");
  144. this.maxExtract = nbt.getInteger("MaxExtract");
  145. }
  146.  
  147. /**
  148. * Write all of the data to the {@link NBTTagCompound} provided
  149. *
  150. * @param nbt The {@link NBTTagCompound} to write to
  151. */
  152. public void writeToNBT(NBTTagCompound nbt) {
  153. nbt.setLong("Energy", this.energy);
  154. nbt.setLong("Capacity", this.capacity);
  155. nbt.setLong("MaxReceive", this.maxReceive);
  156. nbt.setLong("MaxExtract", this.maxExtract);
  157. }
  158.  
  159. /**
  160. * Set the current energy
  161. *
  162. * @param energy The energy to set
  163. * @return The {@link ModEnergyStorage} with the new energy
  164. */
  165. public ModEnergyStorage setEnergyStored(long energy) {
  166. this.energy = energy;
  167. return this;
  168. }
  169.  
  170. /**
  171. * Set the current capacity
  172. *
  173. * @param capacity The capacity to set
  174. * @return The {@link ModEnergyStorage} with the new capacity
  175. */
  176. public ModEnergyStorage setCapacity(long capacity) {
  177. this.capacity = capacity;
  178. return this;
  179. }
  180.  
  181. /**
  182. * Set the current max transfer
  183. *
  184. * @param transfer The max transfer to set
  185. * @return The {@link ModEnergyStorage} with the new max transfer
  186. */
  187. public ModEnergyStorage setMaxTransfer(long transfer) {
  188. this.maxReceive = transfer;
  189. this.maxExtract = transfer;
  190. return this;
  191. }
  192.  
  193. /**
  194. * Set the current max receive
  195. *
  196. * @param maxReceive The max receive to set
  197. * @return The {@link ModEnergyStorage} with the new max receive
  198. */
  199. public ModEnergyStorage setMaxReceive(long maxReceive) {
  200. this.maxReceive = maxReceive;
  201. return this;
  202. }
  203.  
  204. /**
  205. * Set the current max extract
  206. *
  207. * @param maxExtract The max extract to set
  208. * @return The {@link ModEnergyStorage} with the new max extract
  209. */
  210. public ModEnergyStorage setMaxExtract(long maxExtract) {
  211. this.maxExtract = maxExtract;
  212. return this;
  213. }
  214.  
  215. /**
  216. * Get the maximum energy this can extract and receive
  217. *
  218. * @return The max transfer
  219. */
  220. public long getMaxTransfer() {
  221. return this.maxReceive == this.maxExtract ? this.maxReceive : Math.max(this.maxReceive, this.maxExtract);
  222. }
  223.  
  224. /**
  225. * Get the maximum energy this can receive
  226. *
  227. * @return The maximum energy this can receive
  228. */
  229. public long getMaxReceive() {
  230. return this.maxReceive;
  231. }
  232.  
  233. /**
  234. * Get the maximum energy that can be extracted
  235. *
  236. * @return The maximum energy that can be extracted
  237. */
  238. public long getMaxExtract() {
  239. return this.maxExtract;
  240. }
  241.  
  242. /**
  243. * Get the energy inside of the energy storage
  244. *
  245. * @return The energy inside of the energy storage
  246. */
  247. public long getEnergyStored() {
  248. return this.energy;
  249. }
  250.  
  251. /**
  252. * Get the maximum amount of energy in the energy storage
  253. *
  254. * @return The maximum amount of energy in the energy storage
  255. */
  256. public long getMaxEnergyStored() {
  257. return this.capacity;
  258. }
  259.  
  260. /**
  261. * @return whether you can take energy from the energy storage
  262. */
  263. public boolean canExtract() {
  264. return this.maxExtract > 0;
  265. }
  266.  
  267. /**
  268. * @return whether you can give energy to the energy storage
  269. */
  270. public boolean canReceive() {
  271. return this.maxReceive > 0;
  272. }
  273.  
  274. }
Add Comment
Please, Sign In to add comment