Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

[1.7.2] Shift Clicking In Custom Workbench Creates Ghost Items [SOLVED]


Esophose
 Share

Recommended Posts

As you can see from the title, the following code I am using creates ghost items in my custom workbench.

Things to note:

[*]No ghost items are created upon shift clicking on the output slot

[*]I have left comments on pretty much every line for the shift clicking, if I described anything wrong please let me know what the correct comment should be

[*]I am almost 100% sure the ghost items are being created in the transferStackInSlot method

ContainerUpgradeTable

 

package com.gamc.naturalupgrades.table;

import com.gamc.naturalupgrades.NaturalUpgrades;

import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.inventory.Container;
import net.minecraft.inventory.IInventory;
import net.minecraft.inventory.InventoryCraftResult;
import net.minecraft.inventory.InventoryCrafting;
import net.minecraft.inventory.Slot;
import net.minecraft.inventory.SlotCrafting;
import net.minecraft.item.ItemStack;
import net.minecraft.world.World;

public class ContainerUpgradeTable extends Container {

// Notes for important slots and their locations
final int INPUT_1 = 0, INPUT_2 = 1, INPUT_3 = 2, INPUT_4 = 3, OUTPUT = 4;
final int INV_START = 5, INV_END = 31, HOTBAR_START = 32, HOTBAR_END = 40;

public InventoryCrafting craftMatrix;
public IInventory craftResult;
private World worldObj;
private int posX;
private int posY;
private int posZ;

public ContainerUpgradeTable(InventoryPlayer invPlayer, World world, int x, int y, int z){
	craftMatrix = new InventoryCrafting(this, 2, 2);
	craftResult = new InventoryCraftResult();
	worldObj = world;
	posX = x;
	posY = y;
	posZ = z;

	// Slots where you put the items to make the end result
	this.addSlotToContainer(new Slot(this.craftMatrix, INPUT_1, 44 + 0 * 18, 22 + 0 * 18));
	this.addSlotToContainer(new Slot(this.craftMatrix, INPUT_2, 62 + 0 * 18, 22 + 1 * 18));
	this.addSlotToContainer(new Slot(this.craftMatrix, INPUT_3, 44 + 0 * 18, 22 + 2 * 18));
	this.addSlotToContainer(new Slot(this.craftMatrix, INPUT_4, 26 + 0 * 18, -14 + 3 * 18));
	// Slot where the end result of the recipe goes
	this.addSlotToContainer(new SlotCrafting(invPlayer.player, craftMatrix, craftResult, OUTPUT, 116, 39));
	// Slots for player inventory
	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, 96 + i * 18));
		}
	}
	// Slots for player hotbar
	for (int i = 0; i < 9; i++) {
		this.addSlotToContainer(new Slot(invPlayer, i, 8 + i * 18, 154));
	}
	onCraftMatrixChanged(craftMatrix);
}
@Override
public void onContainerClosed(EntityPlayer par1EntityPlayer)
    {
        super.onContainerClosed(par1EntityPlayer);
        
        if (!this.worldObj.isRemote)
        {
            for (int i = 0; i < 4; ++i)
            {
                ItemStack itemstack = this.craftMatrix.getStackInSlotOnClosing(i);
                
                if (itemstack != null)
                {
                    par1EntityPlayer.dropPlayerItemWithRandomChoice(itemstack, false);
                }
                
            }
        }
    }

public void onCraftMatrixChanged(IInventory iinventory){
	craftResult.setInventorySlotContents(0, UpgradeTableCraftingManager.getInstance().findMatchingRecipe(craftMatrix, worldObj));
}

@Override
public boolean canInteractWith(EntityPlayer player) {
	if(worldObj.getBlock(posX, posY, posZ) != NaturalUpgrades.upgradeTable){
		return true;
	}else{
		return player.getDistanceSq((double)posX + 0.5D, (double)posY + 0.5D, (double)posZ + 0.5D) <= 64.0D;
	}
}

// Set shift-clicking for this container and ensure the game doesn't crash when trying to do so
    public ItemStack transferStackInSlot(EntityPlayer par1EntityPlayer, int parSlot)
    {
    	// Prepare the itemstack
    	ItemStack itemstack = null;
    	// Save the slot you clicked
        Slot slot = (Slot)this.inventorySlots.get(parSlot);
        // Make sure the slot you clicked actually has something there
        if (slot != null && slot.getHasStack())
        {
        	// Set the itemstack
            ItemStack itemstack1 = slot.getStack();
            // Make a copy of the itemstack
            itemstack = itemstack1.copy();

            // Check if the shift click occurred in the output slot
            if (parSlot == OUTPUT)
            {
            	// Try to move the clicked item to somewhere starting on the hotbar
                if (!this.mergeItemStack(itemstack1, INV_START, HOTBAR_END + 1, true))
                {
                	// Give up on life and stop here
                    return null;
                }
                // If itemstack has more items than itemstack1, look for a different itemstack
                slot.onSlotChange(itemstack1, itemstack);
            }
            // Was the click anywhere outside of the inventory slots?
            else if (parSlot >= INV_START && parSlot < INV_END)
            {
            	// Try to move the clicked item into the hotbar
                if (!this.mergeItemStack(itemstack1, HOTBAR_START, HOTBAR_END, false))
                {
                	// Give up on life and stop here
                    return null;
                }
            }
            // Was the click anywhere outside of the hotbar slots?
            else if (parSlot >= HOTBAR_START && parSlot < HOTBAR_END)
            {
            	// Try to move the clicked item into the inventory
                if (!this.mergeItemStack(itemstack1, INV_START, INV_END, false))
                {
                	// Give up on life and stop here
                    return null;
                }
            }
            // Try to move the item anywhere possible in the player's inventory
            else if (!this.mergeItemStack(itemstack1, INV_START, HOTBAR_END, false))
            {
            	// Give up on life and stop here
                return null;
            }
            // If itemstack1's stacksize is the same as output put a null stack
            if (itemstack1.stackSize == OUTPUT)
            {
            	// Put a null stack
                slot.putStack((ItemStack)null);
            }
            else
            {
            	// Let the game know the slot has changed
                slot.onSlotChanged();
            }
            // Check if the two itemstacks' sizes are equal
            if (itemstack1.stackSize == itemstack.stackSize)
            {
            	// Give up on life and stop here
                return null;
            }
         	// Let the game know the slot has changed
            slot.onPickupFromSlot(par1EntityPlayer, itemstack1);
        }
        // Success?
        return itemstack;
    }
}

 

 

If you require anything else from me, please let me know.

 

Thank you for your time :)

 

EDIT: Solved by simply adding slot.onSlotChanged(); after setting the itemstack to null when the itemstack has reached a size of 0.

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
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.

 Share



×
×
  • Create New...

Important Information

By using this site, you agree to our Privacy Policy.