diff --git a/src/main/java/space/yurisi/universecorev2/subplugins/universediscord/UniverseDiscord.java b/src/main/java/space/yurisi/universecorev2/subplugins/universediscord/UniverseDiscord.java index 261bab07..39ee7ff8 100644 --- a/src/main/java/space/yurisi/universecorev2/subplugins/universediscord/UniverseDiscord.java +++ b/src/main/java/space/yurisi/universecorev2/subplugins/universediscord/UniverseDiscord.java @@ -3,6 +3,7 @@ import net.dv8tion.jda.api.JDA; import net.dv8tion.jda.api.JDABuilder; import net.dv8tion.jda.api.entities.Guild; +import net.dv8tion.jda.api.entities.channel.ChannelType; import net.dv8tion.jda.api.entities.channel.concrete.TextChannel; import net.dv8tion.jda.api.requests.GatewayIntent; import org.bukkit.Bukkit; @@ -39,17 +40,21 @@ public void onEnable(UniverseCoreV2 core) { try { jda.awaitReady(); } catch (InterruptedException e) { - throw new DiscordJDANotReadyException("JDA is not ready!"); + throw new DiscordJDANotReadyException("JDA の初期化に失敗しました。詳しくは JDA のスタックトレースを確認してください"); } Guild guild = jda.getGuildById(config.getDiscordGuildId()); if (guild == null) { - throw new DiscordGuildNotFoundException("Guild not found with ID: " + config.getDiscordGuildId()); + throw new DiscordGuildNotFoundException("指定されたギルドIDは無効です: " + config.getDiscordGuildId()); } TextChannel channel = guild.getTextChannelById(config.getDiscordChannelId()); if (channel == null) { - throw new DiscordChannelNotFoundException("Channel not found with ID: " + config.getDiscordChannelId()); + throw new DiscordChannelNotFoundException("指定されたチャンネルIDは無効です: " + config.getDiscordChannelId()); + } + + if (channel.getType() != ChannelType.TEXT) { + throw new DiscordChannelNotFoundException("指定されたチャンネルIDはテキストチャンネルではありません: " + config.getDiscordChannelId()); } new EventManager(core, channel); diff --git a/src/main/java/space/yurisi/universecorev2/subplugins/universediscord/UniverseDiscordChannel.java b/src/main/java/space/yurisi/universecorev2/subplugins/universediscord/UniverseDiscordChannel.java deleted file mode 100644 index efff74c5..00000000 --- a/src/main/java/space/yurisi/universecorev2/subplugins/universediscord/UniverseDiscordChannel.java +++ /dev/null @@ -1,12 +0,0 @@ -package space.yurisi.universecorev2.subplugins.universediscord; - -import net.dv8tion.jda.api.entities.channel.concrete.TextChannel; -import org.bukkit.Bukkit; - -public class UniverseDiscordChannel { - - public static void updateDiscordChannelTopic(TextChannel channel, int playerCount) { - channel.getManager().setTopic(playerCount + "人が接続中").queue(); - } - -} diff --git a/src/main/java/space/yurisi/universecorev2/subplugins/universediscord/UniverseDiscordMessage.java b/src/main/java/space/yurisi/universecorev2/subplugins/universediscord/UniverseDiscordMessage.java index f8950db8..cb51485d 100644 --- a/src/main/java/space/yurisi/universecorev2/subplugins/universediscord/UniverseDiscordMessage.java +++ b/src/main/java/space/yurisi/universecorev2/subplugins/universediscord/UniverseDiscordMessage.java @@ -9,6 +9,8 @@ import org.bukkit.entity.Player; import space.yurisi.universecorev2.utils.Message; +import java.util.stream.Collectors; + public class UniverseDiscordMessage { /** Minecraft へのメッセージ */ @@ -26,7 +28,7 @@ public static void sendMessageToMinecraft(Member discordMember, String discordMe name_format = discordMember.getUser().getName() + " (" + discordMember.getNickname() + ")"; } - Bukkit.broadcast(Component.text("§a[Discord#" + name_format + "] " + "§f" + discordMessage)); + Bukkit.broadcast(Component.text("§a[Discord] §r<" + name_format + "> " + discordMessage)); } @@ -36,7 +38,7 @@ public static void sendMessageToDiscord(Player player, TextChannel discordChanne Location location = player.getLocation(); String world_name = location.getWorld().getName(); - discordChannel.sendMessage("[" + player.getName() + " | " + world_name + "]" + " " + message).queue(); + discordChannel.sendMessage("[" + player.getName() + "@" + world_name + "]" + " " + message).queue(); } public static void sendEventMessageToDiscord(TextChannel discordChannel, String message, int color) { @@ -51,7 +53,7 @@ public static void sendJoinMessageToDiscord(Player player, TextChannel discordCh EmbedBuilder embed = new EmbedBuilder(); embed.setTitle("入室"); embed.setDescription(player.getName() + " がログインしました"); - embed.setThumbnail("https://mc-heads.net/avatar/" + player.getUniqueId() + "/100/nohelm.png"); + embed.setThumbnail("https://mc-heads.net/head/" + player.getUniqueId()); embed.setColor(0x2AFF5C); discordChannel.sendMessageEmbeds(embed.build()).queue(); @@ -61,10 +63,27 @@ public static void sendQuitMessageToDiscord(Player player, TextChannel discordCh EmbedBuilder embed = new EmbedBuilder(); embed.setTitle("退出"); embed.setDescription(player.getName() + " がログアウトしました"); - embed.setThumbnail("https://mc-heads.net/avatar/" + player.getUniqueId() + "/100/nohelm.png"); + embed.setThumbnail("https://mc-heads.net/head/" + player.getUniqueId()); embed.setColor(0x687EFF); discordChannel.sendMessageEmbeds(embed.build()).queue(); } + public static void sendStatusMessageToMinecraft(TextChannel discordChannel) { + String players = Bukkit.getOnlinePlayers().stream() + .map(Player::getName) + .collect(Collectors.joining(", ")); + int online_players = Bukkit.getOnlinePlayers().size(); + int max_players = Bukkit.getMaxPlayers(); + + EmbedBuilder embed = new EmbedBuilder(); + embed.setTitle("サーバー情報"); + embed.setDescription("```\n" + players + "\n```"); + embed.setFooter("現在のプレイヤー数: " + online_players + " / " + max_players); + embed.setColor(0xFFA74B); + + discordChannel.sendMessageEmbeds(embed.build()).queue(); + + } + } diff --git a/src/main/java/space/yurisi/universecorev2/subplugins/universediscord/event/DiscordEvent.java b/src/main/java/space/yurisi/universecorev2/subplugins/universediscord/event/DiscordEvent.java index a6305fed..7b4729c5 100644 --- a/src/main/java/space/yurisi/universecorev2/subplugins/universediscord/event/DiscordEvent.java +++ b/src/main/java/space/yurisi/universecorev2/subplugins/universediscord/event/DiscordEvent.java @@ -32,6 +32,12 @@ public void onMessageReceived(MessageReceivedEvent event) { return; } + if (content.equals("!playerlist")) { + // すでに環境変数で指定されたチャンネルであることは確認済みなので, 暗黙的キャストでテキストチャンネル *として* 扱う + UniverseDiscordMessage.sendStatusMessageToMinecraft(event.getChannel().asTextChannel()); + return; + } + UniverseDiscordMessage.sendMessageToMinecraft(member, content); } diff --git a/src/main/java/space/yurisi/universecorev2/subplugins/universediscord/event/PlayerEvent.java b/src/main/java/space/yurisi/universecorev2/subplugins/universediscord/event/PlayerEvent.java index b8b98d48..c075334e 100644 --- a/src/main/java/space/yurisi/universecorev2/subplugins/universediscord/event/PlayerEvent.java +++ b/src/main/java/space/yurisi/universecorev2/subplugins/universediscord/event/PlayerEvent.java @@ -13,7 +13,6 @@ import org.bukkit.event.player.PlayerAdvancementDoneEvent; import org.bukkit.event.player.PlayerJoinEvent; import org.bukkit.event.player.PlayerQuitEvent; -import space.yurisi.universecorev2.subplugins.universediscord.UniverseDiscordChannel; import space.yurisi.universecorev2.subplugins.universediscord.UniverseDiscordMessage; import space.yurisi.universecorev2.utils.Message; @@ -28,13 +27,11 @@ public PlayerEvent(TextChannel discordChannel) { @EventHandler(priority = EventPriority.HIGHEST) public void onJoin(PlayerJoinEvent event) { UniverseDiscordMessage.sendJoinMessageToDiscord(event.getPlayer(), discordChannel); - UniverseDiscordChannel.updateDiscordChannelTopic(discordChannel, event.getPlayer().getServer().getOnlinePlayers().size()); } @EventHandler(priority = EventPriority.HIGHEST) public void onQuit(PlayerQuitEvent event) { UniverseDiscordMessage.sendQuitMessageToDiscord(event.getPlayer(), discordChannel); - UniverseDiscordChannel.updateDiscordChannelTopic(discordChannel, event.getPlayer().getServer().getOnlinePlayers().size()); } @EventHandler(priority = EventPriority.HIGHEST)