<commit_before>package net.krinsoft.jobsuite;

import net.krinsoft.jobsuite.db.Database;
import org.bukkit.command.CommandSender;
import org.bukkit.command.ConsoleCommandSender;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.Player;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author krinsdeath
 */
public class JobManager {
    private final JobCore plugin;
    private Database database;

    private Map<Integer, Job> jobs = new HashMap<Integer, Job>();
    private List<Job> claims = new ArrayList<Job>();

    private int nextJob;

    public JobManager(JobCore instance) {
        this.plugin = instance;
    }

    public void load() {
        database = new Database(plugin);
        nextJob = plugin.getConfig().getInt("jobs.total", 0);
    }

    public void close() {
        persist();
        database.close();
    }

    public void persist() {
        PreparedStatement schema = database.prepare("REPLACE INTO jobsuite_schema (id, NEXT_ID) VALUES (?, ?);");
        PreparedStatement jobStatement = database.prepare("REPLACE INTO jobsuite_base (job_id, owner, name, description, expiry, reward, lock, finished, claimed) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?);");
        PreparedStatement itemStatement = database.prepare("REPLACE INTO jobsuite_items (job_id, item_entry, enchantment_entry, type, amount) VALUES (?, ?, ?, ?, ?);");
        PreparedStatement enchStatement = database.prepare("REPLACE INTO jobsuite_enchantments (job_id, enchantment_entry, enchantment, power) VALUES (?, ?, ?, ?);");
        try {
            schema.setInt(1, 1);
            schema.setInt(2, nextJob);
            schema.executeUpdate();
            jobStatement.getConnection().setAutoCommit(false);
            for (Map.Entry<Integer, Job> entry : jobs.entrySet()) {
                Job job = entry.getValue();
                if (job.isExpired()) {
                    continue;
                }
                jobStatement.setInt(1, job.getId());
                jobStatement.setString(2, job.getOwner());
                jobStatement.setString(3, job.getName());
                jobStatement.setString(4, job.getDescription());
                jobStatement.setLong(5, job.getDuration());
                jobStatement.setDouble(6, job.getReward());
                jobStatement.setString(7, job.getLock());
                jobStatement.setBoolean(8, job.isFinished());
                jobStatement.setBoolean(9, job.isClaimed());
                jobStatement.executeUpdate();
                for (JobItem item : job.getItems()) {
                    itemStatement.setInt(1, job.getId());
                    itemStatement.setInt(2, item.getId());
                    itemStatement.setInt(3, item.hashCode());
                    itemStatement.setString(4, item.getItem().getType().toString());
                    itemStatement.setInt(5, item.getItem().getAmount());
                    itemStatement.executeUpdate();
                    for (Map.Entry<Enchantment, Integer> ench : item.getItem().getEnchantments().entrySet()) {
                        enchStatement.setInt(1, job.getId());
                        enchStatement.setInt(2, item.hashCode());
                        enchStatement.setInt(3, ench.getKey().getId());
                        enchStatement.setInt(4, ench.getValue());
                        enchStatement.executeUpdate();
                    }
                }
            }
            schema.getConnection().commit();
        } catch (SQLException e) {
            plugin.getLogger().warning("An SQLException occurred: " + e.getMessage());
        }
    }

    public List<Job> getJobs() {
        return new ArrayList<Job>(jobs.values());
    }

    /**
     * Gets the specified Job by its ID
     * @param id The ID of the job we're fetching
     * @return The Job and all of its details, otherwise null
     */
    public Job getJob(int id) {
        return jobs.get(id);
    }

    public boolean addJob(Job job) {
        plugin.getLogger().finer("Job '" + job.getName() + "' registered.");
        jobs.put(job.getId(), job);
        return true;
    }

    /**
     * Registers the specified job
     * @param owner The owner of the job
     * @return true if the job is valid and added, otherwise false
     */
    public boolean addJob(String owner) {
        if (owner == null) { return false; }
        Job job = queued.get(owner);
        if (job == null) { return false; }
        jobs.put(job.getId(), job);
        queued.remove(owner);
        plugin.getConfig().set("jobs.total", nextJob);
        return true;
    }

    /**
     * Attempts to cancel the specified job.
     * @param sender The person who is issuing the command.
     * @param job The job we're attempting to cancel.
     * @return true if the job was successfully canceled, otherwise false.
     */
    public boolean cancelJob(CommandSender sender, Job job) {
        Job cancel = jobs.remove(job.getId());
        if (cancel != null && (cancel.getOwner().equals(sender.getName()) || cancel.getLock().equals(sender.getName()) || sender.hasPermission("jobsuite.admin.cancel") || cancel.isExpired())) {
            PreparedStatement basePrep = database.prepare("DELETE FROM jobsuite_base WHERE job_id = ? ;");
            PreparedStatement itemPrep = database.prepare("DELETE FROM jobsuite_items WHERE job_id = ? ;");
            PreparedStatement enchPrep = database.prepare("DELETE FROM jobsuite_enchantments WHERE job_id = ? ;");
            try {
                basePrep.setInt(1, cancel.getId());
                basePrep.executeUpdate();
                itemPrep.setInt(1, cancel.getId());
                itemPrep.executeUpdate();
                enchPrep.setInt(1, cancel.getId());
                enchPrep.executeUpdate();
            } catch (SQLException e) {
                plugin.getLogger().warning("An SQLException occurred: " + e.getMessage());
            }
        }
        return cancel != null;
    }
<commit_msg>Fixed JobManager.cancelJob() not properly checking credentials.<commit_after>package net.krinsoft.jobsuite;

import net.krinsoft.jobsuite.db.Database;
import org.bukkit.command.CommandSender;
import org.bukkit.command.ConsoleCommandSender;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.Player;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author krinsdeath
 */
public class JobManager {
    private final JobCore plugin;
    private Database database;

    private Map<Integer, Job> jobs = new HashMap<Integer, Job>();
    private List<Job> claims = new ArrayList<Job>();

    private int nextJob;

    public JobManager(JobCore instance) {
        this.plugin = instance;
    }

    public void load() {
        database = new Database(plugin);
        nextJob = plugin.getConfig().getInt("jobs.total", 0);
    }

    public void close() {
        persist();
        database.close();
    }

    public void persist() {
        PreparedStatement schema = database.prepare("REPLACE INTO jobsuite_schema (id, NEXT_ID) VALUES (?, ?);");
        PreparedStatement jobStatement = database.prepare("REPLACE INTO jobsuite_base (job_id, owner, name, description, expiry, reward, lock, finished, claimed) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?);");
        PreparedStatement itemStatement = database.prepare("REPLACE INTO jobsuite_items (job_id, item_entry, enchantment_entry, type, amount) VALUES (?, ?, ?, ?, ?);");
        PreparedStatement enchStatement = database.prepare("REPLACE INTO jobsuite_enchantments (job_id, enchantment_entry, enchantment, power) VALUES (?, ?, ?, ?);");
        try {
            schema.setInt(1, 1);
            schema.setInt(2, nextJob);
            schema.executeUpdate();
            jobStatement.getConnection().setAutoCommit(false);
            for (Map.Entry<Integer, Job> entry : jobs.entrySet()) {
                Job job = entry.getValue();
                if (job.isExpired()) {
                    continue;
                }
                jobStatement.setInt(1, job.getId());
                jobStatement.setString(2, job.getOwner());
                jobStatement.setString(3, job.getName());
                jobStatement.setString(4, job.getDescription());
                jobStatement.setLong(5, job.getDuration());
                jobStatement.setDouble(6, job.getReward());
                jobStatement.setString(7, job.getLock());
                jobStatement.setBoolean(8, job.isFinished());
                jobStatement.setBoolean(9, job.isClaimed());
                jobStatement.executeUpdate();
                for (JobItem item : job.getItems()) {
                    itemStatement.setInt(1, job.getId());
                    itemStatement.setInt(2, item.getId());
                    itemStatement.setInt(3, item.hashCode());
                    itemStatement.setString(4, item.getItem().getType().toString());
                    itemStatement.setInt(5, item.getItem().getAmount());
                    itemStatement.executeUpdate();
                    for (Map.Entry<Enchantment, Integer> ench : item.getItem().getEnchantments().entrySet()) {
                        enchStatement.setInt(1, job.getId());
                        enchStatement.setInt(2, item.hashCode());
                        enchStatement.setInt(3, ench.getKey().getId());
                        enchStatement.setInt(4, ench.getValue());
                        enchStatement.executeUpdate();
                    }
                }
            }
            schema.getConnection().commit();
        } catch (SQLException e) {
            plugin.getLogger().warning("An SQLException occurred: " + e.getMessage());
        }
    }

    public List<Job> getJobs() {
        return new ArrayList<Job>(jobs.values());
    }

    /**
     * Gets the specified Job by its ID
     * @param id The ID of the job we're fetching
     * @return The Job and all of its details, otherwise null
     */
    public Job getJob(int id) {
        return jobs.get(id);
    }

    public boolean addJob(Job job) {
        plugin.getLogger().finer("Job '" + job.getName() + "' registered.");
        jobs.put(job.getId(), job);
        return true;
    }

    /**
     * Registers the specified job
     * @param owner The owner of the job
     * @return true if the job is valid and added, otherwise false
     */
    public boolean addJob(String owner) {
        if (owner == null) { return false; }
        Job job = queued.get(owner);
        if (job == null) { return false; }
        jobs.put(job.getId(), job);
        queued.remove(owner);
        plugin.getConfig().set("jobs.total", nextJob);
        return true;
    }

    /**
     * Attempts to cancel the specified job.
     * @param sender The person who is issuing the command.
     * @param job The job we're attempting to cancel.
     * @return true if the job was successfully canceled, otherwise false.
     */
    public boolean cancelJob(CommandSender sender, Job job) {
        if (!(job != null && (job.getOwner().equals(sender.getName()) || sender.hasPermission("jobsuite.admin.cancel") || job.isExpired()))) {
            return false;
        }
        Job cancel = jobs.remove(job.getId());
        if (cancel != null) {
            if (sender instanceof Player) {
                plugin.getBank().give((Player) sender, cancel.getReward(), -1);
            }
            PreparedStatement basePrep = database.prepare("DELETE FROM jobsuite_base WHERE job_id = ? ;");
            PreparedStatement itemPrep = database.prepare("DELETE FROM jobsuite_items WHERE job_id = ? ;");
            PreparedStatement enchPrep = database.prepare("DELETE FROM jobsuite_enchantments WHERE job_id = ? ;");
            try {
                basePrep.setInt(1, cancel.getId());
                basePrep.executeUpdate();
                itemPrep.setInt(1, cancel.getId());
                itemPrep.executeUpdate();
                enchPrep.setInt(1, cancel.getId());
                enchPrep.executeUpdate();
            } catch (SQLException e) {
                plugin.getLogger().warning("An SQLException occurred: " + e.getMessage());
            }
        }
        return cancel != null;
    }

{'fraction_non_alphanumeric': 0.12599693869330542, 'fraction_numerical': 0.004108595826955611, 'mean_word_length': 2.2168955688002074, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4148014', 'n_tokens_mistral': 3441, 'n_tokens_neox': 3304, 'n_words': 769}