Get Player's Swing/attack packet?

Discussion in 'Plugin Development' started by Xp10d3, Sep 12, 2020.

Thread Status:
Not open for further replies.
  1. Offline

    Xp10d3

    I am having some issues with checking if the player is sending a swing/attack packet. How do I check if the player is sending such things if it's only client side? I did a tutorial from TheSourceCode on checking packets, however I couldn't find any info on attack packets SPECIFICALLY. I am using this bit of code (entire credit goes to TheSourceCode) that I mostly understand (replace PacketPlayOutOfBed with another packet to read it and block it, inject the player on join, remove the player on leave, pretty straight forward):
    Code:java
    1.  
    2. package me.tsccoding.packets;
    3. import io.netty.channel.*;
    4. import net.minecraft.server.v1_12_R1.PacketPlayOutBed;
    5. import org.bukkit.Bukkit;
    6. import org.bukkit.ChatColor;
    7. import org.bukkit.craftbukkit.v1_12_R1.entity.CraftPlayer;
    8. import org.bukkit.entity.Player;
    9. import org.bukkit.event.EventHandler;
    10. import org.bukkit.event.Listener;
    11. import org.bukkit.event.player.PlayerJoinEvent;
    12. import org.bukkit.event.player.PlayerQuitEvent;
    13. import org.bukkit.plugin.java.JavaPlugin;
    14. public class PacketMain extends JavaPlugin implements Listener {
    15. @Override
    16. public void onEnable() {
    17. Bukkit.getPluginManager().registerEvents(this, this);
    18. }
    19. @EventHandler
    20. public void onjoin(PlayerJoinEvent event){
    21. injectPlayer(event.getPlayer());
    22. }
    23. @EventHandler
    24. public void onleave(PlayerQuitEvent event){
    25. removePlayer(event.getPlayer());
    26. }
    27. private void removePlayer(Player player) {
    28. Channel channel = ((CraftPlayer) player).getHandle().playerConnection.networkManager.channel;
    29. channel.eventLoop().submit(() -> {
    30. channel.pipeline().remove(player.getName());
    31. return null;
    32. });
    33. }
    34. private void injectPlayer(Player player) {
    35. ChannelDuplexHandler channelDuplexHandler = new ChannelDuplexHandler() {
    36. @Override
    37. public void channelRead(ChannelHandlerContext channelHandlerContext, Object packet) throws Exception {
    38. //Bukkit.getServer().getConsoleSender().sendMessage(ChatColor.YELLOW + "PACKET READ: " + ChatColor.RED + packet.toString());
    39. super.channelRead(channelHandlerContext, packet);
    40. }
    41. @Override
    42. public void write(ChannelHandlerContext channelHandlerContext, Object packet, ChannelPromise channelPromise) throws Exception {
    43. //Bukkit.getServer().getConsoleSender().sendMessage(ChatColor.AQUA + "PACKET WRITE: " + ChatColor.GREEN + packet.toString());
    44. if(packet instanceof PacketPlayOutBed){
    45. PacketPlayOutBed packetPlayOutBed = (PacketPlayOutBed) packet;
    46. Bukkit.getServer().getConsoleSender().sendMessage(ChatColor.AQUA + "PACKET BLOCKED: " + ChatColor.GREEN + packetPlayOutBed.toString());
    47. return;
    48. }
    49. super.write(channelHandlerContext, packet, channelPromise);
    50. }
    51. };
    52. ChannelPipeline pipeline = ((CraftPlayer) player).getHandle().playerConnection.networkManager.channel.pipeline();
    53. pipeline.addBefore("packet_handler", player.getName(), channelDuplexHandler);
    54. }
    55. }
    56.  
    (ahhh it didn't format correctly)
    However, as I said, I can't find any info on swing/attack packets from this site:
    http://wiki.vg/Protocol
    I'm attempting to make a plugin that checks if the player is blatantly cheating. I know there are other anti-cheats out there; this is mostly for playing around and improving my Java skills. I also am wondering how to check if the player is looking at another player/entity. This bit of code always was returned as true:
    Code:java
    1.  
    2. @EventHandler
    3. public void checkAura(EntityDamageByEntityEvent event) {
    4.  
    5. Player player = (Player) event.getDamager();
    6. if (((event.getDamager() instanceof Player)) && ((event.getEntity() instanceof Player))) {
    7. if (player.getEyeLocation() != event.getEntity()) {
    8. player.sendMessage("lol u haxxor TIME TO BAN U AHHHH");
    9. event.getEntity().sendMessage(ChatColor.RED + "someone be haxxing on u and that person is " + player.getDisplayName() + ".");
    10. core.hacksLog(event.getEntity() + " has been killauraed on by " + player + ".");
    11. }
    12. }
    13. }
    14.  

    Thanks :)
     
  2. Offline

    Kars

    What is it with you and packets? Use events.
     
  3. Offline

    Xp10d3

    I tried events, it didn't work. I asked for help in a Discord and they said to use packets.
    EDIT: I also find packets interesting; they are similar to events, but are handled differently. I also need to learn how to use them for my own personal use.
     
  4. Offline

    Kars

    Packets trigger events. Events happen on the server. If you want to check for behavior, check for events. When a player sends an attack packet an event fires.

    Also
    Obviously, because Location is not a Player. Compare the player's location... (compare XYZ)
     
    Last edited: Sep 12, 2020
  5. Offline

    Xp10d3

    Ah I see. Thanks, that makes sense :) Well I can just edit that event then, however I did have an issue with getting the eye location. If player.getEyeLocation() is basically Location, how would I check if the player is looking at event.getEntity()?
     
  6. Offline

    Kars

  7. Offline

    KarimAKL

    Last edited by a moderator: Sep 12, 2020
  8. Offline

    Kars

    Yea well you'd have to compare the player's location. I guess it could work.
     
  9. Offline

    KarimAKL

    I'd think World#rayTraceEntities(...) would be better.
     
  10. Offline

    Xp10d3

    Thanks for the help! However, it seems that RayTraceResult is 1.15.2+, and I'm using 1.8.8 0-0
    I'm checking for whether the player is looking at an entity though; like a player. Within their hitbox and such.
     
  11. Offline

    Kars

    Yes. Like i said, compare the target location with the line of sight locations
     
    Xp10d3 likes this.
  12. Offline

    Xp10d3

    Thanks for the suggestion :) However won't that check if the player has reach instead? I mean killaura COMES with reach, right?
     
  13. Offline

    Kars

    You wanted a way to determine if the attacker was looking at the target. I suggested a way to do it. Not sure what reach has to do with this. You can compare the distance between the two.
     
  14. Offline

    gochi9

    Have your tried player.hasLineOfSight(livingEntity); ?
     
  15. Offline

    Xp10d3

    Ah I see. Thanks Kars :)
    Not yet; I'll have to do that.

    FYI I found this:
    Code:java
    1.  
    2. double backstab = player.getLocation().getDirection().angle(event.getEntity().getLocation().getDirection()) / 180 * Math.PI;
    3. if (backstab >= 180) {
    4. player.sendMessage("Owo, " + backstab + " is greater than 180.");
    5. } else {
    6. player.sendMessage("Owo, " + backstab + " is less than 180.");
    7. }
    8.  

    Which I can use to find the angle at which the player is hitting the other player. 180 is not the correct number, so I'll have to mess around with that.
    EDIT: Wait a sec that is completely useless lol.

    EDIT2: gochi9 hmm didn't seem to work.
    Code:java
    1.  
    2. if (!player.hasLineOfSight(event.getEntity())) {
    3. player.sendMessage("possible haxxor");
    4. } else {
    5. player.sendMessage("ok no haxxor");
    6. }
    7.  

    Kept saying, "ok no haxxor" with KillAura on, not looking at the entity, and having 4 blocks of reach.

    EDIT3: Kars player.getLineOfSight() seems to be deprecated in 1.8.
     
    Last edited: Sep 13, 2020
  16. Offline

    Shqep

    First of all, I agree with the others. You shouldn't go messing around with internal nms system if you could do it in Bukkit, as it'd be quite annoying to clean up if you messed up something and couldn't revert it.

    Anyways, I don't know much about hit detection system, so take this with a grain of salt.
    If you would like to see if the player is looking at another, try raytracing from their eye location out 3 blocks in the direction they're facing, then check if there is an entity on that raytraced line.
    I honestly doubt it would work as expected considering some clients may have sent another look packet to adjust the aim on the target, so raytracing at that moment would just say that the hacker is not cheating.

    Hope I made sense lmao.
     
  17. Offline

    Xp10d3

    It makes sense; thank you :) However, I don't believe 1.8 supports raytracing. Please correct me if I'm wrong xD
     
  18. Offline

    gochi9

    Well too bad that my suggestion didn't work but also i think that a good made cheat won't be detected just by checking if the attacker is looking at the target.Well i could be wrong because i've never tried making cheats before but if it were this simple then a working plugin would already be out there.

    Anyway i think some older anti cheat(i could be wrong) was spawning an armor_stand behind,from the left,from the right and above the player very close (a block or less) when he was left clicking and if the player was hitting that armor stand multiple time that it would be considered killaura.
     
  19. Offline

    KarimAKL

    I believe that has been changed to a player for newer anticheats, because the cheats adapted and began only attacking player entities.
     
  20. Offline

    gochi9

    Well yes i mentioned it was older be said that he only wanted to player around with it so i think this works fine for him or at least it's a starting point
     
  21. Offline

    Xp10d3

    Yep; that is what Hypixel WatchDog uses as well. However, as KarimAKL said, most Anti-Cheats nowadays only attack player entities. Most, that is; some don't, but that is a rarity. I'll see if I can do that, but it does seem to be overcomplicating things. If possible I'd like to work with packets to improve my knowledge on such things as I said above. It does seem to be quite simple, however, to check if the player is looking at an entity while sending attack packets. If I can work with that, that is ideal.
    EDIT: Sorry, hope that makes sense. I'm kinda multitasking atm lol

    EDIT 2: I did some messing around:
    I currently have this bit:
    Code:java
    1.  
    2. if (!player.hasLineOfSight(event.getEntity())) {
    3. player.sendMessage("Didn't have line of sight for entity.");
    4. if (player.getLocation().getBlockZ() != event.getEntity().getLocation().getBlockZ()) {
    5. player.sendMessage("not in location of blockz");
    6. player.sendMessage("possible haxxor");
    7. }
    8. } else {
    9. player.sendMessage("ok no haxxor");
    10. }
    11.  

    However, it keeps saying, "ok no haxxor" even when I have killaura on. I am playing around and testing for blatant killaura cheaters.
    Code: https://hatebin.com/rgjghwcndf
    > try checking first if they have line of sight
    > and then add in the other check
    I just tried this, which is what KoalaOnCaffeine #0001 (from another help Discord, which I'm using to get other people's POV on this) suggested as you can see by the code above and it doesn't seem that the player is in line of sight of another entity at all.
     
    Last edited: Sep 14, 2020
  22. Offline

    gochi9

    I think that most of the cheats can trick the client/server into thinking that the player is looking directly at the player(Here i can be so wrong but it's just a speculation) so what you are trying to check can be more a waste of time(Again i could be so wrong here).

    Yes this is most correct also i think it can do more like attacking only visible players/players with armour/players with the mosts ticks lived (so it can also skip newly spawned entities like a fake player) also i think that some older anti cheats made you click faster (for those who cheat blatantly) so you could check for CPS or i think kill aura extends your reach if i'm not mistaken so you could also look for that
     
  23. Offline

    KarimAKL

    @gochi9 I believe you're correct in all of those. I'd say testing the line of sight (if he gets it working) might be worth it, just in case. It shouldn't be a dead giveaway for cheats though, as people can just stand AFK, looking at another AFK player, and get punished for having followed them with their mouse for a long time. :p
     
  24. Offline

    Shqep

    You could try manually raytracing by using vectors though. As possible as it may be, that seems very inefficient for a large amount of players and the amount of raytracing that has to be done.
     
  25. Offline

    gochi9

    Yes it could help but as you said you can't just ban someone from it you could alert an admin though and then he might spectate him.

    Anyway i belive it might just be straight near to impossible to detect a good cheat.Yes some suggestions we gave here could take care of some other cheats since there are players that just google "free minecraft cheats" and clicking the first link might send them to a bad cheat but still functional which could not be as advanced as others
     
  26. Offline

    Xp10d3

    I could test for a few things, however. Since gochi9 is correct (I believe I've seen these things in action), I could use a few of your ideas in using an armor stand, check if the player is not looking at the player, AND test for afk. But I do agree with gochi9 in that it shouldn't automatically ban, but rather alert an Admin. Thanks for your input everyone :)
    Yeah, it would be quite inefficient. Especially on a KitPvP server. Would cause a lot of lagg.

    EDIT: Welp, I think I'm done testing for KillAura xD I'm working on reach which is quite a bit easier (https://bukkit.org/threads/getting-the-distance-between-two-players.206050/) and I'll work my way up from that. If anyone has any more ideas PLEASE either bump this thread (if that's allowed) or DM it to me. Thanks for everyone's input!
     
  27. Offline

    KarimAKL

    I believe Location#distanceSquared(Location) is preferred instead of Location#distance(Location).
     
    Xp10d3 and Shqep like this.
  28. Offline

    Shqep

    The cat is right. Using #distance every now and then won't affect performance too much. But using it every time a player gets hit is ehh...
     
    KarimAKL and Xp10d3 like this.
  29. Offline

    Xp10d3

    Regarding the reach thread? I am encountering issues with that myself (the distance is different depending on what angle you're hitting a person) and will try that, but if you mean the KillAura plugin are you talking about player.hasLineOfSight()?
    What do you mean? As in check every so often when a person is hit? If so I'll try that :p Could use a random num generator and if the number is between a certain range, check.
     
  30. Offline

    KarimAKL

    Yes. The reach thread. It's using Location#distance(Location), which i just wanted to mention, before you used that as well.

    I don't think i've ever been called a cat. :p
     
    Xp10d3 likes this.
Thread Status:
Not open for further replies.

Share This Page