<commit_before>package com.censoredsoftware.Demigods.API;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.OfflinePlayer;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;

import com.censoredsoftware.Demigods.Engine.Demigods;
import com.censoredsoftware.Demigods.Engine.DemigodsData;
import com.censoredsoftware.Demigods.Engine.PlayerCharacter.PlayerCharacter;

public class PlayerAPI
{
	/**
	 * Adds the <code>player</code> to the database/filesystem.
	 * 
	 * @param player the new player to add.
	 * @return boolean based on the success or failure of adding the player.
	 */
	public static void createNewPlayer(Player player)
	{
		Demigods.message.info("Saving new player: " + player.getName());

		// Create their HashMap data
		DemigodsData.playerData.saveData(player, "player_kills", 0);
		DemigodsData.playerData.saveData(player, "player_deaths", 0);
		DemigodsData.playerData.saveData(player, "current_char", null);
		DemigodsData.playerData.saveData(player, "previous_char", null);
	}

	/**
	 * Returns the current character of the <code>player</code>.
	 * 
	 * @param player the player to check.
	 * @return Demigod
	 */
	public static PlayerCharacter getCurrentChar(OfflinePlayer player)
	{
		try
		{
			return CharacterAPI.getChar(DemigodsData.playerData.getDataInt(player, "current_char"));
		}
		catch(Exception e)
		{
			return null;
		}
	}

	/**
	 * Returns the current alliance for <code>player</code>.
	 * 
	 * @param player the player to check.
	 * @return String
	 */
	public static String getCurrentAlliance(OfflinePlayer player)
	{
		PlayerCharacter character = getCurrentChar(player);
		if(character == null || !character.isImmortal()) return "Mortal";
		return character.getAlliance();
	}

	/**
	 * Returns true if <code>player1</code> is allied with <code>player2</code> based
	 * on their current alliances.
	 * 
	 * @param player1 the first player to check.
	 * @param player2 the second player to check.
	 * @return boolean
	 */
	public static boolean areAllied(Player player1, Player player2)
	{
		String player1Alliance = getCurrentAlliance(player1);
		String player2Alliance = getCurrentAlliance(player2);

		return player1Alliance.equalsIgnoreCase(player2Alliance);
	}

	/**
	 * Returns a List of all of <code>player</code>'s characters.
	 * 
	 * @param player the player to check.
	 * @return List the list of all character IDs.
	 */
	public static List<PlayerCharacter> getChars(OfflinePlayer player)
	{
		List<PlayerCharacter> characters = new ArrayList<PlayerCharacter>();
		for(PlayerCharacter character : CharacterAPI.getAllChars())
		{
			if(character.getOwner() == player) characters.add(character);
		}
		return characters;
	}

	/**
	 * Changes the <code>offlinePlayer</code>'s current character to <code>charID</code>.
	 * 
	 * @param offlinePlayer the player whose character to change.
	 * @param charID the character ID to switch to.
	 * @return boolean based on if the change was successful or not.
	 */
	public static boolean changeCurrentChar(OfflinePlayer offlinePlayer, int charID) // TODO Most of this should be in a listener, not here in the API.
	{
		// Define variables
		Player player = offlinePlayer.getPlayer();

		if(!getChars(offlinePlayer).contains(CharacterAPI.getChar(charID)))
		{
			player.sendMessage(ChatColor.RED + "You can't do that.");
			return false;
		}

		// Disable prayer just to be safe
		togglePraying(player, false);

		// Update the current character (if it exists)
		PlayerCharacter currentChar = getCurrentChar(player);
		if(currentChar != null)
		{
			// Save info
			currentChar.setHealth(player.getHealth());
			currentChar.setHunger(player.getFoodLevel());
			currentChar.setLevel(player.getLevel());
			currentChar.setExperience(player.getExp());
			currentChar.setLocation(player.getLocation());
			currentChar.saveInventory();

			// Set them to inactive
			currentChar.setActive(false);
			DemigodsData.playerData.saveData(player, "previous_char", currentChar.getID());
		}

		// Everything is good, let's switch
		DemigodsData.playerData.saveData(player, "current_char", charID);
		PlayerCharacter character = CharacterAPI.getChar(charID);
		character.setActive(true);

		// If it's their first character save their inventory
		if(getChars(player).size() <= 1) character.saveInventory();

		// Update inventory
		player.getInventory().clear();
		player.getInventory().setBoots(new ItemStack(Material.AIR));
		player.getInventory().setChestplate(new ItemStack(Material.AIR));
		player.getInventory().setLeggings(new ItemStack(Material.AIR));
		player.getInventory().setBoots(new ItemStack(Material.AIR));

		if(character.getInventory() != null)
		{
			TrackedPlayerInvetory charInv = character.getInventory();
			charInv.setToPlayer(player);
		}<commit_msg>Oopsy, herped a derp.<commit_after>package com.censoredsoftware.Demigods.API;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.OfflinePlayer;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;

import com.censoredsoftware.Demigods.Engine.Demigods;
import com.censoredsoftware.Demigods.Engine.DemigodsData;
import com.censoredsoftware.Demigods.Engine.PlayerCharacter.PlayerCharacter;
import com.censoredsoftware.Demigods.Engine.Tracked.TrackedPlayerInventory;

public class PlayerAPI
{
	/**
	 * Adds the <code>player</code> to the database/filesystem.
	 * 
	 * @param player the new player to add.
	 * @return boolean based on the success or failure of adding the player.
	 */
	public static void createNewPlayer(Player player)
	{
		Demigods.message.info("Saving new player: " + player.getName());

		// Create their HashMap data
		DemigodsData.playerData.saveData(player, "player_kills", 0);
		DemigodsData.playerData.saveData(player, "player_deaths", 0);
		DemigodsData.playerData.saveData(player, "current_char", null);
		DemigodsData.playerData.saveData(player, "previous_char", null);
	}

	/**
	 * Returns the current character of the <code>player</code>.
	 * 
	 * @param player the player to check.
	 * @return Demigod
	 */
	public static PlayerCharacter getCurrentChar(OfflinePlayer player)
	{
		try
		{
			return CharacterAPI.getChar(DemigodsData.playerData.getDataInt(player, "current_char"));
		}
		catch(Exception e)
		{
			return null;
		}
	}

	/**
	 * Returns the current alliance for <code>player</code>.
	 * 
	 * @param player the player to check.
	 * @return String
	 */
	public static String getCurrentAlliance(OfflinePlayer player)
	{
		PlayerCharacter character = getCurrentChar(player);
		if(character == null || !character.isImmortal()) return "Mortal";
		return character.getAlliance();
	}

	/**
	 * Returns true if <code>player1</code> is allied with <code>player2</code> based
	 * on their current alliances.
	 * 
	 * @param player1 the first player to check.
	 * @param player2 the second player to check.
	 * @return boolean
	 */
	public static boolean areAllied(Player player1, Player player2)
	{
		String player1Alliance = getCurrentAlliance(player1);
		String player2Alliance = getCurrentAlliance(player2);

		return player1Alliance.equalsIgnoreCase(player2Alliance);
	}

	/**
	 * Returns a List of all of <code>player</code>'s characters.
	 * 
	 * @param player the player to check.
	 * @return List the list of all character IDs.
	 */
	public static List<PlayerCharacter> getChars(OfflinePlayer player)
	{
		List<PlayerCharacter> characters = new ArrayList<PlayerCharacter>();
		for(PlayerCharacter character : CharacterAPI.getAllChars())
		{
			if(character.getOwner() == player) characters.add(character);
		}
		return characters;
	}

	/**
	 * Changes the <code>offlinePlayer</code>'s current character to <code>charID</code>.
	 * 
	 * @param offlinePlayer the player whose character to change.
	 * @param charID the character ID to switch to.
	 * @return boolean based on if the change was successful or not.
	 */
	public static boolean changeCurrentChar(OfflinePlayer offlinePlayer, int charID) // TODO Most of this should be in a listener, not here in the API.
	{
		// Define variables
		Player player = offlinePlayer.getPlayer();

		if(!getChars(offlinePlayer).contains(CharacterAPI.getChar(charID)))
		{
			player.sendMessage(ChatColor.RED + "You can't do that.");
			return false;
		}

		// Disable prayer just to be safe
		togglePraying(player, false);

		// Update the current character (if it exists)
		PlayerCharacter currentChar = getCurrentChar(player);
		if(currentChar != null)
		{
			// Save info
			currentChar.setHealth(player.getHealth());
			currentChar.setHunger(player.getFoodLevel());
			currentChar.setLevel(player.getLevel());
			currentChar.setExperience(player.getExp());
			currentChar.setLocation(player.getLocation());
			currentChar.saveInventory();

			// Set them to inactive
			currentChar.setActive(false);
			DemigodsData.playerData.saveData(player, "previous_char", currentChar.getID());
		}

		// Everything is good, let's switch
		DemigodsData.playerData.saveData(player, "current_char", charID);
		PlayerCharacter character = CharacterAPI.getChar(charID);
		character.setActive(true);

		// If it's their first character save their inventory
		if(getChars(player).size() <= 1) character.saveInventory();

		// Update inventory
		player.getInventory().clear();
		player.getInventory().setBoots(new ItemStack(Material.AIR));
		player.getInventory().setChestplate(new ItemStack(Material.AIR));
		player.getInventory().setLeggings(new ItemStack(Material.AIR));
		player.getInventory().setBoots(new ItemStack(Material.AIR));

		if(character.getInventory() != null)
		{
			TrackedPlayerInventory charInv = character.getInventory();
			charInv.setToPlayer(player);
		}
{'fraction_non_alphanumeric': 0.11384086543322212, 'fraction_numerical': 0.003033060357901122, 'mean_word_length': 4.6013590033975085, 'pattern_counts': {'":': 0, '<': 43, '<?xml version=': 0, '>': 41, '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': '7317888', 'n_tokens_mistral': 3316, 'n_tokens_neox': 2934, 'n_words': 731}