Jump to content

[1.7.10] Problems with TileEntity - Slots/ItemStacks and NBT


Recommended Posts

Posted

Problems:

1) Slots/ItemStacks

Got a TileEntity with functioning GUI and Container.  If the player clicks on a spot looking like a button in the GUI (buttonID=13, triggering 'importMoney()') the money (items) from slot with index 0 (TileEntityATM.inventory[0]) should get set to 'null' and the variable holding the amount of money the currently using player has (moneyBankBalance) should get added up by the amount of money inserted (til here works fine). But after reopening the GUI the ItemStack reappears in slot 0 and the variable 'moneyBankBalance' is set to 0.

 

2) NBT

By using the (entityPlayer.)getEntityData().set/getInteger(<parameter>)-command i want to write and read the variable 'moneyBankBalance' to the player data. That doesn't seem to work at all.

 

What did i forget/make wrong? Please help!

 

All related files:

 

TileEntityATM

 

 

public class TileEntityATM extends TileEntity implements IInventory

{

 

    private GuiButtonHelperATM helper = new GuiButtonHelperATM();

    public GuiButtonHelperATM getHelper(){return helper;}

 

    public BasicSink atm_ic2EnergySink = new BasicSink(this, 128, 1);

 

    public int buttonClicked_ID = -1;

    public int moneyBankBalance = 0;

    public int moneyToExport = 0;

    public int maxOutput = 0;

 

    private String inventoryName = "container.atm";

    private boolean inventoryHasName = true;

    private int inventorySlotAmount = 3;

    private ItemStack[] inventory = new ItemStack[inventorySlotAmount];

 

    public void resetAllVars()

    {

        buttonClicked_ID = -1;

        moneyBankBalance = 0;

        moneyToExport = 0;

    }

 

    public TileEntityATM ()

    {}

 

    public void invalidate() {

        atm_ic2EnergySink.invalidate();

        super.invalidate();

    }

 

    public void onChunkUnload() {

        atm_ic2EnergySink.onChunkUnload();

    }

 

    public void updateEntity() {

        atm_ic2EnergySink.updateEntity();

        boolean max = maxOutput==0 ? false : true;

 

        //recharge energy sink

        if(atm_ic2EnergySink.getEnergyStored()<atm_ic2EnergySink.getCapacity())

        {

            if(inventory[1]!=null)

            {

                if(inventory[1].getItem()!=Items.redstone)

                {

                    if(inventory[1].getItem() instanceof IElectricItem)

                    {

                        atm_ic2EnergySink.discharge(inventory[1], 1);

                        this.markDirty();

                    }

                }

                else

                {

                    if(atm_ic2EnergySink.getEnergyStored()<=(atm_ic2EnergySink.getCapacity()-32))

                    {

                        inventory[1].stackSize -= 1;

                        atm_ic2EnergySink.setEnergyStored(atm_ic2EnergySink.getEnergyStored()+32);

 

                        if(inventory[1].stackSize==0)

                        {

                            inventory[1] = null;

                        }

 

                        this.markDirty();

                    }

                }

            }

        }

        //--------------------

        //Button check

        if(buttonClicked_ID!=-1)

        {

            switch(buttonClicked_ID)

            {

                case 0: addAmountToExport(10000);

                    break;

                case 1: addAmountToExport(1000);

                    break;

                case 2: addAmountToExport(100);

                    break;

                case 3: addAmountToExport(10);

                    break;

                case 4: addAmountToExport(1);

                    break;

                case 5: remAmountToExport(10000);

                    break;

                case 6: remAmountToExport(1000);

                    break;

                case 7: remAmountToExport(100);

                    break;

                case 8: remAmountToExport(10);

                    break;

                case 9: remAmountToExport(1);

                    break;

                case 10: resAmountToExport();

                    break;

                case 11: maxValMoneyOutput();

                    break;

                case 12: minValMoneyOutput();

                    break;

                case 13: importMoney();

                    break;

                case 14: exportMoney();

                    break;

            }

            this.markDirty();

        }

 

        //reset vars

        buttonClicked_ID = -1;

    }

 

    public boolean isItemStackEnergySource(ItemStack stack)

    {

        return stack.getItem() instanceof IElectricItem || stack.getItem()==Items.redstone;

    }

 

    //Gui Money Display Helper Methods

    public double getMoneyBankBalance()

    {

        return this.moneyBankBalance/10;

    }

    public double getMoneyToExport()

    {

        return this.moneyToExport/10;

    }

    //--------------------------------

 

    //Button helper methods

    public void addAmountToExport(int amount)

    {

        if(checkAmount(amount))

        {

            moneyToExport += amount;

        }

    }

    public void remAmountToExport(int amount)

    {

        if(checkAmount(amount))

        {

            moneyToExport -= amount;

        }

 

        if(moneyToExport<0)

        {

            moneyToExport = 0;

        }

    }

    public boolean checkAmount(int amount)

    {

        return amount%10==0 || amount==1;

    }

    public void resAmountToExport()

    {

        moneyToExport = 0;

    }

 

    public void maxValMoneyOutput()

    {

        maxOutput = 1;

    }

    public void minValMoneyOutput()

    {

        maxOutput = 0;

    }

 

    public void exportMoney(){}

    public void importMoney()

    {

        if(inventory[0]!=null)

        {

            if(inventory[0].stackSize!=0)

            {

                if(inventory[0].getItem() instanceof ItemNote)

                {

                    ItemNote item = (ItemNote)inventory[0].getItem();

                    int amount = inventory[0].stackSize;

 

 

                    moneyBankBalance += (item.getValue()*amount)*10;

                    inventory[0].stackSize = 0;

                    inventory[0] = null;

                }

                else if(inventory[0].getItem() instanceof ItemCoin)

                {

                    ItemCoin item = (ItemCoin)inventory[0].getItem();

                    int amount = inventory[0].stackSize;

 

                    moneyBankBalance += (item.getValue()*amount)*10;

                    inventory[0].stackSize = 0;

                    inventory[0] = null;

                }

            }

            else

            {

                inventory[0] = null;

            }

        }

    }

    //---------------------

 

    //Inventory Methods

    public int getSizeInventory()

    {

        return inventorySlotAmount;

    }

 

    public ItemStack getStackInSlot(int slotIndex)

    {

        return slotIndex>=0 && slotIndex<=getSizeInventory() ? inventory[slotIndex] : null;

    }

 

    public ItemStack decrStackSize(int slotIndex, int amountToRemove)

    {

        if(inventory[slotIndex]!=null)

        {

            ItemStack retItemStack;

 

            if(inventory[slotIndex].stackSize<=amountToRemove)

            {

                retItemStack = inventory[slotIndex];

                inventory[slotIndex] = null;

                this.markDirty();

                return retItemStack;

            }

            else

            {

                retItemStack = inventory[slotIndex].splitStack(amountToRemove);

 

                if(inventory[slotIndex].stackSize==0)

                {

                    inventory[slotIndex] = null;

                }

 

                this.markDirty();

                return retItemStack;

            }

        }

        else

        {

            return null;

        }

    }

 

    public ItemStack getStackInSlotOnClosing(int slotIndex)

    {

        if (this.inventory[slotIndex] != null)

        {

            ItemStack itemstack = this.inventory[slotIndex];

            this.inventory[slotIndex] = null;

            return itemstack;

        }

        else

        {

            return null;

        }

    }

 

    public void setInventorySlotContents(int slotIndex, ItemStack toSetToItemStack)

    {

        this.inventory[slotIndex] = toSetToItemStack;

 

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

        {

            toSetToItemStack.stackSize = this.getInventoryStackLimit();

        }

 

        this.markDirty();

    }

 

    public String getInventoryName()

    {

        return inventoryName;

    }

 

    public boolean hasCustomInventoryName()

    {

        return this.inventoryHasName;

    }

 

    public int getInventoryStackLimit()

    {

        return 64;

    }

 

    public boolean isUseableByPlayer(EntityPlayer p_70300_1_)

    {

        return this.worldObj.getTileEntity(this.xCoord, this.yCoord, this.zCoord) != this ? false : p_70300_1_.getDistanceSq((double)this.xCoord + 0.5D, (double)this.yCoord + 0.5D, (double)this.zCoord + 0.5D) <= 64.0D;

    }

 

    public void openInventory() {}

 

    public void closeInventory() {resetAllVars();}

 

    public boolean isItemValidForSlot(int slotIndex, ItemStack toCheckItemStack)

    {

        boolean ret = false;

        if(slotIndex==0)

        {

            ret = toCheckItemStack.getItem() instanceof ItemNote || toCheckItemStack.getItem() instanceof ItemCoin;

        }

        else if(slotIndex==1)

        {

            ret = toCheckItemStack.getItem() instanceof IElectricItem || toCheckItemStack.getItem()== Items.redstone;

        }

        else if(slotIndex==2)

        {

            ret = false;

        }

        else

        {

            ret = false;

        }

        return ret;

    }

    //-----------------

 

    public void readFromNBT(NBTTagCompound tag) {

        super.readFromNBT(tag);

        atm_ic2EnergySink.readFromNBT(tag);

        //nbt read management

        NBTTagList nbtTagList = tag.getTagList("Items", 10);

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

 

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

        {

            NBTTagCompound nbtTagCompound = nbtTagList.getCompoundTagAt(i);

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

 

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

            {

                this.inventory[b0] = ItemStack.loadItemStackFromNBT(nbtTagCompound);

            }

        }

    }

 

    public void writeToNBT(NBTTagCompound tag) {

        super.writeToNBT(tag);

        atm_ic2EnergySink.writeToNBT(tag);

        //nbt write management

        NBTTagList nbtTagList = new NBTTagList();

 

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

        {

            if (this.inventory != null)

            {

                NBTTagCompound nbtTagCompound = new NBTTagCompound();

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

                this.inventory.writeToNBT(nbtTagCompound);

                nbtTagList.appendTag(nbtTagCompound);

            }

        }

 

        tag.setTag("Items", nbtTagList);

    }

 

 

}

 

 

 

GuiATM

 

 

public class GuiATM extends GuiContainer

{

    private static final ResourceLocation guiTexture = new ResourceLocation(Reference.getGui("atm/main"));

    private TileEntityATM tileEntity;

    private EntityPlayer player;

 

    public GuiATM(InventoryPlayer inventoryPlayer, TileEntityATM tileEntityATM)

    {

        super(new ContainerATM(inventoryPlayer, tileEntityATM));

        tileEntity = tileEntityATM;

        player = inventoryPlayer.player;

        tileEntity.moneyBankBalance = player.getEntityData().getInteger("bankBalance");

    }

 

    protected void drawGuiContainerForegroundLayer(int i1, int i2)

    {

        this.xSize = 176;

        this.ySize = 244;

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

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

        this.fontRendererObj.drawString(I18n.format("container.atm", new Object[0]), this.xSize / 2 - this.fontRendererObj.getStringWidth(I18n.format("container.atm", new Object[0])) / 2, 8 -39,4210752);

        this.fontRendererObj.drawString(I18n.format("container.inventory", new Object[0]), 62, 156-39, 4210752);

 

        this.fontRendererObj.drawString(player.getDisplayName()+":", 10, 20-39, 4210752);

        this.fontRendererObj.drawString("Bank Balance: "+tileEntity.getMoneyBankBalance(), 15, 31-39, 4210752);

        this.fontRendererObj.drawString("Withdraw: "+tileEntity.getMoneyToExport(), 15, 42-39, 4210752);

    }

 

    protected void drawGuiContainerBackgroundLayer(float f1, int i1, int i2)

    {

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

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

        this.xSize = 176;

        this.ySize = 244;

        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 h1 = (int)(this.tileEntity.atm_ic2EnergySink.getEnergyStored()/2);

        int h2 = (int)(this.tileEntity.atm_ic2EnergySink.getCapacity()/2);

        this.drawTexturedModalRect(k + 27, l + 101 + (h2-h1), k + 177, l + (h2-h1), 16, h1);

 

        //Energy Amount Window

        int i = Mouse.getEventX() * this.width / this.mc.displayWidth;

        int j = this.height - Mouse.getEventY() * this.height / this.mc.displayHeight - 1;

 

        if(i>=(k+27) && i<=(k+42))

        {

            if(j>=(l+101) && j<=(l+164))

            {

                func_146283_a(Arrays.asList(new String[]{"Energy: "+this.tileEntity.atm_ic2EnergySink.getEnergyStored()+" EU"}), i, j);

            }

        }

        //--------------------

        player.getEntityData().setInteger("bankBalance", tileEntity.moneyBankBalance);

    }

 

    public void mouseClicked(int x, int y, int mouseButtonIndex)

    {

        this.xSize = 176;

        this.ySize = 244;

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

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

        super.mouseClicked(x, y, mouseButtonIndex);

        int index = this.tileEntity.getHelper().searchForClick((x-k), (y-l));

        this.tileEntity.buttonClicked_ID = index;

    }

 

    public void onGuiClosed() {

        super.onGuiClosed();

 

        player.getEntityData().setInteger("bankBalance", tileEntity.moneyBankBalance);

        tileEntity.resetAllVars();

    }

}

 

 

 

ContainerATM

 

 

public class ContainerATM extends Container

{

 

    private final int ATM_INPU = 0;

    private final int ATM_FUEL = 1;

    private final int ATM_OUTP = 2;

 

    private final int INV_STA = 3;

    private final int INV_END = 29;

 

    private final int HOT_STA = 30;

    private final int HOT_END = 38;

 

 

    private TileEntityATM tileEntityATM;

    private int lastButtonClicked_ID;

    private int lastMoneyBankBalance;

    private int lastMoneyToExport;

    private int lastMaxOutput;

 

    public ContainerATM(InventoryPlayer inventoryPlayer, TileEntityATM tileEntityATM)

    {

        this.tileEntityATM = tileEntityATM;

 

        this.addSlotToContainer(new Slot(tileEntityATM, 0, 80, 76));

        this.addSlotToContainer(new Slot(tileEntityATM, 1, 8, 109));

        this.addSlotToContainer(new SlotFurnace(inventoryPlayer.player, tileEntityATM, 2, 134, 76));

 

 

        int i;

 

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

        {

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

            {

                this.addSlotToContainer(new Slot(inventoryPlayer, j + i * 9 + 9, 8 + j * 18, 129 + i * 18));

            }

        }

 

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

        {

            this.addSlotToContainer(new Slot(inventoryPlayer, i, 8 + i * 18, 185));

        }

 

    }

 

    public void addCraftingToCrafters(ICrafting iCrafting)

    {

        super.addCraftingToCrafters(iCrafting);

        iCrafting.sendProgressBarUpdate(this, 0, this.lastButtonClicked_ID);

        iCrafting.sendProgressBarUpdate(this, 1, this.lastMoneyBankBalance);

        iCrafting.sendProgressBarUpdate(this, 2, this.lastMoneyToExport);

        iCrafting.sendProgressBarUpdate(this, 3, this.lastMaxOutput);

    }

 

    public void detectAndSendChanges()

    {

        super.detectAndSendChanges();

 

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

        {

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

 

            if(this.lastButtonClicked_ID != this.tileEntityATM.buttonClicked_ID)

            {

                iCrafting.sendProgressBarUpdate(this, 0, this.tileEntityATM.buttonClicked_ID);

            }

 

            if(this.lastMoneyBankBalance != this.tileEntityATM.moneyBankBalance)

            {

                iCrafting.sendProgressBarUpdate(this, 1, this.tileEntityATM.moneyBankBalance);

            }

 

            if(this.lastMoneyToExport != this.tileEntityATM.moneyToExport)

            {

                iCrafting.sendProgressBarUpdate(this, 2, this.tileEntityATM.moneyToExport);

            }

 

            if(this.lastMaxOutput != this.tileEntityATM.maxOutput)

            {

                iCrafting.sendProgressBarUpdate(this, 3, this.tileEntityATM.maxOutput);

            }

        }

 

        this.lastButtonClicked_ID = this.tileEntityATM.buttonClicked_ID;

        this.lastMoneyBankBalance = this.tileEntityATM.moneyBankBalance;

        this.lastMoneyToExport = this.tileEntityATM.moneyToExport;

        this.lastMaxOutput = this.tileEntityATM.maxOutput;

    }

 

    public void updateProgressBar(int varIndex, int newVal)

    {

        if(varIndex==0)

        {

            this.lastButtonClicked_ID = newVal;

        }

 

        if(varIndex==1)

        {

            this.lastMoneyBankBalance = newVal;

        }

 

        if(varIndex==2)

        {

            this.lastMoneyToExport = newVal;

        }

 

        if(varIndex==3)

        {

            this.lastMaxOutput = newVal;

        }

    }

 

    public boolean canInteractWith(EntityPlayer entityPlayer)

    {

        return this.tileEntityATM.isUseableByPlayer(entityPlayer);

    }

 

    public ItemStack transferStackInSlot(EntityPlayer entityPlayer, int slotIndex)

    {

        ItemStack itemStack = null;

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

 

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

        {

            ItemStack itemStack1 = slot.getStack();

            itemStack = itemStack1.copy();

 

            if(slotIndex==ATM_OUTP)

            {

                if(!this.mergeItemStack(itemStack1, INV_STA, HOT_END+1, true)){return null;}

                slot.onSlotChange(itemStack1, itemStack);

            }

            else if(slotIndex!=ATM_INPU && slotIndex!=ATM_FUEL)

            {

                if (itemStack1.getItem() instanceof ItemNote || itemStack1.getItem() instanceof ItemCoin)

                {

                    if(!this.mergeItemStack(itemStack1, ATM_INPU, ATM_INPU+1, false)){return null;}

                }

                else if (tileEntityATM.isItemStackEnergySource(itemStack1))

                {

                    if(!this.mergeItemStack(itemStack1, ATM_FUEL, ATM_FUEL+1, false)){return null;}

                }

                else if (slotIndex>=INV_STA && slotIndex<=INV_END)

                {

                    if(!this.mergeItemStack(itemStack1, HOT_STA, HOT_END, false)){return null;}

                }

                else if (slotIndex>=HOT_STA && slotIndex<=HOT_END)

                {

                    if(!this.mergeItemStack(itemStack1, INV_STA, INV_END, false)){return null;}

                }

            }

            else if(!this.mergeItemStack(itemStack1, INV_STA, HOT_END+1, false))

            {

                return null;

            }

 

            if(itemStack1.stackSize==0)

            {

                slot.putStack((ItemStack)null);

            }

            else

            {

                slot.onSlotChanged();

            }

 

            if(itemStack1.stackSize==itemStack.stackSize)

            {

                return null;

            }

 

            slot.onPickupFromSlot(entityPlayer, itemStack1);

        }

 

        return itemStack;

    }

}

 

 

The TileEntitys are Going to kill us all and take over the entire (Minecraft) world!

Posted

I don't use the GuiScreen to perform the action. My gui provides an integer to handle in the 'updateEntity()' method, synchronized by the container (works). But my problem is: after the action is performed in the updateEntity() (works): if i reopen the gui the ItemStacks are reset. (correct me please if i am completely false)

 

EDIT: I think the problem might be in the 'importMoney()' Method (look TileEntityATM)

The TileEntitys are Going to kill us all and take over the entire (Minecraft) world!

Posted

Take a look at GuiATM.mouseClicked() and TileEntityATM.updateEntity(). I use the variable 'buttonClicked_ID'

The TileEntitys are Going to kill us all and take over the entire (Minecraft) world!

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.