Skip to content

Commit

Permalink
Finish plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
oliver-ni committed May 20, 2023
1 parent 82a014a commit 6c24d05
Show file tree
Hide file tree
Showing 7 changed files with 121 additions and 7 deletions.
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -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"
}
Expand Down
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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.
4 changes: 4 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
plugins {
application
id("com.github.johnrengelman.shadow") version "8.1.1"
id("java")
}

repositories {
Expand All @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -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));
}
}
Original file line number Diff line number Diff line change
@@ -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()));
}
}
Original file line number Diff line number Diff line change
@@ -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();
}
}
3 changes: 3 additions & 0 deletions src/main/resources/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
bot-token: ""
channel-id: ""
webhook-url: ""

0 comments on commit 6c24d05

Please sign in to comment.