Skip to content
This repository has been archived by the owner on Mar 8, 2024. It is now read-only.

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
MX233 committed Oct 7, 2021
1 parent 572a915 commit 2f15db3
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 128 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@

这是一个基于[mirai](https://github.com/mamoe/mirai) 框架的qq机器人插件

语法/编译版本: `Java11`

用到的依赖 [MinecraftInfoAPI](https://github.com/MX233/MinecraftInfoAPI)

### 功能
- `获取Minecraft java版玩家档案信息(UUID,曾用名)`
- `获取Minecraft java版玩家皮肤`
Expand Down
94 changes: 39 additions & 55 deletions src/tax/cute/mcinfoplugin/MCInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,22 @@
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;

import java.io.IOException;

public class MCInfo extends CommandModel {
public MCInfo() {
Plugin plugin;

public MCInfo(Plugin plugin) {
super("mcinfo");
this.plugin = plugin;
}

@Override
Expand All @@ -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());
}
}
}
51 changes: 23 additions & 28 deletions src/tax/cute/mcinfoplugin/MCSkin.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,20 @@
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;

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
Expand All @@ -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);
}
}
62 changes: 35 additions & 27 deletions src/tax/cute/mcinfoplugin/MCSkinFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,63 +4,71 @@
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;

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);
}
}
}
11 changes: 6 additions & 5 deletions src/tax/cute/mcinfoplugin/Plugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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
Expand Down
15 changes: 2 additions & 13 deletions src/tax/cute/mcinfoplugin/Util.java
Original file line number Diff line number Diff line change
@@ -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;
}
}

0 comments on commit 2f15db3

Please sign in to comment.