-
-
Notifications
You must be signed in to change notification settings - Fork 86
feat(packer): use the Hypixel API for resourcepacks #853
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
41 changes: 41 additions & 0 deletions
41
commons/src/main/java/net/swofty/commons/MinecraftVersion.java
This file contains hidden or 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,41 @@ | ||
| package net.swofty.commons; | ||
|
|
||
| import lombok.Getter; | ||
|
|
||
| @Getter | ||
| public enum MinecraftVersion { | ||
| MINECRAFT_1_20_5(766, 41.0, 32), | ||
| MINECRAFT_1_21(767, 48.0, 34), | ||
| MINECRAFT_1_21_2(768, 57.0, 42), | ||
| MINECRAFT_1_21_4(769, 61.0, 46), | ||
| MINECRAFT_1_21_5(770, 71.0, 55), | ||
| MINECRAFT_1_21_6(771, 80.0, 63), | ||
| MINECRAFT_1_21_7(772, 81.0, 64), | ||
| MINECRAFT_1_21_9(773, 88.0, 69), | ||
| MINECRAFT_1_21_11(774, 94.1, 75), | ||
| MINECRAFT_26_1(775, 101.1, 84), | ||
| MINECRAFT_26_2(776, 107.1, 88); | ||
|
|
||
| private final int protocolVersion; | ||
| private final double dataPackVersion; | ||
| private final int packVersion; | ||
|
|
||
| MinecraftVersion(int protocolVersion, double dataPackVersion, int packVersion) { | ||
| this.protocolVersion = protocolVersion; | ||
| this.dataPackVersion = dataPackVersion; | ||
| this.packVersion = packVersion; | ||
| } | ||
|
|
||
| public static MinecraftVersion byProtocol(int protocolVersion) { | ||
| for (MinecraftVersion version : values()) { | ||
| if (version.protocolVersion == protocolVersion) { | ||
| return version; | ||
| } | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| public static MinecraftVersion latest() { | ||
| return MINECRAFT_26_2; | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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
75 changes: 58 additions & 17 deletions
75
packer/src/main/java/net/swofty/packer/HypixelPackBuilder.java
This file contains hidden or 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 |
|---|---|---|
| @@ -1,36 +1,77 @@ | ||
| package net.swofty.packer; | ||
|
|
||
| import net.kyori.adventure.text.Component; | ||
| import team.unnamed.creative.BuiltResourcePack; | ||
| import team.unnamed.creative.ResourcePack; | ||
| import team.unnamed.creative.metadata.pack.FormatVersion; | ||
| import team.unnamed.creative.metadata.pack.PackFormat; | ||
| import team.unnamed.creative.metadata.pack.PackMeta; | ||
| import team.unnamed.creative.serialize.minecraft.MinecraftResourcePackReader; | ||
| import team.unnamed.creative.serialize.minecraft.MinecraftResourcePackWriter; | ||
| import team.unnamed.creative.base.Writable; | ||
|
|
||
| import java.io.File; | ||
| import java.io.ByteArrayOutputStream; | ||
| import java.io.IOException; | ||
| import java.io.UncheckedIOException; | ||
| import java.nio.file.Files; | ||
| import java.nio.file.Path; | ||
| import java.security.MessageDigest; | ||
| import java.security.NoSuchAlgorithmException; | ||
| import java.util.HexFormat; | ||
| import java.util.stream.Stream; | ||
| import java.util.zip.ZipEntry; | ||
| import java.util.zip.ZipOutputStream; | ||
|
|
||
| public class HypixelPackBuilder { | ||
| private static final FormatVersion FORMAT_VERSION = FormatVersion.of(FormatVersion.FORMAT_26_1); | ||
|
|
||
| private final PackDefinition definition; | ||
|
|
||
| public HypixelPackBuilder(PackDefinition definition) { | ||
| this.definition = definition; | ||
| } | ||
|
|
||
| public BuiltResourcePack build() { | ||
| File packDirectory = new File(definition.getPackDirectory()).getAbsoluteFile(); | ||
| if (!packDirectory.isDirectory()) { | ||
| throw new IllegalStateException("Pack directory does not exist: " + packDirectory.getPath()); | ||
| Path packDirectory = Path.of(definition.getPackDirectory()).toAbsolutePath(); | ||
|
|
||
| if (!Files.isDirectory(packDirectory)) { | ||
| throw new IllegalStateException( | ||
| "Pack directory does not exist: " + packDirectory | ||
| ); | ||
| } | ||
|
|
||
| ResourcePack pack = MinecraftResourcePackReader.minecraft() | ||
| .readFromDirectory(packDirectory); | ||
| pack.packMeta(PackMeta.of(PackFormat.format(FORMAT_VERSION, FORMAT_VERSION), Component.text("Hypixel"))); | ||
| try { | ||
| byte[] bytes = zipDirectory(packDirectory); | ||
|
|
||
| MessageDigest digest = MessageDigest.getInstance("SHA-1"); | ||
| String hash = HexFormat.of().formatHex(digest.digest(bytes)); | ||
|
|
||
| return BuiltResourcePack.of( | ||
| Writable.bytes(bytes), | ||
| hash | ||
| ); | ||
| } catch (IOException e) { | ||
| throw new UncheckedIOException("Failed to build resource pack", e); | ||
| } catch (NoSuchAlgorithmException e) { | ||
| throw new IllegalStateException("SHA-1 is unavailable", e); | ||
| } | ||
| } | ||
|
|
||
| private byte[] zipDirectory(Path directory) throws IOException { | ||
| ByteArrayOutputStream output = new ByteArrayOutputStream(); | ||
|
|
||
| try (ZipOutputStream zip = new ZipOutputStream(output); | ||
| Stream<Path> paths = Files.walk(directory)) { | ||
|
|
||
| paths.filter(Files::isRegularFile).forEach(path -> { | ||
| String entryName = directory.relativize(path) | ||
| .toString() | ||
| .replace('\\', '/'); | ||
|
|
||
| try { | ||
| zip.putNextEntry(new ZipEntry(entryName)); | ||
| Files.copy(path, zip); | ||
| zip.closeEntry(); | ||
| } catch (IOException e) { | ||
| throw new UncheckedIOException(e); | ||
| } | ||
| }); | ||
| } catch (UncheckedIOException e) { | ||
| throw e.getCause(); | ||
| } | ||
|
|
||
| return MinecraftResourcePackWriter.minecraft().build(pack); | ||
| return output.toByteArray(); | ||
| } | ||
|
|
||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
133 changes: 133 additions & 0 deletions
133
...ic/src/main/java/net/swofty/type/skyblockgeneric/resourcepack/HypixelSkyblockPackApi.java
This file contains hidden or 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,133 @@ | ||
| package net.swofty.type.skyblockgeneric.resourcepack; | ||
|
|
||
| import net.swofty.commons.MinecraftVersion; | ||
| import org.json.JSONArray; | ||
| import org.json.JSONObject; | ||
|
|
||
| import java.io.IOException; | ||
| import java.net.URI; | ||
| import java.net.http.HttpClient; | ||
| import java.net.http.HttpRequest; | ||
| import java.net.http.HttpResponse; | ||
| import java.time.Duration; | ||
| import java.util.Collections; | ||
| import java.util.NavigableMap; | ||
| import java.util.Objects; | ||
| import java.util.TreeMap; | ||
|
|
||
| final class HypixelSkyblockPackApi { | ||
| private static final Duration REQUEST_TIMEOUT = Duration.ofSeconds(10); | ||
| private static final HttpClient HTTP_CLIENT = HttpClient.newBuilder() | ||
| .connectTimeout(REQUEST_TIMEOUT) | ||
| .build(); | ||
|
|
||
| private HypixelSkyblockPackApi() { | ||
| } | ||
|
|
||
| static Catalog fetch(String apiUrl) throws IOException, InterruptedException { | ||
| URI uri = URI.create(apiUrl); | ||
| HttpRequest request = HttpRequest.newBuilder(uri) | ||
| .timeout(REQUEST_TIMEOUT) | ||
| .header("Accept", "application/json") | ||
| .GET() | ||
| .build(); | ||
| HttpResponse<String> response = HTTP_CLIENT.send(request, HttpResponse.BodyHandlers.ofString()); | ||
|
|
||
| if (response.statusCode() < 200 || response.statusCode() >= 300) { | ||
| throw new IOException("Hypixel API returned HTTP " + response.statusCode()); | ||
| } | ||
|
|
||
| return parse(response.body()); | ||
| } | ||
|
|
||
| static Catalog parse(String responseBody) { | ||
| JSONObject response = new JSONObject(responseBody); | ||
| if (!response.optBoolean("success")) { | ||
| throw new IllegalStateException("Hypixel API returned an unsuccessful response"); | ||
| } | ||
|
|
||
| JSONArray packs = response.optJSONArray("packs"); | ||
| if (packs == null) { | ||
| throw new IllegalStateException("Hypixel API response did not contain packs"); | ||
| } | ||
|
|
||
| for (int i = 0; i < packs.length(); i++) { | ||
| JSONObject pack = packs.optJSONObject(i); | ||
| if (pack != null && "SkyBlock".equals(pack.optString("id"))) { | ||
| return parseCatalog(pack); | ||
| } | ||
| } | ||
|
|
||
| throw new IllegalStateException("Hypixel API response did not contain the SkyBlock pack"); | ||
| } | ||
|
|
||
| private static Catalog parseCatalog(JSONObject pack) { | ||
| JSONArray versions = pack.optJSONArray("versions"); | ||
| if (versions == null) { | ||
| throw new IllegalStateException("Hypixel API response did not contain SkyBlock pack versions"); | ||
| } | ||
|
|
||
| NavigableMap<Integer, Version> byPackFormat = new TreeMap<>(); | ||
| for (int i = 0; i < versions.length(); i++) { | ||
| JSONObject version = versions.optJSONObject(i); | ||
| if (version == null) { | ||
| continue; | ||
| } | ||
|
|
||
| String url = version.optString("url").trim(); | ||
| String hash = version.optString("hash").trim(); | ||
| if (!version.has("packFormat") || url.isEmpty() || hash.isEmpty()) { | ||
| continue; | ||
| } | ||
|
|
||
| int packFormat = version.getInt("packFormat"); | ||
| URI.create(url); | ||
| byPackFormat.put(packFormat, new Version(packFormat, url, hash)); | ||
| } | ||
|
|
||
| if (byPackFormat.isEmpty()) { | ||
| throw new IllegalStateException("Hypixel API response did not contain usable SkyBlock pack versions"); | ||
| } | ||
|
|
||
| return new Catalog(byPackFormat); | ||
| } | ||
|
|
||
| record Version(int packFormat, String url, String hash) { | ||
| Version { | ||
| Objects.requireNonNull(url, "url"); | ||
| Objects.requireNonNull(hash, "hash"); | ||
| } | ||
| } | ||
|
|
||
| static final class Catalog { | ||
| private final NavigableMap<Integer, Version> versions; | ||
|
|
||
| private Catalog(NavigableMap<Integer, Version> versions) { | ||
| this.versions = Collections.unmodifiableNavigableMap(new TreeMap<>(versions)); | ||
| } | ||
|
|
||
| Version latest() { | ||
| return versions.lastEntry().getValue(); | ||
| } | ||
|
|
||
| Version forProtocol(int protocolVersion) { | ||
| MinecraftVersion minecraftVersion = MinecraftVersion.byProtocol(protocolVersion); | ||
| if (minecraftVersion == null) { | ||
| if (protocolVersion > MinecraftVersion.latest().getProtocolVersion()) { | ||
| return latest(); | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| Version version = versions.get(minecraftVersion.getPackVersion()); | ||
| if (version != null) { | ||
| return version; | ||
| } | ||
| if (minecraftVersion.getPackVersion() > versions.lastKey()) { | ||
| return latest(); | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it's optimal if we just stick to one rather than supporting both and bloating our settings
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
agree, only the api one would be ideal (so we dont need to host a whole packer each time)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
only the API isn't possible, ravengard isn't included. Or do you mean that for skyblockpack, we'd always use the official API but not have any settings to configure it and force people to use that instead of the packer? That's probably fine for everyone
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If the API does not cover all of our cases I don't know what the benefit of mix matching is
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe we could have a shared google drive with write access across collaborators that has our maps and texture packs
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is what I've been thinking of for a while! can those be downloaded programmatically?