Jump to content

1.5.2 IC2 API - Energy is always 0


danbka33

Recommended Posts

import ic2.api.energy.EnergyNet;
import ic2.api.energy.event.EnergyTileLoadEvent;
import ic2.api.energy.event.EnergyTileUnloadEvent;
import ic2.api.energy.tile.IEnergySink;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
import net.minecraftforge.common.ForgeDirection;
import net.minecraftforge.common.MinecraftForge;
import cpw.mods.fml.common.FMLCommonHandler;

public class SCoreEntityTileWorkbench extends TileEntity implements IEnergySink, IInventory {


double energy;
double maxenergy = 50000;
boolean init;

boolean work = true;

private ItemStack[] inv;

public SCoreEntityTileWorkbench()
{
	inv = new ItemStack[9];
}

 @Override
    public void updateEntity() {

	 	super.updateEntity();

        if (!init && !FMLCommonHandler.instance().getEffectiveSide().isClient()) {

            MinecraftForge.EVENT_BUS.post(new EnergyTileLoadEvent(this));
            init = true;
        }
    }

 @Override
 public void invalidate() {

        if (init) {
            MinecraftForge.EVENT_BUS.post(new EnergyTileUnloadEvent(this));

            init = false;
        }
 }
 @Override
    public void readFromNBT(NBTTagCompound nbttagcompound) {
        super.readFromNBT(nbttagcompound);    


            this.energy = nbttagcompound.getDouble("energy");

        

    }
 @Override
    public void writeToNBT(NBTTagCompound nbttagcompound) {
        super.writeToNBT(nbttagcompound);    

        nbttagcompound.setDouble("energy", this.energy);
    }

@Override
public boolean acceptsEnergyFrom(TileEntity emitter, ForgeDirection direction) {
	return true;
}

@Override
public double demandedEnergyUnits() {
	return Math.max(0, this.getMaxEnergy() - this.getEnergy());
}

@Override
public double injectEnergyUnits(ForgeDirection directionFrom, double amount)
{
	if(this.getEnergy() >= this.getMaxEnergy()) return amount;

	double openenergy = this.getMaxEnergy() - this.getEnergy();

	if(openenergy >= amount)
	{
		return this.energy += amount;		
	}
	else if(amount >= openenergy)
	{
		this.energy = this.maxenergy;
		return amount - openenergy;
	}

	return 0;
}

@Override
public int getMaxSafeInput() {
	return EnergyNet.instance.getPowerFromTier(2);
}

public int getScaledLevel(int i)
    {
	return (int) Math.floor(this.getEnergy() * i / (this.getMaxEnergy()));
    }

@Override
public int getSizeInventory()
{
	return inv.length;
}

@Override
    public void setInventorySlotContents(int slot, ItemStack stack)
{
        inv[slot] = stack;
        
        if (stack != null && stack.stackSize > getInventoryStackLimit())
        {
        	stack.stackSize = getInventoryStackLimit();
        }               
    }

    @Override
    public ItemStack decrStackSize(int slot, int amt)
    {
        ItemStack stack = getStackInSlot(slot);
        
        if (stack != null)
        {
            if (stack.stackSize <= amt) {
                    setInventorySlotContents(slot, null);
            }
            else
            {
                stack = stack.splitStack(amt);
                
                if (stack.stackSize == 0)
                {
                        setInventorySlotContents(slot, null);
                }
            }
        }
        
        return stack;
    }

    @Override
    public ItemStack getStackInSlotOnClosing(int slot)
    {
            ItemStack stack = getStackInSlot(slot);
            
            if (stack != null)
            {
                    setInventorySlotContents(slot, null);
            }
            
            return stack;
    }
    
    @Override
    public int getInventoryStackLimit() {
            return 64;
    }

    @Override
    public boolean isUseableByPlayer(EntityPlayer player)
    {
            return worldObj.getBlockTileEntity(xCoord, yCoord, zCoord) == this &&
            player.getDistanceSq(xCoord + 0.5, yCoord + 0.5, zCoord + 0.5) < 64;
    }

    @Override
    public void openChest() {}

    @Override
    public void closeChest() {}

    @Override
    public String getInvName()
    {
            return "Space workbench";
    }

@Override
public ItemStack getStackInSlot(int i)
{
	return inv[i];
}

@Override
public boolean isInvNameLocalized()
{
	return false;
}

@Override
public boolean isItemValidForSlot(int i, ItemStack itemstack)
{
	return false;
}

public double getEnergy()
{
	return this.energy;
}

public double getMaxEnergy()
{
	return this.maxenergy;
}
}

 

If I click on the block, then displays the correct value.

 

public boolean onBlockActivated(World par1World, int par2, int par3, int par4, EntityPlayer par5EntityPlayer, int par6, float par7, float par8, float par9)
    {
	SCoreEntityTileWorkbench tile = (SCoreEntityTileWorkbench) par1World.getBlockTileEntity(par2, par3, par4);

	if(tile == null)
	{
		par5EntityPlayer.addChatMessage("Tile is null");
		return true;
	}

	if(par5EntityPlayer.isSneaking() && tile != null)
	{
		par5EntityPlayer.addChatMessage("Current energy: " + tile.getEnergy());
		return true;
	}

	par5EntityPlayer.openGui(SpaceCore.instance, SCoreConfigManager.idGuiWorkbench, par1World, par2, par3, par4);

        return false;
    }

 

But if I will put it in the Gui always displays 0.

 

package nuclear.mods.atisot.space.client.gui;

import java.util.ArrayList;
import java.util.List;

import net.minecraft.client.gui.inventory.GuiContainer;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.StatCollector;
import nuclear.mods.atisot.space.SLog;
import nuclear.mods.atisot.space.SpaceCore;
import nuclear.mods.atisot.space.inventory.SCoreWorkbenchContainer;
import nuclear.mods.atisot.space.tile.SCoreEntityTileWorkbench;

import org.lwjgl.opengl.GL11;

public class SCoreWorkbenchGui extends SCoreGuiContainer {

private static final ResourceLocation distributorTexture = new ResourceLocation(SpaceCore.ASSET_DOMAIN, "textures/gui/spaceworkbench.png");

private SCoreInfoRegion electricInfoRegion = new SCoreInfoRegion((this.width - this.xSize) / 2 + 4, (this.height - this.ySize) / 2 + 55, 4, 60, new ArrayList<String>(), this.width, this.height);

SCoreEntityTileWorkbench workbench;

    public SCoreWorkbenchGui(InventoryPlayer inventoryPlayer, SCoreEntityTileWorkbench tileEntity){
    	super(new SCoreWorkbenchContainer(inventoryPlayer, tileEntity));
    	this.workbench = tileEntity;
    	this.ySize = 160;
}
    
    @Override
    public void initGui()
    {
        super.initGui();
        
        List<String> electricityDesc = new ArrayList<String>();
        electricityDesc.add("Electrical Storage");
        electricityDesc.add("Energy: " + ((int) Math.floor(this.workbench.getEnergy()) + " / " + (int) Math.floor(this.workbench.getMaxEnergy())));
        this.electricInfoRegion.tooltipStrings = electricityDesc;
        this.electricInfoRegion.xPosition = (this.width - this.xSize) / 2 + 13;
        this.electricInfoRegion.yPosition = (this.height - this.ySize) / 2 + 16;
        this.electricInfoRegion.parentWidth = this.width;
        this.electricInfoRegion.parentHeight = this.height;
        this.infoRegions.add(this.electricInfoRegion);
    }
    
    
    @Override
    protected void drawGuiContainerForegroundLayer(int param1, int param2) {
            //draw text and stuff here
            //the parameters for drawString are: string, x, y, color
            fontRenderer.drawString("Space Workbench", 30, 10, 4210752);
            //draws "Inventory" or your regional equivalent
            fontRenderer.drawString(StatCollector.translateToLocal("container.inventory"), 30, ySize - 90 + 2, 4210752);
    }

    @Override
    protected void drawGuiContainerBackgroundLayer(float par1, int par2, int par3) {    	
    	GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);
        this.mc.getTextureManager().bindTexture(distributorTexture);
        final int var5 = (this.width - this.xSize) / 2;
        final int var6 = (this.height - this.ySize) / 2;
        this.drawTexturedModalRect(var5, var6 + 5, 0, 0, this.xSize, 181);
        
        if (this.workbench != null)
        {
        	int scale = this.workbench.getScaledLevel(55);
        	
        	SLog.info("" + scale);
        	SLog.info("" + this.workbench.getEnergy());
        	
            this.drawTexturedModalRect(var5 + 12, var6 + 20, 176, 0, Math.min(scale, 55), 7);
            
//            if (this.workbench.getEnergy() > 0)
//            {
//            	this.drawTexturedModalRect(var5 + 12, var6 + 20, 176, 0, 4, 55);
//            }
//            
            
        }
    } 

}

 

width=180 height=102http://s24.postimg.org/9wzykqbj5/2014_03_17_19_41_08.png[/img]

 

width=180 height=102http://s24.postimg.org/yc86lsag1/2014_03_17_19_41_11.png[/img]

Link to comment
Share on other sites

Guest
This topic is now closed to further replies.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.