Jump to content
View in the app

A better way to browse. Learn more.

Forge Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Problem With Shift-Clicking items into a GUI [1.12.2] [solved]

Featured Replies

Posted

I am having a problem where you cannot shift-click items into or out of a GUI

This is my Container code:

 

Spoiler

package com.kpzip.SushiMod.inventory.container;

import com.kpzip.SushiMod.blocks.tileentity.TileEntitySushiKnifeHolder;
import com.kpzip.SushiMod.inventory.slots.SlotSushiKnife;

import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.inventory.Container;
import net.minecraft.inventory.Slot;
import net.minecraft.item.ItemStack;

public class ContainerSushiKnifeHolder extends Container {
	private final int numRows;
	private final TileEntitySushiKnifeHolder knife;
	
	public ContainerSushiKnifeHolder(InventoryPlayer playerInv, TileEntitySushiKnifeHolder tileEntityBambooChest, EntityPlayer player) {
		this.knife = tileEntityBambooChest;
		this.numRows = tileEntityBambooChest.getSizeInventory() / 9;
		tileEntityBambooChest.openInventory(player);
		
		this.addSlotToContainer(new SlotSushiKnife(tileEntityBambooChest, 0, 80, 22));
		
		for(int y = 0; y < 3; y++)
		{
			for(int x = 0; x < 9; x++)
			{
				this.addSlotToContainer(new Slot(playerInv, x + y*9 + 9, 8 + x*18, 53 + y*18));
			}
		}
		
		for(int x = 0; x < 9; x++)
		{
			this.addSlotToContainer(new Slot(playerInv, x, 8 + x*18, 111));
		}
		
	}
	
	
	@Override
	public boolean canInteractWith(EntityPlayer playerIn) {
		return this.knife.isUsableByPlayer(playerIn);
				
	}
	
	@Override
	public void onContainerClosed(EntityPlayer playerIn) {
		super.onContainerClosed(playerIn);
		knife.closeInventory(playerIn);
	}
	
	@Override
    public ItemStack transferStackInSlot(EntityPlayer playerIn, int index)
    {
        ItemStack itemstack = ItemStack.EMPTY;
        Slot slot = this.inventorySlots.get(index);
 
        if (slot != null && slot.getHasStack())
        {
            ItemStack itemstack1 = slot.getStack();
            itemstack = itemstack1.copy();
 
            if (index < this.numRows * 9)
            {
                if (!this.mergeItemStack(itemstack1, this.numRows * 9, this.inventorySlots.size(), true))
                {
                    return ItemStack.EMPTY;
                }
            }
            else if (!this.mergeItemStack(itemstack1, 0, this.numRows * 9, false))
            {
                return ItemStack.EMPTY;
            }
 
            if (itemstack1.isEmpty())
            {
                slot.putStack(ItemStack.EMPTY);
            }
            else
            {
                slot.onSlotChanged();
            }
        }
 
        return itemstack;
    }
	
	public TileEntitySushiKnifeHolder getChestInventory() {
		return this.knife;
				
	}

}

 

This is my Tile Entity Code:

Spoiler

package com.kpzip.SushiMod.blocks.tileentity;

import com.kpzip.SushiMod.blocks.SushiKnifeHolder;
import com.kpzip.SushiMod.init.ModItems;
import com.kpzip.SushiMod.inventory.container.ContainerBambooChest;
import com.kpzip.SushiMod.inventory.container.ContainerSushiKnifeHolder;
import com.kpzip.SushiMod.util.Reference;

import net.minecraft.block.state.IBlockState;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.init.SoundEvents;
import net.minecraft.inventory.Container;
import net.minecraft.inventory.ItemStackHelper;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.tileentity.TileEntityLockableLoot;
import net.minecraft.util.ITickable;
import net.minecraft.util.NonNullList;
import net.minecraft.util.SoundCategory;
import net.minecraft.util.math.AxisAlignedBB;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;

public class TileEntitySushiKnifeHolder extends TileEntityLockableLoot implements ITickable {
	public NonNullList<ItemStack> knife = NonNullList.<ItemStack>withSize(1, ItemStack.EMPTY);
	public int numPlayersUsing, ticksSinceSync;

	@Override
	public int getSizeInventory() {
		return 1;
	}

	@Override
	public boolean isEmpty() {
		for (ItemStack stack : knife) {
			if (!stack.isEmpty()) {
				return true;
			}
		}
		return false;
	}

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

	@Override
	public String getName() {
		return this.hasCustomName() ? this.customName : "container.Sushi_Knife_Holder";
	}
	
	@Override
	public void readFromNBT(NBTTagCompound compound)
	{
		super.readFromNBT(compound);
		this.knife = NonNullList.<ItemStack>withSize(this.getSizeInventory(), ItemStack.EMPTY);
		
		if(!this.checkLootAndRead(compound)) ItemStackHelper.loadAllItems(compound, knife);
		if(compound.hasKey("CustomName", 8)) this.customName = compound.getString("CustomName");
	}
	
	@Override
	public NBTTagCompound writeToNBT(NBTTagCompound compound)
	{
		super.writeToNBT(compound);
		
		if(!this.checkLootAndWrite(compound)) ItemStackHelper.saveAllItems(compound, knife);
		if(compound.hasKey("CustomName", 8)) compound.setString("CustomName", this.customName);
		
		return compound;
	}

	@Override
	public Container createContainer(InventoryPlayer playerInventory, EntityPlayer playerIn) {
		
		return new ContainerSushiKnifeHolder(playerInventory, this, playerIn);
	}

	@Override
	public String getGuiID() {
		
		return Reference.MOD_ID + ":Sushi_Kinfe_Holder";
	}

	@Override
    public void update()
    {
        if (!this.world.isRemote && this.numPlayersUsing != 0 && (this.ticksSinceSync + pos.getX() + pos.getY() + pos.getZ()) % 200 == 0)
        {
            this.numPlayersUsing = 0;
 
            for (EntityPlayer entityplayer : this.world.getEntitiesWithinAABB(EntityPlayer.class, new AxisAlignedBB((double)((float)pos.getX() - 5.0F), (double)((float)pos.getY() - 5.0F), (double)((float)pos.getZ() - 5.0F), (double)((float)(pos.getX() + 1) + 5.0F), (double)((float)(pos.getY() + 1) + 5.0F), (double)((float)(pos.getZ() + 1) + 5.0F))))
            {
                if (entityplayer.openContainer instanceof ContainerSushiKnifeHolder)
                {
                    if (((ContainerSushiKnifeHolder)entityplayer.openContainer).getChestInventory() == this)
                    {
                        ++this.numPlayersUsing;
                    }
                }
            }
        }
        if (!this.world.isRemote) {
        	if (knife.get(0).getItem().equals(ModItems.SUSHI_KNIFE) && this.world.getBlockState(pos).getValue(SushiKnifeHolder.HASKNIFE) == false) {
        		this.world.setBlockState(pos, this.world.getBlockState(pos).withProperty(SushiKnifeHolder.HASKNIFE, Boolean.valueOf(true)));
        	}
        	else if (knife.get(0).equals(ItemStack.EMPTY) && this.world.getBlockState(pos).getValue(SushiKnifeHolder.HASKNIFE) == true) {
        		this.world.setBlockState(pos, this.world.getBlockState(pos).withProperty(SushiKnifeHolder.HASKNIFE, Boolean.valueOf(false)));
        	}
        	
        }
    }

	@Override
	protected NonNullList<ItemStack> getItems() {
		
		return this.knife;
	}
	
	@Override
	public void openInventory(EntityPlayer player)
	{
		++this.numPlayersUsing;
		this.world.addBlockEvent(pos, this.getBlockType(), 1, this.numPlayersUsing);
		this.world.notifyNeighborsOfStateChange(pos, this.getBlockType(), false);
	}
	
	@Override
	public void closeInventory(EntityPlayer player) 
	{
		--this.numPlayersUsing;
		this.world.addBlockEvent(pos, this.getBlockType(), 1, this.numPlayersUsing);
		this.world.notifyNeighborsOfStateChange(pos, this.getBlockType(), false);
	}
	@Override
	public boolean shouldRefresh(World world, BlockPos pos, IBlockState oldState, IBlockState newState) {
		if (oldState.getBlock() == newState.getBlock()) {
			return false;
			
		}
		else {
			return true;
		}
	}


}

 

 

Edited by kpzip

  • 7 months later...
Guest
This topic is now closed to further replies.

Important Information

By using this site, you agree to our Terms of Use.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.