Advertisement
Guest User

Energy Source

a guest
Mar 21st, 2013
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.58 KB | None | 0 0
  1. final static public float ConversionRate = 100F;
  2.  
  3. // _bufferSize = ConversationRate * getMaxEnergyOutput()
  4. private float _bufferSize;
  5. private float _buffered;
  6. private boolean _added;
  7.  
  8.  
  9. // Called in some updateEntity, this is where I publish EnergyTileSourceEvent
  10.   @Override
  11.   public void distribute(float amount) {
  12.     if(!_added || worldObj.isRemote)
  13.       return;
  14.  
  15.     if((_buffered += amount) > _bufferSize) {
  16.       _buffered = _bufferSize;
  17.     }
  18.  
  19.     if(_buffered < ConversionRate) {
  20.       return;
  21.     }
  22.  
  23.     float real = _buffered / ConversionRate;
  24.     int rounded = (int)real;
  25.     float left = real - rounded;
  26.  
  27.     EnergyTileSourceEvent e = new EnergyTileSourceEvent(this, rounded);
  28.     MinecraftForge.EVENT_BUS.post(e);
  29.     _buffered = e.amount * ConversionRate + left;
  30.   }
  31.  
  32.   // Called from updateEntity after some initialization, can be in the first updateEntity call but can also be much later
  33.   @Override
  34.   public void onUpdate() {
  35.     if(!worldObj.isRemote && !_added) {
  36.       EnergyTileLoadEvent loadEvent = new EnergyTileLoadEvent(this);
  37.       MinecraftForge.EVENT_BUS.post(loadEvent);
  38.       _added = true;
  39.     }
  40.   }
  41.  
  42.   @Override
  43.   public void invalidate() {
  44.     if(_added) {
  45.       MinecraftForge.EVENT_BUS.post(new EnergyTileUnloadEvent(this));
  46.       _added = false;
  47.     }
  48.     super.invalidate();
  49.   }
  50.  
  51.   @Override
  52.   public abstract int getMaxEnergyOutput();
  53.  
  54.   @Override
  55.   public boolean emitsEnergyTo(TileEntity receiver, Direction direction) {
  56.     return true;
  57.   }
  58.  
  59.   @Override
  60.   public boolean isAddedToEnergyNet() {
  61.     return _added;
  62.   }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement