Untitled

 avatar
unknown
plain_text
a year ago
15 kB
3
Indexable
package org.lifestealsmp.duels;

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, Queue<Player>> kitUnRankedQueues = new HashMap<>();
	public HashMap<String, Queue<Player>> kitRankedQueues = 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)) {
	        // Save the player's X, Y, Z coordinates
	        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());

	        // Save the player's world
	        Main.getInstance().getConfig().set("Arenas." + arenaName + ".SpawnPoint-1" + ".World",
	                player.getLocation().getWorld().getName());

	        // Save the player's yaw and pitch
	        Main.getInstance().getConfig().set("Arenas." + arenaName + ".SpawnPoint-1" + ".Yaw",
	                player.getLocation().getYaw());
	        Main.getInstance().getConfig().set("Arenas." + arenaName + ".SpawnPoint-1" + ".Pitch",
	                player.getLocation().getPitch());

	        // Save the configuration
	        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)) {
	        
	        // Save the player's X, Y, Z coordinates
	        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());

	        // Save the player's world
	        Main.getInstance().getConfig().set("Arenas." + arenaName + ".SpawnPoint-2" + ".World",
	                player.getLocation().getWorld().getName());

	        // Save the player's yaw and pitch
	        Main.getInstance().getConfig().set("Arenas." + arenaName + ".SpawnPoint-2" + ".Yaw",
	                player.getLocation().getYaw());
	        Main.getInstance().getConfig().set("Arenas." + arenaName + ".SpawnPoint-2" + ".Pitch",
	                player.getLocation().getPitch());

	        // Save the configuration
	        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) {
		// Remove both players from all other queues
	    for (Queue<Player> queue : kitUnRankedQueues.values()) {
	        queue.remove(playerA);
	        queue.remove(playerB);
	    }
	    
	 // Remove both players from all other queues
	    for (Queue<Player> queue : kitRankedQueues.values()) {
	        queue.remove(playerA);
	        queue.remove(playerB);
	    }
		
		String availableArena = findAvailableArena();
		List<Player> players = Arrays.asList(playerA, playerB);
		map.put(availableArena, players);

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

		SavePlayerData(playerA);
		SavePlayerData(playerB);
		ClearPlayerData(playerA);
		ClearPlayerData(playerB);
		teleportPlayersToArena(availableArena, playerA, playerB);
		playerA.setHealth(20);
		playerB.setHealth(20);
		Bukkit.dispatchCommand(Bukkit.getServer().getConsoleSender(), "kit " + kitName + " " + playerA.getName());
		Bukkit.dispatchCommand(Bukkit.getServer().getConsoleSender(), "kit " + kitName + " " + playerB.getName());
	}

	public void CancelDuel(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);
		}
	}

	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());
	}

	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());
		}
	}

	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) {

		Queue<Player> queue = kitUnRankedQueues.getOrDefault(kitName, new LinkedList<>());

		if (queue.contains(player)) {
			player.sendMessage("§cYou're already in this queue!");
			return;
		}

		queue.add(player);
		kitUnRankedQueues.put(kitName, queue);
		player.sendMessage("§aYou've joined the queue for the kit " + kitName);

		if (queue.size() >= 2) {
			Player player1 = queue.poll();
			Player player2 = queue.poll();

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

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

	public void addPlayerToRankedQueue(Player player, String kitName) {

		Queue<Player> queue = kitRankedQueues.getOrDefault(kitName, new LinkedList<>());

		if (queue.contains(player)) {
			player.sendMessage("§cYou're already in this queue!");
			return;
		}

		queue.add(player);
		kitRankedQueues.put(kitName, queue);
		player.sendMessage("§aYou've joined the queue for the kit " + kitName);

		if (queue.size() >= 2) {
			Player player1 = queue.poll();
			Player player2 = queue.poll();

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

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

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

	public void RemoveQueue(Player player) {
		List<String> tempList = new ArrayList<>();
		tempList.addAll(Main.getInstance().getConfig().getStringList("Un-Ranked-Queue"));
		tempList.remove(player.getName());
		Main.getInstance().getConfig().set("Un-Ranked-Queue", tempList);
		Main.getInstance().saveConfig();

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

	public void FinishDuelMatch(String arenaName, Player winner, Player loser) {
		Main.getInstance().functionsManager.CancelDuel(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);
		
		
		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");
			int totalWins = winnerConfig.getInt("Total-Wins");
			loserConfig.set("Un-Ranked-Loss", unRankedLoss + 1);
			winnerConfig.set("Un-Ranked-Wins", unRankedWins + 1);
			winnerConfig.set("Total-Wins", totalWins + 1);
			
			try {
			    winnerConfig.save(winnerFile);
			    loserConfig.save(loserFile);
			} catch (IOException e) {
				e.printStackTrace(); // Handle any exceptions
			}
			
			return;
		} else {
			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 RankedLoss = loserConfig.getInt("Ranked-Loss");
			int RankedWins = winnerConfig.getInt("Ranked-Wins");
			int totalWins = winnerConfig.getInt("Total-Wins");
			
			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 = 15; // 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

		    try {
		    	winnerConfig.set("Elo", newWinnerRating);
			    loserConfig.set("Elo", newLoserRating);
			    loserConfig.set("Ranked-Loss", RankedLoss + 1);
				winnerConfig.set("Ranked-Wins", RankedWins + 1);
				winnerConfig.set("Total-Wins", totalWins + 1);
			    winnerConfig.save(winnerFile);
			    loserConfig.save(loserFile);
			} catch (IOException e) {
				e.printStackTrace(); // Handle any exceptions
			}
		}
	}

	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);
	}

}
Editor is loading...
Leave a Comment