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 e5d05242a..ec2efcb1c 100644 --- a/api/src/main/java/net/kyori/adventure/audience/Audience.java +++ b/api/src/main/java/net/kyori/adventure/audience/Audience.java @@ -739,8 +739,10 @@ default void setResourcePacks(final @NotNull ResourcePackRequestLike request, fi * @see ResourcePackRequest * @since 4.15.0 */ + @ForwardingAudienceOverrideNotRequired @SuppressWarnings("checkstyle:MethodName") default void setResourcePacks(final @NotNull ResourcePackRequest request, final @NotNull ResourcePackRequest@NotNull... others) { + this.setResourcePacks(ResourcePackCallback.noOp(), request, others); } /** @@ -808,7 +810,9 @@ default void sendResourcePacks(final @NotNull ResourcePackRequestLike request, f * @see ResourcePackRequest * @since 4.15.0 */ + @ForwardingAudienceOverrideNotRequired default void sendResourcePacks(final @NotNull ResourcePackRequest request, final @NotNull ResourcePackRequest@NotNull... others) { + this.sendResourcePacks(ResourcePackCallback.noOp(), request, others); } /** diff --git a/api/src/main/java/net/kyori/adventure/audience/Audiences.java b/api/src/main/java/net/kyori/adventure/audience/Audiences.java index ae560eef8..f151e6f85 100644 --- a/api/src/main/java/net/kyori/adventure/audience/Audiences.java +++ b/api/src/main/java/net/kyori/adventure/audience/Audiences.java @@ -28,6 +28,7 @@ import java.util.function.Consumer; import java.util.stream.Collector; import java.util.stream.Collectors; +import net.kyori.adventure.resource.ResourcePackCallback; import net.kyori.adventure.text.ComponentLike; import org.jetbrains.annotations.NotNull; @@ -55,4 +56,12 @@ private Audiences() { public static @NotNull Consumer super Audience> sendingMessage(final @NotNull ComponentLike message) { return audience -> audience.sendMessage(message); } + + static @NotNull ResourcePackCallback unwrapCallback(final Audience forwarding, final Audience dest, final @NotNull ResourcePackCallback cb) { + if (cb == ResourcePackCallback.noOp()) return cb; + + return (uuid, status, audience) -> { + cb.packEventReceived(uuid, status, audience == dest ? forwarding : audience); + }; + } } diff --git a/api/src/main/java/net/kyori/adventure/audience/ForwardingAudience.java b/api/src/main/java/net/kyori/adventure/audience/ForwardingAudience.java index 2d30fcd21..bf27374de 100644 --- a/api/src/main/java/net/kyori/adventure/audience/ForwardingAudience.java +++ b/api/src/main/java/net/kyori/adventure/audience/ForwardingAudience.java @@ -202,21 +202,11 @@ default void openBook(final @NotNull Book book) { for (final Audience audience : this.audiences()) audience.openBook(book); } - @Override - default void setResourcePacks(final @NotNull ResourcePackRequest request, final @NotNull ResourcePackRequest@NotNull... others) { - for (final Audience audience : this.audiences()) audience.setResourcePacks(request, others); - } - @Override default void setResourcePacks(final @NotNull ResourcePackCallback cb, final @NotNull ResourcePackRequest request, final @NotNull ResourcePackRequest @NotNull ... others) { for (final Audience audience : this.audiences()) audience.setResourcePacks(cb, request, others); } - @Override - default void sendResourcePacks(final @NotNull ResourcePackRequest request, final @NotNull ResourcePackRequest@NotNull... others) { - for (final Audience audience : this.audiences()) audience.sendResourcePacks(request, others); - } - @Override default void sendResourcePacks(final @NotNull ResourcePackCallback cb, final @NotNull ResourcePackRequest request, final @NotNull ResourcePackRequest@NotNull ... others) { for (final Audience audience : this.audiences()) audience.sendResourcePacks(cb, request, others); @@ -395,24 +385,14 @@ default void openBook(final @NotNull Book book) { this.audience().openBook(book); } - @Override - default void setResourcePacks(final @NotNull ResourcePackRequest request, final @NotNull ResourcePackRequest... others) { - this.audience().setResourcePacks(request, others); - } - @Override default void setResourcePacks(final @NotNull ResourcePackCallback cb, final @NotNull ResourcePackRequest request, final @NotNull ResourcePackRequest @NotNull ... others) { - this.audience().setResourcePacks((uuid, status, audience) -> cb.packEventReceived(uuid, status, this), request, others); - } - - @Override - default void sendResourcePacks(final @NotNull ResourcePackRequest request, final @NotNull ResourcePackRequest@NotNull... others) { - this.audience().sendResourcePacks(request, others); + this.audience().setResourcePacks(Audiences.unwrapCallback(this, this.audience(), cb), request, others); } @Override default void sendResourcePacks(final @NotNull ResourcePackCallback cb, final @NotNull ResourcePackRequest request, final @NotNull ResourcePackRequest @NotNull ... others) { - this.audience().sendResourcePacks((uuid, status, audience) -> cb.packEventReceived(uuid, status, this), request, others); + this.audience().sendResourcePacks(Audiences.unwrapCallback(this, this.audience(), cb), request, others); } @Override diff --git a/api/src/main/java/net/kyori/adventure/resource/ResourcePackCallback.java b/api/src/main/java/net/kyori/adventure/resource/ResourcePackCallback.java index d07db5882..2a9272627 100644 --- a/api/src/main/java/net/kyori/adventure/resource/ResourcePackCallback.java +++ b/api/src/main/java/net/kyori/adventure/resource/ResourcePackCallback.java @@ -35,6 +35,18 @@ */ @FunctionalInterface public interface ResourcePackCallback { + /** + * Create a pack callback that performs no operation. + * + *
Multiple calls to this method are guaranteed to return callback functions with equal identity.
+ * + * @return the no-op callback + * @since 4.15.0 + */ + static @NotNull ResourcePackCallback noOp() { + return ResourcePackCallbacks.NO_OP; + } + /** * Create a pack callback that will only execute the provided functions when the pack application has completed, discarding all intermediate events. * diff --git a/api/src/main/java/net/kyori/adventure/resource/ResourcePackCallbacks.java b/api/src/main/java/net/kyori/adventure/resource/ResourcePackCallbacks.java new file mode 100644 index 000000000..eeee28896 --- /dev/null +++ b/api/src/main/java/net/kyori/adventure/resource/ResourcePackCallbacks.java @@ -0,0 +1,31 @@ +/* + * 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; + +final class ResourcePackCallbacks { + private ResourcePackCallbacks() { + } + + static final ResourcePackCallback NO_OP = (uuid, status, audience) -> {}; +}