Check whether player takes knockback

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

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

    Xp10d3

    Hello, it is I, Xp10d3/Eltik back with another thread (apologies for like 5 threads in 2 days...). I am trying to make a minimalist anti-cheat, and have succeeded in doing so for basically all PvP Hacks (autoclicker, killaura, aim assist, and reach. I got Bhop after anti-kb), but I'm stumped on anti-kb. Reach was easy enough once I got the algorithm, so was killaura and aim assist (although that will probably not work very well...?), and autoclicker was easy as well (get the CPS the player is sending to the server using a Bukkit Runnable, then if the CPS is greater than or equal to the configured amount, increase violation level), but anti-kb is a bit tricky. What I'm going for is listen on the EntityDamageByEntityEvent, then check whether they move x amount of blocks. However, that won't work since they can be moving forward, and thus that won't work. Is there any good way to check for this? If not, how would I accomplish it? Is it worth it? Thanks.
    NOTE: Also, you probably want to see my code, so here it is. I'll probably make it open source since it's not a very good anti-cheat lol. I ask that if you use any of my code, you give credit to the user "Eltik". This is open source.

    Core.java
    https://hatebin.com/ahmuwlasph


    PlayerListeners.java
    https://hatebin.com/jfjdevglyp


    Commands.java
    https://hatebin.com/hgskyilepg
     
  2. Offline

    Kars

    I recon you should compare the victim's velocity with the attacker's hit direction, a few ticks after the damage event. Would require some testing and tweaking for sure.
     
  3. Offline

    Xp10d3

    Ah, I see. So somethin' like:
    Code:java
    1.  
    2. Player player = (Player) event.getDamager();
    3. Player entity = (Player) event.getEntity();
    4. if (player.getDirection() != entity.getHitDirectionOrWhatever()) {
    5. // Ban entity
    6. }
    7.  

    How would I get the victim's velocity, though? I know it would require vectors or whatever, but I'm not exactly familiar with those sadly.
     
  4. Offline

    KarimAKL

  5. Offline

    Kars

    Neither am i. The vector contains (or doesn't contain) values depending on the player's movement- speed and direction. Experiment with that.
     
  6. Offline

    Xp10d3

    mb i knew there was something like that lol. just forgot what it was xD
    huh... well, i might not need to use vectors since i could just get the direction the player is moving based on their velocity. ill see if i can look into vectors tho. this thread mentions a bit on that. ill come back and let anyone know whether i figure something out.

    EDIT: if anyone is interested, i made something very very quick that i have not tested lol:
    Code:java
    1.  
    2. @EventHandler
    3. public void antikb(EntityDamageByEntityEvent event) {
    4. Player player = (Player) event.getDamager();
    5. Player entity = (Player) event.getEntity();
    6. entity.sendMessage("YoUR velocity is astonishing: " + entity.getVelocity());
    7. player.sendMessage("your direction is insane: " + player.getLocation().getDirection());
    8. if (entity.getVelocity() != player.getLocation().getDirection()) {
    9. entity.sendMessage("ok possible haxxor for anti-kb");
    10. }
    11. }
    12.  

    ok done now to test lmao. prob not gonna work cause its way too simple imo.
     
    Last edited: Sep 22, 2020
  7. Offline

    Kars

    getVelocity returns a vector..

    @Xp10d3 you need to brush up on the basics of Java types and instances.
     
    Xp10d3 likes this.
  8. Offline

    Xp10d3

    fact. its been a while since i attempted to learn java. i knew that getVelocity returns a vector however; just didnt exactly spend enough time on it to fix it (yes i actually did know it im serious lol). alright imma go relearn the basics of java then come back to this lol
     
  9. Offline

    Kars

    @Xp10d3 i said that because you are comparing objects (with == or !=).
    This compares instances of an object. getDirection and getVelocity both return vectors, but a vector is a list of 3 values (x, y and z) and the vector object is likely instantiated at the moment the get-method is called. If you compare the two with ==, you will always get false.

    This is why we also compare strings with .equals (and not with ==), because String is an object, and two String objects with the value "string" are not the same instantiation.

    To do what you are after you need to compare the values of the velocity vector. I would experiment with vector values and see what they are while you are walking around to get a feel for how you can write logic to check it.

    Simply comparing x, y and z from both vectors is not the answer here.
     
  10. Offline

    Xp10d3

    Yeah, I know about that. Sorry if I'm coming across as dismissive; that's not what I'm trying to say :) You're right in the fact that I don't really know Java that well, but I do understandthe reason why you compare strings with .equals, and not just that it is cleaner. And I do understand what you mean; I probably shouldn't have sent that bit of code since it's just pseudo code and not the final product. It is a fact that I don't know Java completely, I only spent about a month or so doin' Codecademy, but I usually learn best by doing the actual thing which is extremely frustrating. I don't have the discipline to just sit down and read a Java book or do a course lol. Anyways, thanks for the tip. I'll play around with this and check out the different values of each vector (player.getDirection() and player.getVelocity()), see how they play out. But you're right; comparing two values from different vectors won't work. I don't know if I get this right, but I did this with my anti-reach feature. I tried comparing the distance between two locations, but since there are three values, it won't work since Minecraft can have the player look at a different direction and return completely different values. Coordinates didn't work, so I had to use Vectors and get help from someone else. I could try and play around with the magnitude of the velocity (like idk player.getVelocity().length or something, that probably isn't it) and figure it out from there. Thanks for the help Kars.
     
Thread Status:
Not open for further replies.

Share This Page