/******************************************************************************* * AbyssalCraft * Copyright (c) 2012 - 2017 Shinoow. * All rights reserved. This program and the accompanying materials * are made available under the terms of the GNU Lesser Public License v3 * which accompanies this distribution, and is available at * http://www.gnu.org/licenses/lgpl-3.0.txt * * Contributors: * Shinoow - implementation ******************************************************************************/ package com.shinoow.abyssalcraft.common.blocks.tile; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.network.NetworkManager; import net.minecraft.network.play.server.SPacketUpdateTileEntity; import net.minecraft.tileentity.TileEntity; import com.shinoow.abyssalcraft.api.energy.IEnergyCollector; public class TileEntityEnergyCollector extends TileEntity implements IEnergyCollector { private float energy; @Override public void readFromNBT(NBTTagCompound nbttagcompound) { super.readFromNBT(nbttagcompound); energy = nbttagcompound.getFloat("PotEnergy"); } @Override public NBTTagCompound writeToNBT(NBTTagCompound nbttagcompound) { super.writeToNBT(nbttagcompound); nbttagcompound.setFloat("PotEnergy", energy); return nbttagcompound; } @Override public SPacketUpdateTileEntity getUpdatePacket() { return new SPacketUpdateTileEntity(pos, 1, getUpdateTag()); } @Override public NBTTagCompound getUpdateTag() { return writeToNBT(new NBTTagCompound()); } @Override public void onDataPacket(NetworkManager net, SPacketUpdateTileEntity packet) { readFromNBT(packet.getNbtCompound()); } @Override public float getContainedEnergy() { return energy; } @Override public int getMaxEnergy() { return 1000; } @Override public void addEnergy(float energy) { this.energy += energy; if(this.energy > getMaxEnergy()) this.energy = getMaxEnergy(); world.notifyBlockUpdate(pos, world.getBlockState(pos), world.getBlockState(pos), 2); } @Override public float consumeEnergy(float energy) { if(energy < this.energy){ this.energy -= energy; world.notifyBlockUpdate(pos, world.getBlockState(pos), world.getBlockState(pos), 2); return energy; } else { float ret = this.energy; this.energy = 0; world.notifyBlockUpdate(pos, world.getBlockState(pos), world.getBlockState(pos), 2); return ret; } } @Override public boolean canAcceptPE() { return getContainedEnergy() < getMaxEnergy(); } @Override public boolean canTransferPE() { return getContainedEnergy() > 0; } @Override public TileEntity getContainerTile() { return this; } }