Untitled
unknown
plain_text
2 years ago
8.8 kB
6
Indexable
package org.lifestealsmp.duelscore;
import java.awt.Color;
import java.io.File;
import java.util.Random;
import java.util.UUID;
import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;
import github.scarsz.discordsrv.DiscordSRV;
import github.scarsz.discordsrv.api.Subscribe;
import github.scarsz.discordsrv.api.events.DiscordGuildMessageReceivedEvent;
import github.scarsz.discordsrv.dependencies.jda.api.EmbedBuilder;
import github.scarsz.discordsrv.dependencies.jda.api.JDA;
import github.scarsz.discordsrv.dependencies.jda.api.entities.Message;
import github.scarsz.discordsrv.dependencies.jda.api.entities.MessageEmbed;
import github.scarsz.discordsrv.dependencies.jda.api.entities.TextChannel;
public class DiscordSRVManager{
public void AnnounceDiscordDuelResult(Player winner, Player loser, String kitName) {
File directory = new File("plugins/Duels/players");
File winnerFile = new File(directory, winner.getUniqueId() + ".yml");
FileConfiguration winnerConfig = YamlConfiguration.loadConfiguration(winnerFile);
int winnerElo = winnerConfig.getInt("Elo");
String winnerRatio = Main.getInstance().scoreboardManager.calculateWinLossRatio(winnerConfig);
File loserFile = new File(directory, loser.getUniqueId() + ".yml");
FileConfiguration loserConfig = YamlConfiguration.loadConfiguration(loserFile);
int loserElo = loserConfig.getInt("Elo");
String loserRatio = Main.getInstance().scoreboardManager.calculateWinLossRatio(loserConfig);
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));
int PlusWinnerElo = newWinnerRating - winnerElo;
int MinusLoserElo = loserElo - newLoserRating;
try {
EmbedBuilder embedBuilder = new EmbedBuilder();
embedBuilder.setTitle("Ranked Duel Match Result");
// Using StringBuilder to append multiple lines
StringBuilder description = new StringBuilder();
description.append("**" + winner.getName() + ":**\n");
description.append("- Elo: " + winnerElo + " + (" + PlusWinnerElo + ")\n");
description.append("- W/L: " + winnerRatio + "\n");
description.append("\n");
description.append("**" + loser.getName() + ":**\n");
description.append("- Elo: " + loserElo + " - (" + MinusLoserElo + ")\n");
description.append("- W/L: " + loserRatio + "\n");
description.append("\n");
description.append("**Kit:** " + kitName.replace("&e", "") + "\n");
description.append("**Match Length:** " + DisplayMatchTime(winner) + "\n");
description.append("\n");
description.append("**Winner:** __" + winner.getName() + "__");
embedBuilder.setDescription(description.toString()); // Setting the description
embedBuilder.setColor(Color.YELLOW);
// Setting the image of the winner's head using their username with mc-heads.net
String winnerHeadUrl = "https://mc-heads.net/avatar/" + winner.getName() + "/64";
embedBuilder.setThumbnail(winnerHeadUrl);
MessageEmbed embed = embedBuilder.build();
JDA jda = DiscordSRV.getPlugin().getJda();
TextChannel channel = jda.getTextChannelById("1119383217380798546");
if (channel != null) {
channel.sendMessageEmbeds(embed).queue(
success -> System.out.println("Embed sent successfully!"),
error -> error.printStackTrace()
);
} else {
System.out.println("Channel not found!");
}
} catch (Exception e) {
e.printStackTrace();
}
}
public String DisplayMatchTime(Player player) {
int matchLength = 180 - Main.getInstance().bossBarManager.getTimeLeft(player);
if (matchLength > 60) {
int minutes = matchLength / 60;
int seconds = matchLength % 60;
String timeFormatted = minutes + " minute" + (minutes > 1 ? "s" : "") + " and " + seconds + " seconds";
// Output the formatted time
return timeFormatted;
} else {
return matchLength + " seconds";
}
}
@Subscribe
public void onMessageReceived(DiscordGuildMessageReceivedEvent event) {
if (event.getChannel().getId().equals("1201169027943370833")) {
Message messageToDelete = event.getMessage();
String message = event.getMessage().getContentRaw();
OfflinePlayer playerName = null;
for (OfflinePlayer op : Bukkit.getServer().getOfflinePlayers()) {
if (op.getName() != null && op.getName().equalsIgnoreCase(message)) {
playerName = op;
break;
}
}
if (playerName != null && playerName.hasPlayedBefore()) {
UUID playerUUID = playerName.getUniqueId();
File directory = new File("plugins/Duels/players");
File playerFile = new File(directory, playerUUID + ".yml");
FileConfiguration playerConfig = YamlConfiguration.loadConfiguration(playerFile);
String winnerRatio = Main.getInstance().scoreboardManager.calculateWinLossRatio(playerConfig);
try {
EmbedBuilder embedBuilder = new EmbedBuilder();
embedBuilder.setTitle("Duel Profile:");
StringBuilder description = new StringBuilder();
description.append("**" + playerName.getName() + ":**\n");
description.append("\n");
description.append("Elo Rating: " + playerConfig.getInt("Elo") + "\n");
description.append("W/L Ratio: " + winnerRatio + "\n");
description.append("\n");
description.append("Ranked Wins: " + playerConfig.getInt("Ranked-Wins") + "\n");
description.append("Ranked Loss: " + playerConfig.getInt("Ranked-Loss") + "\n");
description.append("\n");
description.append("UnRanked Wins: " + playerConfig.getInt("Un-Ranked-Wins") + "\n");
description.append("UnRanked Loss: " + playerConfig.getInt("Un-Ranked-Loss") + "\n");
description.append("\n");
description.append("Overall Rank: #" + SortElo.SortEloRank(playerName.getName()));
embedBuilder.setDescription(description.toString());
Random random = new Random();
int red = random.nextInt(256);
int green = random.nextInt(256);
int blue = random.nextInt(256);
Color randomColor = new Color(red, green, blue);
embedBuilder.setColor(randomColor);
String winnerHeadUrl = "https://mc-heads.net/avatar/" + playerName.getName() + "/64";
embedBuilder.setThumbnail(winnerHeadUrl);
MessageEmbed embed = embedBuilder.build();
JDA jda = DiscordSRV.getPlugin().getJda();
TextChannel channel = jda.getTextChannelById("1201169027943370833");
if (channel != null) {
channel.sendMessageEmbeds(embed).queue(
success -> System.out.println("Embed sent successfully!"),
error -> error.printStackTrace()
);
} else {
System.out.println("Channel not found!");
}
} catch (Exception e) {
e.printStackTrace();
}
}
messageToDelete.delete().queue(
success -> System.out.println("Message deleted successfully"),
error -> System.err.println("Error occurred while deleting the message: " + error.getMessage())
);
}
}
}
Editor is loading...
Leave a Comment