-
-
Notifications
You must be signed in to change notification settings - Fork 104
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(api): Add a callback feature for resource pack application
- Loading branch information
Showing
4 changed files
with
283 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
api/src/main/java/net/kyori/adventure/resource/ResourcePackCallback.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/* | ||
* 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.util.UUID; | ||
import java.util.function.BiConsumer; | ||
import net.kyori.adventure.audience.Audience; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
/** | ||
* A callback for a resource pack application operation. | ||
* | ||
* @since 4.15.0 | ||
*/ | ||
@FunctionalInterface | ||
public interface ResourcePackCallback { | ||
/** | ||
* Create a pack callback that will only execute the provided functions when the pack application has completed, discarding all intermediate events. | ||
* | ||
* @param success the success callback | ||
* @param failure the failure callback | ||
* @return the created callback | ||
* @since 4.15.0 | ||
*/ | ||
static @NotNull ResourcePackCallback onTerminal(final @NotNull BiConsumer<UUID, Audience> success, final @NotNull BiConsumer<UUID, Audience> failure) { | ||
return (uuid, status, audience) -> { | ||
if (status == ResourcePackStatus.SUCCESSFULLY_LOADED) { | ||
success.accept(uuid, audience); | ||
} else if (!status.intermediate()) { | ||
failure.accept(uuid, audience); | ||
} | ||
}; | ||
} | ||
|
||
/** | ||
* Called when a pack event has been received. | ||
* | ||
* <p>If the pack apply action was executed on a group audience, {@code audience} will referer to the | ||
* individual member audiences the action is executed on. Forwarding audiences may wrap callbacks to ensure they receive the appropriate wrapped audience.</p> | ||
* | ||
* @param uuid the uuid of the pack that has been applied. | ||
* @param status the current pack status | ||
* @param audience the audience the pack is being applied to | ||
* @since 4.15.0 | ||
*/ | ||
void packEventReceived(final @NotNull UUID uuid, final @NotNull ResourcePackStatus status, final @NotNull Audience audience); | ||
} |
122 changes: 122 additions & 0 deletions
122
api/src/main/java/net/kyori/adventure/resource/ResourcePackStatus.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
/* | ||
* 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; | ||
|
||
/** | ||
* Resource pack application state. | ||
* | ||
* <p>Each status is a phase in the status state machine. | ||
* The client provides this information back to servers as it attempts to download and apply resource packs.</p> | ||
* | ||
* <p>Initial states are {@link #ACCEPTED}, {@link #DECLINED}, or {@link #INVALID_URL}.</p> | ||
* | ||
* @since 4.15.0 | ||
*/ | ||
public enum ResourcePackStatus { | ||
/** | ||
* Indicates that the user has accepted download. | ||
* | ||
* <p>Next states: {@link #FAILED_DOWNLOAD}, {@link #DOWNLOADED}</p> | ||
* | ||
* @since 4.15.0 | ||
*/ | ||
ACCEPTED(true), | ||
/** | ||
* Indicates that the user has declined a pack. | ||
* | ||
* <p>Terminal state.</p> | ||
* | ||
* @since 4.15.0 | ||
*/ | ||
DECLINED(false), | ||
/** | ||
* Indicates that the provided pack URL could not be parsed. | ||
* | ||
* <p>Terminal state.</p> | ||
* | ||
* @since 4.15.0 | ||
* @sinceMinecraft 1.20.3 | ||
*/ | ||
INVALID_URL(false), | ||
/** | ||
* Indicates that the download failed for some other reason. | ||
* | ||
* <p>Terminal state.</p> | ||
* | ||
* @since 4.15.0 | ||
*/ | ||
FAILED_DOWNLOAD(false), | ||
/** | ||
* Indicates that the resource pack has been successfully downloaded. | ||
* | ||
* <p>Next states: {@link #FAILED_RELOAD}, {@link #DISCARDED}, or {@link #SUCCESSFULLY_LOADED}</p> | ||
* | ||
* @since 4.15.0 | ||
* @sinceMinecraft 1.20.3 | ||
*/ | ||
DOWNLOADED(true), | ||
/** | ||
* Indicates that the client's resource manager reload failed. | ||
* | ||
* <p>Terminal state.</p> | ||
* | ||
* @since 4.15.0 | ||
* @sinceMinecraft 1.20.3 | ||
*/ | ||
FAILED_RELOAD(false), | ||
/** | ||
* Indicates that this resource pack did not have issues, but was not applied due to a failure in another server resource pack. | ||
* | ||
* <p>Terminal state.</p> | ||
* | ||
* @since 4.15.0 | ||
* @sinceMinecraft 1.20.3 | ||
*/ | ||
DISCARDED(false), | ||
|
||
/** | ||
* Indicates that the pack has successfully loaded and resource reloading is complete. | ||
* | ||
* <p>Terminal state.</p> | ||
* | ||
* @since 4.15.0 | ||
*/ | ||
SUCCESSFULLY_LOADED(false); | ||
|
||
private final boolean intermediate; | ||
|
||
ResourcePackStatus(final boolean intermediate) { | ||
this.intermediate = intermediate; | ||
} | ||
|
||
/** | ||
* Whether, after receiving this status, further status events might occur. | ||
* | ||
* @return the intermediate status | ||
* @since 4.15.0 | ||
*/ | ||
public boolean intermediate() { | ||
return this.intermediate; | ||
} | ||
} |