Tile Machine
package net.vadamdev.sfbase.core.energy;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.ITickable;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.common.capabilities.ICapabilityProvider;
import net.minecraftforge.energy.CapabilityEnergy;
import net.minecraftforge.energy.EnergyStorage;
import javax.annotation.Nullable;
public abstract class TileMachine extends TileEntity implements ITickable, ICapabilityProvider {
public EnergyStorage energyStorage;
public TileMachine(EnergyStorage energyStorage) {
this.energyStorage = energyStorage;
}
/*
NBT Data
*/
/*@Override
public void readFromNBT(NBTTagCompound compound) {
this.energy = compound.getInteger("Energy");
this.capacity = compound.getInteger("Capacity");
this.maxReceive = compound.getInteger("MaxReceive");
this.maxExtract = compound.getInteger("MaxExtract");
super.readFromNBT(compound);
}
@Override
public NBTTagCompound writeToNBT(NBTTagCompound compound) {
energyStorage.writeToNBT(compound);
return super.writeToNBT(compound);
}*/
/*
public void readFromNBT(NBTTagCompound compound)
{
this.energy = compound.getInteger("Energy");
this.capacity = compound.getInteger("Capacity");
this.maxReceive = compound.getInteger("MaxReceive");
this.maxExtract = compound.getInteger("MaxExtract");
}
public void writeToNBT(NBTTagCompound compound)
{
compound.setInteger("Energy", this.energy);
compound.setInteger("Capacity", this.capacity);
compound.setInteger("MaxReceive", this.maxReceive);
compound.setInteger("MaxExtract", this.maxExtract);
}*/
/*
ITickable
*/
@Override
public void update() {
}
/*
Forge Energy
*/
public int getMaxEnergyStored() {
return energyStorage.getMaxEnergyStored();
}
public int getEnergyStored() {
return energyStorage.getEnergyStored();
}
/*
Capability
*/
@Override
public <T> T getCapability(Capability<T> capability, @Nullable EnumFacing facing) {
if(capability == CapabilityEnergy.ENERGY) return (T) energyStorage;
return super.getCapability(capability, facing);
}
@Override
public boolean hasCapability(Capability<?> capability, @Nullable EnumFacing facing) {
if(capability == CapabilityEnergy.ENERGY) return true;
return super.hasCapability(capability, facing);
}
}
Energy meter
package net.vadamdev.sfbase.init.blocks;
import net.minecraft.block.BlockHorizontal;
import net.minecraft.block.material.Material;
import net.minecraft.block.properties.IProperty;
import net.minecraft.block.properties.PropertyDirection;
import net.minecraft.block.state.BlockStateContainer;
import net.minecraft.block.state.IBlockState;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.EnumHand;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.text.TextComponentString;
import net.minecraft.world.World;
import net.vadamdev.sfbase.core.blocks.BlockBase;
import net.vadamdev.sfbase.core.blocks.BlockBaseDirectional;
import net.vadamdev.sfbase.init.tileentity.TileEntityEnergyMeter;
public class EnergyMeterMachine extends BlockBase {
public EnergyMeterMachine(Material materialIn) {
super(materialIn, 1, 1);
}
@Override
public boolean hasTileEntity(IBlockState state) {
return true;
}
@Override
public TileEntity createTileEntity(World world, IBlockState state) {
return new TileEntityEnergyMeter();
}
@Override
public boolean onBlockActivated(World world, BlockPos pos, IBlockState state, EntityPlayer player, EnumHand hand, EnumFacing facing, float hitX, float hitY, float hitZ) {
TileEntity t = world.getTileEntity(pos);
if(t instanceof TileEntityEnergyMeter) {
TileEntityEnergyMeter tile = (TileEntityEnergyMeter) t;
player.sendMessage(new TextComponentString("Energy stored : " + tile.getEnergyStored() + " RF / " + tile.getMaxEnergyStored() + " RF"));
return true;
}
return false;
}
}
Tile entity meter
package net.vadamdev.sfbase.init.tileentity;
import net.minecraft.util.EnumFacing;
import net.minecraftforge.energy.EnergyStorage;
import net.vadamdev.sfbase.core.energy.TileMachine;
public class TileEntityEnergyMeter extends TileMachine {
public TileEntityEnergyMeter() {
super(new EnergyStorage(10000, 10000, 10000));
}
}