3
0
Mirror von https://github.com/GeyserMC/Geyser.git synchronisiert 2024-11-20 15:00:11 +01:00

Remove options taking a ResourcePack, use uuids

Add uuid shortcut to ResourcePack interface
Make the priority a double, add LOWEST and HIGHEST priorities
Dieser Commit ist enthalten in:
onebeastchris 2024-08-20 19:23:59 +02:00
Ursprung fb4e42de6c
Commit 6c5a05a8b8
5 geänderte Dateien mit 23 neuen und 50 gelöschten Zeilen

Datei anzeigen

@ -61,7 +61,7 @@ public abstract class SessionLoadResourcePacksEvent extends ConnectionEvent {
public abstract boolean register(@NonNull ResourcePack resourcePack); public abstract boolean register(@NonNull ResourcePack resourcePack);
/** /**
* Registers a {@link ResourcePack} to be sent to the client, but alongside * Registers a {@link ResourcePack} to be sent to the client, alongside
* specific options. * specific options.
* *
* @param resourcePack a resource pack that will be sent to the client. * @param resourcePack a resource pack that will be sent to the client.
@ -71,15 +71,6 @@ public abstract class SessionLoadResourcePacksEvent extends ConnectionEvent {
*/ */
public abstract boolean register(@NonNull ResourcePack resourcePack, @Nullable ResourcePackOption... options); public abstract boolean register(@NonNull ResourcePack resourcePack, @Nullable ResourcePackOption... options);
/**
* Sets {@link ResourcePackOption}'s for a resource pack
*
* @param resourcePack the resource pack to register the options for
* @param options the options to register for the pack
* @throws IllegalArgumentException if the pack is not registered.
*/
public abstract void registerOptions(@NonNull ResourcePack resourcePack, @NonNull ResourcePackOption... options);
/** /**
* Sets {@link ResourcePackOption}'s for a resource pack * Sets {@link ResourcePackOption}'s for a resource pack
* *
@ -89,22 +80,14 @@ public abstract class SessionLoadResourcePacksEvent extends ConnectionEvent {
*/ */
public abstract void registerOptions(@NonNull UUID uuid, @NonNull ResourcePackOption... options); public abstract void registerOptions(@NonNull UUID uuid, @NonNull ResourcePackOption... options);
/**
* Returns the subpack options set for a specific resource pack.
*
* @param resourcePack the resourcePack for which the options are set
* @return a list of {@link ResourcePackOption}
*/
public abstract Collection<ResourcePackOption> options(ResourcePack resourcePack);
/** /**
* Returns the subpack options set for a specific resource pack uuid. * Returns the subpack options set for a specific resource pack uuid.
* These are not modifiable. * These are not modifiable.
* *
* @param resourcePack the resourcePack for which the options are set * @param uuid the resourcePack for which the options are set
* @return a list of {@link ResourcePackOption} * @return a list of {@link ResourcePackOption}
*/ */
public abstract Collection<ResourcePackOption> options(UUID resourcePack); public abstract Collection<ResourcePackOption> options(UUID uuid);
/** /**
* Unregisters a resource pack from being sent to the client. * Unregisters a resource pack from being sent to the client.

Datei anzeigen

@ -34,16 +34,18 @@ import org.geysermc.geyser.api.GeyserApi;
*/ */
public interface PriorityOption extends ResourcePackOption { public interface PriorityOption extends ResourcePackOption {
PriorityOption HIGH = PriorityOption.priority(10); PriorityOption HIGHEST = PriorityOption.priority(10);
PriorityOption HIGH = PriorityOption.priority(8);
PriorityOption NORMAL = PriorityOption.priority(5); PriorityOption NORMAL = PriorityOption.priority(5);
PriorityOption LOW = PriorityOption.priority(0); PriorityOption LOW = PriorityOption.priority(3);
PriorityOption LOWEST = PriorityOption.priority(0);
/** /**
* The priority of the resource pack * The priority of the resource pack
* *
* @return priority * @return priority
*/ */
int priority(); double priority();
/** /**
* Constructs a priority option based on a value between 0 and 10 * Constructs a priority option based on a value between 0 and 10
@ -51,7 +53,7 @@ public interface PriorityOption extends ResourcePackOption {
* @param priority an integer that is above 0, but smaller than 10 * @param priority an integer that is above 0, but smaller than 10
* @return the priority option * @return the priority option
*/ */
static PriorityOption priority(int priority) { static PriorityOption priority(double 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

@ -68,7 +68,7 @@ public class SessionLoadResourcePacksEventImpl extends SessionLoadResourcePacksE
} }
public LinkedList<ResourcePackStackPacket.Entry> orderedPacks() { public LinkedList<ResourcePackStackPacket.Entry> orderedPacks() {
TreeSet<Map.Entry<GeyserResourcePack, Integer>> sortedPacks = packs.values().stream() TreeSet<Map.Entry<GeyserResourcePack, Double>> sortedPacks = packs.values().stream()
// Map each ResourcePack to a pair of (ResourcePack, Priority) // Map each ResourcePack to a pair of (ResourcePack, Priority)
.map(pack -> new AbstractMap.SimpleEntry<>(pack, getPriority(pack))) .map(pack -> new AbstractMap.SimpleEntry<>(pack, getPriority(pack)))
// Sort by priority in ascending order // Sort by priority in ascending order
@ -88,11 +88,12 @@ public class SessionLoadResourcePacksEventImpl extends SessionLoadResourcePacksE
} }
// Helper method to get the priority of a ResourcePack // Helper method to get the priority of a ResourcePack
private int getPriority(GeyserResourcePack pack) { private double getPriority(GeyserResourcePack pack) {
ResourcePackOption option; ResourcePackOption option;
OptionHolder holder = options.get(pack.uuid()); OptionHolder holder = options.get(pack.uuid());
if (holder == null) { if (holder == null) {
// priority is always set
option = pack.options().get(ResourcePackOption.Type.PRIORITY); option = pack.options().get(ResourcePackOption.Type.PRIORITY);
} else { } else {
option = options.get(pack.uuid()) option = options.get(pack.uuid())
@ -140,11 +141,15 @@ public class SessionLoadResourcePacksEventImpl extends SessionLoadResourcePacksE
return register(resourcePack, PriorityOption.NORMAL); return register(resourcePack, PriorityOption.NORMAL);
} }
public void registerOption(@NonNull ResourcePack resourcePack, @NonNull ResourcePackOption option) { private void registerOption(@NonNull ResourcePack resourcePack, @Nullable ResourcePackOption... options) {
if (options == null) {
return;
}
GeyserResourcePack pack = validate(resourcePack); GeyserResourcePack pack = validate(resourcePack);
OptionHolder holder = this.options.computeIfAbsent(pack.uuid(), $ -> new OptionHolder()); OptionHolder holder = this.options.computeIfAbsent(pack.uuid(), $ -> new OptionHolder());
holder.add(option); holder.add(options);
holder.validateOptions(pack); holder.validateOptions(pack);
} }
@ -160,37 +165,20 @@ public class SessionLoadResourcePacksEventImpl extends SessionLoadResourcePacksE
packs.put(uuid, pack); packs.put(uuid, pack);
// register options // register options
OptionHolder holder = new OptionHolder(); registerOption(resourcePack, options);
holder.add(options);
holder.validateOptions(pack);
this.options.put(uuid, holder);
return true; return true;
} }
@Override
public void registerOptions(@NonNull ResourcePack resourcePack, @NonNull ResourcePackOption... options) {
validate(resourcePack);
registerOptions(resourcePack.uuid());
}
@Override @Override
public void registerOptions(@NonNull UUID uuid, @NonNull ResourcePackOption... options) { public void registerOptions(@NonNull UUID uuid, @NonNull ResourcePackOption... options) {
Objects.requireNonNull(uuid); Objects.requireNonNull(uuid);
Objects.requireNonNull(options);
GeyserResourcePack resourcePack = packs.get(uuid); GeyserResourcePack resourcePack = packs.get(uuid);
if (resourcePack == null) { if (resourcePack == null) {
throw new IllegalArgumentException("Pack with uuid %s not registered yet!".formatted(uuid)); throw new IllegalArgumentException("Pack with uuid %s not registered yet!".formatted(uuid));
} }
OptionHolder holder = this.options.computeIfAbsent(uuid, $ -> new OptionHolder()); registerOption(resourcePack, options);
holder.add(options);
holder.validateOptions(resourcePack);
}
@Override
public Collection<ResourcePackOption> options(@NonNull ResourcePack resourcePack) {
validate(resourcePack);
return options(resourcePack.uuid());
} }
@Override @Override

Datei anzeigen

@ -31,7 +31,7 @@ import org.geysermc.geyser.api.pack.option.PriorityOption;
import java.util.Objects; import java.util.Objects;
public record GeyserPriorityOption(int priority) implements PriorityOption { public record GeyserPriorityOption(double priority) implements PriorityOption {
public GeyserPriorityOption { public GeyserPriorityOption {
if (priority < 0 || priority > 10) { if (priority < 0 || priority > 10) {

Datei anzeigen

@ -86,7 +86,7 @@ public class ProviderRegistryLoader implements RegistryLoader<Map<Class<?>, Prov
// packs // packs
providers.put(PathPackCodec.class, args -> new GeyserPathPackCodec((Path) args[0])); providers.put(PathPackCodec.class, args -> new GeyserPathPackCodec((Path) args[0]));
providers.put(PriorityOption.class, args -> new GeyserPriorityOption((int) args[0])); providers.put(PriorityOption.class, args -> new GeyserPriorityOption((double) args[0]));
providers.put(SubpackOption.class, args -> new GeyserSubpackOption((String) args[0])); providers.put(SubpackOption.class, args -> new GeyserSubpackOption((String) args[0]));
// items // items