That way of doing it is very inefficient. The PlayerTick event is called on the client and server at both the start and end of each tick.
You should be checking event.side and event.phase to limit when you do things.
The onUpdateAbilities() sends a network packet to the other side.
So, your code would be sending 4 network packets per player per tick.
Here's a more efficient way:
@Mod.EventBusSubscriber(modid = MODID)
public class Events {
@SubscribeEvent
public static void equipmentChange(LivingEquipmentChangeEvent event) {
if (event.getEntity() instanceof Player player && event.getSlot() == EquipmentSlot.OFFHAND) {
// WARNING: Proof of concept code not the correct logic
player.getAbilities().mayfly = event.getTo().is(Items.DIAMOND);
player.onUpdateAbilities();
}
}
}
That event only happens on the server and only when the equipment actually changes.
The above works for me, using diamonds in the shield slot as my trigger.
WARNING. The above logic is not complete.
One example is, it would remove flight from creative mode players when they remove the diamond.
Another is it doesn't set flying=false when the diamonds are removed.