Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.jolteffect.teslamodularsolars.block;
- import javax.annotation.Nullable;
- import com.jolteffect.teslamodularsolars.handler.TeslaModularSolarsConfig;
- import com.jolteffect.teslamodularsolars.utility.LogHelper;
- import net.darkhax.tesla.api.ITeslaConsumer;
- import net.darkhax.tesla.api.implementation.BaseTeslaContainer;
- import net.darkhax.tesla.capability.TeslaCapabilities;
- import net.minecraft.item.ItemStack;
- import net.minecraft.nbt.NBTTagCompound;
- import net.minecraft.network.NetworkManager;
- import net.minecraft.network.play.server.SPacketUpdateTileEntity;
- import net.minecraft.tileentity.TileEntity;
- import net.minecraft.util.EnumFacing;
- import net.minecraft.util.ITickable;
- import net.minecraftforge.common.capabilities.Capability;
- import net.minecraftforge.items.ItemStackHandler;
- public class TileEntitySolarPanel extends TileEntity implements ITickable {
- // An instance of something that implements ITeslaConsumer, ITeslaProducer or
- // ITeslaHandler. In this case we use the BaseTeslaContainer which makes use of all three.
- // The purpose of this instance is to handle all tesla related logic for the TileEntity.
- private ContainerSolarPanel container;
- public ItemStackHandler itemStackHandler = new ItemStackHandler(10);
- public TileEntitySolarPanel() {
- // Initializes the container. Very straight forward.
- this.container = new ContainerSolarPanel();
- }
- @Override
- public void readFromNBT (NBTTagCompound compound) {
- super.readFromNBT(compound);
- this.container.setCapacity(compound.getLong("Capacity"));
- LogHelper.logInfo("TeslaModularSolars --------------readFromNBT Capacity = " + compound.getLong("Capacity"));
- this.container.setPower(compound.getLong("StoredPower"));
- LogHelper.logInfo("TeslaModularSolars --------------readFromNBT StoredPower = " + compound.getLong("StoredPower"));
- }
- @Override
- public NBTTagCompound writeToNBT (NBTTagCompound compound) {
- compound.setLong("Capacity", this.container.getCapacity());
- LogHelper.logInfo("TeslaModularSolars --------------writeToNBT Capacity = " + this.container.getStoredPower());
- compound.setLong("StoredPower", this.container.getStoredPower());
- LogHelper.logInfo("TeslaModularSolars --------------writeToNBT StoredPower = " + this.container.getStoredPower());
- return super.writeToNBT(compound);
- }
- @Override
- @SuppressWarnings("unchecked")
- public <T> T getCapability (Capability<T> capability, EnumFacing facing) {
- if (facing == EnumFacing.DOWN && (capability == TeslaCapabilities.CAPABILITY_PRODUCER || capability == TeslaCapabilities.CAPABILITY_HOLDER))
- return (T) this.container;
- return super.getCapability(capability, facing);
- }
- @Override
- public boolean hasCapability (Capability<?> capability, EnumFacing facing) {
- if (facing == EnumFacing.DOWN && (capability == TeslaCapabilities.CAPABILITY_PRODUCER || capability == TeslaCapabilities.CAPABILITY_HOLDER))
- return true;
- return super.hasCapability(capability, facing);
- }
- @Override
- public void update() {
- if (this.hasWorldObj()) {
- if (!this.worldObj.provider.getHasNoSky() && this.worldObj.canBlockSeeSky(this.pos.offset(EnumFacing.UP)) && !this.worldObj.isRaining() && this.worldObj.getSkylightSubtracted() == 0 && this.container.getStoredPower() != this.container.getCapacity())
- this.container.generatePower();
- final TileEntity tile = this.getWorld().getTileEntity(this.getPos().offset(EnumFacing.DOWN));
- if (tile != null && !tile.isInvalid() && tile.hasCapability(TeslaCapabilities.CAPABILITY_CONSUMER, EnumFacing.UP))
- this.container.takePower(((ITeslaConsumer) tile.getCapability(TeslaCapabilities.CAPABILITY_CONSUMER, EnumFacing.UP)).givePower(Math.min(this.container.getStoredPower(), container.getTransferRate()), false), false);
- }
- }
- @Override
- public NBTTagCompound getUpdateTag() {
- // getUpdateTag() is called whenever the chunkdata is sent to the
- // client. In contrast getUpdatePacket() is called when the tile entity
- // itself wants to sync to the client. In many cases you want to send
- // over the same information in getUpdateTag() as in getUpdatePacket().
- return writeToNBT(new NBTTagCompound());
- }
- @Nullable
- @Override
- public SPacketUpdateTileEntity getUpdatePacket() {
- // Prepare a packet for syncing our TE to the client. Since we only have to sync the stack
- // and that's all we have we just write our entire NBT here. If you have a complex
- // tile entity that doesn't need to have all information on the client you can write
- // a more optimal NBT here.
- NBTTagCompound nbtTag = new NBTTagCompound();
- this.writeToNBT(nbtTag);
- return new SPacketUpdateTileEntity(getPos(), 1, nbtTag);
- }
- @Override
- public void onDataPacket(NetworkManager net, SPacketUpdateTileEntity packet) {
- // Here we get the packet from the server and read it into our client side tile entity
- this.readFromNBT(packet.getNbtCompound());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement