22
33import cn .handyplus .lib .constants .BaseConstants ;
44import cn .handyplus .lib .core .StrUtil ;
5+ import cn .handyplus .lib .util .HandyConfigUtil ;
56import cn .handyplus .region .constants .BaseIpConstants ;
67import cn .handyplus .region .constants .IpGetTypeEnum ;
78import lombok .SneakyThrows ;
1314import java .net .Inet6Address ;
1415import java .net .InetAddress ;
1516import 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+ }
0 commit comments