Add ResourcePack#optional overload to specify prompt (#600)

This commit is contained in:
Andre Roldan 2022-01-24 19:42:32 -05:00 committed by GitHub
parent e0e5fe049d
commit 2d305cb0f0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 12 deletions

View File

@ -9,7 +9,7 @@ import net.minestom.server.utils.binary.BinaryWriter;
import org.jetbrains.annotations.NotNull;
public record ResourcePackSendPacket(String url, String hash, boolean forced,
Component forcedMessage) implements ServerPacket {
Component prompt) implements ServerPacket {
public ResourcePackSendPacket(BinaryReader reader) {
this(reader.readSizedString(), reader.readSizedString(), reader.readBoolean(),
reader.readBoolean() ? reader.readComponent() : null);
@ -17,7 +17,7 @@ public record ResourcePackSendPacket(String url, String hash, boolean forced,
public ResourcePackSendPacket(@NotNull ResourcePack resourcePack) {
this(resourcePack.getUrl(), resourcePack.getHash(), resourcePack.isForced(),
resourcePack.getForcedMessage());
resourcePack.getPrompt());
}
@Override
@ -25,9 +25,9 @@ public record ResourcePackSendPacket(String url, String hash, boolean forced,
writer.writeSizedString(url);
writer.writeSizedString(hash);
writer.writeBoolean(forced);
if (forcedMessage != null) {
if (prompt != null) {
writer.writeBoolean(true);
writer.writeComponent(forcedMessage);
writer.writeComponent(prompt);
} else {
writer.writeBoolean(false);
}

View File

@ -13,22 +13,26 @@ public class ResourcePack {
private final String url;
private final String hash;
private final boolean forced;
private final Component forcedMessage;
private final Component prompt;
private ResourcePack(@NotNull String url, @Nullable String hash, boolean forced, @Nullable Component forcedMessage) {
private ResourcePack(@NotNull String url, @Nullable String hash, boolean forced, @Nullable Component prompt) {
this.url = url;
// Optional, set to empty if null
this.hash = hash == null ? "" : hash;
this.forced = forced;
this.forcedMessage = forcedMessage;
this.prompt = prompt;
}
public static ResourcePack optional(@NotNull String url, @Nullable String hash, @Nullable Component prompt) {
return new ResourcePack(url, hash, false, prompt);
}
public static ResourcePack optional(@NotNull String url, @Nullable String hash) {
return new ResourcePack(url, hash, false, null);
return optional(url, hash, null);
}
public static ResourcePack forced(@NotNull String url, @Nullable String hash, @Nullable Component forcedMessage) {
return new ResourcePack(url, hash, true, forcedMessage);
public static ResourcePack forced(@NotNull String url, @Nullable String hash, @Nullable Component prompt) {
return new ResourcePack(url, hash, true, prompt);
}
public static ResourcePack forced(@NotNull String url, @Nullable String hash) {
@ -60,7 +64,7 @@ public class ResourcePack {
return forced;
}
public @Nullable Component getForcedMessage() {
return forcedMessage;
public @Nullable Component getPrompt() {
return prompt;
}
}