Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: !playerlist の追加 #169

Merged
merged 4 commits into from
Oct 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import org.bukkit.entity.Player;
import space.yurisi.universecorev2.utils.Message;

import java.util.stream.Collectors;

public class UniverseDiscordMessage {

/** Minecraft へのメッセージ */
Expand All @@ -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));
}


Expand All @@ -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) {
Expand All @@ -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();
Expand All @@ -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();

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ public void onMessageReceived(MessageReceivedEvent event) {
return;
}

if (content.equals("!playerlist")) {
// すでに環境変数で指定されたチャンネルであることは確認済みなので, 暗黙的キャストでテキストチャンネル *として* 扱う
UniverseDiscordMessage.sendStatusMessageToMinecraft(event.getChannel().asTextChannel());
return;
}

UniverseDiscordMessage.sendMessageToMinecraft(member, content);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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)
Expand Down