-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implement runner and separate http transfer logic
- Loading branch information
alex2276564
committed
Feb 6, 2025
1 parent
e67425e
commit 7b9d5a2
Showing
5 changed files
with
151 additions
and
23 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
51 changes: 51 additions & 0 deletions
51
src/main/java/uz/alex2276564/leverlock/task/BukkitRunner.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,51 @@ | ||
package uz.alex2276564.leverlock.task; | ||
|
||
import org.bukkit.plugin.java.JavaPlugin; | ||
import org.bukkit.scheduler.BukkitScheduler; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
public class BukkitRunner implements Runner { | ||
|
||
private final JavaPlugin plugin; | ||
private final BukkitScheduler scheduler; | ||
|
||
public BukkitRunner(JavaPlugin plugin) { | ||
this.plugin = plugin; | ||
this.scheduler = plugin.getServer().getScheduler(); | ||
} | ||
|
||
@Override | ||
public void run(@NotNull Runnable task) { | ||
scheduler.runTask(plugin, task); | ||
} | ||
|
||
@Override | ||
public void runAsync(@NotNull Runnable task) { | ||
scheduler.runTaskAsynchronously(plugin, task); | ||
} | ||
|
||
@Override | ||
public void runDelayed(@NotNull Runnable task, long delayTicks) { | ||
scheduler.runTaskLater(plugin, task, delayTicks); | ||
} | ||
|
||
@Override | ||
public void runDelayedAsync(@NotNull Runnable task, long delayTicks) { | ||
scheduler.runTaskLaterAsynchronously(plugin, task, delayTicks); | ||
} | ||
|
||
@Override | ||
public void runPeriodical(@NotNull Runnable task, long delayTicks, long periodTicks) { | ||
scheduler.runTaskTimer(plugin, task, delayTicks, periodTicks); | ||
} | ||
|
||
@Override | ||
public void runPeriodicalAsync(@NotNull Runnable task, long delayTicks, long periodTicks) { | ||
scheduler.runTaskTimerAsynchronously(plugin, task, delayTicks, periodTicks); | ||
} | ||
|
||
@Override | ||
public void cancelTasks() { | ||
scheduler.cancelTasks(plugin); | ||
} | ||
} |
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,20 @@ | ||
package uz.alex2276564.leverlock.task; | ||
|
||
import org.jetbrains.annotations.NotNull; | ||
|
||
public interface Runner { | ||
|
||
void run(@NotNull Runnable task); | ||
|
||
void runAsync(@NotNull Runnable task); | ||
|
||
void runDelayed(@NotNull Runnable task, long delayTicks); | ||
|
||
void runDelayedAsync(@NotNull Runnable task, long delayTicks); | ||
|
||
void runPeriodical(@NotNull Runnable task, long delayTicks, long periodTicks); | ||
|
||
void runPeriodicalAsync(@NotNull Runnable task, long delayTicks, long periodTicks); | ||
|
||
void cancelTasks(); | ||
} |
51 changes: 51 additions & 0 deletions
51
src/main/java/uz/alex2276564/leverlock/utils/HttpUtils.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,51 @@ | ||
package uz.alex2276564.leverlock.utils; | ||
|
||
import com.google.gson.JsonObject; | ||
import com.google.gson.JsonParser; | ||
import lombok.Getter; | ||
|
||
import java.io.InputStreamReader; | ||
import java.net.HttpURLConnection; | ||
import java.net.URL; | ||
import java.nio.charset.StandardCharsets; | ||
|
||
public class HttpUtils { | ||
|
||
@Getter | ||
public static class HttpResponse { | ||
private final int responseCode; | ||
private final JsonObject jsonBody; | ||
|
||
public HttpResponse(int responseCode, JsonObject jsonBody) { | ||
this.responseCode = responseCode; | ||
this.jsonBody = jsonBody; | ||
} | ||
} | ||
|
||
/** | ||
* Executes a GET request and returns an HttpResponse containing the response code and JSON body. | ||
* | ||
* @param urlString URL for the request. | ||
* @param userAgent User-Agent for the header (you can pass null if not needed). | ||
* @return HttpResponse containing the response code and JSON body. | ||
* @throws Exception if the request fails. | ||
*/ | ||
public static HttpResponse getResponse(String urlString, String userAgent) throws Exception { | ||
URL url = new URL(urlString); | ||
HttpURLConnection connection = (HttpURLConnection) url.openConnection(); | ||
|
||
connection.setRequestMethod("GET"); | ||
if (userAgent != null) { | ||
connection.setRequestProperty("User-Agent", userAgent); | ||
} | ||
|
||
try (InputStreamReader reader = new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8)) { | ||
int responseCode = connection.getResponseCode(); | ||
JsonObject jsonBody = JsonParser.parseReader(reader).getAsJsonObject(); | ||
|
||
return new HttpResponse(responseCode, jsonBody); | ||
} finally { | ||
connection.disconnect(); | ||
} | ||
} | ||
} |
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