Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions commons/src/main/java/net/swofty/commons/MinecraftVersion.java
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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ public static class LimboSettings {
public static class ResourcePackSettings {
@Comment("Base URL of the pack server (e.g. http://0.0.0.0:7270)")
private String serverUrl = "http://127.0.0.1:7270";

Copy link
Copy Markdown
Owner

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

Copy link
Copy Markdown
Collaborator

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)

Copy link
Copy Markdown
Collaborator Author

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)

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

Copy link
Copy Markdown
Owner

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

Copy link
Copy Markdown
Owner

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

Copy link
Copy Markdown
Collaborator Author

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

this is what I've been thinking of for a while! can those be downloaded programmatically?

@Comment("Whether to use Hypixel's official resource pack API instead of building a local pack")
private boolean useHypixelApi = false;

@Comment("URL of Hypixel's resource pack metadata API")
private String hypixelApiUrl = "https://api.hypixel.net/v2/resources/packs";
}

@Getter
Expand Down
2 changes: 2 additions & 0 deletions configuration/config.docker.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,7 @@ limbo:
resource-packs:
skyblockpack:
server-url: http://127.0.0.1:7270
use-hypixel-api: false
hypixel-api-url: https://api.hypixel.net/v2/resources/packs
ravengard:
server-url: http://127.0.0.1:7270
9 changes: 7 additions & 2 deletions configuration/config.example.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,10 @@ integrations:
limbo:
host-name: 127.0.0.1
port: 65535
# Resource pack settings keyed by pack name (e.g. testingpack, bedwarspack)
resource-packs: { }
# Resource pack settings keyed by pack name (e.g. testingpack, skyblockpack)
resource-packs:
skyblockpack:
server-url: http://127.0.0.1:7270
# Use Hypixel's official pack and select its format for each player's version
use-hypixel-api: false
hypixel-api-url: https://api.hypixel.net/v2/resources/packs
75 changes: 58 additions & 17 deletions packer/src/main/java/net/swofty/packer/HypixelPackBuilder.java
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();
}

}
28 changes: 28 additions & 0 deletions proxy.api/src/main/java/net/swofty/proxyapi/ProxyPlayer.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,34 @@ public CompletableFuture<Boolean> isOnline() {
return future;
}

public CompletableFuture<Integer> getProtocolVersion() {
CompletableFuture<Integer> future = new CompletableFuture<>();
RedisClient.requestProxy(PLAYER_HANDLER,
new PlayerHandlerProtocol.Request(uuid.toString(), PlayerHandlerProtocol.Action.VERSION, Map.of()))
.thenAccept(response -> {
if (!response.success()) {
future.completeExceptionally(new IllegalStateException(response.error()));
return;
}

Object version = response.data().get("protocolVersion");
if (version instanceof Number number) {
future.complete(number.intValue());
} else {
future.completeExceptionally(new IllegalStateException("Proxy returned no player version"));
}
})
.exceptionally(error -> {
future.completeExceptionally(error);
return null;
});
return future;
}

public CompletableFuture<Integer> getVersion() {
return getProtocolVersion();
}

public void runEvent(ProxyUnderstandableEvent event) {
RedisClient.requestProxy(PLAYER_HANDLER,
new PlayerHandlerProtocol.Request(uuid.toString(), PlayerHandlerProtocol.Action.EVENT,
Expand Down
2 changes: 2 additions & 0 deletions setup/internal/installer/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,8 @@ resource-pack:
resource-packs:
skyblockpack:
server-url: http://127.0.0.1:7270
use-hypixel-api: false
hypixel-api-url: https://api.hypixel.net/v2/resources/packs
ravengard:
server-url: http://127.0.0.1:7270
`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,17 @@
import net.swofty.type.generic.user.HypixelPlayer;

public interface HypixelResourcePack {
record PackInfo(String url, String hash) {
}

String getPackName();
String getPackUrl();
String getPackHash();

default PackInfo getPackFor(HypixelPlayer player) {
return new PackInfo(getPackUrl(), getPackHash());
}

boolean isRequired();

void initialize();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import net.kyori.adventure.resource.ResourcePackRequest;
import net.kyori.adventure.text.Component;
import net.minestom.server.entity.Player;
import net.swofty.type.generic.user.HypixelPlayer;
import org.tinylog.Logger;

import java.net.URI;
Expand Down Expand Up @@ -37,8 +38,17 @@ public void sendPack(Player player) {
* already-received chunk sections permanently unrendered on the client.
*/
public void sendPackBlocking(Player player, int timeoutSeconds) {
String packUrl = activePack.getPackUrl();
String packHash = activePack.getPackHash();
HypixelResourcePack.PackInfo pack = player instanceof HypixelPlayer hypixelPlayer
? activePack.getPackFor(hypixelPlayer)
: new HypixelResourcePack.PackInfo(activePack.getPackUrl(), activePack.getPackHash());

if (pack == null) {
Logger.warn("Resource pack could not be resolved for " + player.getUsername() + ", skipping pack send");
return;
}

String packUrl = pack.url();
String packHash = pack.hash();

if (packUrl == null || packUrl.isEmpty() || packHash == null || packHash.isEmpty()) {
Logger.warn("Resource pack URL or hash not configured, skipping pack send for " + player.getUsername());
Expand Down
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;
}

}
}
Loading
Loading