Guest User

Untitled

a guest
Feb 3rd, 2015
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.06 KB | None | 0 0
  1.     private static final String nbtEnergyGeneration = "EnergyGeneration";
  2.     private EnergyStorage energyStorage;
  3.     private int energyGeneration;
  4.    
  5.     public TileEntitySolarPanel(int energyGeneration, int energyTransfer, int energyStorage)
  6.     {
  7.         this.energyGeneration = energyGeneration;
  8.         this.energyStorage = new EnergyStorage(energyStorage, energyTransfer);
  9.     }
  10.     @Override
  11.     public void updateEntity()
  12.     {
  13.         if (isServer())
  14.         {
  15.             generateEnergy();
  16.  
  17.             if (shouldTransferEnergy())
  18.             {
  19.                 transferEnergy();
  20.             }
  21.         }
  22.     }
  23.     public int getEnergyProduced()
  24.     {
  25.         if (worldObj.canBlockSeeTheSky(xCoord, yCoord + 1, zCoord))
  26.         {
  27.             int sunProduction = getSunProduction();
  28.  
  29.             if (sunProduction > 0)
  30.             {
  31.                 if (worldObj.isRaining())
  32.                     sunProduction = 0;
  33.                 if (worldObj.isThundering())
  34.                     sunProduction = 0;
  35.                 return Math.min(energyGeneration, sunProduction);
  36.             }
  37.         }
  38.         return 0;
  39.     }
  40.     private int getSunProduction()
  41.     {
  42.         float multiplicator = 1.5f;
  43.         float displacement = 1.2f;
  44.         float celestialAngleRadians = worldObj.getCelestialAngleRadians(1.0f);
  45.  
  46.         if (celestialAngleRadians > Math.PI) {
  47.             celestialAngleRadians = (2 * 3.141592f - celestialAngleRadians);
  48.         }
  49.  
  50.         return Math.round(energyGeneration * multiplicator * MathHelper.cos(celestialAngleRadians / displacement));
  51.     }
  52.     protected void generateEnergy()
  53.     {
  54.         int produced = getEnergyProduced();
  55.  
  56.         if (produced > 0)
  57.         {
  58.             energyStorage.receiveEnergy(produced, false);
  59.         }
  60.     }
  61.     protected boolean shouldTransferEnergy()
  62.     {
  63.         return energyStorage.getEnergyStored() > 0;
  64.     }
  65.     protected void transferEnergy()
  66.     {
  67.         for (ForgeDirection direction : ForgeDirection.VALID_DIRECTIONS)
  68.         {
  69.             TileEntity tile = getWorldObj().getTileEntity(xCoord + direction.offsetX, yCoord + direction.offsetY, zCoord + direction.offsetZ);
  70.  
  71.             if (!(tile instanceof TileEntitySolarPanel))
  72.             {
  73.                 if (tile instanceof IEnergyHandler)
  74.                 {
  75.                     IEnergyHandler receiver = (IEnergyHandler) tile;
  76.                     energyStorage.sendMaxTo(receiver, direction.getOpposite());
  77.                 }
  78.             }
  79.         }
  80.     }
  81.     @Override
  82.     protected void loadDataFromNBT(NBTTagCompound nbtTagCompound)
  83.     {
  84.         energyGeneration = nbtTagCompound.getInteger(nbtEnergyGeneration);
  85.         energyStorage.readFromNBT(nbtTagCompound);
  86.     }
  87.     @Override
  88.     protected void addDataToNBT(NBTTagCompound nbtTagCompound)
  89.     {
  90.         nbtTagCompound.setInteger(nbtEnergyGeneration, energyGeneration);
  91.         energyStorage.writeToNBT(nbtTagCompound);
  92.     }
  93.     @Override
  94.     public boolean canConnectEnergy(ForgeDirection from)
  95.     {
  96.         return from == ForgeDirection.DOWN;
  97.     }
  98.     @Override
  99.     public int receiveEnergy(ForgeDirection from, int receive, boolean simulate)
  100.     {
  101.         return 0;
  102.     }
  103.     @Override
  104.     public int extractEnergy(ForgeDirection from, int extract, boolean simulate)
  105.     {
  106.         return energyStorage.extractEnergy(energyStorage.getMaxExtract(), simulate);
  107.     }
  108.     public int getEnergyStored()
  109.     {
  110.         return getEnergyStored(ForgeDirection.DOWN);
  111.     }
  112.     public void setEnergyStored(int energy)
  113.     {
  114.         energyStorage.setEnergyStored(energy);
  115.     }
  116.     @Override
  117.     public int getEnergyStored(ForgeDirection from)
  118.     {
  119.         return energyStorage.getEnergyStored();
  120.     }
  121.     @Override
  122.     public int getMaxEnergyStored(ForgeDirection from)
  123.     {
  124.         return energyStorage.getMaxEnergyStored();
  125.     }
  126.     @Override
  127.     public String toString()
  128.     {
  129.         return Objects.toStringHelper(this).add("hash", hashCode()).add("MaxProduction", energyGeneration).add("energyStorage", energyStorage).toString();
  130.     }
Advertisement
Add Comment
Please, Sign In to add comment