Jump to content

[1.10.2] Adding item from belt in chest


Egietje
 Share

Recommended Posts

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

Guest
This topic is now closed to further replies.
 Share



×
×
  • Create New...

Important Information

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