This repository was archived by the owner on Jan 30, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
789 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xmlns="http://maven.apache.org/POM/4.0.0" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<groupId>cn.lingsmc</groupId> | ||
<artifactId>LingsHTTPUtils</artifactId> | ||
<version>1.0</version> | ||
<packaging>jar</packaging> | ||
|
||
<name>LingsHTTPUtils</name> | ||
|
||
<description>Connect website text with PAPI.</description> | ||
<properties> | ||
<java.version>1.8</java.version> | ||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
</properties> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-compiler-plugin</artifactId> | ||
<version>3.8.1</version> | ||
<configuration> | ||
<source>${java.version}</source> | ||
<target>${java.version}</target> | ||
</configuration> | ||
</plugin> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-shade-plugin</artifactId> | ||
<version>3.2.4</version> | ||
<executions> | ||
<execution> | ||
<phase>package</phase> | ||
<goals> | ||
<goal>shade</goal> | ||
</goals> | ||
<configuration> | ||
<createDependencyReducedPom>false</createDependencyReducedPom> | ||
</configuration> | ||
</execution> | ||
</executions> | ||
</plugin> | ||
</plugins> | ||
<resources> | ||
<resource> | ||
<directory>src/main/resources</directory> | ||
<filtering>true</filtering> | ||
</resource> | ||
</resources> | ||
</build> | ||
|
||
<repositories> | ||
<repository> | ||
<id>spigotmc-repo</id> | ||
<url>https://hub.spigotmc.org/nexus/content/repositories/snapshots/</url> | ||
</repository> | ||
<repository> | ||
<id>sonatype</id> | ||
<url>https://oss.sonatype.org/content/groups/public/</url> | ||
</repository> | ||
<repository> | ||
<id>placeholderapi</id> | ||
<url>https://repo.extendedclip.com/content/repositories/placeholderapi/</url> | ||
</repository> | ||
</repositories> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>org.spigotmc</groupId> | ||
<artifactId>spigot-api</artifactId> | ||
<version>1.12-R0.1-SNAPSHOT</version> | ||
<scope>provided</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.projectlombok</groupId> | ||
<artifactId>lombok</artifactId> | ||
<version>1.18.24</version> | ||
<scope>provided</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>me.clip</groupId> | ||
<artifactId>placeholderapi</artifactId> | ||
<version>2.11.2</version> | ||
<scope>provided</scope> | ||
</dependency> | ||
</dependencies> | ||
</project> |
54 changes: 54 additions & 0 deletions
54
src/main/java/cn/lingsmc/lingshttputils/LingsHTTPUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package cn.lingsmc.lingshttputils; | ||
|
||
|
||
import cn.lingsmc.lingshttputils.asyncworkers.WorkerOptions; | ||
import cn.lingsmc.lingshttputils.commands.Commands; | ||
import cn.lingsmc.lingshttputils.commands.TabComplete; | ||
import cn.lingsmc.lingshttputils.placeholderapi.PlaceholderAPI; | ||
import com.google.common.collect.Maps; | ||
import lombok.Getter; | ||
import org.bukkit.Bukkit; | ||
import org.bukkit.command.PluginCommand; | ||
import org.bukkit.plugin.java.JavaPlugin; | ||
|
||
import java.util.Map; | ||
|
||
|
||
/** | ||
* @author Crsuh2er0 | ||
* @date 2022/8/30 | ||
* @apiNote | ||
*/ | ||
public final class LingsHTTPUtils extends JavaPlugin { | ||
@Getter | ||
private static LingsHTTPUtils instance; | ||
|
||
@Getter | ||
private final Map<String, String> httpData = Maps.newConcurrentMap(); | ||
|
||
@Override | ||
public void onLoad() { | ||
instance = this; | ||
saveDefaultConfig(); | ||
getConfig().options().copyDefaults(true); | ||
} | ||
|
||
@Override | ||
public void onEnable() { | ||
// Plugin startup logic | ||
if (Bukkit.getPluginManager().getPlugin("PlaceholderAPI") != null) { | ||
new PlaceholderAPI(this).register(); | ||
} | ||
final PluginCommand command = this.getCommand(instance.getName()); | ||
command.setExecutor(new Commands()); | ||
command.setTabCompleter(new TabComplete()); | ||
// 运行Cycle worker | ||
WorkerOptions.runWorkers(); | ||
} | ||
|
||
@Override | ||
public void onDisable() { | ||
// Plugin shutdown logic | ||
WorkerOptions.stopWorkers(); | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
src/main/java/cn/lingsmc/lingshttputils/asyncworkers/AsyncWorkers.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package cn.lingsmc.lingshttputils.asyncworkers; | ||
|
||
import cn.lingsmc.lingshttputils.LingsHTTPUtils; | ||
import cn.lingsmc.lingshttputils.requesters.HTTPRequester; | ||
import cn.lingsmc.lingshttputils.utils.JsonUtils; | ||
import lombok.Getter; | ||
import org.bukkit.Bukkit; | ||
import org.bukkit.configuration.file.FileConfiguration; | ||
import org.bukkit.scheduler.BukkitRunnable; | ||
|
||
|
||
/** | ||
* @author Crsuh2er0 | ||
* @date 2022/8/30 | ||
* @apiNote | ||
*/ | ||
public class AsyncWorkers { | ||
@Getter | ||
private final FileConfiguration config = LingsHTTPUtils.getInstance().getConfig(); | ||
@Getter | ||
Runnable taskB; | ||
@Getter | ||
Runnable task; | ||
|
||
public void asyncworker(String module, int reqTime, String url, String method, int refInterval, String[] keys) { | ||
task = new BukkitRunnable() { | ||
@Override | ||
public void run() { | ||
String res = HTTPRequester.request(url, reqTime, method); | ||
if (res == null) { | ||
return; | ||
} | ||
if ("json".equals(config.getString(String.format("%s.mode", module)))) { | ||
res = JsonUtils.getValue(JsonUtils.parseStr(res), keys, 0); | ||
} | ||
LingsHTTPUtils.getInstance().getHttpData().put(module, res); | ||
try { | ||
Thread.sleep(refInterval); | ||
} catch (InterruptedException e) { | ||
throw new RuntimeException(e); | ||
} | ||
Bukkit.getScheduler().runTaskAsynchronously(Bukkit.getPluginManager().getPlugin(LingsHTTPUtils.getInstance().getName()), task); | ||
} | ||
}; | ||
Bukkit.getScheduler().runTaskAsynchronously(Bukkit.getPluginManager().getPlugin(LingsHTTPUtils.getInstance().getName()), task); | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
src/main/java/cn/lingsmc/lingshttputils/asyncworkers/WorkerOptions.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package cn.lingsmc.lingshttputils.asyncworkers; | ||
|
||
import cn.lingsmc.lingshttputils.LingsHTTPUtils; | ||
import lombok.Getter; | ||
import org.bukkit.configuration.file.FileConfiguration; | ||
|
||
import java.util.Objects; | ||
import java.util.Set; | ||
import java.util.concurrent.ExecutorService; | ||
import java.util.concurrent.Executors; | ||
|
||
/** | ||
* @author Crsuh2er0 | ||
* @date 2022/8/30 | ||
* @apiNote | ||
*/ | ||
public class WorkerOptions { | ||
@Getter | ||
static boolean started = true; | ||
private static ExecutorService newFixedThreadPool; | ||
|
||
|
||
public WorkerOptions() { | ||
} | ||
|
||
public static void runWorkers() { | ||
started = true; | ||
newFixedThreadPool = Executors.newFixedThreadPool(2); | ||
Set<String> modules = LingsHTTPUtils.getInstance().getConfig().getKeys(false); | ||
FileConfiguration config = LingsHTTPUtils.getInstance().getConfig(); | ||
for (String module : modules) { | ||
if (Objects.equals(config.getString(String.format("%s.reqMode", module)), "Cycle")) { | ||
// 一定要为每个module设置一个worker | ||
AsyncWorkers asyncWorkers = new AsyncWorkers(); | ||
final int reqTime = config.getInt(String.format("%s.reqTime", module)); | ||
final String url = config.getString(String.format("%s.url", module)); | ||
final String method; | ||
final int refInterval = config.getInt(String.format("%s.refInterval", module)); | ||
if (config.getString(String.format("%s.method", module)) != null) { | ||
method = config.getString(String.format("%s.method", module)); | ||
} else { | ||
method = "GET"; | ||
} | ||
String[] keys = new String[0]; | ||
if ("json".equals(config.getString(String.format("%s.mode", module)))) { | ||
keys = config.getString(String.format("%s.key", module)).split("\\."); | ||
} | ||
String[] finalKeys = keys; | ||
Runnable runnable = () -> asyncWorkers.asyncworker(module, reqTime, url, method, refInterval, finalKeys); | ||
LingsHTTPUtils.getInstance().getLogger().info("尝试启动Cycle Worker..."); | ||
newFixedThreadPool.execute(runnable); | ||
} | ||
} | ||
} | ||
|
||
public static void stopWorkers() { | ||
LingsHTTPUtils.getInstance().getLogger().info("尝试关闭Cycle Worker..."); | ||
newFixedThreadPool.shutdownNow(); | ||
started = false; | ||
} | ||
} |
Oops, something went wrong.