Skip to content
This repository has been archived by the owner on Aug 28, 2022. It is now read-only.

Commit

Permalink
修复bug
Browse files Browse the repository at this point in the history
1.修复服务器返回错误格式Base64导致无法解析的问题.
2.优化查询失败判定
  • Loading branch information
Pmaru-top authored Jul 30, 2021
1 parent ea65107 commit 88d214a
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 36 deletions.
16 changes: 11 additions & 5 deletions src/tax/cute/mcpingplugin/JETypeset.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,24 @@ public static JETypeset getTypeset(String host, int port, String typesetText) th
final String type = "%type";
final String mod_list = "%mod_list";

MCPing motd = MCPing.getMotd(host, port,2000);
MCPing motd = MCPing.getMotd(host, port,2500);

boolean sendFavicon = typesetText.contains(favicon);
List<String> modList = null;
if (typesetText.contains(mod_list) || motd.getModList().size() > 0) {
modList = motd.getModList();
}
byte[] favicon_bytes = null;
if (sendFavicon) {
if (motd.getFavicon().equals("null"))
favicon_bytes = Base64.getDecoder().decode(Util.MC_SERVER_DEFAULT_FAVICON_BASE64);
else favicon_bytes = Base64.getDecoder().decode(motd.getFavicon());
try {
if (sendFavicon) {
if (motd.getFavicon().equals("null"))
favicon_bytes = Base64.getDecoder().decode(Util.MC_SERVER_DEFAULT_FAVICON_BASE64);
else {
favicon_bytes = Base64.getDecoder().decode(motd.getFavicon().replace("\n",""));
}
}
} catch (Exception e) {
favicon_bytes = Base64.getDecoder().decode(Util.MC_SERVER_DEFAULT_FAVICON_BASE64);
}

String motdText = typesetText
Expand Down
8 changes: 3 additions & 5 deletions src/tax/cute/mcpingplugin/MCJEPingThread.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import net.mamoe.mirai.utils.ExternalResource;
import tax.cute.mcpingplugin.Util.Srv;
import tax.cute.mcpingplugin.Util.Util;
import top.mrxiaom.miraiutils.CommandListener;

import java.io.IOException;
import java.util.List;
Expand All @@ -20,7 +19,7 @@ public class MCJEPingThread extends Thread {
Object sendObject;
public int status = -1;

public MCJEPingThread(Plugin plugin, String host, int port,Object sendObject) {
public MCJEPingThread(Plugin plugin, String host, int port, Object sendObject) {
this.host = host;
this.port = port;
this.sendObject = sendObject;
Expand All @@ -35,15 +34,14 @@ public void run() {
public void ping() {
if (port == -1) port = 25565;

if(sendObject instanceof Group) {
if (sendObject instanceof Group) {
Group group = (Group) sendObject;
Srv srv = Srv.getSrv(host, Util.MC_SRV);
Srv srv = Srv.getSrv(host,Util.MC_SRV);
if (srv != null) {
host = srv.getSrvHost();
port = srv.getSrvPort();
group.sendMessage("检测到存在Srv记录 已自动跳转到\n>>\n" + host + ":" + port);
}

JETypeset typeset;
try {
//获取信息并排版
Expand Down
20 changes: 8 additions & 12 deletions src/tax/cute/mcpingplugin/Util/Srv.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,40 +4,36 @@
import javax.naming.directory.InitialDirContext;
import java.util.Hashtable;

public class Srv {
public class Srv{
private String srvHost;
private int srvPort;

public Srv(String srvHost,int srvPort) {
this.srvHost = srvHost;
this.srvPort = srvPort;
}

public static Srv getSrv(String host,String Srv) {
String SrvHost;
int SrvPort;
public static Srv getSrv(String host, String srvName) {
Hashtable<String, String> hashtable = new Hashtable<String, String>();
hashtable.put("java.naming.factory.initial", "com.sun.jndi.dns.DnsContextFactory");
hashtable.put("java.naming.provider.url", "dns:");
try {
Attribute qwq = (new InitialDirContext(hashtable)).getAttributes((new StringBuilder()).append(Srv).append(host).toString(), new String[]{"SRV"}).get("srv");
Attribute qwq = (new InitialDirContext(hashtable)).getAttributes(srvName + host, new String[]{"SRV"}).get("srv");
if (qwq != null) {
String[] re = qwq.get().toString().split(" ", 4);
SrvHost = re[3].substring(0, re[3].length() - 1);
SrvPort = Integer.parseInt(re[2]);
return new Srv(SrvHost, SrvPort);
String srvHost = re[3].substring(0, re[3].length() - 1);
int srvPort = Integer.parseInt(re[2]);
return new Srv(srvHost,srvPort);
}
} catch (Exception ignored) {
return null;
}
return null;
}

public String getSrvHost() {
return this.srvHost;
return srvHost;
}

public int getSrvPort() {
return this.srvPort;
return srvPort;
}
}
4 changes: 2 additions & 2 deletions src/tax/cute/mcpingplugin/commands/BindServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ private void getBind(Object sendObject) {
for (int i = 0; i < array.size(); i++) {
count++;
JSONObject json = array.getJSONObject(i);
builder.add(group.getBot().getId(), "Server" + i, new PlainText(
builder.add(group.getBot().getId(), "Server" + (i + 1), new PlainText(
"Group:" + json.get("GroupNum") +
"\nCmd:" + json.get("CMD") +
"\nHost:" + json.get("Host")
Expand Down Expand Up @@ -193,7 +193,7 @@ private void getBind(Object sendObject) {
for (int i = 0; i < array.size(); i++) {
count++;
JSONObject json = array.getJSONObject(i);
builder.add(friend.getBot().getId(), "Server" + i, new PlainText(
builder.add(friend.getBot().getId(), "Server" + (i + 1), new PlainText(
"Group:" + json.get("GroupNum") +
"\nCmd:" + json.get("CMD") +
"\nHost:" + json.get("Host")
Expand Down
12 changes: 0 additions & 12 deletions src/tax/cute/mcpingplugin/commands/MCPing.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,6 @@ public static void sendMCPing(Plugin plugin, Object sendObject, String ip, int p
public void run() {
if (be.status == 0 && je.status == 0)
group.sendMessage("查询失败 请检测服务器是否开启");
if (je.isAlive() && be.status == 0)
group.sendMessage("查询失败 请检查服务器是否开启");
if (je.isAlive() && be.isAlive())
group.sendMessage("查询失败 请检查服务器是否开启");
je.stop();
be.stop();
}
}, 3000);
} else if (sendObject instanceof Friend) {
Expand All @@ -101,12 +95,6 @@ public void run() {
public void run() {
if (be.status == 0 && je.status == 0)
friend.sendMessage("查询失败 请检测服务器是否开启");
if (je.isAlive() && be.status == 0)
friend.sendMessage("查询失败 请检查服务器是否开启");
if (je.isAlive() && be.isAlive())
friend.sendMessage("查询失败 请检查服务器是否开启");
je.stop();
be.stop();
}
}, 3000);
}
Expand Down

0 comments on commit 88d214a

Please sign in to comment.