Jump to content

NBT not working.[1.7.2]


gmod622

Recommended Posts

Hello, I have created a furnace type block, with 2 input slots. Everything works fine, then I added a power varible. I saved it to NBT, then read it, but when I place down 2 Charging Station, both power levels read the same.

 

If you need any more code or infomation, just say.

 

Here is a small video of whats happening:

 

https://www.youtube.com/watch?v=AIOpvkqamOc

 

Tile Entity:

 

 

 package com.ProcessingCraft.main.block.tileentity;

import net.minecraft.block.Block;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Blocks;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.Item;
import net.minecraft.item.ItemBlock;
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 com.ProcessingCraft.main.main_ProcessCraft;
import com.ProcessingCraft.main.block.recipes.ChargingStationRecipes;

import cpw.mods.fml.common.registry.GameRegistry;

public class TileEntityChargingStation extends TileEntity implements IInventory
{
private ItemStack blastItemStacks[];
public int dualBurnTime;
public int currentItemBurnTime;
public int dualCookTime;
public static double power;
public int progressBar;
private String customName;
public TileEntityChargingStation()
{
         blastItemStacks = new ItemStack[4];
         dualBurnTime = 0;
         currentItemBurnTime = 0;
         dualCookTime = 0;
         power = 0;
         progressBar = 1000;
}
/**
         * Returns the number of slots in the inventory.
         */
public int getSizeInventory()
{
         return blastItemStacks.length;
}
/**
         * Returns the stack in slot i
         */
public ItemStack getStackInSlot(int i)
{
         return blastItemStacks[i];
}
public void setInventorySlotConatainers(int i, ItemStack itemstack)
{
         blastItemStacks[i] = itemstack;
         if (itemstack != null && itemstack.stackSize > getInventoryStackLimit())
         {
                 itemstack.stackSize = getInventoryStackLimit();
         }
}
/**
         * Reads a tile entity from NBT.
         */
public void readFromNBT(NBTTagCompound nbttagcompound)
{
         super.readFromNBT(nbttagcompound);
         NBTTagList nbttaglist = nbttagcompound.getTagList("Items", 10);
         blastItemStacks = new ItemStack[getSizeInventory()];
         for (int i = 0; i < nbttaglist.tagCount(); i++)
         {
                 NBTTagCompound nbttagcompound1 = (NBTTagCompound)nbttaglist.getCompoundTagAt(i);
                 byte byte0 = nbttagcompound1.getByte("Slot");
                 if (byte0 >= 0 && byte0 < blastItemStacks.length)
                 {
                         blastItemStacks[byte0] = ItemStack.loadItemStackFromNBT(nbttagcompound1);
                 }
         }
         dualBurnTime = nbttagcompound.getShort("BurnTime");
         dualCookTime = nbttagcompound.getShort("CookTime");
         power = nbttagcompound.getDouble("power");
         currentItemBurnTime = getItemBurnTime(blastItemStacks[1]);
}
/**
         * Writes a tile entity to NBT.
         */
public void writeToNBT(NBTTagCompound nbttagcompound)
{
         super.writeToNBT(nbttagcompound);
         nbttagcompound.setShort("BurnTime", (short)dualBurnTime);
         nbttagcompound.setShort("CookTime", (short)dualCookTime);
         nbttagcompound.setDouble("power", power);
         NBTTagList nbttaglist = new NBTTagList();
         for (int i = 0; i < blastItemStacks.length; i++)
         {
                 if (blastItemStacks[i] != null)
                 {
                         NBTTagCompound nbttagcompound1 = new NBTTagCompound();
                         nbttagcompound1.setByte("Slot", (byte)i);
                         blastItemStacks[i].writeToNBT(nbttagcompound1);
                         nbttaglist.appendTag(nbttagcompound1);
                 }
         }
         nbttagcompound.setTag("Items", nbttaglist);
}
/**
         * Returns the maximum stack size for a inventory slot. Seems to always be 64, possibly will be extended. *Isn't
         * this more of a set than a get?*
         */
public int getInventoryStackLimit()
{
         return 64;
}
public int getCookProgressScaled(int i)
{
         return (dualCookTime * i) / progressBar;
}
public int getBurnTimeRemainingScaled(int i)
{
         if (currentItemBurnTime == 0)
         {
                 currentItemBurnTime = 200;
         }
         return (dualBurnTime * i) / currentItemBurnTime;
}
public boolean isBurning()
{
         return dualBurnTime > 0;
}
/**
         * Allows the entity to update its state. Overridden in most subclasses, e.g. the mob spawner uses this to count
         * ticks and creates a new spawn inside its implementation.
         */
public void updateEntity()
{
         boolean flag = dualBurnTime > 0;
         boolean flag1 = false;
         if (dualBurnTime > 0)
         {
                 dualBurnTime--;
         }
         if (!worldObj.isRemote)
         {
                 if (dualBurnTime == 0 && canSmelt())
                 {
                         currentItemBurnTime = dualBurnTime = getItemBurnTime(blastItemStacks[2]);
                         if (dualBurnTime > 0)
                         {
                                 flag1 = true;
                                 if (blastItemStacks[2] != null)
                                 {
                                         if (blastItemStacks[2].stackSize == 0)
                                         {
                                                 blastItemStacks[2] = new ItemStack(blastItemStacks[2].getItem().setFull3D());
                                         }
                                         else
                                         {
                                                 blastItemStacks[2].stackSize--;
                                         }
                                         if (blastItemStacks[2].stackSize == 0)
                                         {
                                                 blastItemStacks[2] = null;
                                         }
                                 }
                         }
                 }
                 if (isBurning() && canSmelt())
                 {
                         dualCookTime++;
                         if (dualCookTime == progressBar)
                         {
                                 dualCookTime = 0;
                                 smeltItem();
                                 flag1 = true;
                         }
                 }
                 else
                 {
                         dualCookTime = 0;
                 }
                 if (flag != (dualBurnTime > 0))
                 {
                         flag1 = true;
                 }
         }
         if (flag1)
         {
                 this.markDirty();
         }
         
         if(power <= 0) {
        	 power = 0;
         }
         if(power >= 50) {
        	 power = 50;
         }
         if(this.worldObj.getTileEntity(this.xCoord+1, this.yCoord, this.zCoord) != null) {
        		if(this.worldObj.getTileEntity(this.xCoord+1, this.yCoord, this.zCoord) instanceof TileEntitySolarPannel) {
        			//can send data
        			power += 0.01;
        		}
        }
         if(this.worldObj.getTileEntity(this.xCoord-1, this.yCoord, this.zCoord) != null) {
     		if(this.worldObj.getTileEntity(this.xCoord-1, this.yCoord, this.zCoord) instanceof TileEntitySolarPannel) {
     			//can send data
     			power += 0.01;
     		}
     }
         if(this.worldObj.getTileEntity(this.xCoord, this.yCoord+1, this.zCoord) != null) {
      		if(this.worldObj.getTileEntity(this.xCoord, this.yCoord+1, this.zCoord) instanceof TileEntitySolarPannel) {
      			//can send data
      			power += 0.01;
      		}
      }
         if(this.worldObj.getTileEntity(this.xCoord, this.yCoord-1, this.zCoord) != null) {
       		if(this.worldObj.getTileEntity(this.xCoord, this.yCoord-1, this.zCoord) instanceof TileEntitySolarPannel) {
       			//can send data
       			power += 0.01;
       		}
       }
         if(this.worldObj.getTileEntity(this.xCoord, this.yCoord, this.zCoord+1) != null) {
       		if(this.worldObj.getTileEntity(this.xCoord, this.yCoord, this.zCoord+1) instanceof TileEntitySolarPannel) {
       			//can send data
       			power += 0.01;
       		}
       }
         if(this.worldObj.getTileEntity(this.xCoord, this.yCoord, this.zCoord-1) != null) {
       		if(this.worldObj.getTileEntity(this.xCoord, this.yCoord, this.zCoord-1) instanceof TileEntitySolarPannel) {
       			//can send data
       			power += 0.01;
       		}
       }
}
private boolean canSmelt()
{
         if (blastItemStacks[0] == null || blastItemStacks[1] == null)
         {
                 return false;
         }
         ItemStack itemstack = ChargingStationRecipes.getSmeltingResult(blastItemStacks[0].getItem(), blastItemStacks[1].getItem());
         if (itemstack == null)
         {
                 return false;
         }
         if (blastItemStacks[3] == null)
         {
                 return true;
         }
         if (!blastItemStacks[3].isItemEqual(itemstack))
         {
                 return false;
         }
         if (blastItemStacks[3].stackSize < getInventoryStackLimit() && blastItemStacks[3].stackSize < blastItemStacks[3].getMaxStackSize())
         {
                 return true;
         }
         else
         {
                 return blastItemStacks[3].stackSize < itemstack.getMaxStackSize();
         }
}

public void onBlockDestroyedByPlayer(World p_149664_1_, int p_149664_2_, int p_149664_3_, int p_149664_4_, int p_149664_5_) {
power = 0;
}


public void smeltItem()
{
if(power >= 2) {
         if (!canSmelt())
         {
                 return;
         }
         ItemStack itemstack = ChargingStationRecipes.getSmeltingResult(blastItemStacks[0].getItem(), blastItemStacks[1].getItem());
         if (blastItemStacks[3] == null)
         {
                 blastItemStacks[3] = itemstack.copy();
         }
         else if (blastItemStacks[3] == itemstack)
         {
                 blastItemStacks[3].stackSize++;
         }
         for (int i = 0; i < 2; i++)
         {
                 if (blastItemStacks[i].stackSize <= 0)
                 {
                         blastItemStacks[i] = new ItemStack(blastItemStacks[i].getItem().setFull3D());
                 }
                 else
                 {
                         blastItemStacks[i].stackSize--;
                 }
                 if (blastItemStacks[i].stackSize <= 0)
                 {
                         blastItemStacks[i] = null;
                 }
         }
}
}

private int getItemBurnTime(ItemStack itemstack)
{
         if (itemstack == null)
         {
                 return 0;
         }

         Item item = itemstack.getItem();
         
         if(item instanceof ItemBlock && Block.getBlockFromItem(item) != Blocks.air) {
         Block block = Block.getBlockFromItem(item);
        	 
         
         if(block == Blocks.redstone_block) {
             if(power >= 2) {
            	 power -= 1.2;
            	 return 1050;
             }
         }
         
         }
	return GameRegistry.getFuelValue(itemstack);
}
/**
         * Do not make give this method the name canInteractWith because it clashes with Container
         */
public boolean isUseableByPlayer(EntityPlayer entityplayer)
{
         if (worldObj.getTileEntity(xCoord, yCoord, zCoord) != this)
         {
                 return false;
         }
         else
         {
                 return entityplayer.getDistanceSq((double)xCoord + 0.5D, (double)yCoord + 0.5D, (double)zCoord + 0.5D) <= 64D;
         }
}
/**
         * Decrease the size of the stack in slot (first int arg) by the amount of the second int arg. Returns the new
         * stack.
         */
public ItemStack decrStackSize(int i, int j)
{
         if (blastItemStacks[i] != null)
         {
                 if (blastItemStacks[i].stackSize <= j)
                 {
                         ItemStack itemstack = blastItemStacks[i];
                         blastItemStacks[i] = null;
                         return itemstack;
                 }
                 ItemStack itemstack1 = blastItemStacks[i].splitStack(j);
                 if (blastItemStacks[i].stackSize == 0)
                 {
                         blastItemStacks[i] = null;
                 }
                 return itemstack1;
         }
         else
         {
                 return null;
         }
}
/**
         * Sets the given item stack to the specified slot in the inventory (can be crafting or armor sections).
         */
public void setInventorySlotContents(int i, ItemStack itemstack)
{
         blastItemStacks[i] = itemstack;
         if (itemstack != null && itemstack.stackSize > getInventoryStackLimit())
         {
                 itemstack.stackSize = getInventoryStackLimit();
         }
}
/**
         * Returns the name of the inventory.
         */
public String getInventoryName()
{
         return "container.chargingStation";
}
public void openInventory()
{
}
public void closeInventory()
{
}
/**
         * When some containers are closed they call this on each slot, then drop whatever it returns as an EntityItem -
         * like when you close a workbench GUI.
         */
public ItemStack getStackInSlotOnClosing(int i)
{
         if (blastItemStacks[i] != null)
         {
                 ItemStack itemstack = blastItemStacks[i];
                 blastItemStacks[i] = null;
                 return itemstack;
         }
         else
         {
                 return null;
         }
}
public boolean hasCustomInventoryName()
{
         return (this.customName != null) && (this.customName.length() > 0);
}
public void setCustomName(String name)
{
         this.customName = name;
}
public boolean isItemValidForSlot(int i, ItemStack itemstack) {
return false;
}

}

 

 

 

 

 

 

Not new to java >> New to modding.

Link to comment
Share on other sites

Don't have time to read it in depth right now, I'll look later.

 

Two things jumped out at me quick.

Why do you have the 'static' designation on your power?  There isn't a need.

Are you sure 'power' isn't accidentaly used by minecraft already?

Long time Bukkit & Forge Programmer

Happy to try and help

Link to comment
Share on other sites

Thanks for the help, but i need the variable to be static, in my GUI class it prints the power lvl:

 

        fontRendererObj.drawString(StatCollector.translateToLocal("Power (DP): " + (short)TileEntityChargingStation.power, 75, 65, 0x404040);

 

Not new to java >> New to modding.

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.