Jump to content

[1.7.2] Inventory problem


Trese759

Recommended Posts

Hi, when I try to take an item from inventory repositions it in the same slot not let me move. Where did I go wrong? I think there is a mistake in decrStackSize but can not find it.

 

public class TileEntitySuperGlueFurnace extends TileEntity implements IInventory {

private ItemStack slots[];

public int dualPower;
public int dualCookTime;
public static final int maxPower = 10000;
public static final int mashingSpeed = 100;

private static final int[] slots_top = new int[] {0, 1};
private static final int[] slots_bottom = new int[] {3};
private static final int[] slots_side = new int[] {2};

private String customName;

public TileEntitySuperGlueFurnace() {
	slots = new ItemStack[4];
}

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

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

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

@Override
public void setInventorySlotContents(int i, ItemStack itemstack) {
	slots[i] = itemstack;
	if (itemstack != null && itemstack.stackSize > getInventoryStackLimit()) {
		itemstack.stackSize = getInventoryStackLimit();
	}
}

public int[] getAccessibleSlotsFromSide(int i) {
	return i == 0 ? slots_bottom : (i == 1 ? slots_top : slots_side);
}




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

@Override
public boolean isUseableByPlayer(EntityPlayer player) {
	if (worldObj.getTileEntity(xCoord, yCoord, zCoord) != this) {
		return false;
	}else{
		return player.getDistanceSq((double)xCoord + 0.5D, (double)yCoord + 0.5D, (double)zCoord + 0.5D) <= 64;
	}
}

public void openInventory() {}
public void closeInventory() {}

@Override
public boolean isItemValidForSlot(int i, ItemStack itemstack) {
	return i == 2 ? false : (i == 1 ? hasItemPower(itemstack) : true);
}

public boolean hasItemPower(ItemStack itemstack) {
	return getItemPower(itemstack) > 0;
}

private static int getItemPower (ItemStack itemstack) {
	if (itemstack == null) {
		return 0;
	}else{
		Item item = itemstack.getItem();

		if (item == Megakron.tackyTyre) return 500;

		return 0;
	}
}

@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;
                return itemstack;
            }
            else
            {
                itemstack = this.slots[i].splitStack(j);

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

                return itemstack;
            }
        }
        else
        {
            return null;
        }
}
}

Link to comment
Share on other sites

You're going to need to post your GUI and Container class as well.

 

By the way,

if (worldObj.getTileEntity(xCoord, yCoord, zCoord) != this) {

That will never be true and you can remove it.

 

You may also want to add the writeToNBT and readFromNBT methods, along with getDescriptionPacket and handleDescriptionPacket (if I spelled those correctly).

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

Grazie, ho rimosso quella stringa, riposto il TileEntity completo e la Gui e Contaire class.

 

TileEntity

 

public class TileEntitySuperGlueFurnace extends TileEntity implements IInventory {

private ItemStack slots[];

public int dualPower;
public int dualCookTime;
public static final int maxPower = 10000;
public static final int mashingSpeed = 100;

private static final int[] slots_top = new int[] {0, 1};
private static final int[] slots_bottom = new int[] {3};
private static final int[] slots_side = new int[] {2};

private String customName;

public TileEntitySuperGlueFurnace() {
	slots = new ItemStack[4];
}

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

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

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

@Override
public void setInventorySlotContents(int i, ItemStack itemstack) {
	slots[i] = itemstack;
	if (itemstack != null && itemstack.stackSize > getInventoryStackLimit()) {
		itemstack.stackSize = getInventoryStackLimit();
	}
}

public int[] getAccessibleSlotsFromSide(int i) {
	return i == 0 ? slots_bottom : (i == 1 ? slots_top : slots_side);
}




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

@Override
public boolean isUseableByPlayer(EntityPlayer player) {
		return player.getDistanceSq((double)xCoord + 0.5D, (double)yCoord + 0.5D, (double)zCoord + 0.5D) <= 64;
}

public void openInventory() {}
public void closeInventory() {}

@Override
public boolean isItemValidForSlot(int i, ItemStack itemstack) {
	return i == 2 ? false : (i == 1 ? hasItemPower(itemstack) : true);
}

public boolean hasItemPower(ItemStack itemstack) {
	return getItemPower(itemstack) > 0;
}

private static int getItemPower (ItemStack itemstack) {
	if (itemstack == null) {
		return 0;
	}else{
		Item item = itemstack.getItem();

		if (item == Megakron.tackyTyre) return 500;

		return 0;
	}
}

@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;
                return itemstack;
            }
            else
            {
                itemstack = this.slots[i].splitStack(j);

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

                return itemstack;
            }
        }
        else
        {
            return null;
        }
}

public void readFromNBT (NBTTagCompound nbt) {
	super.readFromNBT(nbt);
	NBTTagList list = nbt.getTagList("Items", 10);
	slots = new ItemStack[getSizeInventory()];

	for (int i = 0; i < list.tagCount(); i++) {
		NBTTagCompound nbt1 = (NBTTagCompound)list.getCompoundTagAt(i);
		byte b0 = nbt1.getByte("Slot");

		if (b0 >= 0 && b0 < slots.length) {
			slots[b0] = ItemStack.loadItemStackFromNBT(nbt1);
		}
	}

	dualPower = nbt.getShort("PowerTime");
	dualCookTime = nbt.getShort("CookTime");
}

public void writeToNBT(NBTTagCompound nbt) {
	super.writeToNBT(nbt);
	nbt.setShort("PowerTime", (short)dualPower);
	nbt.setShort("CookTime", (short)dualCookTime);
	NBTTagList list = new NBTTagList();

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

	nbt.setTag("Items", list);
}


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

public boolean canInsertItem(int var1, ItemStack itemstack, int var3) {
	return this.isItemValidForSlot(var1, itemstack);
}

public boolean canExtractItem(int i, ItemStack itemstack, int j) {
	return j != 0 || i != 1 || itemstack.getItem() == Items.bucket;
}

@Override
public boolean hasCustomInventoryName() {
	return this.customName != null && this.customName.length() > 0;
}

public int getMasherProgressScaled(int i) {
	return (dualCookTime * i) / this.mashingSpeed;
}

public int getPowerRemainingScaled(int i) {
	return (dualPower * i) / maxPower;
}

private boolean canMash() {

	if (slots[0] == null || slots[1] == null) {
		return false;
	}

	ItemStack itemstack = SuperGlueFurnaceRecipes.getMashingResult(slots[0].getItem(), slots[1].getItem());

	if (itemstack == null) {
		return false;
	}

	if (slots[3] == null) {
		return true;
	}

	if (!slots[3].isItemEqual(itemstack)) {
		return false;
	}

	if (slots[3].stackSize < getInventoryStackLimit() && slots[3].stackSize < slots[3].getMaxStackSize()) {
		return true;
	}else{
		return slots[3].stackSize < itemstack.getMaxStackSize();
	}
}

private void mashItem() {
	if (canMash()) {
		ItemStack itemstack = SuperGlueFurnaceRecipes.getMashingResult(slots[0].getItem(), slots[1].getItem());

		if (slots[3] == null) {
			slots[3] = itemstack.copy();
		}else if (slots[3].isItemEqual(itemstack)) {
			slots[3].stackSize += itemstack.stackSize;
		}

		for (int i = 0; i < 2; i++) {
			if (slots[i].stackSize <= 0) {
				slots[i] = new ItemStack(slots[i].getItem().setFull3D());
			}else{
				slots[i].stackSize--;
			}

			if (slots[i].stackSize <= 0){
				slots[i] = null;
			}
		}
	}
}

public boolean hasPower() {
	return dualPower > 0;
}

public boolean isMashing() {
	return this.dualCookTime > 0;
}

public void updateEntity() {
	boolean flag = this.hasPower();
	boolean flag1= false;

	if(hasPower() && this.isMashing()) {
		this.dualPower--;
	}

	if(!worldObj.isRemote) {
		if (this.hasItemPower(this.slots[2]) && this.dualPower < (this.maxPower - this.getItemPower(this.slots[2]))) {
			this.dualPower += getItemPower(this.slots[2]);

			if(this.slots[2] != null) {
				flag1 = true;

				this.slots[2].stackSize--;

				if(this.slots[2].stackSize == 0) {
					this.slots[2] = this.slots[2].getItem().getContainerItem(this.slots[2]);
				}
			}
		}

		if (hasPower() && canMash()) {
			dualCookTime++;

			if (this.dualCookTime == this.mashingSpeed) {
				this.dualCookTime = 0;
				this.mashItem();
				flag1 = true;
			}
		}else{
			dualCookTime = 0;
		}

		if (flag != this.isMashing()) {
			flag1 = true;
			SuperGlueFurnace.updateBlockState(this.isMashing(), this.worldObj, this.xCoord, this.yCoord, this.zCoord);
		}
	}

	if (flag1) {
		this.markDirty();
	}
    }

}

 

Gui

 

public class GuiSuperGlueFurnace extends GuiContainer {

private ResourceLocation texture = new ResourceLocation("megakron:textures/gui/superGlueFurnace.png");
private TileEntitySuperGlueFurnace superGlueFurnace;

public GuiSuperGlueFurnace(InventoryPlayer invPlayer, TileEntitySuperGlueFurnace teSuperGlueFurnace) {
	super(new ContainerSuperGlueFurnace(invPlayer, teSuperGlueFurnace));

	superGlueFurnace = teSuperGlueFurnace;
}

protected void drawGuiContainerForegroundLayer(int i, int j) {

	String name = this.superGlueFurnace.hasCustomInventoryName() ? this.superGlueFurnace.getInventoryName() : I18n.format(this.superGlueFurnace.getInventoryName());

	this.fontRendererObj.drawString(name, this.xSize / 2 - this.fontRendererObj.getStringWidth(name) / 2, 6, 4210752);
	this.fontRendererObj.drawString(I18n.format(""), 8, this.ySize - 96 + 5, 4210752);

}

@Override
protected void drawGuiContainerBackgroundLayer(float var1, int var2, int var3) {

	GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);
	Minecraft.getMinecraft().getTextureManager().bindTexture(texture);
	drawTexturedModalRect(guiLeft, guiTop, 0, 0, xSize, ySize);

	if (superGlueFurnace.hasPower())
        {
            int i1 = superGlueFurnace.getPowerRemainingScaled(45);
            drawTexturedModalRect(guiLeft + 8, guiTop + 56 - i1, 176, 50 - i1, 2, i1);
        }

        int i1 = superGlueFurnace.getMasherProgressScaled(24);
        drawTexturedModalRect(guiLeft + 77, guiTop + 16, 176, 52, i1 + 1, 54);

}

}

 

ontainer

 

public class ContainerSuperGlueFurnace extends Container {

private TileEntitySuperGlueFurnace superGlue;
private int dualCookTime;
private int dualPower;
private int lastItemBurn;

public ContainerSuperGlueFurnace(InventoryPlayer invPlayer, TileEntitySuperGlueFurnace teSuperGlueFurnace) {

	dualCookTime = 0;
	dualPower = 0;
	lastItemBurn = 0;

	superGlue = teSuperGlueFurnace;

	this.addSlotToContainer(new Slot(teSuperGlueFurnace, 0, 56, 17));
	this.addSlotToContainer(new Slot(teSuperGlueFurnace, 1, 56, 53));
	this.addSlotToContainer(new Slot(teSuperGlueFurnace, 2, 8, 61));
	this.addSlotToContainer(new SlotSuperGlueFurnace(invPlayer.player, teSuperGlueFurnace, 3, 116, 35));

	for(int i = 0; i < 3; i++) {
		for(int j = 0; j < 9; j++) {
			this.addSlotToContainer(new Slot(invPlayer, j + i * 9 + 9, 8 + j * 18, 84 + i * 18));
		}
	}

	for(int i = 0; i < 9; i++) {
		this.addSlotToContainer(new Slot(invPlayer, i, 8 + i * 18, 142));
	}
}

public void addCraftingToCrafters(ICrafting crafting) {
	super.addCraftingToCrafters(crafting);
	crafting.sendProgressBarUpdate(this, 0, this.superGlue.dualCookTime);
	crafting.sendProgressBarUpdate(this, 1, this.superGlue.dualPower);
}

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;
    }	

@Override
public boolean canInteractWith(EntityPlayer player) {
	// TODO Auto-generated method stub
	return superGlue.isUseableByPlayer(player);
}

public void detectAndSendChanges()
    {
        super.detectAndSendChanges();

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

            if (this.dualCookTime != this.superGlue.dualCookTime)
            {
                icrafting.sendProgressBarUpdate(this, 0, this.superGlue.dualCookTime);
            }

            if (this.dualPower != this.superGlue.dualPower)
            {
                icrafting.sendProgressBarUpdate(this, 1, this.superGlue.dualPower);
            }
        }

        this.dualCookTime = this.superGlue.dualCookTime;
        this.dualPower = this.superGlue.dualPower;
    }

    @SideOnly(Side.CLIENT)
    public void updateProgressBar(int par1, int par2)
    {
        if (par1 == 0)
        {
            this.superGlue.dualCookTime = par2;
        }

        if (par1 == 1)
        {
            this.superGlue.dualPower = 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.