Advertisement
Dori_mon

Untitled

Jul 27th, 2019
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.72 KB | None | 0 0
  1. package com.dorilahav.anticheat;
  2.  
  3. import org.bukkit.BanList;
  4. import org.bukkit.Bukkit;
  5. import org.bukkit.entity.Player;
  6. import org.bukkit.event.EventHandler;
  7. import org.bukkit.event.Listener;
  8. import org.bukkit.event.entity.EntityDamageByEntityEvent;
  9. import org.bukkit.plugin.java.JavaPlugin;
  10. import org.bukkit.scheduler.BukkitRunnable;
  11. import org.bukkit.util.Vector;
  12.  
  13. import java.sql.Date;
  14. import java.time.LocalDate;
  15. import java.time.temporal.ChronoUnit;
  16. import java.util.List;
  17. import java.util.Map;
  18. import java.util.UUID;
  19. import java.util.concurrent.ConcurrentHashMap;
  20. import java.util.stream.Collectors;
  21.  
  22. public class AntiCheat extends JavaPlugin implements Listener {
  23.  
  24.     private static final Map<UUID, Double>
  25.             POINTS = new ConcurrentHashMap<>();
  26.  
  27.     @Override
  28.     public void onEnable() {
  29.         Bukkit.getPluginManager().registerEvents(this, this);
  30.  
  31.         new BukkitRunnable() {
  32.             @Override
  33.             public void run() {
  34.                 for (UUID uuid : POINTS.keySet()) {
  35.                     double points = POINTS.get(uuid);
  36.                     if (points - 1 <= 0)
  37.                         POINTS.remove(uuid);
  38.                     else
  39.                         POINTS.put(uuid, points - 1);
  40.                 }
  41.             }
  42.         }.runTaskTimer(this, 3000, 3000);
  43.  
  44.     }
  45.  
  46.     @EventHandler
  47.     public void onEvent(EntityDamageByEntityEvent e) {
  48.  
  49.         if (!(e.getDamager() instanceof Player) || !(e.getEntity() instanceof Player))
  50.             return;
  51.  
  52.         Player player = (Player) e.getDamager();
  53.         double reach = getReach(player);
  54.  
  55.         if (reach > 3.2) {
  56.  
  57.             double pointsBefore = POINTS.containsKey(player.getUniqueId()) ? POINTS.get(player.getUniqueId()) : 0;
  58.             double points = pointsBefore + (reach - 3.2) * 0.5;
  59.             POINTS.put(player.getUniqueId(), points);
  60.  
  61.             System.out.println(player.getName() + " has " + points + " points.");
  62.             if (pointsBefore < 50 && points >= 50)
  63.                 player.kickPlayer("Our anticheat detected that you are using reach. If you are not using reach hacks feel free to join back!");
  64.             else if (pointsBefore < 100 && points >= 100)
  65.                 player.kickPlayer("Our anticheat detected that you are using reach. If you are not using reach hacks feel free to join back!");
  66.             else if (pointsBefore < 150 && points >= 150)
  67.                 Bukkit.getBanList(BanList.Type.NAME).addBan(player.getName(), "Our anticheat detected that you are using reach. Feel free to contact us if it's a mistake.", Date.valueOf(LocalDate.now().plus(1, ChronoUnit.HOURS)), "");
  68.         }
  69.  
  70.     }
  71.  
  72.     public static Player getTargetPlayer(Player player, double max) {
  73.         List<Player> possible = player.getNearbyEntities(max, max, max).stream().filter(entity -> entity instanceof Player).map(entity -> (Player) entity).filter(player::hasLineOfSight).collect(Collectors.toList());
  74.         Ray ray = Ray.from(player);
  75.         double d = -1;
  76.         Player closest = null;
  77.         for (Player player1 : possible) {
  78.             double dis = AABB.from(player1).collidesD(ray, 0, max);
  79.             if (dis != -1) {
  80.                 if (dis < d || d == -1) {
  81.                     d = dis;
  82.                     closest = player1;
  83.                 }
  84.             }
  85.         }
  86.         return closest;
  87.     }
  88.  
  89.     public static double getReach(Player player) {
  90.         return getReach(player, 3.2);
  91.     }
  92.  
  93.     private static double getReach(Player player, double distance) {
  94.  
  95.         Player target = getTargetPlayer(player, distance);
  96.         if (target == null)
  97.             return getReach(player, distance + 0.3);
  98.  
  99.         return distance;
  100.     }
  101.  
  102.  
  103.     public static class AABB {
  104.  
  105.         private Vector min, max; // min/max locations
  106.  
  107.         // Create Bounding Box from min/max locations.
  108.         public AABB(Vector min, Vector max) {
  109.             this(min.getX(), min.getY(), min.getZ(), max.getX(), max.getY(), max.getZ());
  110.         }
  111.  
  112.         // Main constructor for AABB
  113.         public AABB(double x1, double y1, double z1, double x2, double y2, double z2) {
  114.             this.min = new Vector(Math.min(x1, x2), Math.min(y1, y2), Math.min(z1, z2));
  115.             this.max = new Vector(Math.max(x1, x2), Math.max(y1, y2), Math.max(z1, z2));
  116.         }
  117.  
  118.         private AABB(Player player) {
  119.             this.min = getMin(player);
  120.             this.max = getMax(player);
  121.         }
  122.  
  123.         private Vector getMin(Player player) {
  124.             return player.getLocation().toVector().add(new Vector(-0.3, 0, -0.3));
  125.         }
  126.  
  127.         private Vector getMax(Player player) {
  128.             return player.getLocation().toVector().add(new Vector(0.3, 1.8, 0.3));
  129.         }
  130.  
  131.         // Create an AABB based on a player's hitbox
  132.         public static AABB from(Player player) {
  133.             return new AABB(player);
  134.         }
  135.  
  136.         // Returns minimum x, y, or z point from inputs 0, 1, or 2.
  137.         public double min(int i) {
  138.             switch (i) {
  139.                 case 0:
  140.                     return min.getX();
  141.                 case 1:
  142.                     return min.getY();
  143.                 case 2:
  144.                     return min.getZ();
  145.                 default:
  146.                     return 0;
  147.             }
  148.         }
  149.  
  150.         // Returns maximum x, y, or z point from inputs 0, 1, or 2.
  151.         public double max(int i) {
  152.             switch (i) {
  153.                 case 0:
  154.                     return max.getX();
  155.                 case 1:
  156.                     return max.getY();
  157.                 case 2:
  158.                     return max.getZ();
  159.                 default:
  160.                     return 0;
  161.             }
  162.         }
  163.  
  164.         // Same as other collides method, but returns the distance of the nearest
  165.         // point of collision of the ray and box, or -1 if no collision.
  166.         public double collidesD(Ray ray, double tmin, double tmax) {
  167.             for (int i = 0; i < 3; i++) {
  168.                 double d = 1 / ray.direction(i);
  169.                 double t0 = (min(i) - ray.origin(i)) * d;
  170.                 double t1 = (max(i) - ray.origin(i)) * d;
  171.                 if (d < 0) {
  172.                     double t = t0;
  173.                     t0 = t1;
  174.                     t1 = t;
  175.                 }
  176.                 tmin = t0 > tmin ? t0 : tmin;
  177.                 tmax = t1 < tmax ? t1 : tmax;
  178.                 if (tmax <= tmin) return -1;
  179.             }
  180.             return tmin;
  181.         }
  182.     }
  183.  
  184.     public static class Ray {
  185.  
  186.         private Vector origin, direction;
  187.  
  188.         // Create a ray at the origin pointing in a direction.
  189.         public Ray(Vector origin, Vector direction) {
  190.             this.origin = origin;
  191.             this.direction = direction;
  192.         }
  193.  
  194.         // Create a ray based on where the player is looking.
  195.         // Origin: Player Eye Location
  196.         // Direction: Player-looking direction
  197.         public static Ray from(Player player) {
  198.             return new Ray(player.getEyeLocation().toVector(), player.getLocation().getDirection());
  199.         }
  200.  
  201.         public double origin(int i) {
  202.             switch (i) {
  203.                 case 0:
  204.                     return origin.getX();
  205.                 case 1:
  206.                     return origin.getY();
  207.                 case 2:
  208.                     return origin.getZ();
  209.                 default:
  210.                     return 0;
  211.             }
  212.         }
  213.  
  214.         public double direction(int i) {
  215.             switch (i) {
  216.                 case 0:
  217.                     return direction.getX();
  218.                 case 1:
  219.                     return direction.getY();
  220.                 case 2:
  221.                     return direction.getZ();
  222.                 default:
  223.                     return 0;
  224.             }
  225.         }
  226.  
  227.     }
  228.  
  229. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement