Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.camellias.stardust.common.tileentities;
- import com.camellias.stardust.utils.energy.StardustForgeEnergyStorage;
- import net.minecraft.nbt.NBTTagCompound;
- import net.minecraft.tileentity.TileEntity;
- import net.minecraft.util.EnumFacing;
- import net.minecraft.util.ITickable;
- import net.minecraftforge.common.capabilities.Capability;
- import net.minecraftforge.energy.CapabilityEnergy;
- import net.minecraftforge.energy.IEnergyStorage;
- import net.minecraftforge.items.CapabilityItemHandler;
- import net.minecraftforge.items.ItemStackHandler;
- public class TileEntityCable extends TileEntity implements ITickable
- {
- public int capacity = 1024;
- public int maxOutput = 512;
- public int maxInput = 512;
- private StardustForgeEnergyStorage storage = new StardustForgeEnergyStorage(capacity, maxInput, maxOutput);
- public int energy = storage.getEnergyStored();
- @Override
- public void update()
- {
- if(!world.isRemote)
- {
- outputEnergy();
- inputEnergy();
- }
- }
- public void outputEnergy()
- {
- for(EnumFacing dir : EnumFacing.values())
- {
- TileEntity tile = world.getTileEntity(pos.offset(dir));
- if(tile == null)
- {
- continue;
- }
- IEnergyStorage energyCapability = tile.getCapability(CapabilityEnergy.ENERGY, null);
- if(energyCapability == null)
- {
- continue;
- }
- if(energy >= maxOutput)
- {
- energyCapability.receiveEnergy(maxOutput, false);
- energy -= maxOutput;
- }
- if(energy < maxOutput)
- {
- energyCapability.receiveEnergy(energy, false);
- energy -= energy;
- }
- }
- }
- public void inputEnergy()
- {
- for(EnumFacing dir : EnumFacing.values())
- {
- TileEntity tile = world.getTileEntity(pos.offset(dir));
- if(tile == null)
- {
- continue;
- }
- IEnergyStorage energyCapability = tile.getCapability(CapabilityEnergy.ENERGY, null);
- if(energyCapability == null)
- {
- continue;
- }
- int otherEnergy = energyCapability.getEnergyStored();
- int otherEnergyCap = energyCapability.getMaxEnergyStored();
- if(energy <= (capacity - maxInput) && otherEnergy >= maxInput)
- {
- energyCapability.receiveEnergy(maxInput, false);
- energy += maxInput;
- }
- if(energy > (capacity - maxInput) && otherEnergy < maxInput)
- {
- energyCapability.receiveEnergy(otherEnergy, false);
- energy += otherEnergy;
- }
- }
- }
- @Override
- public <T> T getCapability(Capability<T> capability, EnumFacing facing)
- {
- if(capability == CapabilityEnergy.ENERGY)
- {
- return (T)this.storage;
- }
- return super.getCapability(capability, facing);
- }
- @Override
- public boolean hasCapability(Capability<?> capability, EnumFacing facing)
- {
- if(capability == CapabilityEnergy.ENERGY)
- {
- return true;
- }
- return super.hasCapability(capability, facing);
- }
- @Override
- public NBTTagCompound writeToNBT(NBTTagCompound compound)
- {
- super.writeToNBT(compound);
- compound.setInteger("GUIEnergy", this.energy);
- this.storage.writeToNBT(compound);
- return compound;
- }
- @Override
- public void readFromNBT(NBTTagCompound compound)
- {
- super.readFromNBT(compound);
- this.energy = compound.getInteger("GUIEnergy");
- this.storage.readFromNBT(compound);
- }
- public int getEnergyStored()
- {
- return this.energy;
- }
- public int getMaxEnergyStored()
- {
- return this.storage.getMaxEnergyStored();
- }
- public int getField(int id)
- {
- switch(id)
- {
- case 0:
- return this.energy;
- default:
- return 0;
- }
- }
- public void setField(int id, int value)
- {
- switch(id)
- {
- case 0:
- this.energy = value;
- }
- }
- }
Add Comment
Please, Sign In to add comment