Jump to content

Recommended Posts

Posted

I need to spawn a chest with some items. I have these variables:

PlayerEntity player; 
World world; 
BlockPos pos; 
ItemEntity[] items;
// they are assigned, but I don't show the assignments here.

I need to create a chest (or a barrel, or a shulker box) in world "world" at position "pos" with items "items". How can I do that?

P.S. my minecraft forge version is 1.15.2.

Posted
4 minutes ago, evjeny.23 said:

I need to create a chest (or a barrel, or a shulker box) in world "world" at position "pos" with items "items". How can I do that?

World::setBlockState(pos, THE_BLOCK_STATE_YOU_WANT)

This will place the block and the TileEntity into the world.

World::getTileEntity(pos)

This will get the TileEntity at the BlockPos pos.
Use the CapabilityItemHandler.ITEM_HANDLER_CAPABILITY to add the items to it.

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.

Posted
2 minutes ago, Animefan8888 said:

Read the documentation on capabilities here.

I have written the following code based on a related question:

final IItemHandler[] nowHandler = new IItemHandler[1];
world.setBlockState(pos, Blocks.CHEST.getDefaultState());
TileEntity chestTileEntity = worldIn.getTileEntity(pos);
IItemHandler itemHandler;
nowHandler[0] = null;
chestTileEntity.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY).ifPresent(new NonNullConsumer <IItemHandler>() {
  @Override
  public void accept(IItemHandler ih) {
  	nowHandler[0] = ih;
  }
});
while (nowHandler[0] == null) {};
itemhandler = nowHandler[0];
itemHandler.insertItem(1, new ItemStack(Items.SOME_ITEM, 1), false); // also I don't understand what "bool simulate" does

But it doesn't work. Chest spawns, but there are no items in it. (There are no errors in LOG).

Posted
7 minutes ago, evjeny.23 said:

// also I don't understand what "bool simulate" does

If simulate is true it only simulates adding the ItemStack to the IItemHandler.

8 minutes ago, evjeny.23 said:

final IItemHandler[] nowHandler = new IItemHandler[1];

IItemHandler itemHandler;

Why?

9 minutes ago, evjeny.23 said:

itemHandler.insertItem(1, new ItemStack(Items.SOME_ITEM, 1), false); // also I don't understand what "bool simulate" does

Why not just put this in the accept function?

 

10 minutes ago, evjeny.23 said:

But it doesn't work. Chest spawns, but there are no items in it. (There are no errors in LOG).

Where is this code being called? Post all of it,

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.

Posted (edited)
5 minutes ago, Animefan8888 said:

Where is this code being called? Post all of it,

    @SubscribeEvent
    public void onLivingEntityDrops(LivingDropsEvent event) {
    	LOGGER.info("DROP");
    	if (!(event.getEntityLiving() instanceof PlayerEntity)) return;
    	BlockPos bedPos;
    	try {
    		bedPos = ((PlayerEntity)event.getEntityLiving()).getBedPosition().get();
    	} catch (NoSuchElementException e) {
    		bedPos = new BlockPos(0, event.getEntityLiving().getPosition().getY(), 0);
    	}
    	World worldIn = event.getEntityLiving().getEntityWorld();
    	final IItemHandler[] nowHandler = new IItemHandler[1];
    	worldIn.setBlockState(bedPos, Blocks.BARREL.getDefaultState());
    	TileEntity barrelContainer = worldIn.getTileEntity(bedPos);
    	IItemHandler itemhandler;
    	nowHandler[0] = null;
    	barrelContainer.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY).ifPresent(new NonNullConsumer <IItemHandler>() {
    		@Override
    		public void accept(IItemHandler ih) {
    			nowHandler[0] = ih;
    		}
    	});
    	while (nowHandler[0] == null) {};
    	itemhandler = nowHandler[0];
    	int slot = 0;
    	for (ItemEntity item : event.getDrops()) {
    		LOGGER.info("drop");
    		ItemStack leftItemsStack = itemhandler.insertItem(slot, item.getItem().copy(), false);
    		if (!leftItemsStack.equals(ItemStack.EMPTY)) {
    			bedPos = bedPos.north();
    			worldIn.setBlockState(bedPos, Blocks.BARREL.getDefaultState());
    			barrelContainer = worldIn.getTileEntity(bedPos);
    			itemhandler = nowHandler[0];
    			slot = 0;
    		}
    		slot++;
    	}
    }

Now it raises NullPointerException.

Edited by evjeny.23
Posted
21 minutes ago, evjeny.23 said:

Now it raises NullPointerException.

I've got a couple of things to say.

 

First and foremost: Please learn Java before trying to make a Minecraft Forge mod. Java itself is hard to learn let alone trying to learn an extensive API like Forge.

 

Secondly: If you crash or an exception is thrown there is a lot of useful information inside the stacktrace. You should read it. If you don't know what it means google it. If that doesn't supply you with answers/enough information post the error.

 

Thirdly:

30 minutes ago, Animefan8888 said:
40 minutes ago, evjeny.23 said:

final IItemHandler[] nowHandler = new IItemHandler[1];

IItemHandler itemHandler;

Why?

40 minutes ago, evjeny.23 said:

itemHandler.insertItem(1, new ItemStack(Items.SOME_ITEM, 1), false); // also I don't understand what "bool simulate" does

Why not just put this in the accept function?

 

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.

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.

Announcements



×
×
  • Create New...

Important Information

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