Jump to content

[SOLVED] Detect if you hit a player (Client mod)


Cinque

Recommended Posts

Hi,

 

I'm currently working on a PvP mod, and I would like to add a 'friends' feature, where you can add a player as friend so you won't be able to hit them. I can't seem to figure out how to detect if you hit a player though, all I found was LivingHurtEvent, but that doesn't work when using it in multiplayer. I assume I need to listen for a damage packet or something like that, then check the username and cancel it if the player is added to your friendslist, but I can't find much about packets either. Can anyone help me with this?

 

 

 

EDIT: After checking the source of Damage Indicators I found out that I could just listen for the AttackEntityEvent instead. I can detect when I try to hit a friend and display a message that I can't hurt him/her, but I can't figure out how to cancel it (e.setCanceled(true) doesn't seem to work for me).

 

 

SOLVED: I used the MouseEvent then checked if the entity was on my friends list with 'Minecraft.getMinecraft().objectMouseOver.entityHit'. Cancelling this prevented my from hitting people on my friends list.

Link to comment
Share on other sites

It is a

@Cancelable

event, so canceling should work. Show us your code.

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/

Link to comment
Share on other sites

I was looking into it some time ago, so it might have been changed, but it might be the server that actually decides about your attack.

 

Cancelling it on client won't do anything since server still receives packet that you clicked mouse in particular direction, and the server will manage that click on it's own, so you will still attack.

 

You would have to intercept MouseEvent get entity under mouse (MovingObjectPosition I think) and cancel MouseEvent client-side before anything happens.

1.7.10 is no longer supported by forge, you are on your own.

Link to comment
Share on other sites

It is a

@Cancelable

event, so canceling should work. Show us your code.

 

I don't really have any code yet, I tried to just cancel the event in general like this:

 

@SubscribeEvent
public void onAttackEntity(AttackEntityEvent e) {
e.setCanceled(true);
}

 

Using this code, I CAN'T damage entities in singeplayer, but in multiplayer it doesn't work (keep in mind this is a client mod and not a server mod).

Link to comment
Share on other sites

I was looking into it some time ago, so it might have been changed, but it might be the server that actually decides about your attack.

 

Cancelling it on client won't do anything since server still receives packet that you clicked mouse in particular direction, and the server will manage that click on it's own, so you will still attack.

 

You would have to intercept MouseEvent get entity under mouse (MovingObjectPosition I think) and cancel MouseEvent client-side before anything happens.

 

Thanks, I'll start off with just cancelling the MouseEvent and check if that prevents me from hitting entities in multiplayer.

 

EDIT: I just did a quick test with pigs, using this code I can't hit pigs but can hit other entities (which is what I want):

 

@SubscribeEvent
public void onMouse(MouseEvent e) {
	if (mc.objectMouseOver.entityHit instanceof EntityPig) {
		e.setCanceled(true);	
	}
}

 

I'll try to implement this into my friends system now and will post here if it worked or not. Thanks for the help btw :D

 

EDIT2: This code works, I can't hit people on my friends list on :D Thanks a lot for you help, probably wouldn't have figured it out myself!

Link to comment
Share on other sites

Guest
This topic is now closed to further replies.

Announcements



×
×
  • Create New...

Important Information

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