Jump to content

Sending packets for itemstacks in tile entities?


Zeretul4

Recommended Posts

Hey everyone, I'm still working on my custom furnace and I almost have it working. It has multiple outputs and for some reason the GUI is being really glitchy about it. I'm having issues similar to when certain variables were no updating and I worked it out by sending packets in the container class. Well now the itemstacks are acting similarly.

 

The way it works is that a single item goes in through the input slot, then one of several possible outputs are randomly chosen to go into the first available output slot of 15. When i throw a stack in and let it process, what looks like is happening is that 2 are coming out from one item but only one of them is real. I know it's working fine because as soon as i try to take something out or save and quit minecraft, the whole inventory fixes itself and gets rid of all the fake items. I'm pretty sure the issue is with nbt data not being sent to the GUI, all I really need to know is how to update the output slots like i did with other variables. Thanks!

Link to comment
Share on other sites

Please show the code of your TileEntity and ContainerBlock please

 

Sure thing!

 

Tile Entity

package zeretul4.geology.grinder;

import java.util.HashMap;

import zeretul4.geology.Geology;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import net.minecraft.block.Block;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.tileentity.TileEntity;

public class TileEntityGrinder extends TileEntity implements IInventory {

        private ItemStack[] inv;
        public int grindTime = 0;
        public int bladeLevel = 0;
        public int fuelLevel = 0;
        public int burnTime = 0;
        public final int maxFuel = 10000;

        public TileEntityGrinder(){
                inv = new ItemStack[18];
        }
        
        @Override
        public void updateEntity() {
        	
        	bladeLevel = 0;
        	if (getStackInSlot(0) != null){
        		int tempLevel = getBladeLevel();
        		if (tempLevel != -1)
        			bladeLevel = tempLevel;
        	}
        	
        	if(burnTime > 0){
        		if (fuelLevel < maxFuel)
        			fuelLevel++;
        		burnTime--;
        	}        	
        	else if (fuelLevel < maxFuel && getStackInSlot(2) != null){
        		burnTime = 1000;
        		decrStackSize(2, 1);        		        		
        	} 
        	
        	if (fuelLevel > 0 && canGrind()){
        		fuelLevel--;
        		grindTime += 2;
        		if (grindTime >= 200){
        			grindTime = 0;
        			grindItem();
        		}
        	}
        	else
        		grindTime = 0;
        	
        }
        
        private int getBladeLevel(){
        	ItemStack item = getStackInSlot(0);
        	if (item == null)
        		return -1;
        	if (item.itemID == Geology.cobbleBlade.itemID)
        		return 1;
        	return -1;
        }
        
        private boolean checkIfFuel(){
        	ItemStack item = getStackInSlot(2);
        	if (item == null)
        		return false;
        	int id = item.itemID;
        	if (id == Item.coal.itemID)
        		return true;
        	
        	return false;
        }
        
        
        public boolean canGrind(){
        	
        	ItemStack input = getStackInSlot(1);
        	
        	if (!RecipesGrinder.checkRecipeExists(input)) // recipes does not exist
        		return false;
        	if (checkIfRoomForOutput()) // if there is room for any outputs
        		return true;
        		
        	return false;
        }
        
        private boolean checkIfRoomForOutput(){
        	for (int i = 0; i < 15; i++){
        		ItemStack stack = getStackInSlot(i + 3);
        		if (stack == null)
        			return true;
        	}
        	return false;
        }
        
        private void grindItem(){
        	ItemStack input = getStackInSlot(1).copy(); // get the input
        	ItemStack output = RecipesGrinder.getOutputRandom(input, false);
		decrStackSize(1, 1);// RecipesGrinder.getInput(input).stackSize);

		int outputIndex = 2;
		boolean foundIndex = false;
		while(!foundIndex){
			outputIndex++;
			if (getStackInSlot(outputIndex) == null){ // just set if empty
				setInventorySlotContents(outputIndex, output.copy());
				foundIndex = true;
			}
			else if (getStackInSlot(outputIndex).itemID == output.itemID){ // otherwise increase by proper amount
				getStackInSlot(outputIndex).stackSize++;// += output.stackSize;
				foundIndex = true;
			}
		}		

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

        @Override
        public ItemStack getStackInSlot(int slot) {
                return inv[slot];
        }
       
        @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 void readFromNBT(NBTTagCompound tagCompound) {
        	// working code for setting itemstacks, commenting this out breaks the furnace so i know this is being called
                super.readFromNBT(tagCompound);
                NBTTagList tagList = tagCompound.getTagList("Inventory");
                for (int i = 0; i < tagList.tagCount(); i++) {
                        NBTTagCompound tag = (NBTTagCompound) tagList.tagAt(i);
                        byte slot = tag.getByte("Slot");
                        if (slot >= 0 && slot < inv.length) {
                                inv[slot] = ItemStack.loadItemStackFromNBT(tag);
                        }
                } //(ends here)
                

                this.fuelLevel = tagCompound.getInteger("Fuel");
                this.burnTime = tagCompound.getInteger("BurnTime");
                this.bladeLevel = tagCompound.getInteger("BladeLevel");
                this.grindTime = tagCompound.getInteger("GrindTime");
        }

        @Override
        public void writeToNBT(NBTTagCompound tagCompound) {
                super.writeToNBT(tagCompound);
                
                tagCompound.setInteger("Fuel", fuelLevel);
                tagCompound.setInteger("BurnTime", burnTime);
                tagCompound.setInteger("BladeLevel", bladeLevel);
                tagCompound.setInteger("GrindTime", grindTime);
                               
                NBTTagList itemList = new NBTTagList();
                for (int i = 0; i < inv.length; i++) {
                        ItemStack stack = inv[i];
                        if (stack != null) {
                                NBTTagCompound tag = new NBTTagCompound();
                                tag.setByte("Slot", (byte) i);
                                stack.writeToNBT(tag);
                                itemList.appendTag(tag);
                        }
                }
                tagCompound.setTag("Inventory", itemList);        

        } 

        @Override
        public String getInvName() {
                return "zeretul4.geology.grinder.tileEntityGrinder";
        }
        
        @SideOnly(Side.CLIENT)
        public int getGrindTimeScaled(int par1) {
            return this.grindTime * par1 / 200;
        }
        
        @SideOnly(Side.CLIENT)
        public int getBurnTimeScaled(int par1) {
            return this.burnTime * par1 / 1000;
        }
}

 

and container

package zeretul4.geology.grinder;

import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.inventory.Container;
import net.minecraft.inventory.ICrafting;
import net.minecraft.inventory.Slot;
import net.minecraft.item.ItemStack;

public class ContainerGrinder extends Container {

        protected TileEntityGrinder tileEntity;  
        private final static int slots = 18;
        private int lastFuelLevel = 0;
        private int lastBurnTime = 0;
        private int lastBladeLevel = 0;
        private int lastGrindTime = 0;
        private ItemStack[] lastOutputs = new ItemStack[15];

        public ContainerGrinder (InventoryPlayer inventoryPlayer, TileEntityGrinder te){
                tileEntity = te;

                                
                addSlotToContainer(new Slot(tileEntity, 0, 36, 14)); // blade slot
                addSlotToContainer(new Slot(tileEntity, 1, 36, 35)); // input
                addSlotToContainer(new Slot(tileEntity, 2, 36, 56)); // fuel
                
                // 15 output slots
                addSlotToContainer(new Slot(tileEntity, 3, 80, 17));
                addSlotToContainer(new Slot(tileEntity, 4, 98, 17));
                addSlotToContainer(new Slot(tileEntity, 5, 116, 17));
                addSlotToContainer(new Slot(tileEntity, 6, 134, 17));
                addSlotToContainer(new Slot(tileEntity, 7, 152, 17));
                addSlotToContainer(new Slot(tileEntity, 8, 80, 35));
                addSlotToContainer(new Slot(tileEntity, 9, 98, 35));
                addSlotToContainer(new Slot(tileEntity, 10, 116, 35));
                addSlotToContainer(new Slot(tileEntity, 11, 134, 35));
                addSlotToContainer(new Slot(tileEntity, 12, 152, 35));
                addSlotToContainer(new Slot(tileEntity, 13, 80, 53));
                addSlotToContainer(new Slot(tileEntity, 14, 98, 53));
                addSlotToContainer(new Slot(tileEntity, 15, 116, 53));
                addSlotToContainer(new Slot(tileEntity, 16, 134, 53));
                addSlotToContainer(new Slot(tileEntity, 17, 152, 53));

                //commonly used vanilla code that adds the player's inventory
                bindPlayerInventory(inventoryPlayer);
        }
        
        public void addCraftingToCrafters(ICrafting par1ICrafting)
        {
            super.addCraftingToCrafters(par1ICrafting);
            par1ICrafting.sendProgressBarUpdate(this, 0, this.tileEntity.fuelLevel);
            par1ICrafting.sendProgressBarUpdate(this, 1, this.tileEntity.burnTime);
            par1ICrafting.sendProgressBarUpdate(this, 2, this.tileEntity.bladeLevel);
            par1ICrafting.sendProgressBarUpdate(this, 3, this.tileEntity.grindTime);
            for (int i = 0; i < 15; i++){
            	par1ICrafting.sendSlotContents(this, i + 4, tileEntity.getStackInSlot(i + 3));
            }
        }
        
        public void detectAndSendChanges()
        {
            super.detectAndSendChanges();

            for (int var1 = 0; var1 < this.crafters.size(); ++var1)
            {
                ICrafting var2 = (ICrafting)this.crafters.get(var1);

                if (this.lastFuelLevel != this.tileEntity.fuelLevel)
                {
                    var2.sendProgressBarUpdate(this, 0, this.tileEntity.fuelLevel);
                }
                if (this.lastBurnTime != this.tileEntity.burnTime)
                {
                    var2.sendProgressBarUpdate(this, 1, this.tileEntity.burnTime);
                }
                if (this.lastBladeLevel != this.tileEntity.bladeLevel)
                {
                    var2.sendProgressBarUpdate(this, 2, this.tileEntity.bladeLevel);
                }
                if (this.lastBladeLevel != this.tileEntity.grindTime)
                {
                    var2.sendProgressBarUpdate(this, 3, this.tileEntity.grindTime);
                }

                for (int i = 0; i < 15; i++){
                	var2.sendSlotContents(this, i + 4, tileEntity.getStackInSlot(i + 3));
                }

            }

            this.lastFuelLevel = this.tileEntity.fuelLevel;
            this.lastBurnTime = this.tileEntity.burnTime;
            this.lastBladeLevel = this.tileEntity.bladeLevel;
            this.lastGrindTime = this.tileEntity.grindTime;
            for (int i = 0; i < 15; i++){
            	this.lastOutputs[i] = this.tileEntity.getStackInSlot(i + 3);
            }
        }
        
        @SideOnly(Side.CLIENT)
        public void updateProgressBar(int par1, int par2)
        {
            if (par1 == 0)
            {
                this.tileEntity.fuelLevel = par2;
            }
            if (par1 == 1)
            {
                this.tileEntity.burnTime = par2;
            }
            if (par1 == 2)
            {
                this.tileEntity.bladeLevel = par2;
            }
            if (par1 == 3)
            {
                this.tileEntity.grindTime = par2;
            }           
        }
        
      //  @SideOnly(Side.CLIENT)
       // public void updateProgressBar(int par1, ItemStack par2)
       // {
       // 	for (int i = 0; i < 15; i++){
        //    	if (par1 == i + 4)
         //   		this.tileEntity.setInventorySlotContents(i + 3, par2);
          //  }
       // }
        

        @Override
        public boolean canInteractWith(EntityPlayer player) {
                return tileEntity.isUseableByPlayer(player);
        }


        protected void bindPlayerInventory(InventoryPlayer inventoryPlayer) {
                for (int i = 0; i < 3; i++) {
                        for (int j = 0; j < 9; j++) {
                                addSlotToContainer(new Slot(inventoryPlayer, j + i * 9 + 9,
                                                8 + j * 18, 84 + i * 18));
                        }
                }
                for (int i = 0; i < 9; i++) {
                        addSlotToContainer(new Slot(inventoryPlayer, i, 8 + i * 18, 142));
                }
        }

        @Override
        public ItemStack transferStackInSlot(EntityPlayer player, int slot) {
                ItemStack stack = null;
                Slot slotObject = (Slot) inventorySlots.get(slot);

                //null checks and checks if the item can be stacked (maxStackSize > 1)
                if (slotObject != null && slotObject.getHasStack()) {
                        ItemStack stackInSlot = slotObject.getStack();
                        stack = stackInSlot.copy();

                        //merges the item into player inventory since its in the tileEntity
                        if (slot < slots) {
                                if (!this.mergeItemStack(stackInSlot, slots, 36 + slots, true)) {
                                        return null;
                                }
                        }
                        //places it into the tileEntity is possible since its in the player inventory
                        else if (!this.mergeItemStack(stackInSlot, 0, slots, false)) {
                                return null;
                        }

                        if (stackInSlot.stackSize == 0) {
                                slotObject.putStack(null);
                        } else {
                                slotObject.onSlotChanged();
                        }

                        if (stackInSlot.stackSize == stack.stackSize) {
                                return null;
                        }
                        slotObject.onPickupFromSlot(player, stackInSlot);
                }
                return stack;
        }
}

Link to comment
Share on other sites

I am not 100% sure but I think you need to call onInventoryChanged();

 

package zeretul4.geology.grinder;

import java.util.HashMap;

import zeretul4.geology.Geology;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import net.minecraft.block.Block;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.tileentity.TileEntity;

public class TileEntityGrinder extends TileEntity implements IInventory {


        
        @Override
        public void updateEntity() {
  boolean pleaseUpdateMe = false;
         if (fuelLevel > 0 && canGrind()){
          fuelLevel--;
          grindTime += 2;
          if (grindTime >= 200){
           grindTime = 0;
           grindItem();
               pleaseUpdateMe = true;
          }
         }
         else
          grindTime = 0;
         if(pleaseUpdateMe) onInventoryChanged();
        }
}

Link to comment
Share on other sites

I am not 100% sure but I think you need to call onInventoryChanged();

 

package zeretul4.geology.grinder;

import java.util.HashMap;

import zeretul4.geology.Geology;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import net.minecraft.block.Block;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.tileentity.TileEntity;

public class TileEntityGrinder extends TileEntity implements IInventory {


        
        @Override
        public void updateEntity() {
  boolean pleaseUpdateMe = false;
         if (fuelLevel > 0 && canGrind()){
          fuelLevel--;
          grindTime += 2;
          if (grindTime >= 200){
           grindTime = 0;
           grindItem();
               pleaseUpdateMe = true;
          }
         }
         else
          grindTime = 0;
         if(pleaseUpdateMe) onInventoryChanged();
        }
}

 

Hmm, this didn't do anything at all. I'm pretty sure it's an issue with the gui because I had a similar issue with ints doing their job, but not showing up on the gui. I got that worked out by sending int packets, I just don't know how to do the same for inventory slots.

 

Here's the gui code.

 

package zeretul4.geology.grinder;

import java.util.Random;

import net.minecraft.client.gui.inventory.GuiContainer;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.util.StatCollector;

import org.lwjgl.opengl.GL11;

import zeretul4.geology.Geology;

public class GuiGrinder extends GuiContainer {

	TileEntityGrinder te;
	String[] bladeQuality = { "", "Poor" };
	Random rand = new Random();

        public GuiGrinder (InventoryPlayer inventoryPlayer,
        		TileEntityGrinder tileEntity) {
                //the container is instanciated and passed to the superclass for handling
                super(new ContainerGrinder(inventoryPlayer, tileEntity));
                te = tileEntity;
        }

        @Override
        protected void drawGuiContainerForegroundLayer(int param1, int param2) {
                //draw text and stuff here
                //the parameters for drawString are: string, x, y, color
                fontRenderer.drawString("Grinder", 90, 4, 4210752); 
                fontRenderer.drawString("Blade: ", 4, 18, 4210752);
                fontRenderer.drawString(bladeQuality[te.bladeLevel], 4, 26, 4210752);
                fontRenderer.drawString("Fuel: ", 4, 38, 4210752);
                fontRenderer.drawString("" + te.fuelLevel, 4, 46, 4210752);
                //draws "Inventory" or your regional equivalent
                fontRenderer.drawString(StatCollector.translateToLocal("container.inventory"), 8, ySize - 94 + 2, 4210752);
        }

        @Override
        protected void drawGuiContainerBackgroundLayer(float par1, int par2,
                        int par3) {
                //draw your Gui here, only thing you need to change is the path
                int texture = mc.renderEngine.getTexture("/zeretul4/geology/grinder/grinder.png");
                GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);
                this.mc.renderEngine.bindTexture(texture);
                int x = (width - xSize) / 2;
                int y = (height - ySize) / 2;
                this.drawTexturedModalRect(x, y, 0, 0, xSize, ySize);
                
                int var7 = this.te.getGrindTimeScaled(24);
                this.drawTexturedModalRect(x + 54, y + 34, 176, 14, var7 + 1, 16);
                
                if (te.burnTime > 0){
                	int var8 = this.te.getBurnTimeScaled(12);
                	this.drawTexturedModalRect(x + 58, y + 57 + 12 - var8, 176, 12 - var8, 14, var8 + 2);
                }
        }

}

Link to comment
Share on other sites

I still think my first answer is correct except you need to detect if its server side or not

 

using

 

update things needed by your gui outside of the if statment below
if (!worldObj.isRemote)
	{
                      //do everything thats serverside here for example when grinding your item it needs to be in here
grindItem();
and then onInventoryChanged(); //on serverside this will update slots and send to client
                 }

Link to comment
Share on other sites

I still think my first answer is correct except you need to detect if its server side or not

 

using

 

update things needed by your gui outside of the if statment below
if (!worldObj.isRemote)
	{
                      //do everything thats serverside here for example when grinding your item it needs to be in here
grindItem();
and then onInventoryChanged(); //on serverside this will update slots and send to client
                 }

 

Yes! This fixed it. Thanks a lot for the help!

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.