Support for forced resource packs

This commit is contained in:
TheMode 2021-06-11 12:32:24 +02:00
parent 9ae8c96091
commit 88052576b3
3 changed files with 46 additions and 14 deletions

View File

@ -1325,13 +1325,7 @@ public class Player extends LivingEntity implements CommandSender, Localizable,
* @param resourcePack the resource pack
*/
public void setResourcePack(@NotNull ResourcePack resourcePack) {
final String url = resourcePack.getUrl();
final String hash = resourcePack.getHash();
ResourcePackSendPacket resourcePackSendPacket = new ResourcePackSendPacket();
resourcePackSendPacket.url = url;
resourcePackSendPacket.hash = hash;
playerConnection.sendPacket(resourcePackSendPacket);
playerConnection.sendPacket(new ResourcePackSendPacket(resourcePack));
}
/**
@ -2272,7 +2266,6 @@ public class Player extends LivingEntity implements CommandSender, Localizable,
* @param allowFood true if food should be updated, false otherwise
* @return the called {@link ItemUpdateStateEvent},
* null if there is no item to update the state
*
* @deprecated Use {@link #callItemUpdateStateEvent(Hand)} instead
*/
@Deprecated

View File

@ -3,6 +3,7 @@ package net.minestom.server.network.packet.server.play;
import net.kyori.adventure.text.Component;
import net.minestom.server.network.packet.server.ServerPacket;
import net.minestom.server.network.packet.server.ServerPacketIdentifier;
import net.minestom.server.resourcepack.ResourcePack;
import net.minestom.server.utils.binary.BinaryReader;
import net.minestom.server.utils.binary.BinaryWriter;
import org.jetbrains.annotations.NotNull;
@ -17,12 +18,21 @@ public class ResourcePackSendPacket implements ServerPacket {
public ResourcePackSendPacket() {
}
public ResourcePackSendPacket(@NotNull ResourcePack resourcePack) {
this.url = resourcePack.getUrl();
this.hash = resourcePack.getHash();
this.forced = resourcePack.isForced();
this.forcedMessage = resourcePack.getForcedMessage();
}
@Override
public void write(@NotNull BinaryWriter writer) {
writer.writeSizedString(url);
writer.writeSizedString(hash);
writer.writeBoolean(forced);
writer.writeComponent(forcedMessage);
if (forced) {
writer.writeComponent(forcedMessage);
}
}
@Override
@ -30,7 +40,9 @@ public class ResourcePackSendPacket implements ServerPacket {
this.url = reader.readSizedString(Integer.MAX_VALUE);
this.hash = reader.readSizedString(Integer.MAX_VALUE);
this.forced = reader.readBoolean();
this.forcedMessage = reader.readComponent();
if (forced) {
this.forcedMessage = reader.readComponent();
}
}
@Override

View File

@ -1,5 +1,6 @@
package net.minestom.server.resourcepack;
import net.kyori.adventure.text.Component;
import net.minestom.server.entity.Player;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@ -11,11 +12,31 @@ public class ResourcePack {
private final String url;
private final String hash;
private final boolean forced;
private final Component forcedMessage;
/**
* @deprecated use {@link ResourcePack#optional(String, String)}.
*/
@Deprecated
public ResourcePack(@NotNull String url, @Nullable String hash) {
this(url, hash, false, null);
}
private ResourcePack(@NotNull String url, @Nullable String hash, boolean forced, Component forcedMessage) {
this.url = url;
// Optional, set to empty if null
this.hash = hash == null ? "" : hash;
this.forced = forced;
this.forcedMessage = forcedMessage;
}
public static ResourcePack optional(@NotNull String url, @Nullable String hash) {
return new ResourcePack(url, hash);
}
public static ResourcePack forced(@NotNull String url, @Nullable String hash, @NotNull Component forcedMessage) {
return new ResourcePack(url, hash, true, forcedMessage);
}
/**
@ -23,8 +44,7 @@ public class ResourcePack {
*
* @return the resource pack URL
*/
@NotNull
public String getUrl() {
public @NotNull String getUrl() {
return url;
}
@ -36,8 +56,15 @@ public class ResourcePack {
*
* @return the resource pack hash, can be empty
*/
@NotNull
public String getHash() {
public @NotNull String getHash() {
return hash;
}
public boolean isForced() {
return forced;
}
public @Nullable Component getForcedMessage() {
return forcedMessage;
}
}