Advertisement
Guest User

Untitled

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