Jump to content

Recommended Posts

Posted

Hello,
I want to damage an entity when I right click it from a distance.
I saw in the RayTraceResult class that there is something with entityHit.
So I suppose I have to use RayTraceResult. Though I'm doing something
wrong.

@Override
public ActionResult<ItemStack> onItemRightClick(World worldIn, EntityPlayer playerIn, EnumHand handIn) {
  if(!worldIn.isRemote) {
  	RayTraceResult ray = playerIn.rayTrace(100,20);
	Entity entity = ray.entityHit;
	entity.setDead();
  	return new ActionResult(EnumActionResult.SUCCESS, new ItemStack(this));
  	} else {
  	return new ActionResult(EnumActionResult.FAIL, new ItemStack(this));
}
}

What am I doing wrong?
Thanks
 

Posted

If you look at Entity::rayTrace in your IDE, you can see that the method is annotated with @SideOnly(Side.CLIENT). This means this piece of code only exists on the client (it was moved to the client because it was never used on the server, by the deobfuscation process)

You have to copy the logic inside of Entity::rayTrace and put it somewhere in your own code, and call that instead.

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.

Posted

Thanks for your reply.
So this is where I ended up with.

@Override
public ActionResult<ItemStack> onItemRightClick(World worldIn, EntityPlayer playerIn, EnumHand handIn) {
	onclick(worldIn, playerIn, handIn);
	return new ActionResult(EnumActionResult.SUCCESS, new ItemStack(this));
}
      
@SideOnly(Side.CLIENT)
  private void onclick(World worldIn, EntityPlayer playerIn, EnumHand handIn){
  	 RayTraceResult ray = new RayTraceResult(playerIn);
   	 Entity entity = ray.entityHit;
     if(entity == null){
     } else
     entity.onKillCommand();
    }
}

It kills me instead of the entity.
 

Posted

By the way I already have a tutorial that explains this (making weapons with extended reach) here: http://jabelarminecraft.blogspot.com/p/minecraft-modding-extending-reach-of.html

 

You cannot just override the on right click method on the server side because that only gets called after the normal reach is processed on the client. So you have to process the right click on the client to check the extended reach. Basically the approach is to make a copy of the mouse over method on the client to find entities at a longer distance and then send a custom packet that triggers an attack the same as normal attack on the server.

Check out my tutorials here: http://jabelarminecraft.blogspot.com/

Posted
3 hours ago, Poseidon_22 said:

 


  	RayTraceResult ray = playerIn.rayTrace(100,20);
	Entity entity = ray.entityHit;
	entity.setDead();

 

*Right clicks air*

*Crashes game*

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Posted
1 hour ago, Differentiation said:

This will throw a NullPointerException

It will throw an NPE, however, not in the line you quoted. It's actually the next line, when entity is being used (entity.setDead()).

Don't PM me with questions. They will be ignored! Make a thread on the appropriate board for support.

 

1.12 -> 1.13 primer by williewillus.

 

1.7.10 and older versions of Minecraft are no longer supported due to it's age! Update to the latest version for support.

 

http://www.howoldisminecraft1710.today/

Posted
3 hours ago, larsgerrits said:

It will throw an NPE, however, not in the line you quoted. It's actually the next line, when entity is being used (entity.setDead()).

Yes, Entity entity = ray.entityHit; is the starting line that is wrong in his code that would lead to further errors. :)

You're correct, that's the cause of the exception.

Posted (edited)
1 hour ago, Differentiation said:

Yes, Entity entity = ray.entityHit; is the starting line that is wrong in his code that would lead to further errors. :)

You're correct, that's the cause of the exception.

The reason why largerrits corrected you is because it is okay for something to be null, so that line is not really an error and is a perfectly valid line of code. It is only an error if/when later you dereference it to access something within the null instance. In programming it is important to be precise.

 

The whole null thing can be a pain though especially in a situation like modding where you didn't write the rest of the code so it isn't always clear when something may be null. You can be safe and check everything for null before using it but that is unwieldy and seems like a waste of code (imagine if in every method you had to test every parameter for null). That is why the Nullable and Non-Null annotation is a welcome addition to Forge -- it really makes it clear when you should handle the null case. 

 

Tip: In fact you can usually configure your IDE to analyze this for you -- so if the entityHit() method was annotated with Nullable annotation it would have warned him (warning or error depending on how you configure your preferences) when he tried to use setDead().

Edited by jabelar
  • Like 1

Check out my tutorials here: http://jabelarminecraft.blogspot.com/

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.