Skip to content

Commit

Permalink
refactor: use URI instead of url string, update minestom
Browse files Browse the repository at this point in the history
  • Loading branch information
yusshu committed Oct 10, 2023
1 parent 68f069a commit 97633dc
Show file tree
Hide file tree
Showing 9 changed files with 82 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,28 +23,45 @@
*/
package team.unnamed.creative.central.export;

import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;

import java.net.URI;

import static java.util.Objects.requireNonNull;

public final class ResourcePackLocation {

private final String url;
private final URI uri;
private final String hash;

private ResourcePackLocation(String url, String hash) {
this.url = requireNonNull(url, "url");
private ResourcePackLocation(URI uri, String hash) {
this.uri = requireNonNull(uri, "url");
this.hash = requireNonNull(hash, "hash");
}

@Deprecated
@ApiStatus.ScheduledForRemoval(inVersion = "2.0.0")
public String url() {
return url;
return uri.toString();
}

public @NotNull URI uri() {
return uri;
}

public String hash() {
return hash;
}

public static ResourcePackLocation of(String url, String hash) {
return new ResourcePackLocation(url, hash);
public static @NotNull ResourcePackLocation of(final @NotNull URI uri, final @NotNull String hash) {
return new ResourcePackLocation(uri, hash);
}

@Deprecated
@ApiStatus.ScheduledForRemoval(inVersion = "2.0.0")
public static @NotNull ResourcePackLocation of(String url, String hash) {
return new ResourcePackLocation(URI.create(url), hash);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,12 @@
package team.unnamed.creative.central.request;

import net.kyori.adventure.text.Component;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.net.URI;

import static java.util.Objects.requireNonNull;

/**
Expand All @@ -36,20 +40,20 @@
*/
public final class ResourcePackRequest {

private final String url;
private final URI uri;
private final String hash;
private final boolean required;
private final @Nullable Component prompt;

private ResourcePackRequest(
String url,
String hash,
boolean required,
@Nullable Component prompt
final @NotNull URI uri,
final @NotNull String hash,
final boolean required,
final @Nullable Component prompt
) {
requireNonNull(url, "url");
requireNonNull(uri, "url");
requireNonNull(hash, "hash");
this.url = url;
this.uri = uri;
this.hash = hash;
this.required = required;
this.prompt = prompt;
Expand All @@ -61,8 +65,14 @@ private ResourcePackRequest(
*
* @return The resource-pack URL
*/
@Deprecated
@ApiStatus.ScheduledForRemoval(inVersion = "2.0.0")
public String url() {
return url;
return uri.toString();
}

public @NotNull URI uri() {
return uri;
}

/**
Expand Down Expand Up @@ -96,15 +106,28 @@ public boolean required() {
return prompt;
}

public static @NotNull ResourcePackRequest of(
final @NotNull URI uri,
final @NotNull String hash,
final boolean required,
final @Nullable Component prompt
) {
return new ResourcePackRequest(uri, hash, required, prompt);
}

@Deprecated
@ApiStatus.ScheduledForRemoval(inVersion = "2.0.0")
public static ResourcePackRequest of(
String url,
String hash,
boolean required,
@Nullable Component prompt
) {
return new ResourcePackRequest(url, hash, required, prompt);
return of(URI.create(url), hash, required, prompt);
}

@Deprecated
@ApiStatus.ScheduledForRemoval(inVersion = "2.0.0")
public static ResourcePackRequest of(String url, String hash, boolean required) {
return of(url, hash, required, null);
}
Expand All @@ -115,16 +138,23 @@ public static Builder builder() {

public static final class Builder {

private String url;
private URI uri;
private String hash;
private boolean required;
private @Nullable Component prompt;

private Builder() {
}

@Deprecated
@ApiStatus.ScheduledForRemoval(inVersion = "2.0.0")
public Builder url(String url) {
this.url = url;
this.uri = URI.create(url);
return this;
}

public @NotNull Builder uri(final @NotNull URI uri) {
this.uri = requireNonNull(uri, "uri");
return this;
}

Expand All @@ -144,7 +174,7 @@ public Builder prompt(@Nullable Component prompt) {
}

public ResourcePackRequest build() {
return new ResourcePackRequest(url, hash, required, prompt);
return new ResourcePackRequest(uri, hash, required, prompt);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ public ResourcePack generateSync() {

if (location != null) {
serveOptions.request(ResourcePackRequest.of(
location.url(),
location.uri(),
location.hash(),
config.send().request().required(),
Components.deserialize(config.send().request().prompt())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public static ResourcePackRequestSender bukkit() {
// Method that accepts: URL, Hash, Required and Prompt options,
// available since Paper 1.17 (We are currently on 1.20)
return new BukkitResourcePackRequestSender((player, request) -> player.setResourcePack(
request.url(),
request.uri().toString(),
request.hash(),
request.required(),
request.prompt()
Expand All @@ -65,7 +65,7 @@ public static ResourcePackRequestSender bukkit() {
// Method that accepts URL and Hash options
// available since Paper 1.12 and probably before too
return new BukkitResourcePackRequestSender((player, request) -> player.setResourcePack(
request.url(),
request.uri().toString(),
request.hash()
));
}
Expand All @@ -87,7 +87,7 @@ public static ResourcePackRequestSender bukkit() {
}

// now call the stupid method
player.setResourcePack(request.url(), hash);
player.setResourcePack(request.uri().toString(), hash);
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import team.unnamed.creative.central.server.CentralResourcePackServer;
import team.unnamed.creative.serialize.minecraft.MinecraftResourcePackWriter;

import java.net.URI;
import java.util.logging.Logger;

public class LocalHostExporter implements ResourcePackExporter {
Expand Down Expand Up @@ -59,16 +60,16 @@ public LocalHostExporter(CentralResourcePackServer server, Logger logger) {

server.resourcePack(pack);

String url = String.format(
URI uri = URI.create(String.format(
"http://%s:%s/%s.zip",
server.publicAddress(),
server.port(),
pack.hash()
);
));

logger.info("Resource-pack hosted, available in: " + url);
logger.info("Resource-pack hosted, available in: " + uri.toString());

return ResourcePackLocation.of(url, pack.hash());
return ResourcePackLocation.of(uri, pack.hash());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.security.DigestOutputStream;
Expand Down Expand Up @@ -133,7 +134,7 @@ public ResourcePackLocation export(ResourcePack resourcePack) throws IOException
String url = DOWNLOAD_URL_TEMPLATE.replace("%HASH%", hashString);
logger.info("Uploaded resource-pack to: " + url);

return ResourcePackLocation.of(url, hashString);
return ResourcePackLocation.of(URI.create(url), hashString);
}

private char hex(int c) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.URL;
import java.net.URI;
import java.security.DigestOutputStream;
import java.security.MessageDigest;
import java.util.logging.Logger;
Expand All @@ -50,15 +50,15 @@ void test() throws Exception {
.export(resourcePack);

assertNotNull(location, "Location should not be null");
assertEquals("https://download.mc-packs.net/pack/7c708abe63955fefc2ff1fca614688874b9bd3f0.zip", location.url());
assertEquals("https://download.mc-packs.net/pack/7c708abe63955fefc2ff1fca614688874b9bd3f0.zip", location.uri().toString());
assertEquals("7c708abe63955fefc2ff1fca614688874b9bd3f0", location.hash());

// download the resource-pack from MCPacks
URL url = new URL(location.url());
URI uri = location.uri();
ByteArrayOutputStream bos = new ByteArrayOutputStream();
MessageDigest digest = MessageDigest.getInstance("SHA-1");
DigestOutputStream digestOutputStream = new DigestOutputStream(bos, digest);
try (InputStream input = url.openStream()) {
try (InputStream input = uri.toURL().openStream()) {
Streams.pipe(input, digestOutputStream);
}
// byte[] bytes = bos.toByteArray();
Expand Down
2 changes: 1 addition & 1 deletion minestom/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ dependencies {
implementation(libs.creative.serializer.minecraft)
implementation(project(":creative-central-api"))
implementation(project(":creative-central-common"))
compileOnly("com.github.Minestom:Minestom:4f7ff5b474")
compileOnly("com.github.Minestom:Minestom:2cdb3911b0")
}

java {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public void send(Object playerObject, ResourcePackRequest request) {
throw new IllegalArgumentException("Provided 'player' is not an actual Minestom Player: " + playerObject);
}

String url = request.url();
String url = request.uri().toString();
String hash = request.hash();
Component prompt = request.prompt();

Expand Down

0 comments on commit 97633dc

Please sign in to comment.