Jump to content

Recommended Posts

Posted

Hello, I have a custom furnace gui. Every time a player opens the gui it creates a new instance, similar to the crafting table(each people has a separate gui and container). I have every thing working fine, but it has a bug, every time an item finishes smelting and the player takes it out it shows up in the player hand for a tick, but then disappears and turns back into the un-smelted form in slot that the original item goes back into.

I think this may have something to do with client and server side stuff. I'll post my code at the bottom of this post(sorry for the messy code, I have tried to fix the bug by trying weird thing, but they have not worked and now my code is a messy :)))

 

Furnace Inv.

 

 

package mod.xtronius.rc_mod.inventory;

 

import mod.xtronius.rc_mod.rc_mod;

import mod.xtronius.rc_mod.furnaceRecipies.RCCastFurnace1Recipes;

import mod.xtronius.rc_mod.furnaceRecipies.RCIngotFurnace1Recipes;

import mod.xtronius.rc_mod.handlers.ServerTickHandler;

import net.minecraft.entity.Entity;

import net.minecraft.entity.player.EntityPlayer;

import net.minecraft.entity.player.EntityPlayerMP;

import net.minecraft.inventory.IInventory;

import net.minecraft.item.Item;

import net.minecraft.item.ItemStack;

import net.minecraft.item.crafting.FurnaceRecipes;

import net.minecraft.nbt.NBTTagCompound;

import net.minecraft.nbt.NBTTagList;

import net.minecraft.world.World;

import net.minecraftforge.common.IExtendedEntityProperties;

 

public class InvFurnace1 implements IExtendedEntityProperties ,  IInventory

{

 

private static final int[] slots_side_left= new int[] {0};

    private static final int[] slots_mid = new int[] {1};

    private static final int[] slots_side_right = new int[] {2};

   

    public static final int SMELT_INGOT = 0;

    public static final int SMELT_CAST = 1;

    public static int  SMELT_TYPE;

    private ServerTickHandler tick = rc_mod.proxy.getServerTickHandler();

   

    private ItemStack[] furnaceItemStacks = new ItemStack[3];

   

    public int furnaceCookTime;

    private String currentPlayerName;

    private String displayName;

   

    private World world;

    private EntityPlayer player;

    private int XCoord;

    private int YCoord;

    private int ZCoord;

   

    private Item itemToBeSmelted;

   

public InvFurnace1(World world, EntityPlayer player, int x, int y, int z) {

 

this.world = world;

this.player = player;

this.XCoord = x;

this.YCoord = y;

this.ZCoord = z;

 

tick.furnaceGui1Map.put(player, this);

}

 

public Item getItemToBeSmelted() {

if(this.itemToBeSmelted != null)

return itemToBeSmelted;

else return Item.arrow;

}

 

public void setItemToBeSmelted(Item item) {

if(item != null)

this.itemToBeSmelted = item;

}

 

public void setXCoord(int xCoord) {

XCoord = xCoord;

}

 

public int getXCoord() {

return XCoord;

}

 

public void setYCoord(int yCoord) {

YCoord = yCoord;

}

 

public int getYCoord() {

return YCoord;

}

 

public void setZCoord(int ZCoord) {

ZCoord = ZCoord;

}

 

public int getZCoord() {

return ZCoord;

}

 

public boolean hasStackInSlots() {

 

if(tick != null)

tick.isOpen = true;

 

for (int i = 0; i < 2; i++) {

if(this.furnaceItemStacks != null) {

return true;

}

}

return false;

}

   

@Override

public int getSizeInventory() {

 

return this.furnaceItemStacks.length;

}

 

@Override

public ItemStack getStackInSlot(int i) {

 

return this.furnaceItemStacks;

}

 

@Override

public ItemStack decrStackSize(int i, int j) {

 

if (this.furnaceItemStacks != null)

        {

            ItemStack itemstack;

 

            if (this.furnaceItemStacks.stackSize <= j)

            {

                itemstack = this.furnaceItemStacks;

                this.furnaceItemStacks = null;

                return itemstack;

            }

            else

            {

                itemstack = this.furnaceItemStacks.splitStack(j);

 

                if (this.furnaceItemStacks.stackSize == 0)

                {}

                return itemstack;

            }

        }

        else

        {

            return null;

        }

}

 

@Override

public ItemStack getStackInSlotOnClosing(int i) {

 

if(tick != null)

tick.isOpen = false;

 

        if (this.furnaceItemStacks != null)

        {

            ItemStack itemstack = this.furnaceItemStacks;

            this.furnaceItemStacks = null;

            return itemstack;

        }

        else

        {

            return null;

        }

}

 

@Override

public void setInventorySlotContents(int i, ItemStack stack) {

this.furnaceItemStacks = stack;

       

        if (stack != null && stack.stackSize > this.getInventoryStackLimit())

        {

            stack.stackSize = this.getInventoryStackLimit();

        }

}

 

@Override

public String getInvName() {

 

return this.isInvNameLocalized() ? this.displayName : "container.furnace";

}

 

@Override

public boolean isInvNameLocalized() {

 

return this.displayName != null && this.displayName.length() > 0;

}

 

@Override

public int getInventoryStackLimit() {

 

return 1;

}

 

@Override

public void onInventoryChanged() {}

 

@Override

public boolean isUseableByPlayer(EntityPlayer entityplayer) {

 

return true;

}

 

@Override

public void openChest() {}

 

@Override

public void closeChest() {}

 

@Override

public boolean isItemValidForSlot(int i, ItemStack stack) {

 

if(i == 0 || i == 1)

    return true;

    else

    return false;

}

 

private boolean canSmelt()

    {

        if (this.furnaceItemStacks[0] == null)

        {

            return false;

        }

        else

        {

        ItemStack itemstack = RCIngotFurnace1Recipes.smelting().getSmeltingResult(this.furnaceItemStacks[0]);

    ItemStack itemstack2 = RCCastFurnace1Recipes.smelting().getSmeltingResult(this.furnaceItemStacks[1]);

       

        if (this.furnaceItemStacks[0] != null && this.furnaceItemStacks[1] == null) {

        SMELT_TYPE = SMELT_INGOT;

        }

       

        if (this.furnaceItemStacks[0] != null && this.furnaceItemStacks[1] != null) {

        SMELT_TYPE = SMELT_CAST;

        }

 

        if (SMELT_TYPE == SMELT_INGOT) {

        if (itemstack != null) {

                if (this.furnaceItemStacks[2] != null && this.furnaceItemStacks[2].isItemEqual(itemstack)) {

                int result = furnaceItemStacks[2].stackSize + itemstack.stackSize;

                return (result <= getInventoryStackLimit() && result <= itemstack.getMaxStackSize());

                } else if(this.furnaceItemStacks[2] == null){

                return true;

                }

        }

        }

        else if (SMELT_TYPE == SMELT_CAST) {

       

        if(itemstack != null && itemstack2 != null) {

       

        if(itemstack == itemstack2) {

        if (this.furnaceItemStacks[2] == null) return true;

                    if (!this.furnaceItemStacks[2].isItemEqual(itemstack)) return false;

                    int result = furnaceItemStacks[2].stackSize + itemstack.stackSize;

                    return (result <= getInventoryStackLimit() && result <= itemstack.getMaxStackSize());

            }

            else if(itemstack != itemstack){

            if (this.furnaceItemStacks[2] == null) return true;

                        if (!this.furnaceItemStacks[2].isItemEqual(itemstack)) return false;

                        int result = furnaceItemStacks[2].stackSize + itemstack.stackSize;

                        return (result <= getInventoryStackLimit() && result <= itemstack.getMaxStackSize());

            }

        }

        }

        }

return false;

    }

 

    public void smeltItem()

    {

        if (this.canSmelt())

        {

            ItemStack itemstack = FurnaceRecipes.smelting().getSmeltingResult(this.furnaceItemStacks[0]);

 

            this.setItemToBeSmelted(itemstack.getItem());

           

            if (this.furnaceItemStacks[2] == null)

            {

                this.furnaceItemStacks[2] = itemstack.copy();

            }

            else if (this.furnaceItemStacks[2].isItemEqual(itemstack))

            {

                furnaceItemStacks[2].stackSize += itemstack.stackSize;

            }

 

            --this.furnaceItemStacks[0].stackSize;

 

            if (this.furnaceItemStacks[0].stackSize <= 0)

            {

                this.furnaceItemStacks[0] = null;

            }

        }

    }

   

    public int getCookProgressScaled(int par1)

    {

        return this.furnaceCookTime * par1 / 200;

    }

 

@Override

public void saveNBTData(NBTTagCompound nbt) {

 

nbt.setShort("CookTime", (short)this.furnaceCookTime);

        NBTTagList nbttaglist = new NBTTagList();

 

        for (int i = 0; i < this.furnaceItemStacks.length; ++i)

        {

            if (this.furnaceItemStacks != null)

            {

                NBTTagCompound nbttagcompound1 = new NBTTagCompound();

                nbttagcompound1.setByte("Slot", (byte)i);

                this.furnaceItemStacks.writeToNBT(nbttagcompound1);

                nbttaglist.appendTag(nbttagcompound1);

            }

        }

 

        nbt.setTag("Items", nbttaglist);

 

        if (this.isInvNameLocalized())

        {

        nbt.setString("CustomName", this.displayName);

        }

}

 

@Override

public void loadNBTData(NBTTagCompound nbt) {

 

        NBTTagList nbttaglist = nbt.getTagList("Items");

        this.furnaceItemStacks = new ItemStack[this.getSizeInventory()];

 

        for (int i = 0; i < nbttaglist.tagCount(); ++i)

        {

            NBTTagCompound nbttagcompound1 = (NBTTagCompound)nbttaglist.tagAt(i);

            byte b0 = nbttagcompound1.getByte("Slot");

 

            if (b0 >= 0 && b0 < this.furnaceItemStacks.length)

            {

                this.furnaceItemStacks[b0] = ItemStack.loadItemStackFromNBT(nbttagcompound1);

            }

        }

 

        this.furnaceCookTime = nbt.getShort("CookTime");

 

        if (nbt.hasKey("CustomName"))

        {

            this.displayName = nbt.getString("CustomName");

        }

}

 

public void updateEntity()

    {

System.out.println("0");

        boolean flag1 = false;

 

//        if (!player.worldObj.isRemote)

//        {

        System.out.println("1");

            if (this.canSmelt())

            {

            System.out.println("2");

                ++this.furnaceCookTime;

 

                if (this.furnaceCookTime == 50)

                {

                System.out.println("3");

                    this.furnaceCookTime = 0;

                    this.smeltItem();

                    flag1 = true;

                }

            }

            else

            {

                this.furnaceCookTime = 0;

                System.out.println("4");

            }

 

        }

//    }

 

@Override

public void init(Entity entity, World world) {}

 

}

 

 

 

Furnace Container

 

 

package mod.xtronius.rc_mod.container;

 

import cpw.mods.fml.relauncher.Side;

import cpw.mods.fml.relauncher.SideOnly;

import mod.xtronius.rc_mod.inventory.InvFurnace1;

import mod.xtronius.rc_mod.tileEntity.slot.SlotFurnace1;

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.inventory.SlotFurnace;

import net.minecraft.item.ItemStack;

import net.minecraft.item.crafting.FurnaceRecipes;

import net.minecraft.tileentity.TileEntityFurnace;

 

public class Furnace1Container extends Container

{

private InvFurnace1 invFurnace;

    private int lastCookTime;

 

    public Furnace1Container(InventoryPlayer playerInv, InvFurnace1 inv)

    {

    this.invFurnace = inv;

        this.addSlotToContainer(new Slot(inv, 0, 60, 110));

        this.addSlotToContainer(new Slot(inv, 1, 88, 110));

        this.addSlotToContainer(new SlotFurnace1(playerInv.player, inv, 2, 156, 110));

       

        byte b0 = 36;

        short short1 = 137;

        int i;

 

        for (i = 0; i < 3; ++i)

        {

            for (int j = 0; j < 9; ++j)

            {

                this.addSlotToContainer(new Slot(playerInv, j + i * 9 + 9, b0 + j * 18, short1 + i * 18));

            }

        }

 

        for (i = 0; i < 9; ++i)

        {

            this.addSlotToContainer(new Slot(playerInv, i, b0 + i * 18, 58 + short1));

        }

    }

 

    public void addCraftingToCrafters(ICrafting par1ICrafting)

    {

        super.addCraftingToCrafters(par1ICrafting);

        par1ICrafting.sendProgressBarUpdate(this, 0, this.invFurnace.furnaceCookTime);

    }

 

    /**

    * Looks for changes made in the container, sends them to every listener.

    */

    public void detectAndSendChanges()

    {

        super.detectAndSendChanges();

 

        for (int i = 0; i < this.crafters.size(); ++i)

        {

            ICrafting icrafting = (ICrafting)this.crafters.get(i);

 

            if (this.lastCookTime != this.invFurnace.furnaceCookTime)

            {

                icrafting.sendProgressBarUpdate(this, 0, this.invFurnace.furnaceCookTime);

            }

        }

 

        this.lastCookTime = this.invFurnace.furnaceCookTime;

    }

 

    @SideOnly(Side.CLIENT)

    public void updateProgressBar(int par1, int par2)

    {

        if (par1 == 0)

        {

            this.invFurnace.furnaceCookTime = par2;

        }

    }

 

    public boolean canInteractWith(EntityPlayer par1EntityPlayer)

    {

        return this.invFurnace.isUseableByPlayer(par1EntityPlayer);

    }

   

    public void onContainerClosed(EntityPlayer player)

    {

        super.onContainerClosed(player);

 

        if (!player.worldObj.isRemote)

        {

            for (int i = 0; i < 3; ++i)

            {

                ItemStack itemstack = this.invFurnace.getStackInSlotOnClosing(i);

 

                if (itemstack != null)

                {

                    player.dropPlayerItem(itemstack);

                }

            }

        }

    }

 

    /**

    * Called when a player shift-clicks on a slot. You must override this or you will crash when someone does that.

    */

    public ItemStack transferStackInSlot(EntityPlayer par1EntityPlayer, int par2)

    {

        ItemStack itemstack = null;

        Slot slot = (Slot)this.inventorySlots.get(par2);

 

        if (slot != null && slot.getHasStack())

        {

            ItemStack itemstack1 = slot.getStack();

            itemstack = itemstack1.copy();

 

            if (par2 == 2)

            {

                if (!this.mergeItemStack(itemstack1, 3, 39, true))

                {

                    return null;

                }

 

                slot.onSlotChange(itemstack1, itemstack);

            }

            else if (par2 != 1 && par2 != 0)

            {

                if (FurnaceRecipes.smelting().getSmeltingResult(itemstack1) != null)

                {

                    if (!this.mergeItemStack(itemstack1, 0, 1, false))

                    {

                        return null;

                    }

                }

                else if (TileEntityFurnace.isItemFuel(itemstack1))

                {

                    if (!this.mergeItemStack(itemstack1, 1, 2, false))

                    {

                        return null;

                    }

                }

                else if (par2 >= 3 && par2 < 30)

                {

                    if (!this.mergeItemStack(itemstack1, 30, 39, false))

                    {

                        return null;

                    }

                }

                else if (par2 >= 30 && par2 < 39 && !this.mergeItemStack(itemstack1, 3, 30, false))

                {

                    return null;

                }

            }

            else if (!this.mergeItemStack(itemstack1, 3, 39, false))

            {

                return null;

            }

 

            if (itemstack1.stackSize == 0)

            {

                slot.putStack((ItemStack)null);

            }

            else

            {

                slot.onSlotChanged();

            }

 

            if (itemstack1.stackSize == itemstack.stackSize)

            {

                return null;

            }

 

            slot.onPickupFromSlot(par1EntityPlayer, itemstack1);

        }

 

        return itemstack;

    }

}

 

 

 

Furnace Gui

 

 

package mod.xtronius.rc_mod.gui;

 

import mod.xtronius.rc_mod.container.Furnace1Container;

import mod.xtronius.rc_mod.inventory.InvFurnace1;

import mod.xtronius.rc_mod.lib.Reference;

import net.minecraft.client.gui.inventory.GuiContainer;

import net.minecraft.client.resources.I18n;

import net.minecraft.entity.player.InventoryPlayer;

import net.minecraft.inventory.ContainerWorkbench;

import net.minecraft.item.Item;

import net.minecraft.item.ItemStack;

import net.minecraft.tileentity.TileEntity;

import net.minecraft.util.ResourceLocation;

import net.minecraft.world.World;

 

import org.lwjgl.opengl.GL11;

 

import cpw.mods.fml.relauncher.Side;

import cpw.mods.fml.relauncher.SideOnly;

 

@SideOnly(Side.CLIENT)

public class GuiFurnace1 extends GuiContainer

{

    private static final ResourceLocation furnaceGuiTextures = new ResourceLocation(Reference.MOD_Gui, "textures/gui/container/GuiFurnace1.png");

    private InvFurnace1 invFurnace;

 

    public GuiFurnace1(InventoryPlayer playerInv, InvFurnace1 inv)

    {

    super(new Furnace1Container(playerInv, inv));

        this.invFurnace = inv;

        this.xSize = 230;

        this.ySize = 219;

    }

 

    /**

    * Draw the foreground layer for the GuiContainer (everything in front of the items)

    */

    protected void drawGuiContainerForegroundLayer(int par1, int par2)

    {

        String s = invFurnace.isInvNameLocalized() ? invFurnace.getInvName() : I18n.getString(invFurnace.getInvName());

        this.fontRenderer.drawString(s, ((this.xSize / 2 - this.fontRenderer.getStringWidth(s) / 2) + 1), 6, 8355711);

        this.fontRenderer.drawString(I18n.getString("container.inventory"), 8, this.ySize - 96, 8355711);

    }

 

    /**

    * Draw the background layer for the GuiContainer (everything behind the items)

    */

    protected void drawGuiContainerBackgroundLayer(float x, int y, int z)

    {

        GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);

        this.mc.getTextureManager().bindTexture(furnaceGuiTextures);

        int k = (this.width - this.xSize) / 2;

        int l = (this.height - this.ySize) / 2;

        this.drawTexturedModalRect(k, l, 0, 0, this.xSize, this.ySize);

       

        int i1;

 

        i1 = this.invFurnace.getCookProgressScaled(22);

       

        //Progress Bar

        this.drawTexturedModalRect(k + 119, l + 111, 142, 221, i1 + 1, 30);

        itemRenderer.renderItemAndEffectIntoGUI(this.fontRenderer, this.mc.getTextureManager(), new ItemStack(this.invFurnace.getItemToBeSmelted()), k + 42, l + 109);

    }

}

 

 

Don't be afraid to ask question when modding, there are no stupid question! Unless you don't know java then all your questions are stupid!

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.