Skip to content

Commit

Permalink
Merge pull request #981 from yusshu/main/4
Browse files Browse the repository at this point in the history
feat(api): introduce ResourcePackRequest
  • Loading branch information
zml2008 authored Nov 20, 2023
2 parents 47a1035 + 98b9f5f commit 323990f
Show file tree
Hide file tree
Showing 7 changed files with 433 additions and 0 deletions.
28 changes: 28 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 @@ -36,6 +36,8 @@
import net.kyori.adventure.identity.Identity;
import net.kyori.adventure.inventory.Book;
import net.kyori.adventure.pointer.Pointered;
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;
Expand Down Expand Up @@ -700,4 +702,30 @@ default void openBook(final Book.@NotNull Builder book) {
*/
default void openBook(final @NotNull Book book) {
}

// ------------------------
// ---- Resource Packs ----
// ------------------------

/**
* Sends a resource pack request to this audience.
*
* @param request the resource pack request
* @see ResourcePackRequest
* @since 4.15.0
*/
@ForwardingAudienceOverrideNotRequired
default void sendResourcePack(final @NotNull ResourcePackRequestLike request) {
this.sendResourcePack(request.asResourcePackRequest());
}

/**
* Sends a resource pack request to this audience.
*
* @param request the resource pack request
* @see ResourcePackRequest
* @since 4.15.0
*/
default void sendResourcePack(final @NotNull ResourcePackRequest request) {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import net.kyori.adventure.identity.Identity;
import net.kyori.adventure.inventory.Book;
import net.kyori.adventure.pointer.Pointer;
import net.kyori.adventure.resource.ResourcePackRequestLike;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.ComponentLike;
import org.jetbrains.annotations.Contract;
Expand Down Expand Up @@ -118,6 +119,10 @@ public void sendPlayerListHeaderAndFooter(final @NotNull ComponentLike header, f
public void openBook(final Book.@NotNull Builder book) {
}

@Override
public void sendResourcePack(final @NotNull ResourcePackRequestLike request) {
}

@Override
public boolean equals(final Object that) {
return this == that;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import net.kyori.adventure.inventory.Book;
import net.kyori.adventure.pointer.Pointer;
import net.kyori.adventure.pointer.Pointers;
import net.kyori.adventure.resource.ResourcePackRequest;
import net.kyori.adventure.sound.Sound;
import net.kyori.adventure.sound.SoundStop;
import net.kyori.adventure.text.Component;
Expand Down Expand Up @@ -199,6 +200,11 @@ default void openBook(final @NotNull Book book) {
for (final Audience audience : this.audiences()) audience.openBook(book);
}

@Override
default void sendResourcePack(final @NotNull ResourcePackRequest request) {
for (final Audience audience : this.audiences()) audience.sendResourcePack(request);
}

/**
* An audience that forwards everything to a single other audience.
*
Expand Down Expand Up @@ -361,5 +367,10 @@ default void stopSound(final @NotNull SoundStop stop) {
default void openBook(final @NotNull Book book) {
this.audience().openBook(book);
}

@Override
default void sendResourcePack(final @NotNull ResourcePackRequest request) {
this.audience().sendResourcePack(request);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
/*
* 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 java.net.URI;
import net.kyori.adventure.audience.Audience;
import net.kyori.adventure.builder.AbstractBuilder;
import net.kyori.adventure.text.Component;
import net.kyori.examination.Examinable;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

/**
* Represents a resource pack request that can be sent to players.
*
* @see Audience#sendResourcePack(ResourcePackRequest)
* @since 4.15.0
*/
public interface ResourcePackRequest extends Examinable, ResourcePackRequestLike {
/**
* Creates a resource pack request.
*
* @param uri the uri
* @param hash the sha-1 hash
* @param required whether the resource pack is required or not
* @return the resource pack request
* @since 4.15.0
*/
static @NotNull ResourcePackRequest resourcePackRequest(final @NotNull URI uri, final @NotNull String hash, final boolean required) {
return resourcePackRequest(uri, hash, required, null);
}

/**
* Creates a resource pack request.
*
* @param uri the uri
* @param hash the sha-1 hash
* @param required whether the resource pack is required or not
* @param prompt the prompt
* @return the resource pack request
* @since 4.15.0
*/
static @NotNull ResourcePackRequest resourcePackRequest(final @NotNull URI uri, final @NotNull String hash, final boolean required, final @Nullable Component prompt) {
return new ResourcePackRequestImpl(uri, hash, required, prompt);
}

/**
* Create a new builder that will create a {@link ResourcePackRequest}.
*
* @return a builder
* @since 4.15.0
*/
static @NotNull Builder resourcePackRequest() {
return new ResourcePackRequestImpl.BuilderImpl();
}

/**
* Gets the uri.
*
* @return the uri
* @since 4.15.0
*/
@NotNull URI uri();

/**
* Gets the hash.
*
* @return the hash
* @since 4.15.0
*/
@NotNull String hash();

/**
* Gets whether the resource pack is required
* or not.
*
* @return True if the resource pack is required,
* false otherwise
* @since 4.15.0
*/
boolean required();

/**
* Gets the prompt.
*
* @return the prompt
* @since 4.15.0
*/
@Nullable Component prompt();

@Override
default @NotNull ResourcePackRequest asResourcePackRequest() {
return this;
}

/**
* A builder for resource pack requests.
*
* @since 4.15.0
*/
interface Builder extends AbstractBuilder<ResourcePackRequest>, ResourcePackRequestLike {
/**
* Sets the uri.
*
* @param uri the uri
* @return this builder
* @since 4.15.0
*/
@Contract("_ -> this")
@NotNull Builder uri(final @NotNull URI uri);

/**
* Sets the hash.
*
* @param hash the hash
* @return this builder
* @since 4.15.0
*/
@Contract("_ -> this")
@NotNull Builder hash(final @NotNull String hash);

/**
* Sets whether the resource pack is required or not.
*
* @param required whether the resource pack is required or not
* @return this builder
* @since 4.15.0
*/
@Contract("_ -> this")
@NotNull Builder required(final boolean required);

/**
* Sets the prompt.
*
* @param prompt the prompt
* @return this builder
* @since 4.15.0
*/
@Contract("_ -> this")
@NotNull Builder prompt(final @Nullable Component prompt);

/**
* Builds.
*
* @return a new resource pack request
* @since 4.15.0
*/
@Override
@NotNull ResourcePackRequest build();

@Override
default @NotNull ResourcePackRequest asResourcePackRequest() {
return this.build();
}
}
}
Loading

0 comments on commit 323990f

Please sign in to comment.