From 6721cef74c133b4cf58ac92c5a38f115afa45647 Mon Sep 17 00:00:00 2001 From: zszfympx <2792668017@qq.com> Date: Sun, 7 Jan 2024 19:45:17 +0800 Subject: [PATCH] weave loader download --- .../cubewhy/celestial/entities/Assets.java | 32 ++++++++++++++++ .../cubewhy/celestial/entities/Author.java | 35 +++++++++++++++++ .../cubewhy/celestial/entities/Uploader.java | 37 ++++++++++++++++++ .../celestial/entities/releaseEntity.java | 38 +++++++++++++++++++ .../org/cubewhy/celestial/event/Event.java | 3 +- .../celestial/game/addon/WeaveMod.java | 38 +++++++++++++++++++ 6 files changed, 181 insertions(+), 2 deletions(-) create mode 100644 src/main/java/org/cubewhy/celestial/entities/Assets.java create mode 100644 src/main/java/org/cubewhy/celestial/entities/Author.java create mode 100644 src/main/java/org/cubewhy/celestial/entities/Uploader.java create mode 100644 src/main/java/org/cubewhy/celestial/entities/releaseEntity.java diff --git a/src/main/java/org/cubewhy/celestial/entities/Assets.java b/src/main/java/org/cubewhy/celestial/entities/Assets.java new file mode 100644 index 00000000..75bfad5f --- /dev/null +++ b/src/main/java/org/cubewhy/celestial/entities/Assets.java @@ -0,0 +1,32 @@ +/** + * Copyright 2024 json.cn + */ +package org.cubewhy.celestial.entities; +import lombok.Data; + +import java.util.Date; + +/** + * Auto-generated: 2024-01-07 19:7:0 + * + * @author json.cn (i@json.cn) + * @website http://www.json.cn/java2pojo/ + */ +@Data +public class Assets { + + private String url; + private long id; + private String node_id; + private String name; + private String label; + private Uploader uploader; + private String content_type; + private String state; + private long size; + private int download_count; + private Date created_at; + private Date updated_at; + private String browser_download_url; + +} \ No newline at end of file diff --git a/src/main/java/org/cubewhy/celestial/entities/Author.java b/src/main/java/org/cubewhy/celestial/entities/Author.java new file mode 100644 index 00000000..12d1a185 --- /dev/null +++ b/src/main/java/org/cubewhy/celestial/entities/Author.java @@ -0,0 +1,35 @@ +/** + * Copyright 2024 json.cn + */ +package org.cubewhy.celestial.entities; + +import lombok.Data; + +/** + * Auto-generated: 2024-01-07 19:7:0 + * + * @author json.cn (i@json.cn) + * @website http://www.json.cn/java2pojo/ + */ +@Data +public class Author { + + private String login; + private long id; + private String node_id; + private String avatar_url; + private String gravatar_id; + private String url; + private String html_url; + private String followers_url; + private String following_url; + private String gists_url; + private String starred_url; + private String subscriptions_url; + private String organizations_url; + private String repos_url; + private String events_url; + private String received_events_url; + private String type; + private boolean site_admin; +} \ No newline at end of file diff --git a/src/main/java/org/cubewhy/celestial/entities/Uploader.java b/src/main/java/org/cubewhy/celestial/entities/Uploader.java new file mode 100644 index 00000000..55321073 --- /dev/null +++ b/src/main/java/org/cubewhy/celestial/entities/Uploader.java @@ -0,0 +1,37 @@ +/** + * Copyright 2024 json.cn + */ +package org.cubewhy.celestial.entities; + +import lombok.Data; + +/** + * Auto-generated: 2024-01-07 19:7:0 + * + * @author json.cn (i@json.cn) + * @website http://www.json.cn/java2pojo/ + */ +@Data +public class Uploader { + + private String login; + private long id; + private String node_id; + private String avatar_url; + private String gravatar_id; + private String url; + private String html_url; + private String followers_url; + private String following_url; + private String gists_url; + private String starred_url; + private String subscriptions_url; + private String organizations_url; + private String repos_url; + private String events_url; + private String received_events_url; + private String type; + private boolean site_admin; + + +} \ No newline at end of file diff --git a/src/main/java/org/cubewhy/celestial/entities/releaseEntity.java b/src/main/java/org/cubewhy/celestial/entities/releaseEntity.java new file mode 100644 index 00000000..b1166231 --- /dev/null +++ b/src/main/java/org/cubewhy/celestial/entities/releaseEntity.java @@ -0,0 +1,38 @@ +/** + * Copyright 2024 json.cn + */ +package org.cubewhy.celestial.entities; +import lombok.Data; + +import java.util.Date; +import java.util.List; + +/** + * Auto-generated: 2024-01-07 19:7:0 + * + * @author json.cn (i@json.cn) + * @website http://www.json.cn/java2pojo/ + */ +@Data +public class releaseEntity { + + private String url; + private String assets_url; + private String upload_url; + private String html_url; + private long id; + private Author author; + private String node_id; + private String tag_name; + private String target_commitish; + private Date name; + private boolean draft; + private boolean prerelease; + private Date created_at; + private Date published_at; + private List assets; + private String tarball_url; + private String zipball_url; + private String body; + +} \ No newline at end of file diff --git a/src/main/java/org/cubewhy/celestial/event/Event.java b/src/main/java/org/cubewhy/celestial/event/Event.java index a0259a6a..97fd0f45 100644 --- a/src/main/java/org/cubewhy/celestial/event/Event.java +++ b/src/main/java/org/cubewhy/celestial/event/Event.java @@ -22,8 +22,7 @@ public Event call() { final ArrayList dataList = EventManager.get(this.getClass()); if (dataList != null) { - for (int i = 0; i < dataList.size(); i++) { - EventData data = dataList.get(i); + for (EventData data : dataList) { try { data.target.invoke(data.source, this); } catch (Exception e) { diff --git a/src/main/java/org/cubewhy/celestial/game/addon/WeaveMod.java b/src/main/java/org/cubewhy/celestial/game/addon/WeaveMod.java index d6dbd5ef..2c85e4ac 100644 --- a/src/main/java/org/cubewhy/celestial/game/addon/WeaveMod.java +++ b/src/main/java/org/cubewhy/celestial/game/addon/WeaveMod.java @@ -6,8 +6,13 @@ package org.cubewhy.celestial.game.addon; +import com.google.gson.Gson; import lombok.Getter; +import okhttp3.Response; +import org.cubewhy.celestial.entities.Assets; +import org.cubewhy.celestial.entities.releaseEntity; import org.cubewhy.celestial.game.BaseAddon; +import org.cubewhy.celestial.utils.RequestUtils; import org.jetbrains.annotations.Contract; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -53,4 +58,37 @@ public static List findAll() { public String toString() { return this.file.getName(); } + + public boolean downloadWeaveLoader(@Nullable String loaderUrl){ + if(loaderUrl == null){ + String api_json; + try(Response response = RequestUtils.get("https://api.github.com/repos/Weave-MC/Weave-Loader/releases/latest").execute()){ + assert response.body() != null; + api_json = response.body().string(); + } catch (IOException e) { + return false; + } + releaseEntity releaseEntity = JsonToObj(api_json, org.cubewhy.celestial.entities.releaseEntity.class); + if (releaseEntity != null) { + Assets[] assets = releaseEntity.getAssets().toArray(new Assets[0]); + for(Assets i:assets){ + if(i.getName().endsWith(".jar")){ + //TODO: download i.browser_download_url() to ~/.cubewhy/addon/Weave-<%s>.jar, releaseEntity.getName() + } + } + } + } + return true; + } + + public static T JsonToObj(String json, Class clz){ + Gson gson = new Gson(); + if(Objects.isNull(json)) return null; + T obj = gson.fromJson(json, clz); + if(Objects.isNull(obj)){ + return null; + }else{ + return obj; + } + } }