Sabotage XX

Compile it

Kodari AI
KodariNov 15 at 8:25 am 1
  1. Click Compile in the top bar. On mobile, tap ⋮ → Compile Project.

  2. Click Compile in the window.

  3. Once it succeeds, click Download on the build.

Kodari can make mistakes. Review important code before using.
Files
src
build.gradle.kts
settings.gradle.kts
Code
5 199
src/main/java/dev/afkbot/AfkBotPlugin.java
package dev.afkbot;

import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.ArmorStand;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.scheduler.BukkitTask;

public final class AfkBotPlugin extends JavaPlugin {

    private static final String NAME = "afkbot24/7";

    private ArmorStand bot;
    private BukkitTask task;
    private Location home;
    private int tick;

    @Override
    public void onEnable() {
        Bukkit.getScheduler().runTaskLater(this, this::safeSpawn, 100L);
    }

    @Override
    public void onDisable() {
        removeBot();
    }

    @Override
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
        if (args.length == 0) {
            sender.sendMessage("Usage: /afkbot <spawn|remove|status|refresh>");
            return true;
        }
        switch (args[0].toLowerCase()) {
            case "spawn" -> {
                try {
                    spawnBot();
                } catch (Throwable t) {
                    getLogger().log(java.util.logging.Level.SEVERE, "Bot spawn failed", t);
                    sender.sendMessage("Spawn FAILED: " + t + " (see console)");
                    return true;
                }
                sender.sendMessage("AFK display spawned.");
            }
            case "refresh" -> sender.sendMessage(isBotOnline()
                    ? "AFK display is already visible to nearby players."
                    : "AFK display is not spawned.");
            case "status" -> sender.sendMessage(isBotOnline()
                    ? "AFK display is active as " + NAME : "AFK display is not active.");
            case "remove" -> {
                removeBot();
                sender.sendMessage("AFK display removed.");
            }
            default -> sender.sendMessage("Usage: /afkbot <spawn|remove|status|refresh>");
        }
        return true;
    }

    @Override
    public java.util.List<String> onTabComplete(CommandSender sender, Command cmd, String alias, String[] args) {
        if (args.length == 1) {
            return java.util.List.of("spawn", "remove", "status", "refresh").stream()
                    .filter(s -> s.startsWith(args[0].toLowerCase()))
                    .toList();
        }
        return java.util.List.of();
    }

    private void spawnBot() {
        if (isBotOnline()) return;

        World world = Bukkit.getWorlds().get(0);
        home = world.getSpawnLocation().add(0.5, 0, 0.5);
        bot = world.spawn(home, ArmorStand.class);
        bot.setCustomName(NAME);
        bot.setCustomNameVisible(true);
        bot.setGravity(false);
        bot.setMarker(false);
        bot.setArms(true);

        tick = 0;
        task = Bukkit.getScheduler().runTaskTimer(this, this::tickBot, 40L, 1L);
        getLogger().info("AFK display '" + NAME + "' spawned.");
    }

    private void removeBot() {
        if (task != null) {
            task.cancel();
            task = null;
        }
        if (bot != null) {
            bot.remove();
            bot = null;
        }
    }

    private boolean isBotOnline() {
        return bot != null && bot.isValid();
    }

    private void safeSpawn() {
        try {
            spawnBot();
        } catch (Throwable t) {
            getLogger().log(java.util.logging.Level.SEVERE, "Bot spawn failed", t);
        }
    }

    private void tickBot() {
        if (!isBotOnline()) {
            if (task != null) task.cancel();
            task = null;
            bot = null;
            Bukkit.getScheduler().runTaskLater(this, this::safeSpawn, 100L);
            return;
        }
        tick++;

        double x = 0;
        double z = 0;
        switch ((tick / 40) % 4) {
            case 0 -> x = 1;
            case 1 -> x = -1;
            case 2 -> z = 1;
            default -> z = -1;
        }

        Location current = bot.getLocation();
        double y = tick % 60 < 3 ? 0.25 : 0;
        Location next = current.add(x * 0.04, y, z * 0.04);
        next.setYaw(current.getYaw() + 3);
        bot.teleport(next);

        if (home != null && bot.getLocation().distanceSquared(home) > 25) {
            bot.teleport(home);
        }
    }
}