Skip to content

Commit 7266bcd

Browse files
committed
feat: 支持自定义地区值转换
1 parent faa9e0f commit 7266bcd

4 files changed

Lines changed: 43 additions & 12 deletions

File tree

pom.xml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@
66

77
<groupId>cn.handyplus.region</groupId>
88
<artifactId>ip2region</artifactId>
9-
<version>2.1.3</version>
9+
<version>2.2.0</version>
1010

1111
<properties>
1212
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
13-
<HandyLib.version>3.40.4</HandyLib.version>
14-
<ip2region.version>3.1.1</ip2region.version>
13+
<HandyLib.version>3.40.5</HandyLib.version>
14+
<ip2region.version>3.3.7</ip2region.version>
1515
</properties>
1616

1717
<dependencies>
@@ -44,7 +44,7 @@
4444
<dependency>
4545
<groupId>cn.handyplus.lib</groupId>
4646
<artifactId>HandyBom</artifactId>
47-
<version>1.1.1</version>
47+
<version>1.1.3</version>
4848
<type>pom</type>
4949
<scope>import</scope>
5050
</dependency>

src/main/java/cn/handyplus/region/util/IpUtil.java

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import cn.handyplus.lib.constants.BaseConstants;
44
import cn.handyplus.lib.core.StrUtil;
5+
import cn.handyplus.lib.util.HandyConfigUtil;
56
import cn.handyplus.region.constants.BaseIpConstants;
67
import cn.handyplus.region.constants.IpGetTypeEnum;
78
import lombok.SneakyThrows;
@@ -13,6 +14,7 @@
1314
import java.net.Inet6Address;
1415
import java.net.InetAddress;
1516
import java.net.InetSocketAddress;
17+
import java.util.Map;
1618

1719
/**
1820
* 获取ip地址
@@ -49,6 +51,11 @@ public static void getPlayerRegion(Player player) {
4951
if (IpGetTypeEnum.VORE_API.getIpGetType().equalsIgnoreCase(dataSource)) {
5052
VoreApiUtil.getPlayerRegion(player);
5153
}
54+
String region = convertRegion(BaseIpConstants.PLAYER_REGION_MAP.get(player.getUniqueId()));
55+
if (StrUtil.isEmpty(region)) {
56+
return;
57+
}
58+
BaseIpConstants.PLAYER_REGION_MAP.put(player.getUniqueId(), region);
5259
}
5360

5461
/**
@@ -59,27 +66,47 @@ public static void getPlayerRegion(Player player) {
5966
*/
6067
public static String getIpRegion(String ip) {
6168
String dataSource = BaseConstants.CONFIG.getString("dataSource", IpGetTypeEnum.OFFLINE.getIpGetType());
69+
String region = null;
6270
// 离线模式
6371
if (IpGetTypeEnum.OFFLINE.getIpGetType().equalsIgnoreCase(dataSource)) {
64-
return SearcherUtil.getIpRegion(ip);
72+
region = SearcherUtil.getIpRegion(ip);
6573
}
6674
// 请求 ipPlus360 模式
6775
if (IpGetTypeEnum.IP_PLUS_360.getIpGetType().equalsIgnoreCase(dataSource)) {
68-
return IpPlus360Util.getIpRegion(ip, null);
76+
region = IpPlus360Util.getIpRegion(ip, null);
6977
}
7078
// 请求 ipApi 模式
7179
if (IpGetTypeEnum.IP_API.getIpGetType().equalsIgnoreCase(dataSource)) {
72-
return IpApiUtil.getIpRegion(ip);
80+
region = IpApiUtil.getIpRegion(ip);
7381
}
7482
// 请求 whois 模式
7583
if (IpGetTypeEnum.WHOIS.getIpGetType().equalsIgnoreCase(dataSource)) {
76-
return WhoisUtil.getIpRegion(ip);
84+
region = WhoisUtil.getIpRegion(ip);
7785
}
7886
// 请求 voreApi 模式
7987
if (IpGetTypeEnum.VORE_API.getIpGetType().equalsIgnoreCase(dataSource)) {
80-
return VoreApiUtil.getIpRegion(ip);
88+
region = VoreApiUtil.getIpRegion(ip);
89+
}
90+
return convertRegion(region);
91+
}
92+
93+
/**
94+
* 转换地区格式
95+
*
96+
* @param region 地区
97+
* @return 转换后的地区
98+
*/
99+
private static String convertRegion(String region) {
100+
if (StrUtil.isEmpty(region)) {
101+
return region;
102+
}
103+
Map<String, Object> valueMapping = HandyConfigUtil.getChildMap(BaseConstants.CONFIG, "valueMapping");
104+
String[] values = region.split("\\|", -1);
105+
for (int i = 0; i < values.length; i++) {
106+
Object mappedValue = valueMapping.get(values[i]);
107+
values[i] = mappedValue == null ? values[i] : String.valueOf(mappedValue);
81108
}
82-
return null;
109+
return String.join("|", values);
83110
}
84111

85112
/**
@@ -139,4 +166,4 @@ protected static String getStr(String str) {
139166
return StrUtil.isNotEmpty(str) ? str : "0";
140167
}
141168

142-
}
169+
}

src/main/resources/config.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ unknown: "未知"
88

99
local: "内网IP"
1010

11+
# 自定义变量值转换,仅完整匹配获取到的单个值
12+
valueMapping:
13+
Tokyo: "东京"
14+
1115
# 数据来源 自己选择一个最准确的即可
1216
# offline 本地数据源模式(支持ipv4/ipv6 百分之99.9数据准确性)
1317
# ipPlus360 在线api模式(支持ipv4/ipv6 付费 必需先购买key填下面)

src/main/resources/plugin.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: ip2region
22
main: cn.handyplus.region.Ip2region
3-
version: 2.1.3
3+
version: 2.2.0
44
author: handy
55
api-version: 1.13
66
website: https://ricedoc.handyplus.cn/wiki/ip2region/log

0 commit comments

Comments
 (0)