diff --git a/paper-server/src/main/java/org/bukkit/craftbukkit/CraftServer.java b/paper-server/src/main/java/org/bukkit/craftbukkit/CraftServer.java index 115f65ad88..b610641927 100644 --- a/paper-server/src/main/java/org/bukkit/craftbukkit/CraftServer.java +++ b/paper-server/src/main/java/org/bukkit/craftbukkit/CraftServer.java @@ -183,6 +183,7 @@ import org.bukkit.craftbukkit.metadata.EntityMetadataStore; import org.bukkit.craftbukkit.metadata.PlayerMetadataStore; import org.bukkit.craftbukkit.metadata.WorldMetadataStore; import org.bukkit.craftbukkit.packs.CraftDataPackManager; +import org.bukkit.craftbukkit.packs.CraftResourcePack; import org.bukkit.craftbukkit.potion.CraftPotionBrewer; import org.bukkit.craftbukkit.profile.CraftPlayerProfile; import org.bukkit.craftbukkit.scheduler.CraftScheduler; @@ -237,6 +238,7 @@ import org.bukkit.loot.LootTable; import org.bukkit.map.MapPalette; import org.bukkit.map.MapView; import org.bukkit.packs.DataPackManager; +import org.bukkit.packs.ResourcePack; import org.bukkit.permissions.Permissible; import org.bukkit.permissions.Permission; import org.bukkit.plugin.Plugin; @@ -712,6 +714,11 @@ public final class CraftServer implements Server { return this.serverTickManager; } + @Override + public ResourcePack getServerResourcePack() { + return this.getServer().getServerResourcePack().map(CraftResourcePack::new).orElse(null); + } + @Override public String getResourcePack() { return this.getServer().getServerResourcePack().map(MinecraftServer.ServerResourcePackInfo::url).orElse(""); diff --git a/paper-server/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java b/paper-server/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java index 8d50e4a646..aa14dbee85 100644 --- a/paper-server/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java +++ b/paper-server/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java @@ -1734,15 +1734,29 @@ public class CraftPlayer extends CraftHumanEntity implements Player { @Override public void setResourcePack(UUID id, String url, byte[] hash, String prompt, boolean force) { + Preconditions.checkArgument(id != null, "Resource pack ID cannot be null"); Preconditions.checkArgument(url != null, "Resource pack URL cannot be null"); + String hashStr = ""; if (hash != null) { Preconditions.checkArgument(hash.length == 20, "Resource pack hash should be 20 bytes long but was %s", hash.length); - - getHandle().connection.send(new ClientboundResourcePackPushPacket(id, url, BaseEncoding.base16().lowerCase().encode(hash), force, CraftChatMessage.fromStringOrNull(prompt, true))); - } else { - getHandle().connection.send(new ClientboundResourcePackPushPacket(id, url, "", force, CraftChatMessage.fromStringOrNull(prompt, true))); + hashStr = BaseEncoding.base16().lowerCase().encode(hash); } + + this.handlePushResourcePack(new ClientboundResourcePackPushPacket(id, url, hashStr, force, CraftChatMessage.fromStringOrNull(prompt, true)), true); + } + + @Override + public void addResourcePack(UUID id, String url, byte[] hash, String prompt, boolean force) { + Preconditions.checkArgument(url != null, "Resource pack URL cannot be null"); + + String hashStr = ""; + if (hash != null) { + Preconditions.checkArgument(hash.length == 20, "Resource pack hash should be 20 bytes long but was %s", hash.length); + hashStr = BaseEncoding.base16().lowerCase().encode(hash); + } + + this.handlePushResourcePack(new ClientboundResourcePackPushPacket(id, url, hashStr, force, CraftChatMessage.fromStringOrNull(prompt, true)), false); } @Override @@ -1758,6 +1772,15 @@ public class CraftPlayer extends CraftHumanEntity implements Player { getHandle().connection.send(new ClientboundResourcePackPopPacket(Optional.empty())); } + private void handlePushResourcePack(ClientboundResourcePackPushPacket resourcePackPushPacket, boolean resetBeforePush) { + if (getHandle().connection == null) return; + + if (resetBeforePush) { + this.removeResourcePacks(); + } + getHandle().connection.send(resourcePackPushPacket); + } + public void addChannel(String channel) { Preconditions.checkState(channels.size() < 128, "Cannot register channel '%s'. Too many channels registered!", channel); channel = StandardMessenger.validateAndCorrectChannel(channel); diff --git a/paper-server/src/main/java/org/bukkit/craftbukkit/packs/CraftResourcePack.java b/paper-server/src/main/java/org/bukkit/craftbukkit/packs/CraftResourcePack.java new file mode 100644 index 0000000000..9313d17f01 --- /dev/null +++ b/paper-server/src/main/java/org/bukkit/craftbukkit/packs/CraftResourcePack.java @@ -0,0 +1,45 @@ +package org.bukkit.craftbukkit.packs; + +import java.util.UUID; +import net.minecraft.server.MinecraftServer; +import org.bukkit.craftbukkit.util.CraftChatMessage; +import org.bukkit.packs.ResourcePack; + +public class CraftResourcePack implements ResourcePack { + + private final MinecraftServer.ServerResourcePackInfo handle; + + public CraftResourcePack(MinecraftServer.ServerResourcePackInfo handle) { + this.handle = handle; + } + + @Override + public UUID getId() { + return this.handle.id(); + } + + @Override + public String getUrl() { + return this.handle.url(); + } + + @Override + public String getHash() { + return this.handle.hash(); + } + + @Override + public String getPrompt() { + return (this.handle.prompt() == null) ? "" : CraftChatMessage.fromComponent(this.handle.prompt()); + } + + @Override + public boolean isRequired() { + return this.handle.isRequired(); + } + + @Override + public String toString() { + return "CraftResourcePack{id=" + this.getId() + ",url=" + this.getUrl() + ",hash=" + this.getHash() + ",prompt=" + this.getPrompt() + ",required=" + this.isRequired() + "}"; + } +}