Jump to content

Solved [1.7.2] Update Tile Entity


Eractnod

Recommended Posts

I am trying to add a power storage bar to my solar furnace that can be bled off during the night or while raining to smelt items.  I have one variable called solarPower that should be used when I write it to the NBT and also when I send the scaled value to be displayed by the GUI.  In my updateEntity() method, the value for solarPower is being updated correctly when the sun is out and will be saved by the NBT, but it is not being used by my getPowerScaled() method.  If I change the updateEntity() method to have if(this.worldobj.isRemote) from (!this.worldobj.isRemote) then the progress bar on the GUI works, but it does not save the value.  Attached is the Tile Entity code I have.

I know it is not complete, but I am just trying to get the GUI and saves to work correctly.  And if it will help, the block class as it stands now is also attached.

What am I missing?  From the System.out's I have added, it looks like the solarPower is being updated for both the client and server.  I have tried with and without the @SideOnly.

 

package telvarianpipes.solarfurnace;

import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.ISidedInventory;
import net.minecraft.inventory.Slot;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.world.World;
import net.minecraft.world.biome.BiomeGenDesert;

public class TileEntitySolarFurnace extends TileEntity implements ISidedInventory{

private ItemStack[] slots = new ItemStack[2];

public static boolean sunIsVisible = false;


public int furnaceCookTime;
public int maxPower = 10000;
public int solarPower = 0;

public float powerPerTick = 5.0F;


public void updateEntity(){
	updateSunVisibility();

	if(!this.worldObj.isRemote){
		updateSolarPower();
	}

}


public int updateSolarPower(){
	if (sunIsVisible){
		this.solarPower+=this.powerPerTick;
		if(this.solarPower > this.maxPower) this.solarPower = this.maxPower;

		System.out.println ("sp " + this.solarPower);
	}
	return this.solarPower;
}
@SideOnly(Side.CLIENT)

    /**
     * Returns an integer between 0 and the passed value representing how close the current item is to being completely
     * cooked
     */
    public int getCookProgressScaled(int par1)
    {
    	//change this to increase or decrease speed.  Lower number is faster
        return this.furnaceCookTime * par1 / 100;
    }

//@SideOnly(Side.CLIENT)
public int getPowerScaled(int scaled){
	System.out.println("We getting here " + this.solarPower);
	return (this.solarPower * scaled / this.maxPower);

}

public void readFromNBT(NBTTagCompound nbt){
	super.readFromNBT(nbt);

	NBTTagList list = nbt.getTagList("Slots", 10);
	this.slots = new ItemStack[getSizeInventory()];

	for(int i = 0; i < list.tagCount(); i++){
		NBTTagCompound item = list.getCompoundTagAt(i);
		byte b = item.getByte("Item");

		if(b >= 0 && b < this.slots.length){
			this.slots[b] = ItemStack.loadItemStackFromNBT(item);
		}
	}
	this.solarPower = nbt.getInteger("Power");

	if(nbt.hasKey("CustomeName")){
		this.setInventoryName(nbt.getString("CustomerName"));
	}
}

public void writeToNBT(NBTTagCompound nbt){
	super.writeToNBT(nbt);
	System.out.println("Write sp " + this.solarPower);
	nbt.setInteger("Power", this.solarPower);

	NBTTagList list = new NBTTagList();
	for(int i = 0; i < this.slots.length; i++){
		if(this.slots[i] != null){
			NBTTagCompound item = new NBTTagCompound();
			item.setByte("Item", (byte)i);
			this.slots[i].writeToNBT(item);
			list.appendTag(item);
		}
	}

	nbt.setTag("Slots", list);

	if(this.hasCustomInventoryName()){
		nbt.setString("CustomName", this.getInventoryName());
	}

}

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

@Override
public ItemStack getStackInSlot(int i) {
	return this.slots[i];
}

@Override
public ItemStack decrStackSize(int i, int j) {
	if(this.slots[i] != null){
		ItemStack itemstack;

		if(this.slots[i].stackSize <= j){
			itemstack = this.slots[i];
			this.slots[i] = null;
		}else{
			itemstack = this.slots[i].splitStack(j);

			if(this.slots[i].stackSize == 0){
				this.slots[i] = null;
			}
		}
		return itemstack;
	}
	return null;
}

@Override
public ItemStack getStackInSlotOnClosing(int i) {
	if(this.slots[i] != null){
		ItemStack itemstack = this.slots[i];
		this.slots[i] = null;
		return itemstack;
	}
	return null;
}

@Override
public void setInventorySlotContents(int i, ItemStack itemstack) {
	this.slots[i] = itemstack;

	if(itemstack != null && itemstack.stackSize > this.getInventoryStackLimit()){
		itemstack.stackSize = this.getInventoryStackLimit();

	}
}

public void setInventoryName(String string){

}
@Override
public String getInventoryName() {
	return null;
}

@Override
public boolean hasCustomInventoryName() {
	// TODO Auto-generated method stub
	return false;
}

@Override
public int getInventoryStackLimit() {
	return 64;
}

@Override
public boolean isUseableByPlayer(EntityPlayer var1) {
	// TODO Auto-generated method stub
	return false;
}

@Override
public void openInventory() {
	// TODO Auto-generated method stub

}

@Override
public void closeInventory() {
	// TODO Auto-generated method stub

}

@Override
public boolean isItemValidForSlot(int var1, ItemStack var2) {
	// TODO Auto-generated method stub
	return false;
}

@Override
public int[] getAccessibleSlotsFromSide(int var1) {
	// TODO Auto-generated method stub
	return null;
}

@Override
public boolean canInsertItem(int var1, ItemStack var2, int var3) {
	// TODO Auto-generated method stub
	return false;
}

@Override
public boolean canExtractItem(int var1, ItemStack var2, int var3) {
	// TODO Auto-generated method stub
	return false;
}

   
    /**
     * Checks if the specified block is able to see the sky
     */
    public void updateSunVisibility()
    {
      this.sunIsVisible = isSunVisible(this.worldObj, this.xCoord, this.yCoord + 1, this.zCoord);
    }
    
    public static boolean isSunVisible(World world, int x, int y, int z)
    {
      return (world.isDaytime()) && (!world.provider.hasNoSky) && (world.canBlockSeeTheSky(x, y, z)) && (((world.getWorldChunkManager().getBiomeGenAt(x, z) instanceof BiomeGenDesert)) || ((!world.isRaining()) && (!world.isThundering())));
    }
  
    /**
     * Returns the number of ticks that the supplied fuel item will keep the furnace burning, or 0 if the item isn't
     * fuel
     */
    public static int getItemBurnTime(ItemStack par0ItemStack)
    {
        if (par0ItemStack == null) {
        	
        	if (sunIsVisible == true) {
        		
        		return 100;
        	}
        	
        	}
        return 0;
        
    }

}

 

package telvarianpipes.solarfurnace;

import telvarianpipes.TelvarianPipes;
import telvarianpipes.common.VarInit;
import cpw.mods.fml.common.network.internal.FMLNetworkHandler;
import net.minecraft.block.BlockContainer;
import net.minecraft.block.material.Material;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.world.World;

public class SolarFurnaceClass extends BlockContainer{

private static boolean keepFurnaceInventory;

public SolarFurnaceClass(Material material) {
	super(material);
	this.setCreativeTab(TelvarianPipes.tabTelvarianPipe);

}

public boolean onBlockActivated(World world, int x, int y, int z, EntityPlayer player, int side, float hitX, float hitY, float hitZ){

	if(!world.isRemote){
		FMLNetworkHandler.openGui(player, TelvarianPipes.instance, VarInit.guiIDSolarFurnace, world, x, y, z);

	}

	return true;
}


@Override
public TileEntity createNewTileEntity(World world, int var2) {
	return new TileEntitySolarFurnace();
}

}

Link to comment
Share on other sites

I am getting closer.  I added the following code and now the GUI data will update with what is saved in NBT.  However, the GUI still does not update in real time.  Still need some help on that part.

 

 

    public Packet getDescriptionPacket()
    {
    	NBTTagCompound tagCompound = new NBTTagCompound();
    	writeToNBT(tagCompound);
        return new S35PacketUpdateTileEntity(this.xCoord, this.yCoord, this.zCoord, this.solarPower, tagCompound);
    }
    
    public void onDataPacket(NetworkManager networkManager, S35PacketUpdateTileEntity packet){
    	readFromNBT(packet.func_148857_g());

    }

Link to comment
Share on other sites

Thank you Sir.  Have made the changes to the container class and now all is working as it should be.

Added these methods to the container class.  And removed what I had added to the Tile Entity.

 

public void addCraftingToCrafters(ICrafting icrafting)

 

public void detectAndSendChanges()

 

public void updateProgressBar(int par1, int par2)

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

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