From 29abee23e75d228ca72b9733180eda5d190a25a2 Mon Sep 17 00:00:00 2001 From: zml Date: Sat, 9 Dec 2023 22:50:52 -0800 Subject: [PATCH] feat(api): Introduce ResourcePackRequestLike --- .../kyori/adventure/audience/Audience.java | 27 ++++++++++++ .../resource/ResourcePackRequest.java | 14 ++++++- .../resource/ResourcePackRequestLike.java | 41 +++++++++++++++++++ 3 files changed, 80 insertions(+), 2 deletions(-) create mode 100644 api/src/main/java/net/kyori/adventure/resource/ResourcePackRequestLike.java diff --git a/api/src/main/java/net/kyori/adventure/audience/Audience.java b/api/src/main/java/net/kyori/adventure/audience/Audience.java index 0279ce9ef..6d66b23be 100644 --- a/api/src/main/java/net/kyori/adventure/audience/Audience.java +++ b/api/src/main/java/net/kyori/adventure/audience/Audience.java @@ -41,6 +41,7 @@ import net.kyori.adventure.resource.ResourcePackInfo; import net.kyori.adventure.resource.ResourcePackInfoLike; import net.kyori.adventure.resource.ResourcePackRequest; +import net.kyori.adventure.resource.ResourcePackRequestLike; import net.kyori.adventure.sound.Sound; import net.kyori.adventure.sound.SoundStop; import net.kyori.adventure.text.Component; @@ -725,6 +726,20 @@ default void sendResourcePacks(final @NotNull ResourcePackInfoLike first, final this.sendResourcePacks(ResourcePackRequest.addingRequest(first, others)); } + /** + * Sends a request to apply resource packs to this audience. + * + *

Multiple resource packs are only supported since 1.20.3. On older versions, all requests behave as if {@link ResourcePackRequest#replace()} is set to {@code true}.

+ * + * @param request the resource pack request + * @see ResourcePackInfo + * @since 4.15.0 + */ + @ForwardingAudienceOverrideNotRequired + default void sendResourcePacks(final @NotNull ResourcePackRequestLike request) { + this.sendResourcePacks(request.asResourcePackRequest()); + } + /** * Sends a request to apply resource packs to this audience. * @@ -737,6 +752,18 @@ default void sendResourcePacks(final @NotNull ResourcePackInfoLike first, final default void sendResourcePacks(final @NotNull ResourcePackRequest request) { } + /** + * Clear resource packs with the IDs used in the provided requests if they are present. + * + * @param request the request used to originally apply the packs + * @since 4.15.0 + * @sinceMinecraft 1.20.3 + */ + @ForwardingAudienceOverrideNotRequired + default void removeResourcePacks(final @NotNull ResourcePackRequestLike request) { + this.removeResourcePacks(request.asResourcePackRequest()); + } + /** * Clear resource packs with the IDs used in the provided requests if they are present. * diff --git a/api/src/main/java/net/kyori/adventure/resource/ResourcePackRequest.java b/api/src/main/java/net/kyori/adventure/resource/ResourcePackRequest.java index a56b6a300..304f970d5 100644 --- a/api/src/main/java/net/kyori/adventure/resource/ResourcePackRequest.java +++ b/api/src/main/java/net/kyori/adventure/resource/ResourcePackRequest.java @@ -37,7 +37,7 @@ * * @since 4.15.0 */ -public interface ResourcePackRequest extends Examinable { +public interface ResourcePackRequest extends Examinable, ResourcePackRequestLike { /** * Create a basic request to apply the provided resource packs. * @@ -125,12 +125,17 @@ public interface ResourcePackRequest extends Examinable { */ @NotNull ResourcePackRequest replace(final boolean replace); + @Override + default @NotNull ResourcePackRequest asResourcePackRequest() { + return this; + } + /** * A builder for resource pack requests. * * @since 4.15.0 */ - interface Builder extends AbstractBuilder { + interface Builder extends AbstractBuilder, ResourcePackRequestLike { /** * Set the resource packs to apply. * @@ -167,5 +172,10 @@ interface Builder extends AbstractBuilder { * @since 4.15.0 */ @NotNull Builder replace(final boolean replace); + + @Override + default @NotNull ResourcePackRequest asResourcePackRequest() { + return this.build(); + } } } diff --git a/api/src/main/java/net/kyori/adventure/resource/ResourcePackRequestLike.java b/api/src/main/java/net/kyori/adventure/resource/ResourcePackRequestLike.java new file mode 100644 index 000000000..c6cd56d7c --- /dev/null +++ b/api/src/main/java/net/kyori/adventure/resource/ResourcePackRequestLike.java @@ -0,0 +1,41 @@ +/* + * This file is part of adventure, licensed under the MIT License. + * + * Copyright (c) 2017-2023 KyoriPowered + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package net.kyori.adventure.resource; + +import org.jetbrains.annotations.NotNull; + +/** + * Something that can be represented as a {@link ResourcePackRequest}. + * + * @since 4.15.0 + */ +public interface ResourcePackRequestLike { + /** + * Get the pack request representation. + * + * @return the pack request representation of this object + * @since 4.15.0 + */ + @NotNull ResourcePackRequest asResourcePackRequest(); +}