Skip to content

Commit 9774309

Browse files
authored
updata via upload
1 parent a57a4ec commit 9774309

File tree

2 files changed

+100
-3
lines changed

2 files changed

+100
-3
lines changed

src/main/java/ogcl/fun/PlayerIPadmin.java

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,35 @@
44

55
public class PlayerIPadmin extends JavaPlugin {
66
private DatabaseManager dbManager;
7+
private Commands commands;
8+
private PlayerIPadminAPI api;
79

810
@Override
911
public void onEnable() {
10-
saveDefaultConfig(); // 确保配置文件被加载
12+
saveDefaultConfig(); // 确保加载默认配置
1113
dbManager = new DatabaseManager(this); // 初始化数据库管理器
12-
// 注册getip命令执行器
13-
this.getCommand("getip").setExecutor(new Commands(this, dbManager));
14+
commands = new Commands(this, dbManager); // 初始化命令处理器
15+
getCommand("getip").setExecutor(commands); // 注册/getip命令
16+
17+
api = new PlayerIPadminAPI(this); // 初始化API
18+
1419
// 注册事件监听器
1520
getServer().getPluginManager().registerEvents(new PlayerLoginListener(this, dbManager), this);
21+
1622
getLogger().info("PlayerIPadmin 插件已启动!");
1723
}
1824

1925
@Override
2026
public void onDisable() {
2127
getLogger().info("PlayerIPadmin 插件已关闭!");
2228
}
29+
30+
public PlayerIPadminAPI getApi() {
31+
return api;
32+
}
33+
34+
// 如果其他地方需要访问Commands实例,可以提供getter方法
35+
public Commands getCommands() {
36+
return commands;
37+
}
2338
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package ogcl.fun;
2+
3+
import org.bukkit.Bukkit;
4+
import org.bukkit.entity.Player;
5+
import com.google.gson.JsonObject;
6+
import com.google.gson.JsonParser;
7+
8+
import java.io.BufferedReader;
9+
import java.io.InputStreamReader;
10+
import java.net.HttpURLConnection;
11+
import java.net.URL;
12+
13+
public class PlayerIPadminAPI {
14+
private final PlayerIPadmin plugin;
15+
16+
public PlayerIPadminAPI(PlayerIPadmin plugin) {
17+
this.plugin = plugin;
18+
}
19+
20+
/**
21+
* 获取指定玩家的IP地址。
22+
*
23+
* @param player 目标玩家
24+
* @return 返回玩家的IP地址,如果无法获取则返回"未知"。
25+
*/
26+
public String getPlayerIP(Player player) {
27+
if (player.getAddress() != null) {
28+
return player.getAddress().getAddress().getHostAddress();
29+
}
30+
return "未知";
31+
}
32+
33+
/**
34+
* 异步方式获取指定IP的地理位置信息。
35+
*
36+
* @param ip 目标IP地址
37+
* @param callback 当获取到地理位置信息后将通过这个回调返回
38+
*/
39+
public void getPlayerLocationAsync(String ip, LocationInfoCallback callback) {
40+
Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> {
41+
String locationInfo = getLocationInfo(ip);
42+
// 确保回调在主线程执行
43+
Bukkit.getScheduler().runTask(plugin, () -> callback.onLocationInfoReceived(locationInfo));
44+
});
45+
}
46+
47+
/**
48+
* 从外部API获取IP地址对应的地理位置信息。
49+
*
50+
* @param ip 目标IP地址
51+
* @return 返回地理位置信息,如果失败则返回"未知"。
52+
*/
53+
private String getLocationInfo(String ip) {
54+
String apiUrl = "https://www.ip.cn/api/index?ip=" + ip + "&type=1";
55+
try {
56+
URL url = new URL(apiUrl);
57+
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
58+
conn.setRequestMethod("GET");
59+
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
60+
StringBuilder response = new StringBuilder();
61+
String inputLine;
62+
while ((inputLine = in.readLine()) != null) {
63+
response.append(inputLine);
64+
}
65+
in.close();
66+
JsonObject jsonResponse = JsonParser.parseString(response.toString()).getAsJsonObject();
67+
if (jsonResponse.get("code").getAsInt() == 0) {
68+
return jsonResponse.get("address").getAsString();
69+
}
70+
} catch (Exception e) {
71+
Bukkit.getLogger().warning("[PlayerIPadmin] Failed to fetch location for IP: " + ip + " - " + e.getMessage());
72+
}
73+
return "未知";
74+
}
75+
76+
/**
77+
* 地理位置信息获取完成后的回调接口。
78+
*/
79+
public interface LocationInfoCallback {
80+
void onLocationInfoReceived(String locationInfo);
81+
}
82+
}

0 commit comments

Comments
 (0)