-
-
Notifications
You must be signed in to change notification settings - Fork 112
Support for adding multiple resource packs #2791
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
davight
wants to merge
4
commits into
DenizenScript:dev
Choose a base branch
from
davight:resourcepack_update
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+97
−21
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 |
|---|---|---|
| @@ -1,33 +1,34 @@ | ||
| package com.denizenscript.denizen.scripts.commands.player; | ||
|
|
||
| import com.denizenscript.denizen.nms.NMSHandler; | ||
| import com.denizenscript.denizen.nms.NMSVersion; | ||
| import com.denizenscript.denizen.objects.PlayerTag; | ||
| import com.denizenscript.denizen.utilities.PaperAPITools; | ||
| import com.denizenscript.denizen.utilities.Utilities; | ||
| import com.denizenscript.denizencore.exceptions.InvalidArgumentsRuntimeException; | ||
| import com.denizenscript.denizencore.scripts.commands.generator.ArgDefaultNull; | ||
| import com.denizenscript.denizencore.scripts.commands.generator.ArgName; | ||
| import com.denizenscript.denizencore.scripts.commands.generator.ArgPrefixed; | ||
| import com.denizenscript.denizencore.scripts.commands.generator.ArgSubType; | ||
| import com.denizenscript.denizencore.scripts.commands.generator.*; | ||
| import com.denizenscript.denizencore.utilities.debugging.Debug; | ||
| import com.denizenscript.denizencore.scripts.ScriptEntry; | ||
| import com.denizenscript.denizencore.scripts.commands.AbstractCommand; | ||
|
|
||
| import java.nio.charset.StandardCharsets; | ||
| import java.util.Collections; | ||
| import java.util.List; | ||
| import java.util.UUID; | ||
|
|
||
| public class ResourcePackCommand extends AbstractCommand { | ||
|
|
||
| public ResourcePackCommand() { | ||
| setName("resourcepack"); | ||
| setSyntax("resourcepack [url:<url>] [hash:<hash>] (forced) (prompt:<text>) (targets:<player>|...)"); | ||
| setRequiredArguments(2, 5); | ||
| setSyntax("resourcepack ({set}/add/remove) (id:<id>) (url:<url>) (hash:<hash>) (forced) (prompt:<text>) (targets:<player>|...)"); | ||
| setRequiredArguments(1, 7); | ||
| isProcedural = false; | ||
| autoCompile(); | ||
| } | ||
|
|
||
| // <--[command] | ||
| // @Name ResourcePack | ||
| // @Syntax resourcepack [url:<url>] [hash:<hash>] (forced) (prompt:<text>) (targets:<player>|...) | ||
| // @Syntax resourcepack ({set}/add/remove) (id:<id>) (url:<url>) (hash:<hash>) (forced) (prompt:<text>) (targets:<player>|...) | ||
| // @Required 2 | ||
| // @Maximum 5 | ||
| // @Short Prompts a player to download a server resource pack. | ||
|
|
@@ -36,6 +37,9 @@ public ResourcePackCommand() { | |
| // @Description | ||
| // Sets the current resource pack by specifying a valid URL to a resource pack. | ||
| // | ||
| // Optionally, you can send the player additional resource packs by using the "add" argument. | ||
| // The "id" argument allows you to overwrite a specific resource pack or remove one with "remove" argument. | ||
| // | ||
| // The player will be prompted to download the pack, with the optional prompt text or a default vanilla message. | ||
| // Once a player says "yes" once, all future packs will be automatically downloaded. If the player selects "no" once, all future packs will automatically be rejected. | ||
| // Players can change the automatic setting from their server list in the main menu. | ||
|
|
@@ -56,33 +60,101 @@ public ResourcePackCommand() { | |
| // None | ||
| // | ||
| // @Usage | ||
| // Use to send a resource pack with a pre-known hash. | ||
| // Use to set a resource pack with a pre-known hash. | ||
| // - resourcepack url:https://example.com/pack.zip hash:0102030405060708090a0b0c0d0e0f1112131415 | ||
| // | ||
| // @Usage | ||
| // Use to send multiple resource packs to a player. | ||
| // - resourcepack add id:first_pack url:https://example.com/pack1.zip hash:0102030405060708090a0b0c0d0e0f1112131415 | ||
| // - resourcepack add id:second_pack url:https://example.com/pack2.zip hash:0102030405060708090a0b0c0d0e0f1112131415 | ||
| // | ||
| // @Usage | ||
| // Use to remove all resource packs from all online players. | ||
| // - resourcepack remove targets:<server.online_players> | ||
| // --> | ||
|
|
||
| public enum Action { SET, ADD, REMOVE } | ||
|
|
||
| public static void autoExecute(ScriptEntry scriptEntry, | ||
| @ArgName("url") @ArgPrefixed String url, | ||
| @ArgName("hash") @ArgPrefixed String hash, | ||
| @ArgName("action") @ArgDefaultText("set") Action action, | ||
| @ArgName("id") @ArgPrefixed @ArgDefaultNull String id, | ||
| @ArgName("url") @ArgPrefixed @ArgDefaultNull String url, | ||
| @ArgName("hash") @ArgPrefixed @ArgDefaultNull String hash, | ||
| @ArgName("prompt") @ArgPrefixed @ArgDefaultNull String prompt, | ||
| @ArgName("targets") @ArgPrefixed @ArgDefaultNull @ArgSubType(PlayerTag.class) List<PlayerTag> targets, | ||
| @ArgName("forced") boolean forced) { | ||
| if (!NMSHandler.getVersion().isAtLeast(NMSVersion.v1_20) && (action == Action.ADD || id != null)) { | ||
| throw new UnsupportedOperationException("Adding multiple resource packs is not supported on this server version!"); | ||
| } | ||
| if (targets == null) { | ||
| if (!Utilities.entryHasPlayer(scriptEntry)) { | ||
| throw new InvalidArgumentsRuntimeException("Must specify an online player!"); | ||
| } | ||
| targets = Collections.singletonList(Utilities.getEntryPlayer(scriptEntry)); | ||
| } | ||
| if (hash.length() != 40) { | ||
| if ((action == Action.ADD || action == Action.SET) && (url == null || hash == null)) { | ||
| throw new InvalidArgumentsRuntimeException("Must specify both a resource pack URL and hash!"); | ||
| } | ||
| if ((action == Action.ADD || action == Action.SET) && hash.length() != 40) { | ||
| Debug.echoError("Invalid resource_pack hash. Should be 40 characters of hexadecimal data."); | ||
| return; | ||
| } | ||
| for (PlayerTag player : targets) { | ||
| if (!player.isOnline()) { | ||
| Debug.echoDebug(scriptEntry, "Player is offline, can't send resource pack to them. Skipping."); | ||
| continue; | ||
| switch (action) { | ||
| case SET -> { | ||
| for (PlayerTag player : targets) { | ||
| if (checkOnline(player)) { | ||
| PaperAPITools.instance.setResourcePack(player.getPlayerEntity(), url, hash, forced, prompt, id); | ||
| } | ||
| } | ||
| } | ||
| case ADD -> { | ||
| byte[] hashData = new byte[20]; | ||
| for (int i = 0; i < 20; i++) { | ||
| hashData[i] = (byte) Integer.parseInt(hash.substring(i * 2, i * 2 + 2), 16); | ||
| } | ||
| UUID packUUID = id == null ? UUID.nameUUIDFromBytes(url.getBytes(StandardCharsets.UTF_8)) : parseUUID(id); | ||
| for (PlayerTag player : targets) { | ||
| if (checkOnline(player)) { | ||
| player.getPlayerEntity().addResourcePack(packUUID, url, hashData, prompt, forced); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This needs a paper impl as well, otherwise some parts of the command support advanced text and some wont which might be a bit confusing |
||
| } | ||
| } | ||
| } | ||
| PaperAPITools.instance.sendResourcePack(player.getPlayerEntity(), url, hash, forced, prompt); | ||
| case REMOVE -> { | ||
| if (id == null) { | ||
| for (PlayerTag player : targets) { | ||
| if (checkOnline(player)) { | ||
| player.getPlayerEntity().removeResourcePacks(); | ||
| } | ||
| } | ||
| } | ||
| else { | ||
| UUID packUUID = parseUUID(id); | ||
| for (PlayerTag player : targets) { | ||
| if (checkOnline(player)) { | ||
| player.getPlayerEntity().removeResourcePack(packUUID); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| public static boolean checkOnline(PlayerTag player) { | ||
| if (!player.isOnline()) { | ||
| Debug.echoError("Invalid player '" + player.getName() + "' specified: must be online"); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. . |
||
| return false; | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| public static UUID parseUUID(String id) { | ||
| UUID uuid; | ||
| try { | ||
| uuid = UUID.fromString(id); | ||
| } | ||
| catch (IllegalArgumentException ex) { | ||
| uuid = UUID.nameUUIDFromBytes(id.getBytes(StandardCharsets.UTF_8)); | ||
| } | ||
| return uuid; | ||
| } | ||
| } | ||
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You parse
idinto aUUIDmultiple times across the code, I'd just do it once before the whole switch and then pass it here as aUUIDalready.