Untitled

mail@pastecode.io avatar
unknown
plain_text
25 days ago
16 kB
1
Indexable
Never
package org.lifestealsmp.duelscore;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;

public class FunctionsManager {

	public HashMap<String, List<Player>> queueUnRanked = new HashMap<>();
	public HashMap<String, List<Player>> queueRanked = new HashMap<>();

	public ArrayList<String> inUnRankedMatch = new ArrayList<String>();
	public ArrayList<String> inRankedMatch = new ArrayList<String>();

	public HashMap<String, List<Player>> map = new HashMap<>();
	public HashMap<String, ItemStack[]> savedInventories = new HashMap<>();
	public HashMap<String, Location> savedlocation = new HashMap<>();
	public HashMap<String, Double> savedHealth = new HashMap<>();
	public HashMap<String, Integer> savedExp = new HashMap<>();

	public void setSpawnOne(Player player, String arenaName) {
		if (Main.getInstance().getConfig().getConfigurationSection("Arenas") != null && Main.getInstance().getConfig()
				.getConfigurationSection("Arenas").getKeys(false).contains(arenaName)) {
			Main.getInstance().getConfig().set("Arenas." + arenaName + ".SpawnPoint-1" + ".X",
					player.getLocation().getBlockX());
			Main.getInstance().getConfig().set("Arenas." + arenaName + ".SpawnPoint-1" + ".Y",
					player.getLocation().getY());
			Main.getInstance().getConfig().set("Arenas." + arenaName + ".SpawnPoint-1" + ".Z",
					player.getLocation().getBlockZ());
			Main.getInstance().getConfig().set("Arenas." + arenaName + ".SpawnPoint-1" + ".World",
					player.getLocation().getWorld().getName());
			Main.getInstance().getConfig().set("Arenas." + arenaName + ".SpawnPoint-1" + ".Yaw",
					player.getLocation().getYaw());
			Main.getInstance().getConfig().set("Arenas." + arenaName + ".SpawnPoint-1" + ".Pitch",
					player.getLocation().getPitch());
			Main.getInstance().saveConfig();
		} else {
			player.sendMessage("§4[!] §cThe arena " + arenaName + " does not exist!");
		}
	}

	public void setSpawnTwo(Player player, String arenaName) {
		if (Main.getInstance().getConfig().getConfigurationSection("Arenas") != null && Main.getInstance().getConfig()
				.getConfigurationSection("Arenas").getKeys(false).contains(arenaName)) {
			Main.getInstance().getConfig().set("Arenas." + arenaName + ".SpawnPoint-2" + ".X",
					player.getLocation().getBlockX());
			Main.getInstance().getConfig().set("Arenas." + arenaName + ".SpawnPoint-2" + ".Y",
					player.getLocation().getY());
			Main.getInstance().getConfig().set("Arenas." + arenaName + ".SpawnPoint-2" + ".Z",
					player.getLocation().getBlockZ());
			Main.getInstance().getConfig().set("Arenas." + arenaName + ".SpawnPoint-2" + ".World",
					player.getLocation().getWorld().getName());
			Main.getInstance().getConfig().set("Arenas." + arenaName + ".SpawnPoint-2" + ".Yaw",
					player.getLocation().getYaw());
			Main.getInstance().getConfig().set("Arenas." + arenaName + ".SpawnPoint-2" + ".Pitch",
					player.getLocation().getPitch());
			Main.getInstance().saveConfig();
		} else {
			player.sendMessage("§4[!] §cThe arena " + arenaName + " does not exist!");
		}
	}

	public void createArena(Player player, String arenaName) {
		if (Main.getInstance().getConfig().getConfigurationSection("Arenas") != null && Main.getInstance().getConfig()
				.getConfigurationSection("Arenas").getKeys(false).contains(arenaName)) {
			player.sendMessage("§4This arenas name is already in use!");
			return;
		}

		Main.getInstance().getConfig().set("Arenas." + arenaName + ".Active", false);
		Main.getInstance().saveConfig();
		player.sendMessage("§aThe arena " + arenaName + " has been created!");
	}

	public void DuelAssign(String arenaName, Player playerA, Player playerB, String kitName) {
		List<Player> players = Arrays.asList(playerA, playerB);
		map.put(arenaName, players);

		Main.getInstance().getConfig().set("Arenas." + arenaName + ".Active", true);
		Main.getInstance().saveConfig();

		SavePlayerData(playerA);
		SavePlayerData(playerB);
		
		ClearPlayerData(playerA);
		ClearPlayerData(playerB);
		
		teleportPlayersToArena(arenaName, playerA, playerB);
		
		playerA.setHealth(20);
		playerB.setHealth(20);

		Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "effect clear " + playerA.getName());
		Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "effect clear " + playerB.getName());
		
		Main.getInstance().bossBarManager.startBossBarTimer(playerA, playerB);
		Main.getInstance().scoreboardManager.setDuelScoreboard(playerA, playerB);
		
		Bukkit.dispatchCommand(Bukkit.getServer().getConsoleSender(), "kit " + kitName + " " + playerA.getName());
		Bukkit.dispatchCommand(Bukkit.getServer().getConsoleSender(), "kit " + kitName + " " + playerB.getName());
		
		RemoveQueue(playerA);
		RemoveQueue(playerB);
	}

	public void CancelArena(String arenaName) {
		// Check if the specific arena exists in the map
		if (map.containsKey(arenaName)) {
			Main.getInstance().getConfig().set("Arenas." + arenaName + ".Active", false);
			Main.getInstance().saveConfig();
			map.remove(arenaName);
		}
	}

	@SuppressWarnings("deprecation")
	public void SavePlayerData(Player player) {
		savedInventories.put(player.getName(), player.getInventory().getContents());
		savedlocation.put(player.getName(), player.getLocation());
		savedHealth.put(player.getName(), player.getMaxHealth());
		savedExp.put(player.getName(), player.getLevel());
	}

	@SuppressWarnings("deprecation")
	public void RestorePlayerData(Player player) {
		if (savedInventories.containsKey(player.getName())) {
			player.getInventory().setContents(savedInventories.get(player.getName()));
			savedInventories.remove(player.getName());
		}
		if (savedlocation.containsKey(player.getName())) {
			player.teleport(savedlocation.get(player.getName()));
			savedlocation.remove(player.getName());
		}
		if (savedHealth.containsKey(player.getName())) {
			player.setMaxHealth(savedHealth.get(player.getName()));
			player.setHealth(player.getMaxHealth());
			savedHealth.remove(player.getName());
		}
		if (savedExp.containsKey(player.getName())) {
			player.setLevel(savedExp.get(player.getName()));
			savedExp.remove(player.getName());
		}
		Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "effect clear " + player.getName());
	}

	public String findAvailableArena() {
		ConfigurationSection arenas = Main.getInstance().getConfig().getConfigurationSection("Arenas");
		if (arenas == null)
			return null;

		for (String key : arenas.getKeys(false)) {
			if (!arenas.getBoolean(key + ".Active", true)) {
				return key;
			}
		}
		return null;
	}

	public void addPlayerToUnRankedQueue(Player player, String kitName) {
		List<Player> players = queueUnRanked.get(kitName);

		if (inUnRankedMatch.contains(player.getName())) {
			RemoveQueue(player);
			return;
		}

		// Initialize the list if it doesn't exist for the kitName
		if (players == null) {
			players = new ArrayList<>();
			queueUnRanked.put(kitName, players);
		}

		if (players.contains(player)) {
			return;
		} else {
			players.add(player);
			queueUnRanked.put(kitName, players);
		}

		List<String> tempList = new ArrayList<>();
		tempList.addAll(Main.getInstance().getConfig().getStringList("Un-Ranked-Queue"));
		tempList.add(player.getName());
		Main.getInstance().getConfig().set("Un-Ranked-Queue", tempList);
		Main.getInstance().saveConfig();

		if (queueUnRanked.get(kitName).size() >= 2) {
			Player player1 = queueUnRanked.get(kitName).get(0);
			Player player2 = queueUnRanked.get(kitName).get(1);

			inUnRankedMatch.add(player1.getName());
			inUnRankedMatch.add(player2.getName());

			DuelAssign(findAvailableArena(), player1, player2, kitName);
		}
	}

	public void addPlayerToRankedQueue(Player player, String kitName) {
		List<Player> players = queueRanked.get(kitName);

		if (inRankedMatch.contains(player.getName())) {
			RemoveQueue(player);
			return;
		}

		// Initialize the list if it doesn't exist for the kitName
		if (players == null) {
			players = new ArrayList<>();
			queueRanked.put(kitName, players);
		}
		
		if (players.contains(player)) {
			return;
		} else {
			players.add(player);
			queueRanked.put(kitName, players);
		}

		List<String> tempList = new ArrayList<>();
		tempList.addAll(Main.getInstance().getConfig().getStringList("Ranked-Queue"));
		tempList.add(player.getName());
		Main.getInstance().getConfig().set("Ranked-Queue", tempList);
		Main.getInstance().saveConfig();

		File directory = new File("plugins/Duels/players");

		if (queueRanked.get(kitName).size() >= 2) {
			Player player1 = queueRanked.get(kitName).get(0);
			Player player2 = queueRanked.get(kitName).get(1);

			inRankedMatch.add(player1.getName());
			inRankedMatch.add(player2.getName());

			File player1File = new File(directory, player1.getUniqueId() + ".yml");
			File player2File = new File(directory, player2.getUniqueId() + ".yml");

			FileConfiguration player1Config = YamlConfiguration.loadConfiguration(player1File);
			FileConfiguration player2Config = YamlConfiguration.loadConfiguration(player2File);

			String kitDisplayName = Main.getInstance().getConfig().getString("Kits." + kitName + ".name");

			player1Config.set("Kit", kitDisplayName);
			player2Config.set("Kit", kitDisplayName);

			DuelAssign(findAvailableArena(), player1, player2, kitName);
			
			try {
				player1Config.save(player1File);
				player2Config.save(player2File);
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	@SuppressWarnings("deprecation")
	public void ClearPlayerData(Player player) {
		player.getInventory().clear();
		player.setMaxHealth(20);
		player.setLevel(0);
		player.setFoodLevel(20);
	}

	public void RemoveQueue(Player player) {
		removePlayerFromQueue("Un-Ranked-Queue", player.getName());
		removePlayerFromQueue("Ranked-Queue", player.getName());

		ConfigurationSection kitsSection = Main.getInstance().getConfig().getConfigurationSection("Kits");
		if (kitsSection != null) {
			for (String key : kitsSection.getKeys(false)) {
				List<Player> playersInUnRankedQueue = queueUnRanked.get(key);
				List<Player> playersInRankedQueue = queueRanked.get(key);
				
				if (playersInUnRankedQueue != null) {
					playersInUnRankedQueue.remove(player);
				}
				
				if (playersInRankedQueue != null) {
					playersInRankedQueue.remove(player);
				}
			}
		}
	}
	
	private void removePlayerFromQueue(String queueKey, String playerName) {
		List<String> tempList = new ArrayList<>(Main.getInstance().getConfig().getStringList(queueKey));
		while (tempList.contains(playerName)) {
			tempList.remove(playerName);
		}
		Main.getInstance().getConfig().set(queueKey, tempList);
		Main.getInstance().saveConfig();
	}

	public void FinishDuelMatch(String arenaName, Player winner, Player loser) {
		Main.getInstance().scoreboardManager.cancelScoreboard(winner, loser);
		Main.getInstance().functionsManager.CancelArena(arenaName);
		RestorePlayerData(winner);

		File directory = new File("plugins/Duels/players");
		File winnerFile = new File(directory, winner.getUniqueId() + ".yml");
		File loserFile = new File(directory, loser.getUniqueId() + ".yml");
		FileConfiguration winnerConfig = YamlConfiguration.loadConfiguration(winnerFile);
		FileConfiguration loserConfig = YamlConfiguration.loadConfiguration(loserFile);
		int totalWins = winnerConfig.getInt("Total-Wins");

		if (inUnRankedMatch.contains(winner.getName()) && inUnRankedMatch.contains(loser.getName())) {
			loser.sendMessage("§e§lDuels §8> §cYou've lost a duel against §a" + winner.getName());
			winner.sendMessage("§e§lDuels §8> §aYou've won a duel against §c" + loser.getName());
			inUnRankedMatch.remove(winner.getName());
			inUnRankedMatch.remove(loser.getName());

			int unRankedLoss = loserConfig.getInt("Un-Ranked-Loss");
			int unRankedWins = winnerConfig.getInt("Un-Ranked-Wins");
			loserConfig.set("Un-Ranked-Loss", unRankedLoss + 1);
			winnerConfig.set("Un-Ranked-Wins", unRankedWins + 1);
		} else {
			String kitName = winnerConfig.getString("Kit");
			Main.getInstance().discordSRVManager.AnnounceDiscordDuelResult(winner, loser, kitName);
			loser.sendMessage("§e§lDuels §8> §cYou've lost a ranked duel against §a" + winner.getName());
			winner.sendMessage("§e§lDuels §8> §aYou've won a ranked duel against §c" + loser.getName());
			inRankedMatch.remove(winner.getName());
			inRankedMatch.remove(loser.getName());

			int winnerRating = winnerConfig.getInt("Elo"); // Assume this method returns the ELO rating of the winner
			int loserRating = loserConfig.getInt("Elo"); // Assume this method returns the ELO rating of the loser

			double expectedWinner = 1 / (1.0 + Math.pow(10, (loserRating - winnerRating) / 400.0));
			double expectedLoser = 1 / (1.0 + Math.pow(10, (winnerRating - loserRating) / 400.0));

			int kFactor = 30; // You can adjust this value

			int newWinnerRating = (int) (winnerRating + kFactor * (1 - expectedWinner)); // Winner's actual score is 1
																							// because they won
			int newLoserRating = (int) (loserRating + kFactor * (0 - expectedLoser)); // Loser's actual score is 0
																						// because they lost

			int RankedLoss = loserConfig.getInt("Ranked-Loss");
			int RankedWins = winnerConfig.getInt("Ranked-Wins");

			winnerConfig.set("Kit", null);
			winnerConfig.set("Elo", newWinnerRating);
			winnerConfig.set("Ranked-Wins", RankedWins + 1);

			loserConfig.set("Kit", null);
			loserConfig.set("Elo", newLoserRating);
			loserConfig.set("Ranked-Loss", RankedLoss + 1);
		}
		Main.getInstance().bossBarManager.cancelBossBar(winner, loser);
		winnerConfig.set("Total-Wins", totalWins + 1);

		try {
			winnerConfig.save(winnerFile);
			loserConfig.save(loserFile);
		} catch (IOException e) {
			e.printStackTrace(); // Handle any exceptions
		}
		RemoveQueue(winner);
		RemoveQueue(loser);
	}

	private void teleportPlayersToArena(String arenaName, Player player1, Player player2) {
		FileConfiguration config = Main.getInstance().getConfig(); // Get your config file

		// Fetch spawn points for the arena
		ConfigurationSection arenaSection = config.getConfigurationSection("Arenas." + arenaName);
		if (arenaSection == null) {
			// Handle the case where the arena doesn't exist in the config
			return;
		}

		// Teleport player1 to SpawnPoint-1
		World world1 = Bukkit.getWorld(arenaSection.getString("SpawnPoint-1.World"));
		double x1 = arenaSection.getDouble("SpawnPoint-1.X");
		double y1 = arenaSection.getDouble("SpawnPoint-1.Y");
		double z1 = arenaSection.getDouble("SpawnPoint-1.Z");
		float yaw1 = (float) arenaSection.getDouble("SpawnPoint-1.Yaw");
		float pitch1 = (float) arenaSection.getDouble("SpawnPoint-1.Pitch");
		Location spawn1 = new Location(world1, x1, y1, z1, yaw1, pitch1);
		player1.teleport(spawn1);

		// Teleport player2 to SpawnPoint-2
		World world2 = Bukkit.getWorld(arenaSection.getString("SpawnPoint-2.World"));
		double x2 = arenaSection.getDouble("SpawnPoint-2.X");
		double y2 = arenaSection.getDouble("SpawnPoint-2.Y");
		double z2 = arenaSection.getDouble("SpawnPoint-2.Z");
		float yaw2 = (float) arenaSection.getDouble("SpawnPoint-2.Yaw");
		float pitch2 = (float) arenaSection.getDouble("SpawnPoint-2.Pitch");
		Location spawn2 = new Location(world2, x2, y2, z2, yaw2, pitch2);
		player2.teleport(spawn2);
	}

}
Leave a Comment