Compare commits

...

7 commits

6 changed files with 36 additions and 40 deletions

View file

@ -40,3 +40,7 @@ mvnw.cmd package
This will produce a file inside the `target` directory. You can now take the This will produce a file inside the `target` directory. You can now take the
`.jar` file that doesn't start with `original-` and put it in the `plugins` `.jar` file that doesn't start with `original-` and put it in the `plugins`
directory of your Server. directory of your Server.
## LICENSE
The software is licensed under the [MPL-2.0](LICENSE) license.

View file

@ -21,11 +21,11 @@ public final class ChatRelay extends JavaPlugin {
@Override @Override
public void onEnable() { public void onEnable() {
var messageListener = new DiscordMessageListener(guildId, channelId, this.getServer()); var server = this.getServer();
var slashListener = new DiscordSlashCommandListener(this.getServer()); var messageListener = new DiscordEventListener(guildId, channelId, server);
this.jda = JDABuilder.createDefault(discordToken) this.jda = JDABuilder.createDefault(discordToken)
.enableIntents(GatewayIntent.MESSAGE_CONTENT) .enableIntents(GatewayIntent.MESSAGE_CONTENT)
.addEventListeners(messageListener, slashListener) .addEventListeners(messageListener)
.build(); .build();
try { try {
jda.awaitReady(); jda.awaitReady();
@ -34,7 +34,7 @@ public final class ChatRelay extends JavaPlugin {
} }
jda.updateCommands().addCommands(Commands.slash("list", "List of all online players.")).queue(); jda.updateCommands().addCommands(Commands.slash("list", "List of all online players.")).queue();
discordMessageChannel = new DiscordMessageChannel(obtainTargetChannel(jda)); discordMessageChannel = new DiscordMessageChannel(obtainTargetChannel(jda));
getServer().getPluginManager().registerEvents(new MinecraftMessageListener(discordMessageChannel), this); server.getPluginManager().registerEvents(new MinecraftEventListener(discordMessageChannel), this);
logger.log(Level.INFO, "Bot is now online."); logger.log(Level.INFO, "Bot is now online.");
discordMessageChannel.sendServerEvent("Server is now online."); discordMessageChannel.sendServerEvent("Server is now online.");
} }

View file

@ -1,18 +1,21 @@
package de.hhhammer.chatrelay; package de.hhhammer.chatrelay;
import net.dv8tion.jda.api.entities.channel.ChannelType; import net.dv8tion.jda.api.entities.channel.ChannelType;
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
import net.dv8tion.jda.api.events.message.MessageReceivedEvent; import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter; import net.dv8tion.jda.api.hooks.ListenerAdapter;
import net.kyori.adventure.text.Component; import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor; import net.kyori.adventure.text.format.NamedTextColor;
import org.bukkit.Server; import org.bukkit.Server;
public class DiscordMessageListener extends ListenerAdapter { import java.util.stream.Collectors;
public class DiscordEventListener extends ListenerAdapter {
private final String guildId; private final String guildId;
private final String channelId; private final String channelId;
private final Server server; private final Server server;
public DiscordMessageListener(String guildId, String channelId, Server server) { public DiscordEventListener(String guildId, String channelId, Server server) {
this.guildId = guildId; this.guildId = guildId;
this.channelId = channelId; this.channelId = channelId;
this.server = server; this.server = server;
@ -35,4 +38,18 @@ public class DiscordMessageListener extends ListenerAdapter {
.append(Component.text(event.getMessage().getContentDisplay())); .append(Component.text(event.getMessage().getContentDisplay()));
this.server.broadcast(message); this.server.broadcast(message);
} }
@Override
public void onSlashCommandInteraction(SlashCommandInteractionEvent event) {
switch (event.getName()) {
case "list":
var maxPlayer = server.getMaxPlayers();
var players = server.getOnlinePlayers();
var listOfPlayers = players.stream()
.map(player -> "- " + player.getName() + "\n")
.collect(Collectors.joining());
event.reply("_Server_: Following players are online (" + players.size() + "/" + maxPlayer + "):\n" + listOfPlayers).queue();
break;
}
}
} }

View file

@ -13,7 +13,11 @@ public class DiscordMessageChannel {
textChannel.sendMessage(msq).queue(); textChannel.sendMessage(msq).queue();
} }
public void sendServerEvent(String msq) { public void sendPlayerEvent(String name, String msg) {
send("Server: " + msq); send("**" + name + "**: " + msg);
}
public void sendServerEvent(String msg) {
send("_Server_: " + msg);
} }
} }

View file

@ -1,29 +0,0 @@
package de.hhhammer.chatrelay;
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter;
import org.bukkit.Server;
import java.util.stream.Collectors;
public class DiscordSlashCommandListener extends ListenerAdapter {
private final Server server;
public DiscordSlashCommandListener(Server server) {
this.server = server;
}
@Override
public void onSlashCommandInteraction(SlashCommandInteractionEvent event) {
switch (event.getName()) {
case "list":
var players = server.getOnlinePlayers();
var listOfPlayers = players.stream()
.map(player -> "- " + player.getName() + "\n")
.collect(Collectors.joining());
event.reply("Server: Following players are online:\n" + listOfPlayers).queue();
break;
}
}
}

View file

@ -7,10 +7,10 @@ import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent; import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerQuitEvent; import org.bukkit.event.player.PlayerQuitEvent;
public class MinecraftMessageListener implements Listener { public class MinecraftEventListener implements Listener {
private final DiscordMessageChannel discordMessageChannel; private final DiscordMessageChannel discordMessageChannel;
public MinecraftMessageListener(DiscordMessageChannel discordMessageChannel) { public MinecraftEventListener(DiscordMessageChannel discordMessageChannel) {
this.discordMessageChannel = discordMessageChannel; this.discordMessageChannel = discordMessageChannel;
} }
@ -18,7 +18,7 @@ public class MinecraftMessageListener implements Listener {
public void onPlayerMessage(AsyncChatEvent event) { public void onPlayerMessage(AsyncChatEvent event) {
var message = event.message(); var message = event.message();
if (message instanceof TextComponent textMessage) { if (message instanceof TextComponent textMessage) {
discordMessageChannel.send(event.getPlayer().getName() + ": " + textMessage.content()); discordMessageChannel.sendPlayerEvent(event.getPlayer().getName(), textMessage.content());
} }
} }