Jump to content

Recommended Posts

Posted

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.

Posted

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.

Posted

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).

Posted

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!

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.