Files
cookie
src
build.gradle
settings.gradle
Code
10 1,735
src/main/java/com/kodari/cookieai/CookieAI.java
package com.kodari.cookieai;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.command.TabExecutor;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Mob;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.LinkOption;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.nio.file.StandardOpenOption;
import java.time.Duration;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.logging.Level;
import java.util.stream.Stream;
public final class CookieAI extends JavaPlugin implements TabExecutor {
private final Set<UUID> developerMode = new HashSet<>();
private final Set<UUID> pending = new HashSet<>();
private final Set<String> blacklist = new HashSet<>();
private final List<String> memory = new ArrayList<>();
private File dataFile;
private YamlConfiguration data;
private HttpClient http;
private WebTools web;
private static final int CONFIG_VERSION = 2;
private volatile String knowledge = "";
private final Map<UUID, List<String[]>> history = new HashMap<>();
private final Map<UUID, Pending> awaiting = new HashMap<>();
private static final class Pending {
final String response;
final List<WebTools.Download> downloads;
final long created = System.currentTimeMillis();
Pending(String response, List<WebTools.Download> downloads) {
this.response = response;
this.downloads = downloads;
}
}
@Override
public void onEnable() {
saveDefaultConfig();
migrateConfig();
dataFile = new File(getDataFolder(), "memory.yml");
data = YamlConfiguration.loadConfiguration(dataFile);
memory.addAll(data.getStringList("developer-memory"));
for (String name : data.getStringList("blacklist")) {
blacklist.add(name.toLowerCase(Locale.ROOT));
}
http = HttpClient.newBuilder().connectTimeout(Duration.ofSeconds(10)).build();
web = new WebTools(this);
loadKnowledge();
getCommand("cookieai").setExecutor(this);
getCommand("cookieai").setTabCompleter(this);
}
private boolean owner(String name) {
return name.equalsIgnoreCase("Frizz") || name.equalsIgnoreCase("izzzfr");
}
private boolean allowed(Player player) {
return owner(player.getName()) || (player.isOp() && !blacklist.contains(player.getName().toLowerCase(Locale.ROOT)));
}
private void saveData() {
data.set("developer-memory", memory);
data.set("blacklist", new ArrayList<>(blacklist));
try {
data.save(dataFile);
} catch (IOException e) {
getLogger().log(Level.SEVERE, "Could not save Cookie AI memory", e);
}
}
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
if (args.length == 1 && args[0].equalsIgnoreCase("reload") && !(sender instanceof Player)) {
reloadConfig();
loadKnowledge();
sender.sendMessage("Cookie AI configuration reloaded. AI enabled: " + getConfig().getBoolean("enabled", true));
return true;
}
if (!(sender instanceof Player)) {
sender.sendMessage("Only players can use Cookie AI.");
return true;
}
Player player = (Player) sender;
if (!allowed(player)) {
player.sendMessage("Can't Execute Command.");
return true;
}
if (!getConfig().getBoolean("enabled", true)) {
player.sendMessage(ChatColor.RED + "Cookie AI is disabled in its configuration.");
return true;
}
if (args.length == 0) {
player.sendMessage(ChatColor.YELLOW + "Usage: /cookieai <prompt>");
return true;
}
String prompt = String.join(" ", args);
if (prompt.equalsIgnoreCase("reset")) {
synchronized (history) {
history.remove(player.getUniqueId());
}
awaiting.remove(player.getUniqueId());
player.sendMessage(ChatColor.YELLOW + "Cookie AI conversation memory cleared.");
return true;
}
if (prompt.equalsIgnoreCase("confirm") || prompt.equalsIgnoreCase("cancel")) {
Pending waiting = awaiting.remove(player.getUniqueId());
if (waiting == null || System.currentTimeMillis() - waiting.created > 300_000L) {
player.sendMessage(ChatColor.YELLOW + "Nothing is waiting for confirmation.");
} else if (prompt.equalsIgnoreCase("cancel")) {
player.sendMessage(ChatColor.YELLOW + "Cancelled. Nothing was run.");
} else {
executeResponse(player, waiting.response, Math.max(1, Math.min(100, getConfig().getInt("limits.max-commands", 30))), waiting.downloads);
}
return true;
}
if (prompt.equalsIgnoreCase("setup")) {
developerMode.add(player.getUniqueId());
player.sendMessage(ChatColor.GREEN + "Developer mode enabled. Your /cookieai prompts will be saved as persistent instructions. Use /cookieai developer to exit.");
return true;
}
if (prompt.equalsIgnoreCase("developer")) {
developerMode.remove(player.getUniqueId());
player.sendMessage(ChatColor.YELLOW + "Developer mode disabled.");
return true;
}
if (developerMode.contains(player.getUniqueId())) {
if (prompt.length() > Math.max(1, getConfig().getInt("limits.max-memory-entry-length", 1000)) || memory.size() >= Math.max(1, getConfig().getInt("limits.max-memory-entries", 100))) {
player.sendMessage(ChatColor.RED + "Memory limit reached or instruction is too long. Edit memory.yml to remove instructions.");
return true;
}
memory.add(prompt);
saveData();
player.sendMessage(ChatColor.GREEN + "Developer instruction saved.");
return true;
}
if (prompt.length() > Math.max(1, getConfig().getInt("limits.max-prompt-length", 3000))) {
player.sendMessage(ChatColor.RED + "Prompt is too long.");
return true;
}
String key = getConfig().getString("groq.api-key", "").trim();
if (key.isEmpty() || key.equals("PUT_YOUR_GROQ_API_KEY_HERE")) {
player.sendMessage(ChatColor.RED + "Set your Groq API key in plugins/CookieAI/config.yml and restart the server.");
return true;
}
if (!pending.add(player.getUniqueId())) {
player.sendMessage(ChatColor.YELLOW + "Cookie AI is still processing your previous request.");
return true;
}
awaiting.remove(player.getUniqueId());
Location location = player.getLocation();
String context = "Player: " + player.getName() + "; world: " + location.getWorld().getName()
+ "; position: " + location.getBlockX() + " " + location.getBlockY() + " " + location.getBlockZ()
+ "; facing: " + player.getFacing().name() + "; Minecraft version: " + Bukkit.getBukkitVersion()
+ "; privileged blacklist owner: " + owner(player.getName())
+ "; worlds: " + worldNames() + "; online players: " + onlineNames();
String instructions = String.join("\n", memory);
String model = getConfig().getString("groq.model", "llama-3.3-70b-versatile");
int timeout = Math.max(5, Math.min(120, getConfig().getInt("groq.timeout-seconds", 45)));
int tokens = Math.max(256, Math.min(8192, getConfig().getInt("groq.max-output-tokens", 4096)));
int maxCommands = Math.max(1, Math.min(100, getConfig().getInt("limits.max-commands", 30)));
UUID id = player.getUniqueId();