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.

Featured Replies

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

I know, but how can I make it so if there is nothing in a slot it will put the item from the belt there?

setInventorySlotContents(...)

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.

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.

But how would I check if there isn't an item?

// A check if there is an Item
if (container.getStackInSlot(i) != null) {
} else // Check if there is not an item.

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.

  • Author

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

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.

Still does the same

You never kill the entity when the itemstack is null also instead of attacking the EntityItem with a damage source just call setDead().

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.

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.