From 2f15db387bab683e198c4a9e97ee8c6849a83760 Mon Sep 17 00:00:00 2001 From: MX233 <3225971360@qq.com> Date: Thu, 7 Oct 2021 15:59:00 +0800 Subject: [PATCH] Update --- README.md | 4 + src/tax/cute/mcinfoplugin/MCInfo.java | 94 ++++++++++------------- src/tax/cute/mcinfoplugin/MCSkin.java | 51 ++++++------ src/tax/cute/mcinfoplugin/MCSkinFile.java | 62 ++++++++------- src/tax/cute/mcinfoplugin/Plugin.java | 11 +-- src/tax/cute/mcinfoplugin/Util.java | 15 +--- 6 files changed, 109 insertions(+), 128 deletions(-) diff --git a/README.md b/README.md index f2d8a13..290d5e0 100644 --- a/README.md +++ b/README.md @@ -3,6 +3,10 @@ 这是一个基于[mirai](https://github.com/mamoe/mirai) 框架的qq机器人插件 +语法/编译版本: `Java11` + +用到的依赖 [MinecraftInfoAPI](https://github.com/MX233/MinecraftInfoAPI) + ### 功能 - `获取Minecraft java版玩家档案信息(UUID,曾用名)` - `获取Minecraft java版玩家皮肤` diff --git a/src/tax/cute/mcinfoplugin/MCInfo.java b/src/tax/cute/mcinfoplugin/MCInfo.java index 106b088..9dc1427 100644 --- a/src/tax/cute/mcinfoplugin/MCInfo.java +++ b/src/tax/cute/mcinfoplugin/MCInfo.java @@ -6,8 +6,10 @@ import net.mamoe.mirai.message.data.PlainText; import net.mamoe.mirai.message.data.SingleMessage; import net.mamoe.mirai.utils.ExternalResource; -import tax.cute.minecraftinfoapi.PlayerInfo; -import tax.cute.minecraftinfoapi.UUID; +import tax.cute.minecraftinfoapi.CommonException; +import tax.cute.minecraftinfoapi.NameHistory; +import tax.cute.minecraftinfoapi.Player; +import tax.cute.minecraftinfoapi.utils.Http; import top.mrxiaom.miraiutils.CommandModel; import top.mrxiaom.miraiutils.CommandSender; import top.mrxiaom.miraiutils.CommandSenderGroup; @@ -15,8 +17,11 @@ import java.io.IOException; public class MCInfo extends CommandModel { - public MCInfo() { + Plugin plugin; + + public MCInfo(Plugin plugin) { super("mcinfo"); + this.plugin = plugin; } @Override @@ -25,72 +30,51 @@ public void onCommand(CommandSender sender, SingleMessage[] args) { CommandSenderGroup senderGroup = (CommandSenderGroup) sender; Group group = senderGroup.getGroup(); - if (args[0].contentToString().equalsIgnoreCase("/mcInfo")) { - group.sendMessage("÷:/mcinfo [/UUID]"); + if (args[0].contentToString().isEmpty()) { + group.sendMessage("Чû"); return; } - if (args[0].contentToString().isEmpty()) { - group.sendMessage("Чû"); + if (args[0].contentToString().equalsIgnoreCase("/mcInfo")) { + group.sendMessage("÷:/mcInfo [û/uuid]"); return; } + try { - sendMcInfo(group, args[0].contentToString()); - } catch (Exception e) { - group.sendMessage("ȡʧ,Ժ"); - e.printStackTrace(); + send(group, args[0].contentToString()); + } catch (IOException e) { + plugin.getLogger().warning(e.getMessage()); + } catch (CommonException e) { + group.sendMessage("ѯʧ:޷ҵ"); } } } - private void sendMcInfo(Group group, String name) throws IOException { - String id; - if (name.replace("-", "").length() == 32) { - id = name; - } else { - UUID uuid = UUID.getId(name); - if (uuid == null) { - group.sendMessage("޷ҵû"); - return; - } - id = uuid.getId(); - } + private void send(Group group, String name) throws IOException, CommonException { + String uuid; + if (Util.isUuid(name)) + uuid = name; + else + uuid = Player.getPlayer(name).getUuid(); - PlayerInfo info = PlayerInfo.getPlayerInfo(id); - if (info == null) { - group.sendMessage("ѯʧ"); - return; - } - String text = - "==MCѯ==" + - "\n[ û ] " + info.getNowName() + - "\n[ UUID ] " + id; + NameHistory nameHistory = NameHistory.getNameHistory(uuid); - byte[] avatar_bytes = Util.getWebBytes(ApiUrl.MC_HEAD_SKIN + id); - if (avatar_bytes == null) { - group.sendMessage("ѯʧ"); - return; - } + Image image = group.uploadImage(ExternalResource.create(Http.getHttp(ApiUrl.MC_HEAD_SKIN + uuid).getBytes())); - Image avatar = group.uploadImage(ExternalResource.create(avatar_bytes)); - group.sendMessage(avatar.plus(text)); + group.sendMessage( + image.plus( + "---MCҵ---" + + "\n: " + nameHistory.getCurrentName() + + "\nuuid: " + uuid + ) + ); ForwardMessageBuilder builder = new ForwardMessageBuilder(group); - builder.add(group.getBot().getId(), group.getBot().getNick(), new PlainText("")); - - for (int i = 0; i < info.getData().size(); i++) { - String time; - if (info.getData().get(i).getTimestamp() == -1) { - time = "ʼû"; - } else { - time = info.getData().get(i).getTime(); - } - builder.add(group.getBot().getId(), group.getBot().getNick(), new PlainText( - ":" + info.getData().get(i).getName() + - "\n޸ʱ:" + time - )); - } - + builder.add(group.getBot().getId(), group.getBot().getNick(), new PlainText("---MCб---")); + nameHistory.getNameHistoryData().forEach((data -> builder.add(group.getBot().getId(), group.getBot().getNick(), new PlainText( + ": " + data.getName() + + "\n޸ʱ: " + tax.cute.minecraftinfoapi.utils.Util.toTime(data.getChangedToAt()) + )))); group.sendMessage(builder.build()); } -} +} \ No newline at end of file diff --git a/src/tax/cute/mcinfoplugin/MCSkin.java b/src/tax/cute/mcinfoplugin/MCSkin.java index 9e7a4e0..b00fd27 100644 --- a/src/tax/cute/mcinfoplugin/MCSkin.java +++ b/src/tax/cute/mcinfoplugin/MCSkin.java @@ -4,7 +4,9 @@ import net.mamoe.mirai.message.data.Image; import net.mamoe.mirai.message.data.SingleMessage; import net.mamoe.mirai.utils.ExternalResource; -import tax.cute.minecraftinfoapi.UUID; +import tax.cute.minecraftinfoapi.CommonException; +import tax.cute.minecraftinfoapi.Player; +import tax.cute.minecraftinfoapi.utils.Http; import top.mrxiaom.miraiutils.CommandModel; import top.mrxiaom.miraiutils.CommandSender; import top.mrxiaom.miraiutils.CommandSenderGroup; @@ -12,8 +14,10 @@ import java.io.IOException; public class MCSkin extends CommandModel { - public MCSkin() { - super("mcskin"); + Plugin plugin; + public MCSkin(Plugin plugin) { + super("McSkin"); + this.plugin = plugin; } @Override @@ -22,44 +26,35 @@ public void onCommand(CommandSender sender, SingleMessage[] args) { CommandSenderGroup senderGroup = (CommandSenderGroup)sender; Group group = senderGroup.getGroup(); - if (args[0].contentToString().equalsIgnoreCase("/mcskin")) { - group.sendMessage("÷:/mcskin [/UUID]"); + if (args[0].contentToString().isEmpty()) { + group.sendMessage("Чû"); return; } - if (args[0].contentToString().isEmpty()) { - group.sendMessage("Чû"); + if (args[0].contentToString().equalsIgnoreCase("/mcskin")) { + group.sendMessage("÷:/mcskin [û/uuid]"); return; } try { send(group, args[0].contentToString()); - } catch (Exception e) { - group.sendMessage("ȡʧ,Ժ"); - e.printStackTrace(); + } catch (IOException e) { + plugin.getLogger().warning(e.getMessage()); + } catch (CommonException e) { + group.sendMessage("ѯʧ:޷ҵ"); } } } - private void send(Group group,String name) throws IOException { - String id; - if (name.replace("-", "").length() == 32) { - id = name; - } else { - UUID uuid = UUID.getId(name); - if (uuid == null) { - group.sendMessage("޷ҵû"); - return; - } - id = uuid.getId(); - } + private void send(Group group,String name) throws IOException, CommonException { + String uuid; + if (Util.isUuid(name)) + uuid = name; + else + uuid = Player.getPlayer(name).getUuid(); + + Image image = group.uploadImage(ExternalResource.create(Http.getHttp(ApiUrl.MC_BODY_SKIN + uuid).getBytes())); - byte[] skin_bytes = Util.getWebBytes(ApiUrl.MC_BODY_SKIN + id); - if (skin_bytes == null) { - group.sendMessage("ѯʧ"); - return; - } - Image image = group.uploadImage(ExternalResource.create(skin_bytes)); group.sendMessage(image); } } diff --git a/src/tax/cute/mcinfoplugin/MCSkinFile.java b/src/tax/cute/mcinfoplugin/MCSkinFile.java index eeeb589..8f79b55 100644 --- a/src/tax/cute/mcinfoplugin/MCSkinFile.java +++ b/src/tax/cute/mcinfoplugin/MCSkinFile.java @@ -4,8 +4,9 @@ import net.mamoe.mirai.message.data.Image; import net.mamoe.mirai.message.data.SingleMessage; import net.mamoe.mirai.utils.ExternalResource; -import tax.cute.minecraftinfoapi.Skin; -import tax.cute.minecraftinfoapi.UUID; +import tax.cute.minecraftinfoapi.CommonException; +import tax.cute.minecraftinfoapi.Player; +import tax.cute.minecraftinfoapi.Profile; import top.mrxiaom.miraiutils.CommandModel; import top.mrxiaom.miraiutils.CommandSender; import top.mrxiaom.miraiutils.CommandSenderGroup; @@ -13,54 +14,61 @@ import java.io.IOException; public class MCSkinFile extends CommandModel { - public MCSkinFile() { + Plugin plugin; + + public MCSkinFile(Plugin plugin) { super("mcSkinFile"); + this.plugin = plugin; } @Override public void onCommand(CommandSender sender, SingleMessage[] args) { - if (args[0].contentToString().equalsIgnoreCase("/mcSkinFile")) return; if (sender instanceof CommandSenderGroup) { CommandSenderGroup senderGroup = (CommandSenderGroup) sender; Group group = senderGroup.getGroup(); - if (args[0].contentToString().equalsIgnoreCase("/mcInfo")) { - group.sendMessage("÷:/mcinfo [/UUID]"); + if (args[0].contentToString().isEmpty()) { + group.sendMessage("Чû"); return; } - if (args[0].contentToString().isEmpty()) { - group.sendMessage("Чû"); + if (args[0].contentToString().equalsIgnoreCase("/mcSkinFile")) { + group.sendMessage("÷:/mcskinfile [û/uuid]"); return; } try { - sendSkinFile(group, args[0].contentToString()); + send(group, args[0].contentToString()); + } catch (IOException e) { + group.sendMessage("ʧ,Ժ"); + } catch (CommonException e) { + group.sendMessage("ѯʧ:޷ҵ"); } catch (Exception e) { - group.sendMessage("ȡʧ,Ժ"); + group.sendMessage(String.valueOf(e)); + e.printStackTrace(); } } } - private void sendSkinFile(Group group, String name) throws IOException { - String id; - if (name.replace("-", "").length() == 32) { - id = name; + private void send(Group group, String name) throws IOException, CommonException { + String uuid; + if(Util.isUuid(name)) + uuid = name; + else + uuid = Player.getPlayer(name).getUuid(); + + Profile profile = Profile.getProfile(uuid); + + if (profile.getSkinUrl() != null) { + Image image = group.uploadImage(ExternalResource.create(profile.getSkinBytes())); + group.sendMessage(image); } else { - UUID uuid = UUID.getId(name); - if (uuid == null) { - group.sendMessage("޷ҵû"); - return; - } - id = uuid.getId(); - } - if (id == null) { - group.sendMessage("޷ҵû"); - return; + group.sendMessage("ȡʧ"); } - byte[] bytes = Skin.getSkin(id).getSkinBytes(); - Image image = group.uploadImage(ExternalResource.create(bytes)); - group.sendMessage(image); + if (profile.getCapeUrl() != null) { + Image image = group.uploadImage(ExternalResource.create(profile.getCapeBytes())); + group.sendMessage(image); + } } } \ No newline at end of file diff --git a/src/tax/cute/mcinfoplugin/Plugin.java b/src/tax/cute/mcinfoplugin/Plugin.java index e3fff86..98f501b 100644 --- a/src/tax/cute/mcinfoplugin/Plugin.java +++ b/src/tax/cute/mcinfoplugin/Plugin.java @@ -14,7 +14,7 @@ public class Plugin extends JavaPlugin { public Plugin() { super(new JvmPluginDescriptionBuilder( "tax.cute.mcinfoplugin", // id - "1.0.0" // version + "1.1.0" // version ) .name("MCInfoPlugin") .author("CuteStar") @@ -26,14 +26,15 @@ public Plugin() { @Override public void onLoad(PluginComponentStorage pcs) { getLogger().info("MCInfoPluginLoading"); - getLogger().info("Author:CuteStar"); + getLogger().info("Author: CuteStar"); + getLogger().info("Version: 1.1"); getLogger().info("Github: https://github.com/MX233/MCInfoPlugin"); } private void register() { - mcSkin = new MCSkin(); - mcInfo = new MCInfo(); - mcSkinFile = new MCSkinFile(); + mcSkin = new MCSkin(this); + mcInfo = new MCInfo(this); + mcSkinFile = new MCSkinFile(this); } @Override diff --git a/src/tax/cute/mcinfoplugin/Util.java b/src/tax/cute/mcinfoplugin/Util.java index db20768..6991c33 100644 --- a/src/tax/cute/mcinfoplugin/Util.java +++ b/src/tax/cute/mcinfoplugin/Util.java @@ -1,18 +1,7 @@ package tax.cute.mcinfoplugin; -import java.io.IOException; -import java.io.InputStream; -import java.net.HttpURLConnection; -import java.net.URL; - public class Util { - public static byte[] getWebBytes(String url) throws IOException { - URL u = new URL(url); - HttpURLConnection http = (HttpURLConnection)u.openConnection(); - if(http.getResponseCode() != 200) return null; - InputStream in = http.getInputStream(); - byte[] bytes = in.readAllBytes(); - in.close(); - return bytes; + public static boolean isUuid(String s) { + return s.replace("-","").length() == 32; } }