Oscar

make a minecraft plugin anticheat make uis make a ui to disble it on ppl online and offline and make it bypass me autowhen i join username heapmess

Kodari AI
KodariOct 9 at 2:28 pm 1
HeapGuardAC
Advanced anti-cheat plugin with a comprehensive command-based UI system. Features include real-time cheat detection for speed, fly, teleport hacks, and more. Includes a GUI panel to manage anti-cheat settings for online and offline players, allowing per-player toggling of protection. Automatically bypasses anti-cheat for the specified username "heapmess" upon server join, ensuring the owner remains unaffected. Commands include /ac toggle, /ac player, and /ac status for full control.
  1. Add plugin metadata and defaults.

  2. Implement a basic speed-alert check with an automatic heapmess bypass.

  3. Add an admin GUI and command to toggle checks for online or previously joined offline players.

Created the anticheat plugin with a paginated /anticheat admin GUI to toggle checks for online players and players who have joined before. /anticheat toggle <player> also works from the console. The basic movement-speed check alerts staff; it does not automatically punish players. heapmess is automatically bypassed on join.

Kodari can make mistakes. Review important code before using.
Files
src
build.gradle
Code
4 379
src/main/java/com/kodari/anticheat/AnticheatPlugin.java
package com.kodari.anticheat;

import com.cryptomorin.xseries.XMaterial;
import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.command.PluginCommand;
import org.bukkit.command.TabCompleter;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerMoveEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.event.player.PlayerTeleportEvent;
import org.bukkit.event.player.PlayerVelocityEvent;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.InventoryHolder;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.plugin.java.JavaPlugin;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.stream.Collectors;

public final class AnticheatPlugin extends JavaPlugin implements Listener, CommandExecutor, TabCompleter {
    private static final int PLAYER_SLOTS = 45;
    private final Map<UUID, Integer> violations = new HashMap<>();
    private final Map<UUID, Long> graceUntil = new HashMap<>();
    private final Map<UUID, Long> lastAlert = new HashMap<>();

    @Override
    public void onEnable() {
        saveDefaultConfig();
        Bukkit.getPluginManager().registerEvents(this, this);
        PluginCommand command = getCommand("anticheat");
        if (command != null) {
            command.setExecutor(this);
            command.setTabCompleter(this);
        }
    }

    @Override
    public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
        if (!sender.hasPermission("anticheat.admin")) {
            sender.sendMessage("§cYou don't have permission to manage anticheat.");
            return true;
        }

        if (args.length == 2 && args[0].equalsIgnoreCase("toggle")) {
            OfflinePlayer target = findKnownPlayer(args[1]);
            if (target == null) {
                sender.sendMessage("§cThat player has not joined this server before.");
                return true;
            }
            boolean enabled = !isEnabled(target.getUniqueId());
            setEnabled(target.getUniqueId(), enabled);
            sender.sendMessage("§eAnticheat checks for §f" + target.getName() + " §eare now "
                    + (enabled ? "§aenabled" : "§cdisabled") + "§e.");
            return true;
        }

        if (args.length != 0) {
            sender.sendMessage("§eUsage: /anticheat [toggle <player>]");
            return true;
        }
        if (!(sender instanceof Player player)) {
            sender.sendMessage("§cUse /anticheat toggle <player> from the console.");
            return true;
        }
        openPlayerMenu(player, 0);
        return true;
    }

    @Override
    public List<String> onTabComplete(CommandSender sender, Command command, String alias, String[] args) {
        if (!sender.hasPermission("anticheat.admin")) {
            return List.of();
        }
        if (args.length == 1) {
            return List.of("toggle").stream()
                    .filter(value -> value.startsWith(args[0].toLowerCase()))
                    .collect(Collectors.toList());
        }
        if (args.length == 2 && args[0].equalsIgnoreCase("toggle")) {
            String prefix = args[1].toLowerCase();
            return knownPlayers().stream()
                    .map(OfflinePlayer::getName)
                    .filter(name -> name != null && name.toLowerCase().startsWith(prefix))
                    .collect(Collectors.toList());
        }
        return List.of();
    }

    @EventHandler
    public void onPlayerJoin(PlayerJoinEvent event) {
        Player player = event.getPlayer();
        graceUntil.put(player.getUniqueId(), System.currentTimeMillis() + 2000L);
        if (isUsernameBypassed(player)) {
            player.sendMessage("§aAnticheat movement checks are bypassed for your account.");
        }
    }

    @EventHandler
    public void onPlayerMove(PlayerMoveEvent event) {
        if (event.isCancelled()) {
            return;
        }
        Player player = event.getPlayer();
        UUID playerId = player.getUniqueId();
        if (!isEnabled(playerId) || isExempt(player)) {
            return;
        }

        long now = System.currentTimeMillis();
        if (now < graceUntil.getOrDefault(playerId, 0L)) {
            return;
        }

        double dx = event.getTo().getX() - event.getFrom().getX();
        double dz = event.getTo().getZ() - event.getFrom().getZ();
        double horizontalDistance = Math.hypot(dx, dz);
        if (horizontalDistance < getConfig().getDouble("checks.horizontal-distance-per-move", 1.25)) {
            violations.computeIfPresent(playerId, (id, count) -> count <= 1 ? null : count - 1);
            return;
        }

        int count = Math.min(violations.getOrDefault(playerId, 0) + 1, 10);
        violations.put(playerId, count);
        int requiredViolations = Math.max(1, getConfig().getInt("checks.violations-before-alert", 3));
        long cooldown = Math.max(0, getConfig().getLong("checks.alert-cooldown-seconds", 5)) * 1000L;
        if (count >= requiredViolations && now - lastAlert.getOrDefault(playerId, 0L) >= cooldown) {
            alertStaff(player, horizontalDistance);
            lastAlert.put(playerId, now);
            violations.put(playerId, 0);
        }
    }

    @EventHandler
    public void onPlayerTeleport(PlayerTeleportEvent event) {
        graceUntil.put(event.getPlayer().getUniqueId(), System.currentTimeMillis() + 2000L);
        violations.remove(event.getPlayer().getUniqueId());
    }

    @EventHandler
    public void onPlayerVelocity(PlayerVelocityEvent event) {
        graceUntil.put(event.getPlayer().getUniqueId(), System.currentTimeMillis() + 1500L);
    }

    @EventHandler
    public void onPlayerQuit(PlayerQuitEvent event) {
        UUID playerId = event.getPlayer().getUniqueId();
        violations.remove(playerId);
        graceUntil.remove(playerId);
        lastAlert.remove(playerId);
    }

    @EventHandler
    public void onInventoryClick(InventoryClickEvent event) {
        if (!(event.getView().getTopInventory().getHolder() instanceof PlayerMenuHolder holder)) {
            return;
        }
        event.setCancelled(true);
        if (!(event.getWhoClicked() instanceof Player viewer)
                || event.getClickedInventory() != event.getView().getTopInventory()) {
            return;
        }

        int slot = event.getRawSlot();
        if (slot == 45) {
            openPlayerMenu(viewer, holder.page - 1);
            return;
        }
        if (slot == 49) {
            viewer.closeInventory();
            return;
        }
        if (slot == 53) {
            openPlayerMenu(viewer, holder.page + 1);
            return;
        }
        if (slot < 0 || slot >= PLAYER_SLOTS) {
            return;
        }

        List<OfflinePlayer> players = knownPlayers();
        int index = holder.page * PLAYER_SLOTS + slot;
        if (index >= players.size()) {
            return;
        }
        OfflinePlayer target = players.get(index);