<commit_before>package me.xorgon.connect4.util;

import com.google.common.base.Preconditions;
import com.supaham.commons.bukkit.players.Players;
import com.supaham.commons.bukkit.utils.BlockFaceUtils;
import com.supaham.commons.utils.WeakSet;
import me.xorgon.connect4.C4Properties;
import me.xorgon.connect4.Connect4Plugin;
import org.bukkit.*;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.entity.FallingBlock;
import org.bukkit.entity.Player;
import org.bukkit.material.MaterialData;
import org.bukkit.scheduler.BukkitTask;
import org.bukkit.util.Vector;

import java.util.*;

/**
 * Created by Elijah on 14/08/2015.
 */
public class PhysicalBoard {

    private Map<Integer, Block> slots = new HashMap<>();
    private List<Block> spaces = new ArrayList<>();
    private Map<Block, Integer> buttons = new HashMap<>();
    private Block joinButton;
    private Player redPlayer;
    private Player bluePlayer;
    private VirtualBoard board;
    private String tag;
    private BlockFace face;
    private final C4Properties.Board config;
    private final World world;


    private boolean canInteract = true;
    private boolean started = false;
    private boolean redTurn = true;
    private boolean finished = false;
    private Vector center;

    private BukkitTask initTimer;
    private BukkitTask finTimer;

    private WeakSet<FallingBlock> fallingBlocks = new WeakSet<>();

    public PhysicalBoard(World world, C4Properties.Board serialized) {
        Preconditions.checkArgument(BlockFaceUtils.isHorizontal(serialized.getFace()), "Block face must be horizontal.");

        this.world = world;
        config = serialized;
        board = new VirtualBoard();
        loadBoard(world);
        center = config.getRegion().getMaximumPoint().clone().midpoint(config.getRegion().getMinimumPoint());
    }

    public void resetBoard() {
        for (Vector space : config.getRegion()) {
            space.toLocation(world).getBlock().setType(Material.AIR);
        }
        board.initialize();
        finished = false;
        canInteract = true;
        if (initTimer != null){
            initTimer.cancel();
        }
        if (finTimer != null){
            finTimer.cancel();
        }
    }

    public void resetPlayers() {
        redPlayer = null;
        bluePlayer = null;
        started = false;
        redTurn = true;
    }

    public void placePiece(Player player, int column) {
        if ((player == redPlayer && redTurn) || (player == bluePlayer && !redTurn)) {
            MaterialData material;

            //Virtual Board
            VirtualBoard.SpaceType piece;
            if (player == redPlayer) {
                piece = VirtualBoard.SpaceType.RED;
                material = config.getRedBlock();
            } else {
                piece = VirtualBoard.SpaceType.BLUE;
                material = config.getBlueBlock();
            }
            if (board.placePiece(column, piece)) {
                //Physical Board
                Block slot = slots.get(column);
                FallingBlock block = (FallingBlock) slot.getWorld().spawnFallingBlock(slot.getLocation().add(0.5, 1, 0.5), material.getItemType(), material.getData());
                fallingBlocks.add(block);

                canInteract = false;

                initTimer.cancel();
                finTimer.cancel();
            } else {
                player.sendMessage(ChatColor.RED + "You can't place a piece there.");
            }
        } else {
            player.sendMessage(ChatColor.RED + "It is not your turn.");
        }
    }

    public void handleFallingBlock(FallingBlock block) {
        VirtualBoard.WinStatus winStatus = board.testWin();
        if (winStatus != VirtualBoard.WinStatus.NONE && winStatus != VirtualBoard.WinStatus.DRAW) {
            boolean redWin = winStatus == VirtualBoard.WinStatus.RED;
            String winString = (redWin ? ChatColor.RED + redPlayer.getName() : ChatColor.BLUE + bluePlayer.getName())
                    + ChatColor.YELLOW + " wins!";
            String announce = (redWin ? ChatColor.RED + redPlayer.getName() : ChatColor.BLUE + bluePlayer.getName())
            + ChatColor.YELLOW + " beat " + (!redWin ? ChatColor.RED + redPlayer.getName() : ChatColor.BLUE + bluePlayer.getName()) + ChatColor.YELLOW + " at Connect 4!";
            Collection<? extends Player> players = Players.playersByRadius(Players.worldPlayers(block.getWorld()), 20).get(block.getLocation());
            for (Player player : players) {
                player.sendMessage(announce);
            }

            TitleUtil.sendTitle(redPlayer, winString, redWin ? ChatColor.YELLOW + "Congratulations!" : ChatColor.YELLOW + "Better luck next time.");
            TitleUtil.sendTitle(bluePlayer, winString, !redWin ? ChatColor.YELLOW + "Congratulations!" : ChatColor.YELLOW + "Better luck next time.");<commit_msg>Undid offset fix as it has been fixed 'officially'.<commit_after>package me.xorgon.connect4.util;

import com.google.common.base.Preconditions;
import com.supaham.commons.bukkit.players.Players;
import com.supaham.commons.bukkit.utils.BlockFaceUtils;
import com.supaham.commons.utils.WeakSet;
import me.xorgon.connect4.C4Properties;
import me.xorgon.connect4.Connect4Plugin;
import org.bukkit.*;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.entity.FallingBlock;
import org.bukkit.entity.Player;
import org.bukkit.material.MaterialData;
import org.bukkit.scheduler.BukkitTask;
import org.bukkit.util.Vector;

import java.util.*;

/**
 * Created by Elijah on 14/08/2015.
 */
public class PhysicalBoard {

    private Map<Integer, Block> slots = new HashMap<>();
    private List<Block> spaces = new ArrayList<>();
    private Map<Block, Integer> buttons = new HashMap<>();
    private Block joinButton;
    private Player redPlayer;
    private Player bluePlayer;
    private VirtualBoard board;
    private String tag;
    private BlockFace face;
    private final C4Properties.Board config;
    private final World world;


    private boolean canInteract = true;
    private boolean started = false;
    private boolean redTurn = true;
    private boolean finished = false;
    private Vector center;

    private BukkitTask initTimer;
    private BukkitTask finTimer;

    private WeakSet<FallingBlock> fallingBlocks = new WeakSet<>();

    public PhysicalBoard(World world, C4Properties.Board serialized) {
        Preconditions.checkArgument(BlockFaceUtils.isHorizontal(serialized.getFace()), "Block face must be horizontal.");

        this.world = world;
        config = serialized;
        board = new VirtualBoard();
        loadBoard(world);
        center = config.getRegion().getMaximumPoint().clone().midpoint(config.getRegion().getMinimumPoint());
    }

    public void resetBoard() {
        for (Vector space : config.getRegion()) {
            space.toLocation(world).getBlock().setType(Material.AIR);
        }
        board.initialize();
        finished = false;
        canInteract = true;
        if (initTimer != null){
            initTimer.cancel();
        }
        if (finTimer != null){
            finTimer.cancel();
        }
    }

    public void resetPlayers() {
        redPlayer = null;
        bluePlayer = null;
        started = false;
        redTurn = true;
    }

    public void placePiece(Player player, int column) {
        if ((player == redPlayer && redTurn) || (player == bluePlayer && !redTurn)) {
            MaterialData material;

            //Virtual Board
            VirtualBoard.SpaceType piece;
            if (player == redPlayer) {
                piece = VirtualBoard.SpaceType.RED;
                material = config.getRedBlock();
            } else {
                piece = VirtualBoard.SpaceType.BLUE;
                material = config.getBlueBlock();
            }
            if (board.placePiece(column, piece)) {
                //Physical Board
                Block slot = slots.get(column);
                FallingBlock block = (FallingBlock) slot.getWorld().spawnFallingBlock(slot.getLocation().add(0, 1, 0), material.getItemType(), material.getData());
                fallingBlocks.add(block);

                canInteract = false;

                initTimer.cancel();
                finTimer.cancel();
            } else {
                player.sendMessage(ChatColor.RED + "You can't place a piece there.");
            }
        } else {
            player.sendMessage(ChatColor.RED + "It is not your turn.");
        }
    }

    public void handleFallingBlock(FallingBlock block) {
        VirtualBoard.WinStatus winStatus = board.testWin();
        if (winStatus != VirtualBoard.WinStatus.NONE && winStatus != VirtualBoard.WinStatus.DRAW) {
            boolean redWin = winStatus == VirtualBoard.WinStatus.RED;
            String winString = (redWin ? ChatColor.RED + redPlayer.getName() : ChatColor.BLUE + bluePlayer.getName())
                    + ChatColor.YELLOW + " wins!";
            String announce = (redWin ? ChatColor.RED + redPlayer.getName() : ChatColor.BLUE + bluePlayer.getName())
            + ChatColor.YELLOW + " beat " + (!redWin ? ChatColor.RED + redPlayer.getName() : ChatColor.BLUE + bluePlayer.getName()) + ChatColor.YELLOW + " at Connect 4!";
            Collection<? extends Player> players = Players.playersByRadius(Players.worldPlayers(block.getWorld()), 20).get(block.getLocation());
            for (Player player : players) {
                player.sendMessage(announce);
            }

            TitleUtil.sendTitle(redPlayer, winString, redWin ? ChatColor.YELLOW + "Congratulations!" : ChatColor.YELLOW + "Better luck next time.");
            TitleUtil.sendTitle(bluePlayer, winString, !redWin ? ChatColor.YELLOW + "Congratulations!" : ChatColor.YELLOW + "Better luck next time.");
{'fraction_non_alphanumeric': 0.11121267519804998, 'fraction_numerical': 0.004468819825309771, 'mean_word_length': 2.567753623188406, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, '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': '6537805', 'n_tokens_mistral': 2754, 'n_tokens_neox': 2608, 'n_words': 621}