Camellias_

TileEntityCable

Nov 8th, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.14 KB | None | 0 0
  1. package com.camellias.stardust.common.tileentities;
  2.  
  3. import com.camellias.stardust.utils.energy.StardustForgeEnergyStorage;
  4.  
  5. import net.minecraft.nbt.NBTTagCompound;
  6. import net.minecraft.tileentity.TileEntity;
  7. import net.minecraft.util.EnumFacing;
  8. import net.minecraft.util.ITickable;
  9. import net.minecraftforge.common.capabilities.Capability;
  10. import net.minecraftforge.energy.CapabilityEnergy;
  11. import net.minecraftforge.energy.IEnergyStorage;
  12. import net.minecraftforge.items.CapabilityItemHandler;
  13. import net.minecraftforge.items.ItemStackHandler;
  14.  
  15. public class TileEntityCable extends TileEntity implements ITickable
  16. {
  17.     public int capacity = 1024;
  18.     public int maxOutput = 512;
  19.     public int maxInput = 512;
  20.     private StardustForgeEnergyStorage storage = new StardustForgeEnergyStorage(capacity, maxInput, maxOutput);
  21.     public int energy = storage.getEnergyStored();
  22.    
  23.     @Override
  24.     public void update()
  25.     {
  26.         if(!world.isRemote)
  27.         {
  28.             outputEnergy();
  29.             inputEnergy();
  30.         }
  31.     }
  32.    
  33.     public void outputEnergy()
  34.     {
  35.         for(EnumFacing dir : EnumFacing.values())
  36.         {
  37.             TileEntity tile = world.getTileEntity(pos.offset(dir));
  38.            
  39.             if(tile == null)
  40.             {
  41.                 continue;
  42.             }
  43.            
  44.             IEnergyStorage energyCapability = tile.getCapability(CapabilityEnergy.ENERGY, null);
  45.            
  46.             if(energyCapability == null)
  47.             {
  48.                 continue;
  49.             }
  50.            
  51.            
  52.            
  53.             if(energy >= maxOutput)
  54.             {
  55.                 energyCapability.receiveEnergy(maxOutput, false);
  56.                 energy -= maxOutput;
  57.             }
  58.            
  59.             if(energy < maxOutput)
  60.             {
  61.                 energyCapability.receiveEnergy(energy, false);
  62.                 energy -= energy;
  63.             }
  64.         }
  65.     }
  66.    
  67.     public void inputEnergy()
  68.     {
  69.         for(EnumFacing dir : EnumFacing.values())
  70.         {
  71.             TileEntity tile = world.getTileEntity(pos.offset(dir));
  72.            
  73.             if(tile == null)
  74.             {
  75.                 continue;
  76.             }
  77.            
  78.             IEnergyStorage energyCapability = tile.getCapability(CapabilityEnergy.ENERGY, null);
  79.            
  80.             if(energyCapability == null)
  81.             {
  82.                 continue;
  83.             }
  84.            
  85.             int otherEnergy = energyCapability.getEnergyStored();
  86.             int otherEnergyCap = energyCapability.getMaxEnergyStored();
  87.            
  88.            
  89.            
  90.             if(energy <= (capacity - maxInput) && otherEnergy >= maxInput)
  91.             {
  92.                 energyCapability.receiveEnergy(maxInput, false);
  93.                 energy += maxInput;
  94.             }
  95.            
  96.             if(energy > (capacity - maxInput) && otherEnergy < maxInput)
  97.             {
  98.                 energyCapability.receiveEnergy(otherEnergy, false);
  99.                 energy += otherEnergy;
  100.             }
  101.         }
  102.     }
  103.    
  104.     @Override
  105.     public <T> T getCapability(Capability<T> capability, EnumFacing facing)
  106.     {
  107.         if(capability == CapabilityEnergy.ENERGY)
  108.         {
  109.             return (T)this.storage;
  110.         }
  111.        
  112.         return super.getCapability(capability, facing);
  113.     }
  114.    
  115.     @Override
  116.     public boolean hasCapability(Capability<?> capability, EnumFacing facing)
  117.     {
  118.         if(capability == CapabilityEnergy.ENERGY)
  119.         {
  120.             return true;
  121.         }
  122.        
  123.         return super.hasCapability(capability, facing);
  124.     }
  125.    
  126.     @Override
  127.     public NBTTagCompound writeToNBT(NBTTagCompound compound)
  128.     {
  129.         super.writeToNBT(compound);
  130.        
  131.         compound.setInteger("GUIEnergy", this.energy);
  132.         this.storage.writeToNBT(compound);
  133.        
  134.         return compound;
  135.     }
  136.    
  137.     @Override
  138.     public void readFromNBT(NBTTagCompound compound)
  139.     {
  140.         super.readFromNBT(compound);
  141.        
  142.         this.energy = compound.getInteger("GUIEnergy");
  143.         this.storage.readFromNBT(compound);
  144.     }
  145.    
  146.     public int getEnergyStored()
  147.     {
  148.         return this.energy;
  149.     }
  150.    
  151.     public int getMaxEnergyStored()
  152.     {
  153.         return this.storage.getMaxEnergyStored();
  154.     }
  155.    
  156.     public int getField(int id)
  157.     {
  158.         switch(id)
  159.         {
  160.         case 0:
  161.             return this.energy;
  162.         default:
  163.             return 0;
  164.         }
  165.     }
  166.    
  167.     public void setField(int id, int value)
  168.     {
  169.         switch(id)
  170.         {
  171.         case 0:
  172.             this.energy = value;
  173.         }
  174.     }
  175. }
Add Comment
Please, Sign In to add comment