Jump to content

Recommended Posts

Posted

In my mod I create a new block class, which when right-clicked it should give the player an item. Well, I managed to get it working properly, but then I added more lines of code to it and it stopped working. I've tried removing everything but the required lines (as I think it was when it worked) and it is not working. I hope you can fin what I'm doing wrong in the code I attach (note that some method overrides are missing, I'm not attaching the whole code, just the needed).

 

package com.name.modid.objects.blocks;

//Imports here

public class ModBlock extends Block {
	
  	//Some missing methods here, don't mind. They exist in my code.
  
	@ObjectHolder("modid:moditem")
	public static final Item item_var = null;
	
	ItemStack item_stack = new ItemStack(item_var);
	
	@Override
	public ActionResultType onBlockActivated(BlockState state, World worldIn, BlockPos pos, PlayerEntity player, Hand handIn, BlockRayTraceResult hit) {
				
		player.inventory.addItemStackToInventory(item_stack);
		return ActionResultType.SUCCESS;
      
	}
}

 

Posted (edited)
1 hour ago, TowerMX said:

ItemStack item_stack = new ItemStack(item_var);

You cannot do that statically (well not exactly static, but the block is constructed way before the correct time to do this). The item isn't initialized at that time, and the object holder is still null. You probably don't want it to be singletons anyways.

Create a new stack and give it to the player inside onBlockActivated.

 

You are also adding to the player's inventory on both sides, while it should only be done on the server. Use World#isRemote (returns false on server and true on client) to check if you are on the server side.

Edited by DavidM

Some tips:

Spoiler

Modder Support:

Spoiler

1. Do not follow tutorials on YouTube, especially TechnoVision (previously called Loremaster) and HarryTalks, due to their promotion of bad practice and usage of outdated code.

2. Always post your code.

3. Never copy and paste code. You won't learn anything from doing that.

4. 

Quote

Programming via Eclipse's hotfixes will get you nowhere

5. Learn to use your IDE, especially the debugger.

6.

Quote

The "picture that's worth 1000 words" only works if there's an obvious problem or a freehand red circle around it.

Support & Bug Reports:

Spoiler

1. Read the EAQ before asking for help. Remember to provide the appropriate log(s).

2. Versions below 1.11 are no longer supported due to their age. Update to a modern version of Minecraft to receive support.

 

 

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.