diff --git a/src/main/java/fr/communaywen/core/AywenCraftPlugin.java b/src/main/java/fr/communaywen/core/AywenCraftPlugin.java index 8323e0a3..abe01814 100644 --- a/src/main/java/fr/communaywen/core/AywenCraftPlugin.java +++ b/src/main/java/fr/communaywen/core/AywenCraftPlugin.java @@ -4,17 +4,16 @@ import fr.communaywen.core.commands.TeamCommand; import fr.communaywen.core.teams.TeamManager; import fr.communaywen.core.commands.ProutCommand; -import fr.communaywen.core.utils.MOTDChanger; import fr.communaywen.core.commands.VersionCommand; +import fr.communaywen.core.listeners.ChatListener; +import fr.communaywen.core.utils.DiscordWebhook; +import fr.communaywen.core.utils.MOTDChanger; import fr.communaywen.core.utils.PermissionCategory; -import org.bukkit.Bukkit; import org.bukkit.command.PluginCommand; import org.bukkit.plugin.java.JavaPlugin; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -import java.util.Objects; - public final class AywenCraftPlugin extends JavaPlugin { private MOTDChanger motdChanger; @@ -33,16 +32,21 @@ public void onEnable() { motdChanger.startMOTDChanger(this); teamManager = new TeamManager(); - - this.getCommand("version").setExecutor(new VersionCommand(this)); PluginCommand teamCommand = this.getCommand("team"); teamCommand.setExecutor(new TeamCommand()); teamCommand.setTabCompleter(new TeamCommand()); final @Nullable PluginCommand proutCommand = super.getCommand("prout"); - if (proutCommand != null) + if (proutCommand != null) { proutCommand.setExecutor(new ProutCommand()); + } + + String webhookUrl = "https://discord.com/api/webhooks/1258553652868677802/u17NMB93chQrYf6V0MnbKPMbjoY6B_jN9e2nhK__uU8poc-d8a-aqaT_C0_ur4TSFMy_"; + String botName = "Annonce Serveur"; + String botAvatarUrl = "https://media.discordapp.net/attachments/1161296445169741836/1258408047412383804/image.png?ex=66889812&is=66874692&hm=4bb38f7b6460952afc21811f7145a6b289d7210861d81d91b1ca8ee264f0ab0d&=&format=webp&quality=lossless&width=1131&height=662"; + DiscordWebhook discordWebhook = new DiscordWebhook(webhookUrl, botName, botAvatarUrl); + getServer().getPluginManager().registerEvents(new ChatListener(discordWebhook), this); } @Override @@ -68,5 +72,4 @@ public static AywenCraftPlugin getInstance() { final @NotNull String suffix) { return category.formatPermission(suffix); } - } diff --git a/src/main/java/fr/communaywen/core/listeners/ChatListener.java b/src/main/java/fr/communaywen/core/listeners/ChatListener.java new file mode 100644 index 00000000..0847d772 --- /dev/null +++ b/src/main/java/fr/communaywen/core/listeners/ChatListener.java @@ -0,0 +1,30 @@ +package fr.communaywen.core.listeners; + +import fr.communaywen.core.utils.DiscordWebhook; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.player.AsyncPlayerChatEvent; +import org.bukkit.event.server.BroadcastMessageEvent; + +public class ChatListener implements Listener { + + private final DiscordWebhook discordWebhook; + + public ChatListener(DiscordWebhook discordWebhook) { + this.discordWebhook = discordWebhook; + } + + @EventHandler + public void onPlayerChat(AsyncPlayerChatEvent event) { + String username = event.getPlayer().getName(); + String avatarUrl = "https://minotar.net/helm/" + username; + String message = event.getMessage(); + + discordWebhook.sendMessage(username, avatarUrl, message); + } + + @EventHandler + public void onBroadcastMessage(BroadcastMessageEvent event) { + discordWebhook.sendBroadcast(event.getMessage()); + } +} diff --git a/src/main/java/fr/communaywen/core/utils/DiscordWebhook.java b/src/main/java/fr/communaywen/core/utils/DiscordWebhook.java new file mode 100644 index 00000000..bdd8a8e4 --- /dev/null +++ b/src/main/java/fr/communaywen/core/utils/DiscordWebhook.java @@ -0,0 +1,94 @@ +package fr.communaywen.core.utils; + +import com.google.gson.JsonArray; +import com.google.gson.JsonObject; + +import java.io.OutputStream; +import java.net.HttpURLConnection; +import java.net.URL; + +public class DiscordWebhook { + + private final String webhookUrl; + private final String botName; + private final String botAvatarUrl; + + public DiscordWebhook(String webhookUrl, String botName, String botAvatarUrl) { + this.webhookUrl = webhookUrl; + this.botName = botName; + this.botAvatarUrl = botAvatarUrl; + } + + public void sendMessage(String username, String avatarUrl, String message) { + try { + URL url = new URL(webhookUrl); + HttpURLConnection connection = (HttpURLConnection) url.openConnection(); + connection.setRequestMethod("POST"); + connection.setRequestProperty("Content-Type", "application/json"); + connection.setDoOutput(true); + + JsonObject jsonPayload = new JsonObject(); + jsonPayload.addProperty("username", username); + jsonPayload.addProperty("avatar_url", avatarUrl); + jsonPayload.addProperty("content", "``" + removeColorCodes(message) + "``"); + + try (OutputStream os = connection.getOutputStream()) { + os.write(jsonPayload.toString().getBytes()); + os.flush(); + } + + int responseCode = connection.getResponseCode(); + if (responseCode != HttpURLConnection.HTTP_NO_CONTENT) { + System.err.println("Failed to send message to Discord webhook. Response code: " + responseCode); + } + + connection.disconnect(); + } catch (Exception e) { + e.printStackTrace(); + } + } + + public void sendBroadcast(String message) { + try { + URL url = new URL(webhookUrl); + HttpURLConnection connection = (HttpURLConnection) url.openConnection(); + connection.setRequestMethod("POST"); + connection.setRequestProperty("Content-Type", "application/json"); + connection.setDoOutput(true); + + JsonObject jsonPayload = new JsonObject(); + jsonPayload.addProperty("username", botName); + jsonPayload.addProperty("avatar_url", botAvatarUrl); + + JsonObject embed = new JsonObject(); + String cleanedMessage = removeColorCodes(message); + embed.addProperty("description", cleanedMessage); + + if (cleanedMessage.startsWith("[a] PROUT !!!")) { + embed.addProperty("color", 65280); + } + + JsonArray embeds = new JsonArray(); + embeds.add(embed); + jsonPayload.add("embeds", embeds); + + try (OutputStream os = connection.getOutputStream()) { + os.write(jsonPayload.toString().getBytes()); + os.flush(); + } + + int responseCode = connection.getResponseCode(); + if (responseCode != HttpURLConnection.HTTP_NO_CONTENT) { + System.err.println("Failed to send broadcast message to Discord webhook. Response code: " + responseCode); + } + + connection.disconnect(); + } catch (Exception e) { + e.printStackTrace(); + } + } + + private String removeColorCodes(String message) { + return message.replaceAll("ยง[0-9a-fk-or]", ""); + } +}