Skip to content
CJMinecraft edited this page Jun 26, 2017 · 18 revisions

Energy is a crucial part of the API. The API does not provide a way to hold energy (apart from coFH Redstone Flux) but does allow easy integration between most tech mods.

Storing Energy

Energy can be stored using many different methods. You could use the Forge Energy (built in in the forge 1.9.4 onwards) or you could use Tesla and you can also use the CoFH Redstone Flux API which is built in, in the API. Whatever you use, the concept is simple:

  • Have a "Energy Storage" which holds the energy
  • Either add a capability for that storage or use interfaces on the actual tile entity class

Now all of this can also be applied to items as well but it is probably better to take a look at the API's github on how to do so.

Note - This might be updated in the future to actually show how to store energy with each different API

Now when actually storing energy you have got to think of what your block / whatever is storing the energy is actually going to be. It will fit in one of the three following categories:

  • Energy Storage (Like an energy cell or battery)
  • Energy Producer (Anything that makes energy (typically a generator))
  • Energy Consumer (Like a machine which will turn one thing into another) Each one handles energy in its own way

Energy Storage

Anything which is an Energy Storage cannot have its energy accessed by any neighbouring block. Let's say a generator (Energy Producer) was placed next to a block which is an Energy Storage, the generator will not give any energy to the Energy Storage, it will be handled internally by the Energy Storage. This is the same when an Energy Consumer is placed next to it, it will not receive any energy but will only be given this energy from the Energy Storage. So anything which is an Energy Storage must handle all of energy transmitting internally. This can be done really easily using the API, all you need to use is two methods. Now all handlers of an Energy Storage like an EnergyStorage, BaseTeslaContainer or even the forge EnergyStorage will have the ability to give energy in a method either called giveEnergy or receiveEnergy all of which will take in a boolean which will say whether it is a simulation, this must be false of course as it is not a simulation. Now call this function and the energy you will give to your Energy Storage can be found by using EnergyUtils.takeEnergyAllFaces where the parameters are self explanatory. This will coincidentally take energy from all of the tile entities surrounding your block if it has the support available. Now we have received the energy, we must now transfer this internally. Obviously this is optional but this is the typical approach to an Energy Storage. Now we need to transmit this energy to the neighbouring tile entities which can receive energy. This can be done again using the handler for the Energy Storage which will have a function by the name of takeEnergy or extractEnergy. Again like the giveEnergy and receiveEnergy functions, there is a boolean stating whether it is a simulation which again must be false. The parameter for energy in takeEnergy or extractEnergy will be given similarly to the taking of energy use EnergyUtils.giveEnergyAllFaces which respectively does what it says on the tin. This will give energy to all of the surrounding tile entities. This will now handle the energy like you would expect anything which stores energy to.

Energy Producer

Anything which is an Energy Producer cannot have energy given to it as it already produces energy. Energy can only be taken from an Energy Producer and this will typically only be used by an Energy Storage. An Energy Producer will most likely consume something to create this energy which should be handled in your tile entity (obviously if your block generates energy passively this is not necessary). One of the most difficult concepts to grasp is which gives energy to the other, does the Energy Producer give energy to the Energy Consumer or is it vice versa? The answer is simple, the Energy Producer should always give energy to neighbouring blocks. This can be done, like with the Energy Storage using EnergyUtils.giveEnergyAllFaces making sure to take that energy from the internal container / handler for energy as mentioned above. It is recommended that you balance out the how much energy is produced for how much of what your input is consumed.

Energy Consumer

Again like above anything which is an Energy Consumer cannot have energy taken from it as it already consumes energy. This should be taken internally from the internal buffer from your handler (mentioned above). As previously mentioned, the Energy Consumer will not, take energy from neighbouring blocks. This should be given only by the Energy Producer or any Energy Storage.

Showing Energy

All tile entities which hold energy typically want a way to display their energy. The best way to achieve this is through the use of the EnergyBar class. This class acts as a button and I'll explain why. One of the fundamental reasons I created this API was to aim to fix a problem I've encountered before, lot's of mods use lots of different API's, I wanted to be able to make a bridge between all of these. So what I did was created a way to have different EnergyUnits which converts the energy to the appropriate unit. Just by clicking on the EnergyBar you can cycle the unit which will update a configuration file. This will allow for you to easily display energy in a way which suits you best. Either in one of the internal energy units or your own which you can code. The internal energy units are as follows:

  • Minecraft Joules
  • Forge Energy
  • Redstone Flux
  • Tesla
  • Joules
  • Energy Unit (IC2)

Each unit has a colour and will be shown in the energy bar represented by the chosen colour. Clicking will just cycle the unit. Now a few methods you will need to use to get your energy bar working: In your gui class, you will need the initGui method where you just add the button to the list of buttons which (quite respectively) is called buttonList. Now to make the cycle work, you need to add the actionPerformed method which will allow you to check if your energy bar has been clicked and then call the energyBar.actionPerformed method. Everything else is handled respectively. Now that all of the actual rendering functionality is working, we need to now set the energy and capacity for the EnergyBar. To do this just do energyBar.updateEnergyBar which takes in the energy and capacity respectively. Unfortunately, majority of the time, your energy will be server side, however this can be synced to the client which will be covered later on.

Syncing server side data to the client

TODO


Clone this wiki locally