<commit_before>package com.skelril.aurora.jail;

import com.sk89q.commandbook.CommandBook;
import com.sk89q.commandbook.CommandBookUtil;
import com.sk89q.commandbook.util.PlayerUtil;
import com.sk89q.minecraft.util.commands.*;
import com.skelril.aurora.admin.AdminComponent;
import com.skelril.aurora.events.PrayerApplicationEvent;
import com.skelril.aurora.util.ChanceUtil;
import com.skelril.aurora.util.ChatUtil;
import com.zachsthings.libcomponents.ComponentInformation;
import com.zachsthings.libcomponents.Depend;
import com.zachsthings.libcomponents.InjectComponent;
import com.zachsthings.libcomponents.bukkit.BukkitComponent;
import com.zachsthings.libcomponents.config.ConfigurationBase;
import com.zachsthings.libcomponents.config.Setting;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.Server;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerCommandPreprocessEvent;
import org.bukkit.event.player.PlayerInteractEntityEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.event.player.PlayerKickEvent;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;

/**
 * Author: Turtle9598
 */
@ComponentInformation(friendlyName = "Jail", desc = "Jail System")
@Depend(plugins = {"WorldEdit"}, components = {AdminComponent.class})
public class JailComponent extends BukkitComponent implements Listener, Runnable {

    private final CommandBook inst = CommandBook.inst();
    private final Logger log = CommandBook.logger();
    private final Server server = CommandBook.server();

    @InjectComponent
    private AdminComponent adminComponent;

    private InmateDatabase inmates;
    private JailCellDatabase jailCells;
    private LocalConfiguration config;
    private Map<Player, JailCell> cell = new HashMap<>();
    private static final int MOVE_THRESHOLD = 8;
    private static final int MOVE_THRESHOLD_SQ = MOVE_THRESHOLD * MOVE_THRESHOLD;

    @Override
    public void enable() {

        //super.enable();
        config = configure(new LocalConfiguration());

        // Setup the inmates database
        File jailDirectory = new File(inst.getDataFolder().getPath() + "/jail");
        if (!jailDirectory.exists()) jailDirectory.mkdir();

        inmates = new CSVInmateDatabase(jailDirectory);
        jailCells = new CSVJailCellDatabase(jailDirectory);
        inmates.load();
        jailCells.load();
        registerCommands(Commands.class);
        //noinspection AccessStaticViaInstance
        inst.registerEvents(this);
        server.getScheduler().scheduleSyncRepeatingTask(CommandBook.inst(), this, 20 * 2, 20 * 2);
    }

    @Override
    public void reload() {

        super.reload();
        getInmateDatabase().load();
        getJailCellDatabase().load();
        configure(config);
    }

    @Override
    public void disable() {

        inmates.unload();
        jailCells.unload();
    }

    private static class LocalConfiguration extends ConfigurationBase {

        @Setting("message")
        public String jailMessage = "You have been jailed";
        @Setting("broadcast-jails")
        public boolean broadcastJails = true;
    }

    /**
     * Get the inmate database.
     *
     * @return Inmates
     */
    public InmateDatabase getInmateDatabase() {

        return inmates;
    }

    /**
     * Get the jailcell database.
     *
     * @return Jail cells
     */
    public JailCellDatabase getJailCellDatabase() {

        return jailCells;
    }

    public void jail(String name, long time) {

        getInmateDatabase().jail(name, "lava-flow", server.getConsoleSender(), "", System.currentTimeMillis() + time);
    }

    public boolean isJailed(Player player) {

        return isJailed(player.getName());
    }

    public boolean isJailed(String name) {

        return getInmateDatabase().isJailedName(name);
    }

    @Override
    public void run() {

        for (Player player : server.getOnlinePlayers()) {
            try {

                if (getInmateDatabase().isJailedName(player.getName())) {

                    if (!player.isOnline() && cell.containsKey(player)) {
                        cell.remove(player);
                        continue;
                    }

                    if (player.isOnline() && !cell.containsKey(player)) {
                        Inmate inmate = getInmateDatabase().getJailedName(player.getName());
                        assignCell(player, inmate.getPrisonName());
                    }

                    adminComponent.standardizePlayer(player, true);
                    player.setFoodLevel(5);

                    Location loc = player.getLocation();
                    Location cellLoc = cell.get(player).getLocation();
                    if (player.getWorld() != cellLoc.getWorld()
                            || loc.distanceSquared(cellLoc) > MOVE_THRESHOLD_SQ) {
                        player.teleport(cell.get(player).getLocation());
                    }
                }


            } catch (Exception e) {
                log.warning("Could not find a cell for the player: " + player.getName() + ".");
                PlayerKickEvent playerKickEvent = new PlayerKickEvent(player, "Jail", "Kicked!");
                if (!playerKickEvent.isCancelled()) {
                    Bukkit.broadcastMessage("The player: " + player.getName() + " has been kicked.");
                    player.kickPlayer("Kicked!");
                }
            }
        }
    }

    @EventHandler(ignoreCancelled = true, priority = EventPriority.MONITOR)
    public void onPlayerInteractEvent(PlayerInteractEvent event) {

        Player player = event.getPlayer();
        if (isJailed(player)) {
            ChatUtil.sendWarning(player, "You are jailed!");
            event.setCancelled(true);
        }
    }

    @EventHandler(ignoreCancelled = true, priority = EventPriority.MONITOR)
    public void onPlayerInteractEntityEvent(PlayerInteractEntityEvent event) {

        Player player = event.getPlayer();
        if (isJailed(player)) {<commit_msg>Fixed teleport issue with the Jail<commit_after>package com.skelril.aurora.jail;

import com.sk89q.commandbook.CommandBook;
import com.sk89q.commandbook.CommandBookUtil;
import com.sk89q.commandbook.util.PlayerUtil;
import com.sk89q.minecraft.util.commands.*;
import com.skelril.aurora.admin.AdminComponent;
import com.skelril.aurora.events.PrayerApplicationEvent;
import com.skelril.aurora.util.ChanceUtil;
import com.skelril.aurora.util.ChatUtil;
import com.zachsthings.libcomponents.ComponentInformation;
import com.zachsthings.libcomponents.Depend;
import com.zachsthings.libcomponents.InjectComponent;
import com.zachsthings.libcomponents.bukkit.BukkitComponent;
import com.zachsthings.libcomponents.config.ConfigurationBase;
import com.zachsthings.libcomponents.config.Setting;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.Server;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.player.*;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;

/**
 * Author: Turtle9598
 */
@ComponentInformation(friendlyName = "Jail", desc = "Jail System")
@Depend(plugins = {"WorldEdit"}, components = {AdminComponent.class})
public class JailComponent extends BukkitComponent implements Listener, Runnable {

    private final CommandBook inst = CommandBook.inst();
    private final Logger log = CommandBook.logger();
    private final Server server = CommandBook.server();

    @InjectComponent
    private AdminComponent adminComponent;

    private InmateDatabase inmates;
    private JailCellDatabase jailCells;
    private LocalConfiguration config;
    private Map<Player, JailCell> cell = new HashMap<>();
    private static final int MOVE_THRESHOLD = 8;
    private static final int MOVE_THRESHOLD_SQ = MOVE_THRESHOLD * MOVE_THRESHOLD;

    @Override
    public void enable() {

        //super.enable();
        config = configure(new LocalConfiguration());

        // Setup the inmates database
        File jailDirectory = new File(inst.getDataFolder().getPath() + "/jail");
        if (!jailDirectory.exists()) jailDirectory.mkdir();

        inmates = new CSVInmateDatabase(jailDirectory);
        jailCells = new CSVJailCellDatabase(jailDirectory);
        inmates.load();
        jailCells.load();
        registerCommands(Commands.class);
        //noinspection AccessStaticViaInstance
        inst.registerEvents(this);
        server.getScheduler().scheduleSyncRepeatingTask(CommandBook.inst(), this, 20 * 2, 20 * 2);
    }

    @Override
    public void reload() {

        super.reload();
        getInmateDatabase().load();
        getJailCellDatabase().load();
        configure(config);
    }

    @Override
    public void disable() {

        inmates.unload();
        jailCells.unload();
    }

    private static class LocalConfiguration extends ConfigurationBase {

        @Setting("message")
        public String jailMessage = "You have been jailed";
        @Setting("broadcast-jails")
        public boolean broadcastJails = true;
    }

    /**
     * Get the inmate database.
     *
     * @return Inmates
     */
    public InmateDatabase getInmateDatabase() {

        return inmates;
    }

    /**
     * Get the jailcell database.
     *
     * @return Jail cells
     */
    public JailCellDatabase getJailCellDatabase() {

        return jailCells;
    }

    public void jail(String name, long time) {

        getInmateDatabase().jail(name, "lava-flow", server.getConsoleSender(), "", System.currentTimeMillis() + time);
    }

    public boolean isJailed(Player player) {

        return isJailed(player.getName());
    }

    public boolean isJailed(String name) {

        return getInmateDatabase().isJailedName(name);
    }

    @Override
    public void run() {

        for (Player player : server.getOnlinePlayers()) {
            try {

                if (getInmateDatabase().isJailedName(player.getName())) {

                    if (!player.isOnline() && cell.containsKey(player)) {
                        cell.remove(player);
                        continue;
                    }

                    if (player.isOnline() && !cell.containsKey(player)) {
                        Inmate inmate = getInmateDatabase().getJailedName(player.getName());
                        assignCell(player, inmate.getPrisonName());
                    }

                    adminComponent.standardizePlayer(player, true);
                    player.setFoodLevel(5);

                    Location loc = player.getLocation();
                    Location cellLoc = cell.get(player).getLocation();
                    if (player.getWorld() != cellLoc.getWorld()
                            || loc.distanceSquared(cellLoc) > MOVE_THRESHOLD_SQ) {
                        player.teleport(cell.get(player).getLocation(), PlayerTeleportEvent.TeleportCause.UNKNOWN);
                    }
                }


            } catch (Exception e) {
                log.warning("Could not find a cell for the player: " + player.getName() + ".");
                PlayerKickEvent playerKickEvent = new PlayerKickEvent(player, "Jail", "Kicked!");
                if (!playerKickEvent.isCancelled()) {
                    Bukkit.broadcastMessage("The player: " + player.getName() + " has been kicked.");
                    player.kickPlayer("Kicked!");
                }
            }
        }
    }

    @EventHandler(ignoreCancelled = true, priority = EventPriority.MONITOR)
    public void onPlayerInteractEvent(PlayerInteractEvent event) {

        Player player = event.getPlayer();
        if (isJailed(player)) {
            ChatUtil.sendWarning(player, "You are jailed!");
            event.setCancelled(true);
        }
    }

    @EventHandler(ignoreCancelled = true, priority = EventPriority.MONITOR)
    public void onPlayerInteractEntityEvent(PlayerInteractEntityEvent event) {

        Player player = event.getPlayer();
        if (isJailed(player)) {
{'fraction_non_alphanumeric': 0.10556131812016277, 'fraction_numerical': 0.003191574243995851, 'mean_word_length': 2.6767380463479027, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, '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': '4329644', 'n_tokens_mistral': 3594, 'n_tokens_neox': 3389, 'n_words': 696}