SPIGOT-7530, #1314: Improve Resource Pack API with new 1.20.3 functionality

By: md_5 <git@md-5.net>
This commit is contained in:
CraftBukkit/Spigot 2024-01-31 22:02:45 +11:00
parent 4a1ae2fa02
commit 4fde00f8dc
3 changed files with 79 additions and 4 deletions

View file

@ -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("");

View file

@ -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);

View file

@ -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() + "}";
}
}