Jump to content

Custom container hopper interaction


Defacto

Recommended Posts

Hi !

I have created a custom container, and I need to have a slot which keep the item, without any type of extraction (like the blaze powder in the brewing stand).

I already tried custom slot, and check the code of the brewing stand. Can you please help me ?


Here are my tile entity and container class (the slot I need has the index 0 in the block inventory)
 

public class CollectorTE extends LockableLootTileEntity implements ITickableTileEntity, ISidedInventory, IItemHandler 
{
	
	private NonNullList<ItemStack> inventory = NonNullList.withSize(19, ItemStack.EMPTY);
	private static final int[] SLOTS_UP = new int[]{0};
	private static final int[] SLOTS_DOWN = new int[]{2, 1};
	
    public CollectorTE() {
        super(TileEntityInit.COLLECTOR_TE.get());
    }
    
	@Override
	public void tick() {
		int area = 5;
		int range = (area - 1)/2;
		if(!world.isRemote && world.getBlockState(pos).getBlock() instanceof BlockCollector) {
			Direction direction = world.getBlockState(pos).get(BlockCollector.FACING);
			BlockPos start = pos.offset(direction).offset(direction.rotateY(), range);
			BlockPos end = pos.offset(direction, area).offset(direction.rotateY(), -range);
			BlockPos.getAllInBox(start, end).forEach(position -> {
				if(world.getBlockState(position).getBlock() instanceof CropsBlock) {
					BlockState state = world.getBlockState(position);
					CropsBlock block = (CropsBlock)state.getBlock();
					if(block.isMaxAge(state))
					{
						for(ItemStack stack : Block.getDrops(state, world.getServer().getWorld(world.getDimensionKey()), position, world.getTileEntity(position))) {
							addItemStackInInventory(stack);
						}
						if(world.getBlockState(position).getBlock() instanceof BeetrootBlock) {
							world.setBlockState(position, block.getDefaultState().with(BeetrootBlock.BEETROOT_AGE, 1));
						}
						else {
							world.setBlockState(position, block.getDefaultState().with(CropsBlock.AGE, 4));
						}
					}
				} 
			});
		}
	}
	
	
	
	public void read(BlockState state, CompoundNBT nbt) {
	     super.read(state, nbt);
	     this.inventory = NonNullList.withSize(this.getSizeInventory(), ItemStack.EMPTY);
	     if (!this.checkLootAndRead(nbt)) {
	        ItemStackHelper.loadAllItems(nbt, this.inventory);
	     }
	}

	public CompoundNBT write(CompoundNBT compound) {
		super.write(compound);
      	if (!this.checkLootAndWrite(compound)) {
      		ItemStackHelper.saveAllItems(compound, this.inventory);
      	}
      	return compound;
	}
	
	public void addItemStackInInventory(ItemStack itemstack) {
		int i = this.getSizeInventory();
		ItemStack stack = itemstack;
        for(int j = 1; j < i && !stack.isEmpty(); ++j) {
           if(this.getStackInSlot(j).getCount() < 64 && this.getStackInSlot(j).getItem() == stack.getItem() || this.getStackInSlot(j).isEmpty()) {
        	   this.setInventorySlotContents(j, new ItemStack(stack.getItem(), stack.getCount() + this.getStackInSlot(j).getCount()));
        	   stack = new ItemStack(stack.getItem(), stack.getCount() - this.getStackInSlot(j).getCount());
           }
        }
	}

	@Override
	public int getSizeInventory() {
		return this.inventory.size();
	}

	@Override
	protected NonNullList<ItemStack> getItems() {
		return this.inventory;
	}

	@Override
	protected void setItems(NonNullList<ItemStack> itemsIn) {
		this.inventory = itemsIn;
	}

	@Override
	protected ITextComponent getDefaultName() {
		return new TranslationTextComponent("container.collector");
	}

	@Override
	protected Container createMenu(int id, PlayerInventory player) {
		 return new CollectorContainer(id, player, this);
	}
	
	public boolean isItemValidForSlot(int index, ItemStack stack) {
	      if (index == 0) {
	    	  return true;
	      } else {
	         return false;
	      }
	   }

	@Override
	public int[] getSlotsForFace(Direction side) {
		if (side == Direction.DOWN) {
	         return SLOTS_DOWN;
	      } else{
	    	  return SLOTS_UP;
	      }
	}

	@Override
	public boolean canInsertItem(int index, ItemStack itemStackIn, Direction direction) {
		return this.isItemValidForSlot(index, itemStackIn);
	}

	@Override
	public boolean canExtractItem(int index, ItemStack stack, Direction direction) {
		return !this.isItemValidForSlot(index, stack);
	}

	@Override
	public int getSlots() {
		return this.getSizeInventory();
	}

	@Override
	public ItemStack insertItem(int slot, ItemStack stack, boolean simulate) {
		if(this.isItemValidForSlot(slot, stack) && this.isItemValid(slot, stack)) {
			return stack;
		} else {
			return ItemStack.EMPTY;
		}
	}

	@Override
	public ItemStack extractItem(int slot, int amount, boolean simulate) {
		if(slot != 0) {
			return this.getStackInSlot(slot);
		}
		else {
			return ItemStack.EMPTY;
		}
	}

	@Override
	public int getSlotLimit(int slot) {
		if(slot == 0) {
			return 1;
		}
		return 64;
	}

	@Override
	public boolean isItemValid(int slot, ItemStack stack) {
		if(slot == 0) {
			return stack == new ItemStack(ItemInit.ELEMENTAL_TIME.get());
		}
		return true;
	}
}
public class CollectorContainer extends Container{

	public final IInventory collectorInventory;
	
	public CollectorContainer(final int windowId, final PlayerInventory playerInv) {
		this(windowId, playerInv, new Inventory(19));
	}
	
	public CollectorContainer(final int windowId, final PlayerInventory playerInv, IInventory inventory) {
		super(ContainerInit.COLLECTOR_CONTAINER.get(), windowId);
		assertInventorySize(inventory, 19);
		
		collectorInventory = inventory;
		
	    inventory.openInventory(playerInv.player);
		
		
		//Player Hotbar, slot 0 -> 8
		for(int col = 0; col < 9; col++) {
			this.addSlot(new Slot(playerInv, col, 8 + col * 18, 142));
		}
		
		//Main Player Inventory, slot 9 ->
		for(int row = 0; row < 3; row++) {
			for(int col = 0; col < 9; col++) {
				this.addSlot(new Slot(playerInv, col + row * 9 + 9, 8 + col * 18, 166 - (4 - row) * 18 - 10));
			}
		}
		
		//Elemental Time
		this.addSlot(new TimeSlot(inventory, 0, 8, 35));
		
		//Collector Inventory
		for(int row = 0; row < 3; row++) {
			for(int col = 0; col < 6; col++) {
				this.addSlot(new Slot(inventory, col + 1 + row * 6, 35 + col*18, 17 + row * 18));
			}
		}
	}

	@Override
	  public boolean canInteractWith(PlayerEntity playerIn) {
	    return this.collectorInventory.isUsableByPlayer(playerIn);
	  }
	
	public ItemStack transferStackInSlot(PlayerEntity 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 < 3 * 6 + 1) {
	            if (!this.mergeItemStack(itemstack1, 3 * 6 + 1, this.inventorySlots.size(), true)) {
	               return ItemStack.EMPTY;
	            }
	         } else if (!this.mergeItemStack(itemstack1, 0, 3 * 6 + 1, false)) {
	            return ItemStack.EMPTY;
	         }

	         if (itemstack1.isEmpty()) {
	            slot.putStack(ItemStack.EMPTY);
	         } else {
	            slot.onSlotChanged();
	         }
	      }

	      return itemstack;
	   }
	
	@Override
	  public void onContainerClosed(PlayerEntity playerIn) {
	    super.onContainerClosed(playerIn);
	    this.collectorInventory.closeInventory(playerIn);
	  }
	
	
	
}


 

Link to comment
Share on other sites

Don't use IInventory, its sub-interfaces, or any class that implements them.

Use Capabilities. You do not need to implement IItemHandler manually, an implementation already exists.

Edited by Draco18s

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

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.



×
×
  • Create New...

Important Information

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