Skip to content
This repository was archived by the owner on Jan 30, 2023. It is now read-only.

Commit

Permalink
Merge pull request #7 from Crsuh2er0/main
Browse files Browse the repository at this point in the history
修复对1.14+的支持
  • Loading branch information
Crsuh2er0 authored Oct 18, 2022
2 parents 27a6702 + a1af160 commit 8780b15
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 29 deletions.
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,12 @@
<version>1.8-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.spigotmc</groupId>
<artifactId>spigot-api</artifactId>
<version>1.14-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/cn/lingsmc/lingshttputils/LingsHttpUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,17 @@ public final class LingsHttpUtils extends JavaPlugin {
private static LingsHttpUtils instance;
@Getter
private final Map<String, String> httpData = Maps.newConcurrentMap();
public boolean gson = false;
@Getter
private String pluginName;
private String serverVersion;

@Override
public void onLoad() {
serverVersion = Bukkit.getBukkitVersion();
if (serverVersion.compareTo("1.14") > 0) {
gson = true;
}
instance = this;
saveDefaultConfig();
config = getConfig();
Expand Down
43 changes: 38 additions & 5 deletions src/main/java/cn/lingsmc/lingshttputils/utils/JsonUtils.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package cn.lingsmc.lingshttputils.utils;

import cn.lingsmc.lingshttputils.LingsHttpUtils;
import org.bukkit.plugin.Plugin;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.json.simple.JSONObject;
Expand All @@ -15,12 +16,23 @@
* @apiNote
*/
public class JsonUtils {
static Plugin plugin = LingsHttpUtils.getInstance();
static LingsHttpUtils plugin = LingsHttpUtils.getInstance();

private JsonUtils() {
}

public static @Nullable JSONObject parseStr(String jsonString) {
public static String getValue(String jsonString, String @NotNull [] keys) {
if (plugin.gson) {
return getValueGson(jsonString, keys);
} else {
return getValueJsonSimple(jsonString, keys);
}
}

/**
* 使用com.googlecode.json-simple(1.13-)
*/
public static @Nullable JSONObject parseStrJsonsimple(String jsonString) {
JSONParser parser = new JSONParser();
try {
return (JSONObject) parser.parse(jsonString);
Expand All @@ -30,8 +42,11 @@ private JsonUtils() {
}
}

public static @Nullable String getValue(String jsonString, String @NotNull [] keys) {
JSONObject json = parseStr(jsonString);
/**
* 使用com.googlecode.json-simple(1.13-)
*/
public static @Nullable String getValueJsonSimple(String jsonString, String @NotNull [] keys) {
JSONObject json = parseStrJsonsimple(jsonString);
if (Objects.isNull(json)) {
return null;
}
Expand All @@ -43,4 +58,22 @@ private JsonUtils() {
}
return value.toString();
}

/**
* 使用Gson(1.14+)
*/
public static JsonObject parseStrGson(String str) {
return new JsonParser().parse(str).getAsJsonObject();
}

/**
* 使用Gson(1.14+)
*/
public static String getValueGson(String str, String @NotNull [] keys) {
JsonObject value = parseStrGson(str);
for (String key : keys) {
value = value.get(key).getAsJsonObject();
}
return value.toString();
}
}
48 changes: 24 additions & 24 deletions src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,38 +6,38 @@ version: 1.2.2
# 每个模块名字随意
# 样例
bilibili_followers:
# 是否已开启
# 是否已开启
enabled: false
#
# mode
# json 获取json格式的字符串并解析
# string 直接获取文本
#
# mode
# json 获取json格式的字符串并解析
# string 直接获取文本
mode: "json"
#
# key 当模式为json时要获取的值对应的键
# 只在模式为json时可用
#
# key 当模式为json时要获取的值对应的键
# 只在模式为json时可用
key: "data.follower"
#
# apiname 比如你想要PAPI变量名为 %lhu_ab_cd% 那么apiname为"ab_cd"
# 此插件的变量名头为lhu
#
# apiname 比如你想要PAPI变量名为 %lhu_ab_cd% 那么apiname为"ab_cd"
# 此插件的变量名头为lhu
apiname: "bilibili_followers"
#
# url 要请求的网页链接
#
# url 要请求的网页链接
url: "https://api.bilibili.com/x/relation/stat?vmid=359206390"
#
# reqTime 请求超时时间(ms)
# 仅当reqMode: Cycle时可用
#
# reqTime 请求超时时间(ms)
# 仅当reqMode: Cycle时可用
reqTime: 10000
#
# refInterval 请求间隔时间
#
# refInterval 请求间隔时间
refInterval: 30000
#
# method 请求方法 不指定则使用GET
#
# method 请求方法 不指定则使用GET
method: "GET"
#
# reqMode 请求方式
# Cycle 循环请求
# inTime 即时请求(注意可能卡住服务器)
#
# reqMode 请求方式
# Cycle 循环请求
# inTime 即时请求(注意可能卡住服务器)
reqMode: "Cycle"

hitokoto:
Expand Down

0 comments on commit 8780b15

Please sign in to comment.