rohitmaurya799123
๐Ÿ“Ž 1 file 6,671 chars
๐Ÿ“„
pasted-code-1794116454412.txt
txt 175 lines
โ–ถ
package com.studgamers.anticheat;

import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.GameMode;
import org.bukkit.Location;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.Action;
import org.bukkit.event.entity.EntityDamageByEntityEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.event.player.PlayerMoveEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.plugin.java.JavaPlugin;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

public class WatchDogLite extends JavaPlugin implements Listener {

    // Thresholds
    private final int MAX_CPS = 18;
    private final double MAX_REACH = 4.2; // Slightly lenient for latency
    private final double MAX_SPEED = 0.65; // Max horizontal block distance per tick

    // Data tracking
    private final Map lastClickTime = new HashMap<>();
    private final Map clicksPerSecond = new HashMap<>();

    @Override
    public void onEnable() {
        getServer().getPluginManager().registerEvents(this, this);
        getLogger().info("WatchDogLite Enabled. Running in Alert-Only mode.");
    }

    @Override
    public void onDisable() {
        lastClickTime.clear();
        clicksPerSecond.clear();
    }

    /**
     * Core alert system. Sends notifications to Operators.
     */
    private void alertStaff(Player player, String cheatType, String details) {
        String alertMessage = ChatColor.translateAlternateColorCodes('&', 
            "&8[&cWatchDog&8] &e" + player.getName() + " &7failed &c" + cheatType + " &8(&7" + details + "&8)");

        // Notify console
        getLogger().warning("[ALERT] " + player.getName() + " flagged for " + cheatType + " (" + details + ")");

        // Notify online admins/operators
        for (Player online : Bukkit.getOnlinePlayers()) {
            if (online.hasPermission("watchdog.notify")) {
                online.sendMessage(alertMessage);
            }
        }
    }

    /**
     * Bedrock/Geyser Cross-play Check
     * Bedrock players naturally move differently. Since we aren't using the Floodgate API,
     * we check if their name starts with common Geyser prefixes (* or .) to ignore them.
     */
    private boolean isBedrockPlayer(Player player) {
        String name = player.getName();
        return name.startsWith("*") || name.startsWith(".");
    }

    /**
     * Permission and state validation
     */
    private boolean shouldBypass(Player player) {
        return player.hasPermission("watchdog.bypass") || 
               player.getGameMode() == GameMode.CREATIVE || 
               player.getGameMode() == GameMode.SPECTATOR ||
               player.isFlying();
    }

    // --- CLEANUP ---
    @EventHandler
    public void onQuit(PlayerQuitEvent event) {
        UUID uuid = event.getPlayer().getUniqueId();
        lastClickTime.remove(uuid);
        clicksPerSecond.remove(uuid);
    }

    // --- COMBAT: AUTOCLICKER ---
    @EventHandler
    public void onClick(PlayerInteractEvent event) {
        if (event.getAction() != Action.LEFT_CLICK_AIR && event.getAction() != Action.LEFT_CLICK_BLOCK) return;
        
        Player player = event.getPlayer();
        if (shouldBypass(player)) return;

        UUID uuid = player.getUniqueId();
        long now = System.currentTimeMillis();

        lastClickTime.putIfAbsent(uuid, now);
        clicksPerSecond.putIfAbsent(uuid, 0);

        // Reset CPS counter every second
        if (now - lastClickTime.get(uuid) >= 1000) {
            int cps = clicksPerSecond.get(uuid);
            if (cps > MAX_CPS) {
                alertStaff(player, "AutoClicker", "CPS: " + cps);
            }
            clicksPerSecond.put(uuid, 1);
            lastClickTime.put(uuid, now);
        } else {
            clicksPerSecond.put(uuid, clicksPerSecond.get(uuid) + 1);
        }
    }

    // --- COMBAT: REACH ---
    @EventHandler
    public void onDamage(EntityDamageByEntityEvent event) {
        if (!(event.getDamager() instanceof Player)) return;
        
        Player player = (Player) event.getDamager();
        if (shouldBypass(player)) return;

        Location attackerLoc = player.getLocation();
        Location victimLoc = event.getEntity().getLocation();
        
        double distance = attackerLoc.distance(victimLoc);

        if (distance > MAX_REACH) {
            // Check if player is not using a projectile (bow/snowball)
            if (event.getCause() == EntityDamageByEntityEvent.DamageCause.ENTITY_ATTACK) {
                alertStaff(player, "Reach", "Distance: " + String.format("%.2f", distance) + " blocks");
            }
        }
    }

    // --- MOVEMENT: SPEED / FLY ---
    @EventHandler
    public void onMove(PlayerMoveEvent event) {
        Player player = event.getPlayer();
        
        // Skip checks for Bedrock players to prevent massive false positives
        if (shouldBypass(player) || isBedrockPlayer(player)) return;
        
        Location from = event.getFrom();
        Location to = event.getTo();
        if (to == null) return;

        // Calculate horizontal distance traveled in one tick
        double deltaX = Math.abs(to.getX() - from.getX());
        double deltaZ = Math.abs(to.getZ() - from.getZ());
        double horizontalDistance = Math.sqrt((deltaX * deltaX) + (deltaZ * deltaZ));
        double verticalDistance = to.getY() - from.getY();

        // Speed Check
        // Walking/Sprinting limit without potions is generally under 0.4 per tick, 0.65 accounts for latency/ice
        if (horizontalDistance > MAX_SPEED) {
            // Ignore if in a vehicle or gliding
            if (!player.isInsideVehicle() && !player.isGliding()) {
                alertStaff(player, "Speed", "Moved " + String.format("%.2f", horizontalDistance) + " blocks/tick");
            }
        }

        // Basic Fly / AirWalk Check
        // If a player moves horizontally without losing Y altitude while not standing on ground
        if (verticalDistance == 0.0 && horizontalDistance > 0.1 && !player.isOnGround()) {
            // Check the block beneath them to account for client/server ground state desync
            Location below = player.getLocation().subtract(0, 0.5, 0);
            if (below.getBlock().getType().isAir()) {
                alertStaff(player, "Flight", "Hovering in air");
            }
        }
    }
}
Kodari AI
KodariNov 8 at 5:45 am 1
WatchDogLite
A lightweight anti-cheat plugin for Bukkit servers that detects and alerts staff to common cheats. Features include autoclicker detection via CPS tracking, reach detection through distance calculations, and speed/fly detection using movement analysis. Includes permission-based bypass system, Bedrock player detection for Geyser compatibility, and alert notifications to staff with watchdog.notify permission. Runs in alert-only mode with configurable thresholds for maximum CPS, reach distance, and movement speed.
  • Create WatchDogLite with the supplied alert-only checks.

  • Register its notification and bypass permissions.

Created WatchDogLite with the supplied alert-only checks and staff permissions.

The Bedrock exemption uses name prefixes, not Floodgate detection. These basic checks can produce false positives.

Kodari can make mistakes. Review important code before using.
Files
src
build.gradle
Code
3 161
src/main/java/com/studgamers/anticheat/WatchDogLite.java
package com.studgamers.anticheat;

import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.GameMode;
import org.bukkit.Location;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.Action;
import org.bukkit.event.entity.EntityDamageByEntityEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.event.player.PlayerMoveEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.plugin.java.JavaPlugin;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

public class WatchDogLite extends JavaPlugin implements Listener {
    private static final int MAX_CPS = 18;
    private static final double MAX_REACH = 4.2;
    private static final double MAX_SPEED = 0.65;

    private final Map<UUID, Long> lastClickTime = new HashMap<>();
    private final Map<UUID, Integer> clicksPerSecond = new HashMap<>();

    @Override
    public void onEnable() {
        getServer().getPluginManager().registerEvents(this, this);
        getLogger().info("WatchDogLite enabled in alert-only mode.");
    }

    @Override
    public void onDisable() {
        lastClickTime.clear();
        clicksPerSecond.clear();
    }

    private void alertStaff(Player player, String cheatType, String details) {
        String message = ChatColor.translateAlternateColorCodes('&',
                "&8[&cWatchDog&8] &e" + player.getName() + " &7failed &c" + cheatType + " &8(&7" + details + "&8)");
        getLogger().warning("[ALERT] " + player.getName() + " flagged for " + cheatType + " (" + details + ")");
        for (Player online : Bukkit.getOnlinePlayers()) {
            if (online.hasPermission("watchdog.notify")) {
                online.sendMessage(message);
            }
        }
    }

    private boolean isBedrockPlayer(Player player) {
        String name = player.getName();
        return name.startsWith("*") || name.startsWith(".");
    }

    private boolean shouldBypass(Player player) {
        return player.hasPermission("watchdog.bypass")
                || player.getGameMode() == GameMode.CREATIVE
                || player.getGameMode() == GameMode.SPECTATOR
                || player.isFlying();
    }

    @EventHandler
    public void onQuit(PlayerQuitEvent event) {
        UUID uuid = event.getPlayer().getUniqueId();
        lastClickTime.remove(uuid);
        clicksPerSecond.remove(uuid);
    }

    @EventHandler
    public void onClick(PlayerInteractEvent event) {
        if (event.getAction() != Action.LEFT_CLICK_AIR && event.getAction() != Action.LEFT_CLICK_BLOCK) return;
        Player player = event.getPlayer();
        if (shouldBypass(player)) return;

        UUID uuid = player.getUniqueId();
        long now = System.currentTimeMillis();
        lastClickTime.putIfAbsent(uuid, now);
        clicksPerSecond.putIfAbsent(uuid, 0);
        if (now - lastClickTime.get(uuid) >= 1000) {
            int cps = clicksPerSecond.get(uuid);
            if (cps > MAX_CPS) {
                alertStaff(player, "AutoClicker", "CPS: " + cps);
            }
            clicksPerSecond.put(uuid, 1);
            lastClickTime.put(uuid, now);
        } else {
            clicksPerSecond.put(uuid, clicksPerSecond.get(uuid) + 1);
        }
    }

    @EventHandler
    public void onDamage(EntityDamageByEntityEvent event) {
        if (!(event.getDamager() instanceof Player)) return;
        Player player = (Player) event.getDamager();
        if (shouldBypass(player)) return;

        Location attacker = player.getLocation();
        Location victim = event.getEntity().getLocation();
        double distance = attacker.distance(victim);
        if (distance > MAX_REACH && event.getCause() == EntityDamageByEntityEvent.DamageCause.ENTITY_ATTACK) {
            alertStaff(player, "Reach", "Distance: " + String.format("%.2f", distance) + " blocks");
        }
    }

    @EventHandler
    public void onMove(PlayerMoveEvent event) {
        Player player = event.getPlayer();
        if (shouldBypass(player) || isBedrockPlayer(player)) return;

        Location from = event.getFrom();
        Location to = event.getTo();
        if (to == null) return;
        double deltaX = to.getX() - from.getX();
        double deltaZ = to.getZ() - from.getZ();
        double horizontalDistance = Math.sqrt(deltaX * deltaX + deltaZ * deltaZ);
        double verticalDistance = to.getY() - from.getY();

        if (horizontalDistance > MAX_SPEED && !player.isInsideVehicle() && !player.isGliding()) {
            alertStaff(player, "Speed", "Moved " + String.format("%.2f", horizontalDistance) + " blocks/tick");
        }
        if (verticalDistance == 0.0 && horizontalDistance > 0.1 && !player.isOnGround()) {
            Location below = player.getLocation().subtract(0, 0.5, 0);
            if (below.getBlock().getType().isAir()) {
                alertStaff(player, "Flight", "Hovering in air");
            }
        }
    }
}