Jump to content

Crash when trying to use onItemRightClick


t_h0e

Recommended Posts

I'm currently working on a method for a custom item that will print information in the chat about what is being looked at by the player.

 I am using the onItemRightClick method, but my game crashes when this code is ran:

	@Override
	public ActionResult<ItemStack> onItemRightClick(World worldIn, PlayerEntity playerIn, Hand handIn) {
		
		RayTraceResult rtr = this.rayTrace(worldIn, playerIn, RayTraceContext.FluidMode.NONE);
		playerIn.sendMessage(ITextComponent.Serializer.fromJson(rtr.hitInfo.toString()));
		return onItemRightClick(worldIn, playerIn, handIn);
	}

There are multiple parts of this snippet that I am very uncertain about, so it wouldn't surprise me if I am doing more than one thing wrong ?.

Any advice would be much appreciated.

 

EDIT: Updated my code as well as added crash log

crash-2019-08-04_12.45.37-client.txt

Edited by t_h0e
added crash log
Link to comment
Share on other sites

1 hour ago, t_h0e said:

return super.onItemRightClick(worldIn, playerIn, handIn);

Change this to return your own ActionResult.

 

1 hour ago, t_h0e said:

but my game crashes when this code is ran:

Unsure of the crash, but perhaps a NullPointerException? Post your crash.

Edited by Animefan8888
  • Like 1

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

Hmm, Ok. What field tell me which object (if any) was hit from a RayTraceResult?

Also I believe I am serializing my string into an ITextComponent wrong. Is my best option to just implement ITextComponent in my own method?

Link to comment
Share on other sites

Thanks for the help! I got it working by changing the method to itemInteractionForEntity after a bit of tweaking. I believe onItemRightClick is called too many times for how I am applying it.

	@Override
	public boolean itemInteractionForEntity(ItemStack stackIn, PlayerEntity playerIn, LivingEntity livingIn, Hand handIn) {
		if (m.objectMouseOver.getType() == RayTraceResult.Type.ENTITY) {
			EntityRayTraceResult ertr = (EntityRayTraceResult) m.objectMouseOver;
			playerIn.sendMessage(ertr.getEntity().getName());
			return true;
		}
		else return false;
	}

 

Link to comment
Share on other sites

When I use this code, I receive no message at all. I read another post that said RayTraceResult does not return entities because it is the same as World::RayTraceBlocks, however I'm not sure if that's why I'm receiving nothing at all.

	@Override
	public boolean itemInteractionForEntity(ItemStack stackIn, PlayerEntity playerIn, LivingEntity livingIn, Hand handIn) {
		RayTraceResult rtr = this.rayTrace(playerIn.world, playerIn, RayTraceContext.FluidMode.NONE);
		if (rtr.getType() == RayTraceResult.Type.ENTITY) {
			EntityRayTraceResult ertr = (EntityRayTraceResult) m.objectMouseOver;
			playerIn.sendMessage(ertr.getEntity().getName());
			
			return true;
		}
	}

 

Link to comment
Share on other sites

Well, I believe I need to learn more about how to send network packets between the logical sides, because that is something I am not familiar with at all. It's unfortunate that a solely single player mod needs to be that complicated due to minecraft's design.

Link to comment
Share on other sites

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.