Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.dorilahav.anticheat;
- import org.bukkit.BanList;
- import org.bukkit.Bukkit;
- import org.bukkit.entity.Player;
- import org.bukkit.event.EventHandler;
- import org.bukkit.event.Listener;
- import org.bukkit.event.entity.EntityDamageByEntityEvent;
- import org.bukkit.plugin.java.JavaPlugin;
- import org.bukkit.scheduler.BukkitRunnable;
- import org.bukkit.util.Vector;
- import java.sql.Date;
- import java.time.LocalDate;
- import java.time.temporal.ChronoUnit;
- import java.util.List;
- import java.util.Map;
- import java.util.UUID;
- import java.util.concurrent.ConcurrentHashMap;
- import java.util.stream.Collectors;
- public class AntiCheat extends JavaPlugin implements Listener {
- private static final Map<UUID, Double>
- POINTS = new ConcurrentHashMap<>();
- @Override
- public void onEnable() {
- Bukkit.getPluginManager().registerEvents(this, this);
- new BukkitRunnable() {
- @Override
- public void run() {
- for (UUID uuid : POINTS.keySet()) {
- double points = POINTS.get(uuid);
- if (points - 1 <= 0)
- POINTS.remove(uuid);
- else
- POINTS.put(uuid, points - 1);
- }
- }
- }.runTaskTimer(this, 3000, 3000);
- }
- @EventHandler
- public void onEvent(EntityDamageByEntityEvent e) {
- if (!(e.getDamager() instanceof Player) || !(e.getEntity() instanceof Player))
- return;
- Player player = (Player) e.getDamager();
- double reach = getReach(player);
- if (reach > 3.2) {
- double pointsBefore = POINTS.containsKey(player.getUniqueId()) ? POINTS.get(player.getUniqueId()) : 0;
- double points = pointsBefore + (reach - 3.2) * 0.5;
- POINTS.put(player.getUniqueId(), points);
- System.out.println(player.getName() + " has " + points + " points.");
- if (pointsBefore < 50 && points >= 50)
- player.kickPlayer("Our anticheat detected that you are using reach. If you are not using reach hacks feel free to join back!");
- else if (pointsBefore < 100 && points >= 100)
- player.kickPlayer("Our anticheat detected that you are using reach. If you are not using reach hacks feel free to join back!");
- else if (pointsBefore < 150 && points >= 150)
- 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)), "");
- }
- }
- public static Player getTargetPlayer(Player player, double max) {
- List<Player> possible = player.getNearbyEntities(max, max, max).stream().filter(entity -> entity instanceof Player).map(entity -> (Player) entity).filter(player::hasLineOfSight).collect(Collectors.toList());
- Ray ray = Ray.from(player);
- double d = -1;
- Player closest = null;
- for (Player player1 : possible) {
- double dis = AABB.from(player1).collidesD(ray, 0, max);
- if (dis != -1) {
- if (dis < d || d == -1) {
- d = dis;
- closest = player1;
- }
- }
- }
- return closest;
- }
- public static double getReach(Player player) {
- return getReach(player, 3.2);
- }
- private static double getReach(Player player, double distance) {
- Player target = getTargetPlayer(player, distance);
- if (target == null)
- return getReach(player, distance + 0.3);
- return distance;
- }
- public static class AABB {
- private Vector min, max; // min/max locations
- // Create Bounding Box from min/max locations.
- public AABB(Vector min, Vector max) {
- this(min.getX(), min.getY(), min.getZ(), max.getX(), max.getY(), max.getZ());
- }
- // Main constructor for AABB
- public AABB(double x1, double y1, double z1, double x2, double y2, double z2) {
- this.min = new Vector(Math.min(x1, x2), Math.min(y1, y2), Math.min(z1, z2));
- this.max = new Vector(Math.max(x1, x2), Math.max(y1, y2), Math.max(z1, z2));
- }
- private AABB(Player player) {
- this.min = getMin(player);
- this.max = getMax(player);
- }
- private Vector getMin(Player player) {
- return player.getLocation().toVector().add(new Vector(-0.3, 0, -0.3));
- }
- private Vector getMax(Player player) {
- return player.getLocation().toVector().add(new Vector(0.3, 1.8, 0.3));
- }
- // Create an AABB based on a player's hitbox
- public static AABB from(Player player) {
- return new AABB(player);
- }
- // Returns minimum x, y, or z point from inputs 0, 1, or 2.
- public double min(int i) {
- switch (i) {
- case 0:
- return min.getX();
- case 1:
- return min.getY();
- case 2:
- return min.getZ();
- default:
- return 0;
- }
- }
- // Returns maximum x, y, or z point from inputs 0, 1, or 2.
- public double max(int i) {
- switch (i) {
- case 0:
- return max.getX();
- case 1:
- return max.getY();
- case 2:
- return max.getZ();
- default:
- return 0;
- }
- }
- // Same as other collides method, but returns the distance of the nearest
- // point of collision of the ray and box, or -1 if no collision.
- public double collidesD(Ray ray, double tmin, double tmax) {
- for (int i = 0; i < 3; i++) {
- double d = 1 / ray.direction(i);
- double t0 = (min(i) - ray.origin(i)) * d;
- double t1 = (max(i) - ray.origin(i)) * d;
- if (d < 0) {
- double t = t0;
- t0 = t1;
- t1 = t;
- }
- tmin = t0 > tmin ? t0 : tmin;
- tmax = t1 < tmax ? t1 : tmax;
- if (tmax <= tmin) return -1;
- }
- return tmin;
- }
- }
- public static class Ray {
- private Vector origin, direction;
- // Create a ray at the origin pointing in a direction.
- public Ray(Vector origin, Vector direction) {
- this.origin = origin;
- this.direction = direction;
- }
- // Create a ray based on where the player is looking.
- // Origin: Player Eye Location
- // Direction: Player-looking direction
- public static Ray from(Player player) {
- return new Ray(player.getEyeLocation().toVector(), player.getLocation().getDirection());
- }
- public double origin(int i) {
- switch (i) {
- case 0:
- return origin.getX();
- case 1:
- return origin.getY();
- case 2:
- return origin.getZ();
- default:
- return 0;
- }
- }
- public double direction(int i) {
- switch (i) {
- case 0:
- return direction.getX();
- case 1:
- return direction.getY();
- case 2:
- return direction.getZ();
- default:
- return 0;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement