From 6c24d05b577c2b64272427d80d830deff2fc60b0 Mon Sep 17 00:00:00 2001 From: Oliver Ni Date: Sat, 20 May 2023 16:01:38 +0900 Subject: [PATCH] Finish plugin --- .vscode/settings.json | 2 +- README.md | 5 ++ build.gradle.kts | 4 ++ .../DiscordListener.java | 30 +++++++++++ .../MinecraftListener.java | 53 +++++++++++++++++++ .../bmt/minecraft_discord_bridge/Plugin.java | 31 ++++++++--- src/main/resources/config.yml | 3 ++ 7 files changed, 121 insertions(+), 7 deletions(-) create mode 100644 README.md create mode 100644 src/main/java/edu/berkeley/bmt/minecraft_discord_bridge/DiscordListener.java create mode 100644 src/main/java/edu/berkeley/bmt/minecraft_discord_bridge/MinecraftListener.java create mode 100644 src/main/resources/config.yml diff --git a/.vscode/settings.json b/.vscode/settings.json index 2dd06b4..aae5ecf 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,6 +1,6 @@ { "java.configuration.updateBuildConfiguration": "automatic", - "java.compile.nullAnalysis.mode": "automatic", + "java.compile.nullAnalysis.mode": "disabled", "yaml.schemas": { "https://json.schemastore.org/bukkit-plugin.json": "file:///Users/oliver/gh/berkeleymt/minecraft-bridge/app/src/main/resources/plugin.yml" } diff --git a/README.md b/README.md new file mode 100644 index 0000000..30f833f --- /dev/null +++ b/README.md @@ -0,0 +1,5 @@ +# Minecraft ↔ Discord Chat Bridge + +Bridges Minecraft and Discord chats. + +I wrote this in like one hour, it works, don't judge code pls. diff --git a/build.gradle.kts b/build.gradle.kts index dd26c36..1261c7e 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -1,5 +1,7 @@ plugins { application + id("com.github.johnrengelman.shadow") version "8.1.1" + id("java") } repositories { @@ -9,6 +11,8 @@ repositories { dependencies { compileOnly("io.papermc.paper:paper-api:1.19.4-R0.1-SNAPSHOT") + implementation("net.dv8tion:JDA:5.0.0-beta.9") + implementation("club.minnced:discord-webhooks:0.8.2") } java { diff --git a/src/main/java/edu/berkeley/bmt/minecraft_discord_bridge/DiscordListener.java b/src/main/java/edu/berkeley/bmt/minecraft_discord_bridge/DiscordListener.java new file mode 100644 index 0000000..79185a7 --- /dev/null +++ b/src/main/java/edu/berkeley/bmt/minecraft_discord_bridge/DiscordListener.java @@ -0,0 +1,30 @@ +package edu.berkeley.bmt.minecraft_discord_bridge; + +import net.dv8tion.jda.api.events.message.MessageReceivedEvent; +import net.dv8tion.jda.api.hooks.ListenerAdapter; +import net.kyori.adventure.text.Component; + +public class DiscordListener extends ListenerAdapter { + private Plugin plugin; + private String channelId; + + public DiscordListener(Plugin plugin, String channelId) { + this.plugin = plugin; + this.channelId = channelId; + } + + @Override + public void onMessageReceived(MessageReceivedEvent event) { + plugin.getLogger().info("Received message: " + event.getMessage().getContentDisplay()); + + if (event.getAuthor().isBot()) { + return; + } + if (!event.getGuildChannel().getId().equals(channelId)) { + return; + } + + String msg = String.format("《%s》 %s", event.getAuthor().getAsTag(), event.getMessage().getContentDisplay()); + plugin.getServer().broadcast(Component.text(msg)); + } +} diff --git a/src/main/java/edu/berkeley/bmt/minecraft_discord_bridge/MinecraftListener.java b/src/main/java/edu/berkeley/bmt/minecraft_discord_bridge/MinecraftListener.java new file mode 100644 index 0000000..8910aba --- /dev/null +++ b/src/main/java/edu/berkeley/bmt/minecraft_discord_bridge/MinecraftListener.java @@ -0,0 +1,53 @@ +package edu.berkeley.bmt.minecraft_discord_bridge; + +import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.entity.PlayerDeathEvent; +import org.bukkit.event.player.PlayerJoinEvent; +import org.bukkit.event.player.PlayerQuitEvent; + +import club.minnced.discord.webhook.WebhookClient; +import club.minnced.discord.webhook.send.WebhookMessageBuilder; +import io.papermc.paper.event.player.AsyncChatEvent; +import net.dv8tion.jda.api.JDA; +import net.kyori.adventure.text.Component; +import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer; + +public class MinecraftListener implements Listener { + private WebhookClient webhookClient; + + public MinecraftListener(JDA jda, WebhookClient webhookClient) { + this.webhookClient = webhookClient; + } + + private String componentToString(Component component) { + PlainTextComponentSerializer plainSerializer = PlainTextComponentSerializer.plainText(); + return plainSerializer.serialize(component); + } + + @EventHandler + public void onChat(AsyncChatEvent event) { + Player player = event.getPlayer(); + WebhookMessageBuilder builder = new WebhookMessageBuilder(); + builder.setUsername(player.getName()); + builder.setAvatarUrl(String.format("https://crafatar.com/avatars/%s?overlay", player.getUniqueId().toString())); + builder.setContent(componentToString(event.message())); + webhookClient.send(builder.build()); + } + + @EventHandler + public void onPlayerJoin(PlayerJoinEvent event) { + webhookClient.send(componentToString(event.joinMessage())); + } + + @EventHandler + public void onPlayerLeave(PlayerQuitEvent event) { + webhookClient.send(componentToString(event.quitMessage())); + } + + @EventHandler + public void onPlayerDeath(PlayerDeathEvent event) { + webhookClient.send(componentToString(event.deathMessage())); + } +} diff --git a/src/main/java/edu/berkeley/bmt/minecraft_discord_bridge/Plugin.java b/src/main/java/edu/berkeley/bmt/minecraft_discord_bridge/Plugin.java index 9639922..ce1935c 100644 --- a/src/main/java/edu/berkeley/bmt/minecraft_discord_bridge/Plugin.java +++ b/src/main/java/edu/berkeley/bmt/minecraft_discord_bridge/Plugin.java @@ -1,15 +1,34 @@ package edu.berkeley.bmt.minecraft_discord_bridge; -import org.bukkit.Bukkit; -import org.bukkit.event.EventHandler; -import org.bukkit.event.Listener; +import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.plugin.java.JavaPlugin; -import io.papermc.paper.event.player.AsyncChatEvent; +import club.minnced.discord.webhook.WebhookClient; +import net.dv8tion.jda.api.JDA; +import net.dv8tion.jda.api.JDABuilder; +import net.dv8tion.jda.api.requests.GatewayIntent; + +public class Plugin extends JavaPlugin { + private FileConfiguration config = getConfig(); + private JDA jda; + private DiscordListener discordListener; + private MinecraftListener minecraftListener; -public class Plugin extends JavaPlugin implements Listener { @Override public void onEnable() { - Bukkit.getPluginManager().registerEvents(this, this); + saveDefaultConfig(); + discordListener = new DiscordListener(this, config.getString("channel-id")); + jda = JDABuilder.createDefault(config.getString("bot-token")) + .enableIntents(GatewayIntent.MESSAGE_CONTENT) + .addEventListeners(discordListener) + .build(); + jda.getRestPing().queue(ping -> getLogger().info("Connected to Discord with ping: " + ping)); + minecraftListener = new MinecraftListener(jda, WebhookClient.withUrl(config.getString("webhook-url"))); + getServer().getPluginManager().registerEvents(minecraftListener, this); + } + + @Override + public void onDisable() { + jda.shutdown(); } } diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml new file mode 100644 index 0000000..e197979 --- /dev/null +++ b/src/main/resources/config.yml @@ -0,0 +1,3 @@ +bot-token: "" +channel-id: "" +webhook-url: ""