Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private static final String nbtEnergyGeneration = "EnergyGeneration";
- private EnergyStorage energyStorage;
- private int energyGeneration;
- public TileEntitySolarPanel(int energyGeneration, int energyTransfer, int energyStorage)
- {
- this.energyGeneration = energyGeneration;
- this.energyStorage = new EnergyStorage(energyStorage, energyTransfer);
- }
- @Override
- public void updateEntity()
- {
- if (isServer())
- {
- generateEnergy();
- if (shouldTransferEnergy())
- {
- transferEnergy();
- }
- }
- }
- public int getEnergyProduced()
- {
- if (worldObj.canBlockSeeTheSky(xCoord, yCoord + 1, zCoord))
- {
- int sunProduction = getSunProduction();
- if (sunProduction > 0)
- {
- if (worldObj.isRaining())
- sunProduction = 0;
- if (worldObj.isThundering())
- sunProduction = 0;
- return Math.min(energyGeneration, sunProduction);
- }
- }
- return 0;
- }
- private int getSunProduction()
- {
- float multiplicator = 1.5f;
- float displacement = 1.2f;
- float celestialAngleRadians = worldObj.getCelestialAngleRadians(1.0f);
- if (celestialAngleRadians > Math.PI) {
- celestialAngleRadians = (2 * 3.141592f - celestialAngleRadians);
- }
- return Math.round(energyGeneration * multiplicator * MathHelper.cos(celestialAngleRadians / displacement));
- }
- protected void generateEnergy()
- {
- int produced = getEnergyProduced();
- if (produced > 0)
- {
- energyStorage.receiveEnergy(produced, false);
- }
- }
- protected boolean shouldTransferEnergy()
- {
- return energyStorage.getEnergyStored() > 0;
- }
- protected void transferEnergy()
- {
- for (ForgeDirection direction : ForgeDirection.VALID_DIRECTIONS)
- {
- TileEntity tile = getWorldObj().getTileEntity(xCoord + direction.offsetX, yCoord + direction.offsetY, zCoord + direction.offsetZ);
- if (!(tile instanceof TileEntitySolarPanel))
- {
- if (tile instanceof IEnergyHandler)
- {
- IEnergyHandler receiver = (IEnergyHandler) tile;
- energyStorage.sendMaxTo(receiver, direction.getOpposite());
- }
- }
- }
- }
- @Override
- protected void loadDataFromNBT(NBTTagCompound nbtTagCompound)
- {
- energyGeneration = nbtTagCompound.getInteger(nbtEnergyGeneration);
- energyStorage.readFromNBT(nbtTagCompound);
- }
- @Override
- protected void addDataToNBT(NBTTagCompound nbtTagCompound)
- {
- nbtTagCompound.setInteger(nbtEnergyGeneration, energyGeneration);
- energyStorage.writeToNBT(nbtTagCompound);
- }
- @Override
- public boolean canConnectEnergy(ForgeDirection from)
- {
- return from == ForgeDirection.DOWN;
- }
- @Override
- public int receiveEnergy(ForgeDirection from, int receive, boolean simulate)
- {
- return 0;
- }
- @Override
- public int extractEnergy(ForgeDirection from, int extract, boolean simulate)
- {
- return energyStorage.extractEnergy(energyStorage.getMaxExtract(), simulate);
- }
- public int getEnergyStored()
- {
- return getEnergyStored(ForgeDirection.DOWN);
- }
- public void setEnergyStored(int energy)
- {
- energyStorage.setEnergyStored(energy);
- }
- @Override
- public int getEnergyStored(ForgeDirection from)
- {
- return energyStorage.getEnergyStored();
- }
- @Override
- public int getMaxEnergyStored(ForgeDirection from)
- {
- return energyStorage.getMaxEnergyStored();
- }
- @Override
- public String toString()
- {
- return Objects.toStringHelper(this).add("hash", hashCode()).add("MaxProduction", energyGeneration).add("energyStorage", energyStorage).toString();
- }
Advertisement
Add Comment
Please, Sign In to add comment