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

View File

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

View File

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