3
0
Mirror von https://github.com/GeyserMC/Geyser.git synchronisiert 2024-10-03 16:31:14 +02:00

attempt at implementing resource pack order specifying

Dieser Commit ist enthalten in:
onebeastchris 2024-08-13 00:42:01 +02:00
Ursprung c316d09754
Commit a868ced1a7
5 geänderte Dateien mit 138 neuen und 52 gelöschten Zeilen

Datei anzeigen

@ -27,6 +27,9 @@ package org.geysermc.geyser.api.pack;
import org.checkerframework.checker.nullness.qual.NonNull; import org.checkerframework.checker.nullness.qual.NonNull;
import org.geysermc.geyser.api.GeyserApi; import org.geysermc.geyser.api.GeyserApi;
import org.geysermc.geyser.api.pack.option.ResourcePackOption;
import java.util.Collection;
/** /**
* Represents a resource pack sent to Bedrock clients * Represents a resource pack sent to Bedrock clients
@ -63,12 +66,15 @@ public interface ResourcePack {
String contentKey(); String contentKey();
/** /**
* The default subpack to tell Bedrock clients to load. Lack of a subpack to load is represented by an empty string. * Gets the currently set default options of this resource pack.
* These can be a priority defining how the Bedrock client applies multiple packs,
* or a default subpack.
* <p>
* These can be overridden in the {@link org.geysermc.geyser.api.event.bedrock.SessionLoadResourcePacksEvent}
* *
* @return the subpack name, or an empty string if not set. * @return a collection of default {@link ResourcePackOption}s
*/ */
@NonNull Collection<ResourcePackOption> defaultOptions();
String defaultSubpackName();
/** /**
* Creates a resource pack with the given {@link PackCodec}. * Creates a resource pack with the given {@link PackCodec}.
@ -98,18 +104,44 @@ public interface ResourcePack {
*/ */
interface Builder { interface Builder {
/**
* @return the {@link ResourcePackManifest} of this resource pack
*/
ResourcePackManifest manifest(); ResourcePackManifest manifest();
/**
* @return the {@link PackCodec} of this resource pack
*/
PackCodec codec(); PackCodec codec();
/**
* @return the current content key, or an empty string if not set
*/
String contentKey(); String contentKey();
String defaultSubpackName(); /**
* Sets a content key for this resource pack.
*
* @param contentKey the content key
* @return this builder
*/
Builder contentKey(@NonNull String contentKey); Builder contentKey(@NonNull String contentKey);
Builder defaultSubpackName(@NonNull String subpackName); /**
* @return the current default {@link ResourcePackOption}s
*/
Collection<ResourcePackOption> defaultOptions();
/**
* Sets default options for this resource pack.
*
* @return this builder
*/
Builder defaultOptions(ResourcePackOption... defaultOptions);
/**
* @return the resource pack
*/
ResourcePack build(); ResourcePack build();
} }
} }

Datei anzeigen

@ -29,7 +29,8 @@ import org.geysermc.geyser.api.GeyserApi;
/** /**
* Allows specifying a pack priority that decides the order on how packs are sent to the client. * Allows specifying a pack priority that decides the order on how packs are sent to the client.
* Multiple resource packs can override each other. The higher the priority, the * Multiple resource packs can override each other. The higher the priority, the "higher" in the stack
* a pack is, and the more a pack can override other packs.
*/ */
public interface PriorityOption extends ResourcePackOption { public interface PriorityOption extends ResourcePackOption {
@ -37,8 +38,19 @@ public interface PriorityOption extends ResourcePackOption {
PriorityOption NORMAL = PriorityOption.priority(5); PriorityOption NORMAL = PriorityOption.priority(5);
PriorityOption LOW = PriorityOption.priority(0); PriorityOption LOW = PriorityOption.priority(0);
/**
* The priority of the resource pack
*
* @return priority
*/
int priority(); int priority();
/**
* Constructs a priority option based on a value between 0 and 10
*
* @param priority an integer that is above 0, but smaller than 10
* @return the priority option
*/
static PriorityOption priority(int priority) { static PriorityOption priority(int priority) {
if (priority < 0 || priority > 10) { if (priority < 0 || priority > 10) {
throw new IllegalArgumentException("Priority must be between 0 and 10 inclusive!"); throw new IllegalArgumentException("Priority must be between 0 and 10 inclusive!");

Datei anzeigen

@ -26,15 +26,21 @@
package org.geysermc.geyser.event.type; package org.geysermc.geyser.event.type;
import it.unimi.dsi.fastutil.objects.Object2ObjectLinkedOpenHashMap; import it.unimi.dsi.fastutil.objects.Object2ObjectLinkedOpenHashMap;
import lombok.Getter;
import org.checkerframework.checker.nullness.qual.NonNull; import org.checkerframework.checker.nullness.qual.NonNull;
import org.cloudburstmc.protocol.bedrock.packet.ResourcePackStackPacket; import org.cloudburstmc.protocol.bedrock.packet.ResourcePackStackPacket;
import org.cloudburstmc.protocol.bedrock.packet.ResourcePacksInfoPacket;
import org.geysermc.geyser.api.event.bedrock.SessionLoadResourcePacksEvent; import org.geysermc.geyser.api.event.bedrock.SessionLoadResourcePacksEvent;
import org.geysermc.geyser.api.pack.ResourcePack; import org.geysermc.geyser.api.pack.ResourcePack;
import org.geysermc.geyser.api.pack.ResourcePackManifest;
import org.geysermc.geyser.api.pack.option.PriorityOption; import org.geysermc.geyser.api.pack.option.PriorityOption;
import org.geysermc.geyser.api.pack.option.ResourcePackOption; import org.geysermc.geyser.api.pack.option.ResourcePackOption;
import org.geysermc.geyser.api.pack.option.SubpackOption;
import org.geysermc.geyser.registry.Registries; import org.geysermc.geyser.registry.Registries;
import org.geysermc.geyser.session.GeyserSession; import org.geysermc.geyser.session.GeyserSession;
import java.util.AbstractMap;
import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
@ -42,28 +48,72 @@ import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.UUID; import java.util.UUID;
import java.util.stream.Collectors;
public class SessionLoadResourcePacksEventImpl extends SessionLoadResourcePacksEvent { public class SessionLoadResourcePacksEventImpl extends SessionLoadResourcePacksEvent {
@Getter
private final Map<String, ResourcePack> packs; private final Map<String, ResourcePack> packs;
private final Map<String, Collection<ResourcePackOption>> options; private final Map<String, Collection<ResourcePackOption>> options = new HashMap<>();
public SessionLoadResourcePacksEventImpl(GeyserSession session) { public SessionLoadResourcePacksEventImpl(GeyserSession session) {
super(session); super(session);
this.packs = new Object2ObjectLinkedOpenHashMap<>(Registries.RESOURCE_PACKS.get()); this.packs = new Object2ObjectLinkedOpenHashMap<>(Registries.RESOURCE_PACKS.get());
this.options = new HashMap<>(); this.packs.values().forEach(
} pack -> options.put(pack.manifest().header().uuid().toString(), pack.defaultOptions())
);
public @NonNull Map<String, ResourcePack> getPacks() {
return packs;
} }
public LinkedList<ResourcePackStackPacket.Entry> orderedPacks() { public LinkedList<ResourcePackStackPacket.Entry> orderedPacks() {
// TODO sort by priority here return packs.values().stream()
// Map each ResourcePack to a pair of (ResourcePack, Priority)
return new LinkedList<>(); .map(pack -> new AbstractMap.SimpleEntry<>(pack, getPriority(pack)))
// Sort by priority in descending order (higher priority first)
.sorted((entry1, entry2) -> Integer.compare(entry2.getValue(), entry1.getValue()))
// Extract the ResourcePack from the sorted entries
.map(entry -> {
ResourcePack pack = entry.getKey();
ResourcePackManifest.Header header = pack.manifest().header();
return new ResourcePackStackPacket.Entry(header.uuid().toString(), header.version().toString(), getSubpackName(header.uuid()));
})
// Collect to a LinkedList
.collect(Collectors.toCollection(LinkedList::new));
} }
// Helper method to get the priority of a ResourcePack
private int getPriority(ResourcePack pack) {
return options.get(pack.manifest().header().uuid().toString()).stream()
// Filter to find the PriorityOption
.filter(option -> option instanceof PriorityOption)
// Map to the priority value
.mapToInt(option -> ((PriorityOption) option).priority())
// Get the highest priority (or a default value, if none found)
.max().orElse(PriorityOption.NORMAL.priority());
}
public List<ResourcePacksInfoPacket.Entry> infoPacketEntries() {
List<ResourcePacksInfoPacket.Entry> entries = new ArrayList<>();
for (ResourcePack pack : packs.values()) {
ResourcePackManifest.Header header = pack.manifest().header();
entries.add(new ResourcePacksInfoPacket.Entry(
header.uuid().toString(), header.version().toString(), pack.codec().size(), pack.contentKey(),
getSubpackName(header.uuid()), header.uuid().toString(), false, false)
);
}
return entries;
}
private String getSubpackName(UUID uuid) {
return options.get(uuid.toString()).stream()
.filter(option -> option instanceof SubpackOption)
.map(option -> ((SubpackOption) option).subpackName())
.findFirst()
.orElse(""); // Return an empty string if none is found
}
@Override @Override
public @NonNull List<ResourcePack> resourcePacks() { public @NonNull List<ResourcePack> resourcePacks() {
return List.copyOf(packs.values()); return List.copyOf(packs.values());
@ -92,9 +142,8 @@ public class SessionLoadResourcePacksEventImpl extends SessionLoadResourcePacksE
} }
@Override @Override
public Collection<ResourcePackOption> options(UUID resourcePack) { public Collection<ResourcePackOption> options(UUID uuid) {
Collection<ResourcePackOption> packOptions = options.get(resourcePack.toString()); return Collections.unmodifiableCollection(options.get(uuid.toString()));
return packOptions == null ? List.of() : Collections.unmodifiableCollection(packOptions);
} }
@Override @Override

Datei anzeigen

@ -203,12 +203,7 @@ public class UpstreamPacketHandler extends LoggingPacketHandler {
this.geyser.eventBus().fire(this.resourcePackLoadEvent); this.geyser.eventBus().fire(this.resourcePackLoadEvent);
ResourcePacksInfoPacket resourcePacksInfo = new ResourcePacksInfoPacket(); ResourcePacksInfoPacket resourcePacksInfo = new ResourcePacksInfoPacket();
for (ResourcePack pack : this.resourcePackLoadEvent.resourcePacks()) { resourcePacksInfo.getResourcePackInfos().addAll(this.resourcePackLoadEvent.infoPacketEntries());
ResourcePackManifest.Header header = pack.manifest().header();
resourcePacksInfo.getResourcePackInfos().add(new ResourcePacksInfoPacket.Entry(
header.uuid().toString(), header.version().toString(), pack.codec().size(), pack.contentKey(),
pack.defaultSubpackName(), header.uuid().toString(), false, false));
}
resourcePacksInfo.setForcedToAccept(GeyserImpl.getInstance().getConfig().isForceResourcePacks()); resourcePacksInfo.setForcedToAccept(GeyserImpl.getInstance().getConfig().isForceResourcePacks());
session.sendUpstreamPacket(resourcePacksInfo); session.sendUpstreamPacket(resourcePacksInfo);
@ -235,7 +230,6 @@ public class UpstreamPacketHandler extends LoggingPacketHandler {
break; break;
case HAVE_ALL_PACKS: case HAVE_ALL_PACKS:
// TODO apply pack order here
ResourcePackStackPacket stackPacket = new ResourcePackStackPacket(); ResourcePackStackPacket stackPacket = new ResourcePackStackPacket();
stackPacket.setExperimentsPreviouslyToggled(false); stackPacket.setExperimentsPreviouslyToggled(false);
stackPacket.setForcedToAccept(false); // Leaving this as false allows the player to choose to download or not stackPacket.setForcedToAccept(false); // Leaving this as false allows the player to choose to download or not

Datei anzeigen

@ -25,16 +25,25 @@
package org.geysermc.geyser.pack; package org.geysermc.geyser.pack;
import org.checkerframework.checker.nullness.qual.Nullable; import org.checkerframework.checker.nullness.qual.NonNull;
import org.geysermc.geyser.api.pack.PackCodec; import org.geysermc.geyser.api.pack.PackCodec;
import org.geysermc.geyser.api.pack.ResourcePack; import org.geysermc.geyser.api.pack.ResourcePack;
import org.geysermc.geyser.api.pack.ResourcePackManifest; import org.geysermc.geyser.api.pack.ResourcePackManifest;
import org.geysermc.geyser.api.pack.option.PriorityOption;
import org.geysermc.geyser.api.pack.option.ResourcePackOption;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
public record GeyserResourcePack( public record GeyserResourcePack(
PackCodec codec, PackCodec codec,
ResourcePackManifest manifest, ResourcePackManifest manifest,
String contentKey, String contentKey,
String defaultSubpackName Collection<ResourcePackOption> defaultOptions
) implements ResourcePack { ) implements ResourcePack {
/** /**
@ -43,10 +52,9 @@ public record GeyserResourcePack(
public static final int CHUNK_SIZE = 102400; public static final int CHUNK_SIZE = 102400;
public GeyserResourcePack(PackCodec codec, ResourcePackManifest manifest, String contentKey) { public GeyserResourcePack(PackCodec codec, ResourcePackManifest manifest, String contentKey) {
this(codec, manifest, contentKey, ""); this(codec, manifest, contentKey, new ArrayList<>(List.of(PriorityOption.NORMAL)));
} }
public static class Builder implements ResourcePack.Builder { public static class Builder implements ResourcePack.Builder {
public Builder(PackCodec codec, ResourcePackManifest manifest) { public Builder(PackCodec codec, ResourcePackManifest manifest) {
@ -62,8 +70,8 @@ public record GeyserResourcePack(
private final PackCodec codec; private final PackCodec codec;
private final ResourcePackManifest manifest; private final ResourcePackManifest manifest;
private String contentKey; private String contentKey = "";
private String defaultSubpackName; private final Collection<ResourcePackOption> defaultOptions = new ArrayList<>(List.of(PriorityOption.NORMAL));
@Override @Override
public ResourcePackManifest manifest() { public ResourcePackManifest manifest() {
@ -77,38 +85,29 @@ public record GeyserResourcePack(
@Override @Override
public String contentKey() { public String contentKey() {
return this.contentKey == null ? "" : this.contentKey; return contentKey;
} }
@Override @Override
public String defaultSubpackName() { public Collection<ResourcePackOption> defaultOptions() {
return this.defaultSubpackName == null ? "" : this.defaultSubpackName; return Collections.unmodifiableCollection(defaultOptions);
} }
public Builder contentKey(@Nullable String contentKey) { public Builder contentKey(@NonNull String contentKey) {
Objects.requireNonNull(contentKey);
this.contentKey = contentKey; this.contentKey = contentKey;
return this; return this;
} }
public Builder defaultSubpackName(@Nullable String subpackName) { public Builder defaultOptions(ResourcePackOption... defaultOptions) {
if (manifest.subpacks().stream().anyMatch(subpack -> subpack.name().equals(subpackName))) { this.defaultOptions.addAll(Arrays.stream(defaultOptions).toList());
this.defaultSubpackName = subpackName;
} else {
throw new IllegalArgumentException("A subpack with the name '" + subpackName + "' does not exist!");
}
return this; return this;
} }
public GeyserResourcePack build() { public GeyserResourcePack build() {
if (contentKey == null) { GeyserResourcePack pack = new GeyserResourcePack(codec, manifest, contentKey, defaultOptions);
contentKey = ""; defaultOptions.forEach(option -> option.validate(pack));
} return pack;
if (defaultSubpackName == null) {
defaultSubpackName = "";
}
return new GeyserResourcePack(codec, manifest, contentKey, defaultSubpackName);
} }
} }
} }