Skip to content

Commit

Permalink
ported from pmmp
Browse files Browse the repository at this point in the history
  • Loading branch information
xxAROX committed Feb 17, 2024
1 parent 638dd50 commit b7bf9d2
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 37 deletions.
16 changes: 14 additions & 2 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ jobs:
- name: "Get version"
id: metadata
run: |
echo "NEW_VERSION=$(php -r 'echo (version_compare(yaml_parse_file(getcwd() . "/src/main/resources/plugin.yml")["version"], file_get_contents(__DIR__ + "/version-nukkit.txt"), ">") ? "y" : "n");')" >> $GITHUB_OUTPUT
echo "VERSION=$(php -r 'echo explode("+", explode("-", yaml_parse_file(getcwd() . "/src/main/resources/plugin.yml")["version"])[0])[0];')" >> $GITHUB_OUTPUT
echo "FULL_VERSION=$(php -r 'echo yaml_parse_file(getcwd() . "/src/main/resources/plugin.yml")["version"];')" >> $GITHUB_OUTPUT
echo "NAME=$(php -r 'echo yaml_parse_file(getcwd() . "/src/main/resources/plugin.yml")["name"];')" >> $GITHUB_OUTPUT
Expand Down Expand Up @@ -52,8 +53,19 @@ jobs:
title: "Release v${{ steps.metadata.outputs.VERSION }}"
files: "target/**.jar"

#- name: "Create version-nukkit.txt"
# run: "echo \"${{ steps.metadata.outputs.VERSION }}\" >> version-nukkit.txt"
- name: "Create version-nukkit.txt"
if: "${{ steps.metadata.outputs.NEW_VERSION }} === 'y'"
run: "echo \"${{ steps.metadata.outputs.VERSION }}\" > version-nukkit.txt"

- name: "Update version.txt"
uses: "test-room-7/action-update-file@v1"
if: "${{ steps.metadata.outputs.NEW_VERSION }} === 'y'"
with:
file-path: version-nukkit.txt
commit-msg: Updated version - ci ignore
github-token: ${{ secrets.GITHUB_TOKEN }}
committer-name: "xxAROX"
committer-email: "[email protected]"

#- name: "Pushes latest version-nukkit.txt file"
# uses: dmnemec/copy_file_to_another_repo_action@main
Expand Down
47 changes: 25 additions & 22 deletions src/main/java/xxAROX/PresenceMan/NukkitX/PresenceMan.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ public static PresenceMan getInstance() {
return instance;
}

private static final int HEAD_SIZE_MAX = 512;
private static final int HEAD_SIZE_MIN = 16;

private static String token = "undefined";
public static String client_id = null;
public static String server = "undefined";
Expand Down Expand Up @@ -73,7 +76,7 @@ public static PresenceMan getInstance() {
@Override public void onEnable() {
Server.getInstance().getPluginManager().registerEvents(new EventListener(), this);
Server.getInstance().getScheduler().scheduleRepeatingTask(this, new UpdateCheckerTask(), 20 *60 *60); // NOTE: 60 minutes
Server.getInstance().getScheduler().scheduleAsyncTask(this, new FetchGatewayInformationTask());
FetchGatewayInformationTask.unga_bunga();
}
@Override public void onDisable() {
for (Player player : Server.getInstance().getOnlinePlayers().values()) offline(player);
Expand All @@ -84,6 +87,27 @@ private static void runTask(BackendRequest task){
else Server.getInstance().getScheduler().scheduleAsyncTask(PresenceMan.getInstance(), task);
}

public static String getHeadURL(String xuid, boolean gray, Integer size) {
size = size != null ? Math.min(512, Math.max(16, size)) : null;
String url = ApiRequest.URI_GET_HEAD + xuid;
if (size != null) url += "?size=" + size;
if (gray) url += size != null ? "&gray" : "?gray";
return Gateway.getUrl() + url;
}
public static String getHeadURL(String xuid, boolean gray){
return getHeadURL(xuid, gray, null);
}
public static String getHeadURL(String xuid, Integer size){
return getHeadURL(xuid, false, size);
}
public static String getHeadURL(String xuid){
return getHeadURL(xuid, false, null);
}

public static String getSkinURL(String xuid){
return Gateway.getUrl() + ApiRequest.URI_GET_SKIN + xuid;
}

public static void setActivity(@NonNull Player player, @Nullable ApiActivity activity) {
if (Utils.isFromSameHost(player.getAddress())) return;
if (!Server.getInstance().isRunning()) return;
Expand Down Expand Up @@ -113,27 +137,6 @@ public static void setActivity(@NonNull Player player, @Nullable ApiActivity act
));
}

public static String getHeadURL(String xuid, boolean gray, Integer size) {
size = size != null ? Math.min(512, Math.max(16, size)) : null;
String url = ApiRequest.URI_GET_HEAD + xuid;
if (size != null) url += "?size=" + size;
if (gray) url += size != null ? "&gray" : "?gray";
return Gateway.getUrl() + url;
}
public static String getHeadURL(String xuid, boolean gray){
return getHeadURL(xuid, gray, null);
}
public static String getHeadURL(String xuid, Integer size){
return getHeadURL(xuid, false, size);
}
public static String getHeadURL(String xuid){
return getHeadURL(xuid, false, null);
}

public static String getSkinURL(String xuid){
return Gateway.getUrl() + ApiRequest.URI_GET_SKIN + xuid;
}




Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

package xxAROX.PresenceMan.NukkitX.entity;

import cn.nukkit.Server;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import lombok.Getter;
Expand All @@ -23,15 +24,16 @@ public final class ApiRequest {
private String uri;

public static final String URI_UPDATE_PRESENCE = "/api/v1/servers/update_presence";
public static final String URI_UPDATE_OFFLINE = "/api/v1/servers/offline";
public static final String URI_UPDATE_SKIN = "/api/v1/images/skins/update";
public static final String URI_UPDATE_OFFLINE = "/api/v1/servers/offline";
public static final String URI_GET_SKIN = "/api/v1/images/skins/";
public static final String URI_GET_HEAD = "/api/v1/images/heads/";

public ApiRequest(String uri, JsonObject body, boolean postMethod) {
this.uri = uri;
this.body = body;
this.postMethod = postMethod;
header("Serversoftware", Server.getInstance().getName());
}

public String serialize() {
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/xxAROX/PresenceMan/NukkitX/entity/Gateway.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ public final class Gateway {
public static Integer port = null;
public static boolean broken = false;

public static String getUrl() {
if (broken) throw new Error("Presence-Man Backend server is not reachable");
public static String getUrl() throws RuntimeException{
if (broken) throw new RuntimeException("Presence-Man Backend server is not reachable");
return protocol + address + (port != null ? ":" + port : "");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import xxAROX.PresenceMan.NukkitX.entity.Gateway;

import java.io.IOException;
import java.net.InetAddress;
import java.util.Map;
import java.util.Objects;
import java.util.function.Consumer;
Expand All @@ -26,7 +27,7 @@ public final class BackendRequest extends AsyncTask {
private final int timeout;
private final String url;

public BackendRequest(String request, Consumer<JsonObject> onResponse, Consumer<JsonObject> onError, int timeout) {
public BackendRequest(String request, Consumer<JsonObject> onResponse, Consumer<JsonObject> onError, int timeout) throws RuntimeException{
this.url = Gateway.getUrl();
this.request = request;
this.onResponse = onResponse;
Expand All @@ -39,6 +40,16 @@ public void onRun() {
ApiRequest apiRequest = ApiRequest.deserialize(request);
Map<String, String> headers = apiRequest.getHeaders();

try {
if (!InetAddress.getByName("google.com").isReachable(5000)) {
setResult(null);
return;
}
} catch (IOException e) {
setResult(null);
return;
}

OkHttpClient client = new OkHttpClient.Builder()
.connectTimeout(timeout, java.util.concurrent.TimeUnit.SECONDS)
.readTimeout(timeout, java.util.concurrent.TimeUnit.SECONDS)
Expand Down Expand Up @@ -86,9 +97,9 @@ public void onCompletion(Server server) {
PresenceMan.getInstance().getLogger().error("[CLIENT-ERROR] [" + request.getUri() + "]: " + body.toString());
if (onError != null) onError.accept(body);
} else if (code >= 500 && code <= 599) { // Server-Errors
PresenceMan.getInstance().getLogger().error("[API-ERROR] [" + request.getUri() + "]: " + body.toString());
if (!body.toString().contains("<html>")) PresenceMan.getInstance().getLogger().error("[API-ERROR] [" + request.getUri() + "]: " + body.toString());
if (onError != null) onError.accept(body);
}
} else PresenceMan.getInstance().getLogger().error("[JUST-IN-CASE-ERROR] [" + request.getUri() + "]: got null, that's not good"); // TODO: remove this
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,25 +69,29 @@ public void onRun() {
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder().url(Gateway.getUrl()).build();
Response response = client.newCall(request).execute();
setResult(response.isSuccessful());
setResult(response.code());
response.close();
} catch (IOException e) {
setResult(false);
setResult(404);
}
}

@Override
public void onCompletion(Server server) {
boolean success = (boolean) getResult();
if (!success) {
int code = (int) getResult();
if (code != 200) {
Gateway.broken = true;
ReconnectingTask.activate();
} else {
ReconnectingTask.deactivate();
PresenceMan.getInstance().getLogger().notice("This server will be displayed as '" + PresenceMan.server + "' in presences!");
}
callback.accept(success);
callback.accept(code == 200);
}
});
}

public static void unga_bunga() {
Server.getInstance().getScheduler().scheduleAsyncTask(PresenceMan.getInstance(), new FetchGatewayInformationTask());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import java.util.regex.Pattern;

public final class PerformUpdateTask extends AsyncTask {
private static final String LATEST_VERSION_URL = "https://github.com/Presence-Man/releases/raw/main/version-nukkit.txt";
private static final String LATEST_VERSION_URL = "https://github.com/Presence-Man/Nukkit/raw/main/version-nukkit.txt";

private final String currentVersion;
private boolean notified = false;
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: "Presence-Man"
version: "1.1.0"
version: "1.1.1"
api: 1.0.0
main: xxAROX.PresenceMan.NukkitX.PresenceMan
description: "Presence-Man client for nukkit to support this server in discord rich-presences."
Expand Down

0 comments on commit b7bf9d2

Please sign in to comment.