Mouamle

EnergyStorage

Jan 24th, 2017
356
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.03 KB | None | 0 0
  1. package net.minecraftforge.energy;
  2.  
  3. /**
  4.  * Reference implementation of {@link IEnergyStorage}. Use/extend this or implement your own.
  5.  *
  6.  * Derived from the Redstone Flux power system designed by King Lemming and originally utilized in Thermal Expansion and related mods.
  7.  * Created with consent and permission of King Lemming and Team CoFH. Released with permission under LGPL 2.1 when bundled with Forge.
  8.  */
  9. public class EnergyStorage implements IEnergyStorage
  10. {
  11.     protected int energy;
  12.     protected int capacity;
  13.     protected int maxReceive;
  14.     protected int maxExtract;
  15.  
  16.     public EnergyStorage(int capacity)
  17.     {
  18.         this(capacity, capacity, capacity);
  19.     }
  20.  
  21.     public EnergyStorage(int capacity, int maxTransfer)
  22.     {
  23.         this(capacity, maxTransfer, maxTransfer);
  24.     }
  25.  
  26.     public EnergyStorage(int capacity, int maxReceive, int maxExtract)
  27.     {
  28.         this.capacity = capacity;
  29.         this.maxReceive = maxReceive;
  30.         this.maxExtract = maxExtract;
  31.     }
  32.  
  33.     @Override
  34.     public int receiveEnergy(int maxReceive, boolean simulate)
  35.     {
  36.         if (!canReceive())
  37.             return 0;
  38.  
  39.         int energyReceived = Math.min(capacity - energy, Math.min(this.maxReceive, maxReceive));
  40.         if (!simulate)
  41.             energy += energyReceived;
  42.         return energyReceived;
  43.     }
  44.  
  45.     @Override
  46.     public int extractEnergy(int maxExtract, boolean simulate)
  47.     {
  48.         if (!canExtract())
  49.             return 0;
  50.  
  51.         int energyExtracted = Math.min(energy, Math.min(this.maxExtract, maxExtract));
  52.         if (!simulate)
  53.             energy -= energyExtracted;
  54.         return energyExtracted;
  55.     }
  56.  
  57.     @Override
  58.     public int getEnergyStored()
  59.     {
  60.         return energy;
  61.     }
  62.  
  63.     @Override
  64.     public int getMaxEnergyStored()
  65.     {
  66.         return capacity;
  67.     }
  68.  
  69.     @Override
  70.     public boolean canExtract()
  71.     {
  72.         return this.maxExtract > 0;
  73.     }
  74.  
  75.     @Override
  76.     public boolean canReceive()
  77.     {
  78.         return this.maxReceive > 0;
  79.     }
  80. }
Add Comment
Please, Sign In to add comment