Compare commits

..

No commits in common. "bcee524b185b552629126828f810efd04d9c5571" and "0ba4836840d66ecb7c84bd563a6767b09be78193" have entirely different histories.

6 changed files with 40 additions and 36 deletions

View file

@ -40,7 +40,3 @@ 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 server = this.getServer(); var messageListener = new DiscordMessageListener(guildId, channelId, this.getServer());
var messageListener = new DiscordEventListener(guildId, channelId, server); var slashListener = new DiscordSlashCommandListener(this.getServer());
this.jda = JDABuilder.createDefault(discordToken) this.jda = JDABuilder.createDefault(discordToken)
.enableIntents(GatewayIntent.MESSAGE_CONTENT) .enableIntents(GatewayIntent.MESSAGE_CONTENT)
.addEventListeners(messageListener) .addEventListeners(messageListener, slashListener)
.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));
server.getPluginManager().registerEvents(new MinecraftEventListener(discordMessageChannel), this); getServer().getPluginManager().registerEvents(new MinecraftMessageListener(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

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

View file

@ -1,21 +1,18 @@
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;
import java.util.stream.Collectors; public class DiscordMessageListener extends ListenerAdapter {
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 DiscordEventListener(String guildId, String channelId, Server server) { public DiscordMessageListener(String guildId, String channelId, Server server) {
this.guildId = guildId; this.guildId = guildId;
this.channelId = channelId; this.channelId = channelId;
this.server = server; this.server = server;
@ -38,18 +35,4 @@ public class DiscordEventListener 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

@ -0,0 +1,29 @@
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 MinecraftEventListener implements Listener { public class MinecraftMessageListener implements Listener {
private final DiscordMessageChannel discordMessageChannel; private final DiscordMessageChannel discordMessageChannel;
public MinecraftEventListener(DiscordMessageChannel discordMessageChannel) { public MinecraftMessageListener(DiscordMessageChannel discordMessageChannel) {
this.discordMessageChannel = discordMessageChannel; this.discordMessageChannel = discordMessageChannel;
} }
@ -18,7 +18,7 @@ public class MinecraftEventListener 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.sendPlayerEvent(event.getPlayer().getName(), textMessage.content()); discordMessageChannel.send(event.getPlayer().getName() + ": " + textMessage.content());
} }
} }