suman731220

Act as an Expert Senior Minecraft Java Plugin Developer (Paper API 1.21.x). I am getting an error with the database initialization in the "UltimateTasksAndQuiz" plugin.

When use_mysql: false is set in the config, the plugin successfully switches to SQLite using HikariCP, but it fails to create the tables. Here is the exact error from the console:

[UltimateTasksAndQuiz] Failed to create database tables: [SQLITE_ERROR] SQL error or missing database (no such table: main.ultimate_player_tasks) WARN: org.sqlite.SQLiteException: [SQLITE_ERROR] SQL error or missing database (no such table: main.ultimate_player_tasks)

THE ISSUE:

This error occurs because the CREATE TABLE IF NOT EXISTS query has a syntax error specific to SQLite (most likely using MySQL's AUTO_INCREMENT instead of SQLite's AUTOINCREMENT, or having incompatible data types). Since the table creation fails silently or errors out, any subsequent queries fail because the table doesn't exist.

INSTRUCTIONS TO FIX:

  1. Fix SQLite Dialect: Open DatabaseManager.java. You must check if the plugin is using MySQL or SQLite before executing the table creation queries.
  2. Dynamic Queries: - If using MySQL: Use AUTO_INCREMENT.

- If using SQLite: Use AUTOINCREMENT (and ensure primary keys are INTEGER PRIMARY KEY AUTOINCREMENT).

  1. Execution Order: Ensure that the table creation query is fully completed and successful BEFORE any other database operations attempt to query the ultimate_player_tasks table.
  2. Default Config: Ensure use_mysql: false is the default in the config.yml generation so it defaults to the local SQLite file out-of-the-box.

Please provide the corrected DatabaseManager.java class that perfectly handles both MySQL and SQLite syntaxes without throwing "no such table" errors on first startup.

Kodari AI
KodariMar 15 at 11:13 am 16,700 (v2)

I’ll read the DatabaseManager and config files to identify and fix the SQL syntax issues.

Looking at the code, I found the critical bug! The SQLite index is being created before the table exists. The index creation statement runs inside the else block before the actual table creation statements execute.

Analysis:

  • The SQLite index creation (CREATE INDEX IF NOT EXISTS idx_uuid ON ultimate_player_tasks) runs BEFORE the tables are created

  • This causes SQLite to error because it’s trying to create an index on a non-existent table

  • The error propagates and tables never get created

Fix: Move index creation to AFTER table creation.

Fixed! The SQLite index creation now runs after the tables are created, preventing the “no such table” error. The plugin will now properly initialize SQLite on first startup with use_mysql: false.

Kodari can make mistakes. Review important code before using.
Files
src
build.gradle
manifest.kod
Code
25 2,395
src/main/java/com/example/ultimatetasksandquiz/UltimateTasksAndQuiz.java
package com.example.ultimatetasksandquiz;

import com.example.ultimatetasksandquiz.command.TasksAdminCommand;
import com.example.ultimatetasksandquiz.command.TasksCommand;
import com.example.ultimatetasksandquiz.expansion.TaskQuizExpansion;
import com.example.ultimatetasksandquiz.inventory.gui.GUIListener;
import com.example.ultimatetasksandquiz.inventory.gui.GUIManager;
import com.example.ultimatetasksandquiz.listener.ChatListener;
import com.example.ultimatetasksandquiz.listener.TaskListener;
import com.example.ultimatetasksandquiz.manager.*;
import lombok.Getter;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;

@Getter
public class UltimateTasksAndQuiz extends JavaPlugin {
    private DatabaseManager databaseManager;
    private DataManager dataManager;
    private TaskManager taskManager;
    private QuizManager quizManager;
    private GUIManager guiManager;
    private StreakManager streakManager;
    private GiveawayManager giveawayManager;
    
    @Override
    public void onEnable() {
        saveDefaultConfig();
        
        getLogger().info("Initializing database connection...");
        databaseManager = new DatabaseManager(this);
        
        databaseManager.waitUntilReady().thenRun(() -> {
            getLogger().info("Database ready! Initializing plugin components...");
            
            Bukkit.getScheduler().runTask(this, () -> {
                dataManager = new DataManager(this, databaseManager);
                taskManager = new TaskManager(this, dataManager);
                quizManager = new QuizManager(this);
                guiManager = new GUIManager();
                streakManager = new StreakManager(this);
                giveawayManager = new GiveawayManager(this);
                
                Bukkit.getPluginManager().registerEvents(new GUIListener(guiManager), this);
                Bukkit.getPluginManager().registerEvents(new TaskListener(this, taskManager), this);
                Bukkit.getPluginManager().registerEvents(new ChatListener(quizManager), this);
                
                getCommand("tasks").setExecutor(new TasksCommand(this));
                getCommand("tasksadmin").setExecutor(new TasksAdminCommand(this));
                
                if (Bukkit.getPluginManager().getPlugin("PlaceholderAPI") != null) {
                    new TaskQuizExpansion(this).register();
                    getLogger().info("PlaceholderAPI hooked successfully!");
                } else {
                    getLogger().warning("PlaceholderAPI not found. Placeholders will not work.");
                }
                
                Bukkit.getScheduler().runTaskLater(this, () -> {
                    for (Player player : Bukkit.getOnlinePlayers()) {
                        dataManager.loadPlayerDataAsync(player.getUniqueId());
                    }
                }, 40L);
                
                getLogger().info("=========================================");
                getLogger().info("UltimateTasksAndQuiz has been enabled!");
                getLogger().info("Database: " + databaseManager.getDatabaseType());
                getLogger().info("Daily Streak System: " + (getConfig().getBoolean("streaks.enabled") ? "ENABLED" : "DISABLED"));
                getLogger().info("Global Giveaway System: " + (getConfig().getBoolean("global_giveaway.enabled") ? "ENABLED" : "DISABLED"));
                getLogger().info("PlaceholderAPI: " + (Bukkit.getPluginManager().getPlugin("PlaceholderAPI") != null ? "HOOKED" : "NOT FOUND"));
                getLogger().info("=========================================");
            });
        });
    }
    
    @Override
    public void onDisable() {
        if (taskManager != null) {
            for (Player player : Bukkit.getOnlinePlayers()) {
                taskManager.removePlayerBossBar(player);
            }
        }
        
        if (dataManager != null) {
            dataManager.saveAllData();
        }
        
        if (databaseManager != null) {
            databaseManager.shutdown();
        }
        
        getLogger().info("UltimateTasksAndQuiz has been disabled!");
    }
}