Jump to content

Recommended Posts

Posted

Hello, I have a belt block (well 2 but) and I want to make it so if the belt runs directly into a chest it will put the item in the chest, it works but only if there allready is at least one of that item as a seperate stack (and not a full stack) but how can I make it so if there is not an available place in the chest it will just put it an empty slot? My code so far (just the onEntityCollidedWithBlock method):

@Override
public void onEntityCollidedWithBlock(World worldIn, BlockPos pos, IBlockState state, Entity entityIn) {
	EnumFacing enumfacing = (EnumFacing)state.getValue(FACING);

	switch (enumfacing) {
            case WEST:
            	if(entityIn instanceof EntityLivingBase || entityIn instanceof EntityItem) {
        			entityIn.motionX = -1.25;
        			entityIn.motionZ *= 0.2;
        		}
            	if(entityIn instanceof EntityItem) {
            		IBlockState westState = worldIn.getBlockState(pos.west());
            		Block westBlock = westState.getBlock();
            		EntityItem entityItem = (EntityItem) entityIn;
            		if(westBlock.equals(Blocks.CHEST)) {
            			BlockChest chestWest = (BlockChest) westBlock;
                		ILockableContainer container = chestWest.getContainer(worldIn, pos.west(), false);
            			for (int i = 0;i < container.getSizeInventory();i++) {
            				if (container.getStackInSlot(i) != null) {
            					if (container.getStackInSlot(i).getItem().equals(entityItem.getEntityItem().getItem())) {
                    				if (container.getStackInSlot(i).stackSize < 64) {
                    					container.getStackInSlot(i).stackSize++;
                    					entityItem.attackEntityFrom(DamageSource.fall, 500);
                    				}
                    			}
                			} else {
                			}
            			}
            		}
            	}
                break;
            case EAST:
            	if(entityIn instanceof EntityLivingBase || entityIn instanceof EntityItem) {
        			entityIn.motionX = 1.25;
        			entityIn.motionZ *= 0.2;
        		}
                break;
            case NORTH:
            	if(entityIn instanceof EntityLivingBase || entityIn instanceof EntityItem) {
        			entityIn.motionZ = -1.25;
        			entityIn.motionX *= 0.2;
        		}
                break;
            case SOUTH:
            	if(entityIn instanceof EntityLivingBase || entityIn instanceof EntityItem) {
        			entityIn.motionZ = 1.25;
        			entityIn.motionX *= 0.2;
        		}
            	break;
	case DOWN:
		break;
	case UP:
		break;
	default:
		break;
        }
}

Posted

mostly pseudo code, don't have access to my IDE atm.

//Checking each possible slot
for(i = 0; i < slots; i++){
   //If one is null
   if(getSlot(i) == null){
      //change the slot from null, to new itemstack
      setInventorySlotContents(...);
      break;
   }
}

Also previously known as eAndPi.

"Pi, is there a station coming up where we can board your train of thought?" -Kronnn

Published Mods: Underworld

Handy links: Vic_'s Forge events Own WIP Tutorials.

Posted

ah, I needed to add a break; to the else

Still doesn't work, I now have this:

	public void addItemToChest(BlockPos pos, World worldIn, Entity entityIn) {
	IBlockState state = worldIn.getBlockState(pos);
	Block block = state.getBlock();
	EntityItem entityItem = (EntityItem) entityIn;
	if(block.equals(Blocks.CHEST)) {
		BlockChest chest = (BlockChest) block;
    		ILockableContainer container = chest.getContainer(worldIn, pos, false);
		for (int i = 0;i < container.getSizeInventory();i++) {
			if (container.getStackInSlot(i) != null) {
				if (container.getStackInSlot(i).getItem().equals(entityItem.getEntityItem().getItem())) {
        				if (container.getStackInSlot(i).stackSize < 64) {
        					container.getStackInSlot(i).stackSize++;
        					entityItem.attackEntityFrom(DamageSource.fall, 500);
        				}
        			}
    			} else {
    				container.setInventorySlotContents(i, entityItem.getEntityItem());
    				break;
    			}
		}
	}
}

But when I put an item on it it put 2 stacks in there with 3 items..?

Posted

ah, I needed to add a break; to the else

Still doesn't work, I now have this:

	public void addItemToChest(BlockPos pos, World worldIn, Entity entityIn) {
	IBlockState state = worldIn.getBlockState(pos);
	Block block = state.getBlock();
	EntityItem entityItem = (EntityItem) entityIn;
	if(block.equals(Blocks.CHEST)) {
		BlockChest chest = (BlockChest) block;
    		ILockableContainer container = chest.getContainer(worldIn, pos, false);
		for (int i = 0;i < container.getSizeInventory();i++) {
			if (container.getStackInSlot(i) != null) {
				if (container.getStackInSlot(i).getItem().equals(entityItem.getEntityItem().getItem())) {
        				if (container.getStackInSlot(i).stackSize < 64) {
        					container.getStackInSlot(i).stackSize++;
        					entityItem.attackEntityFrom(DamageSource.fall, 500);
        				}
        			}
    			} else {
    				container.setInventorySlotContents(i, entityItem.getEntityItem());
    				break;
    			}
		}
	}
}

But when I put an item on it it put 2 stacks in there with 3 items..?

Try wrapping it in a !world.isRemote

VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING

I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect.

Forge and vanilla BlockState generator.

Guest
This topic is now closed to further replies.

Announcements



×
×
  • Create New...

Important Information

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