|
| 1 | +package tax.cute.mcinfoplugin; |
| 2 | + |
| 3 | +import net.mamoe.mirai.contact.Group; |
| 4 | +import net.mamoe.mirai.message.data.ForwardMessageBuilder; |
| 5 | +import net.mamoe.mirai.message.data.Image; |
| 6 | +import net.mamoe.mirai.message.data.PlainText; |
| 7 | +import net.mamoe.mirai.message.data.SingleMessage; |
| 8 | +import net.mamoe.mirai.utils.ExternalResource; |
| 9 | +import tax.cute.minecraftinfoapi.PlayerInfo; |
| 10 | +import tax.cute.minecraftinfoapi.UUID; |
| 11 | +import top.mrxiaom.miraiutils.CommandModel; |
| 12 | +import top.mrxiaom.miraiutils.CommandSender; |
| 13 | +import top.mrxiaom.miraiutils.CommandSenderGroup; |
| 14 | + |
| 15 | +import java.io.IOException; |
| 16 | + |
| 17 | +public class MCInfo extends CommandModel { |
| 18 | + public MCInfo() { |
| 19 | + super("mcinfo"); |
| 20 | + } |
| 21 | + |
| 22 | + @Override |
| 23 | + public void onCommand(CommandSender sender, SingleMessage[] args) { |
| 24 | + if (sender instanceof CommandSenderGroup) { |
| 25 | + CommandSenderGroup senderGroup = (CommandSenderGroup) sender; |
| 26 | + Group group = senderGroup.getGroup(); |
| 27 | + |
| 28 | + if (args[0].contentToString().equalsIgnoreCase("/mcInfo")) { |
| 29 | + group.sendMessage("用法:/mcinfo [名称/UUID]"); |
| 30 | + return; |
| 31 | + } |
| 32 | + |
| 33 | + if (args[0].contentToString().isEmpty()) { |
| 34 | + group.sendMessage("请输入有效用户名"); |
| 35 | + return; |
| 36 | + } |
| 37 | + try { |
| 38 | + sendMcInfo(group, args[0].contentToString()); |
| 39 | + } catch (Exception e) { |
| 40 | + group.sendMessage("获取失败,请稍后重试"); |
| 41 | + e.printStackTrace(); |
| 42 | + } |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + private void sendMcInfo(Group group, String name) throws IOException { |
| 47 | + String id; |
| 48 | + if (name.replace("-", "").length() == 32) { |
| 49 | + id = name; |
| 50 | + } else { |
| 51 | + UUID uuid = UUID.getId(name); |
| 52 | + if (uuid == null) { |
| 53 | + group.sendMessage("无法找到这个用户"); |
| 54 | + return; |
| 55 | + } |
| 56 | + id = uuid.getId(); |
| 57 | + } |
| 58 | + |
| 59 | + PlayerInfo info = PlayerInfo.getPlayerInfo(id); |
| 60 | + if (info == null) { |
| 61 | + group.sendMessage("查询失败"); |
| 62 | + return; |
| 63 | + } |
| 64 | + String text = |
| 65 | + "==MC档案查询==" + |
| 66 | + "\n[ 用户名 ] " + info.getNowName() + |
| 67 | + "\n[ UUID ] " + id; |
| 68 | + |
| 69 | + byte[] avatar_bytes = Util.getWebBytes(ApiUrl.MC_HEAD_SKIN + id); |
| 70 | + if (avatar_bytes == null) { |
| 71 | + group.sendMessage("查询失败"); |
| 72 | + return; |
| 73 | + } |
| 74 | + |
| 75 | + Image avatar = group.uploadImage(ExternalResource.create(avatar_bytes)); |
| 76 | + group.sendMessage(avatar.plus(text)); |
| 77 | + |
| 78 | + ForwardMessageBuilder builder = new ForwardMessageBuilder(group); |
| 79 | + builder.add(group.getBot().getId(), group.getBot().getNick(), new PlainText("曾用名")); |
| 80 | + |
| 81 | + for (int i = 0; i < info.getData().size(); i++) { |
| 82 | + String time; |
| 83 | + if (info.getData().get(i).getTimestamp() == -1) { |
| 84 | + time = "初始用户名"; |
| 85 | + } else { |
| 86 | + time = info.getData().get(i).getTime(); |
| 87 | + } |
| 88 | + builder.add(group.getBot().getId(), group.getBot().getNick(), new PlainText( |
| 89 | + "名称:" + info.getData().get(i).getName() + |
| 90 | + "\n修改时间:" + time |
| 91 | + )); |
| 92 | + } |
| 93 | + |
| 94 | + group.sendMessage(builder.build()); |
| 95 | + } |
| 96 | +} |
0 commit comments