Skip to content

Commit

Permalink
introduce no-op callback concept to simplify implementations
Browse files Browse the repository at this point in the history
  • Loading branch information
zml2008 committed Dec 10, 2023
1 parent 3e30a6f commit e992318
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 22 deletions.
4 changes: 4 additions & 0 deletions api/src/main/java/net/kyori/adventure/audience/Audience.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down Expand Up @@ -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);
}

/**
Expand Down
9 changes: 9 additions & 0 deletions api/src/main/java/net/kyori/adventure/audience/Audiences.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,18 @@
*/
@FunctionalInterface
public interface ResourcePackCallback {
/**
* Create a pack callback that performs no operation.
*
* <p>Multiple calls to this method are guaranteed to return callback functions with equal identity.</p>
*
* @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.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -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) -> {};
}

0 comments on commit e992318

Please sign in to comment.