Skip to content

Commit 2f2af7f

Browse files
committed
Support adding/removing multiple resource packs
1 parent a7d9c48 commit 2f2af7f

File tree

4 files changed

+108
-18
lines changed

4 files changed

+108
-18
lines changed

paper/src/main/java/com/denizenscript/denizen/paper/utilities/PaperAPIToolsImpl.java

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
import org.bukkit.scoreboard.Team;
4141
import org.bukkit.util.Consumer;
4242

43+
import java.nio.charset.StandardCharsets;
4344
import java.util.*;
4445
import java.util.function.Predicate;
4546
import java.util.stream.Collectors;
@@ -109,13 +110,22 @@ public void setSignLine(Sign sign, int line, String text) {
109110
}
110111

111112
@Override
112-
public void sendResourcePack(Player player, String url, String hash, boolean forced, String prompt) {
113-
if (prompt == null && !forced) {
114-
super.sendResourcePack(player, url, hash, false, null);
113+
public void setResourcePack(Player player, String url, String hash, boolean forced, String prompt, String id) {
114+
if (prompt == null && !forced && id == null) {
115+
super.setResourcePack(player, url, hash, false, null, null);
115116
}
116-
else {
117+
else if (id == null) {
117118
player.setResourcePack(url, CoreUtilities.toLowerCase(hash), forced, PaperModule.parseFormattedText(prompt, ChatColor.WHITE));
118119
}
120+
else {
121+
UUID packUUID;
122+
try {
123+
packUUID = UUID.fromString(id);
124+
} catch (IllegalArgumentException e) {
125+
packUUID = UUID.nameUUIDFromBytes(id.getBytes(StandardCharsets.UTF_8));
126+
}
127+
player.setResourcePack(packUUID, url, CoreUtilities.toLowerCase(hash), PaperModule.parseFormattedText(prompt, ChatColor.WHITE), forced);
128+
}
119129
}
120130

121131
@Override

plugin/src/main/java/com/denizenscript/denizen/objects/PlayerTag.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
import org.bukkit.scoreboard.Team;
5757
import org.bukkit.util.RayTraceResult;
5858

59+
import java.nio.charset.StandardCharsets;
5960
import java.util.*;
6061

6162
public class PlayerTag implements ObjectTag, Adjustable, EntityFormObject, FlaggableObject {
@@ -2607,6 +2608,40 @@ else if (foodLevel / maxHunger < 1) {
26072608
});
26082609
}
26092610

2611+
if (NMSHandler.getVersion().isAtLeast(NMSVersion.v1_20)) {
2612+
2613+
// <--[mechanism]
2614+
// @object PlayerTag
2615+
// @name remove_resource_pack
2616+
// @input ElementTag
2617+
// @description
2618+
// Removes a server resource pack from a player by id.
2619+
// To apply a resource pack, use <@link command resourcepack>.
2620+
// -->
2621+
registerOnlineOnlyMechanism("remove_resource_pack", ElementTag.class, (object, mechanism, input) -> {
2622+
UUID packUUID;
2623+
try {
2624+
packUUID = UUID.fromString(input.asString());
2625+
}
2626+
catch (IllegalArgumentException e) {
2627+
packUUID = UUID.nameUUIDFromBytes(input.asString().getBytes(StandardCharsets.UTF_8));
2628+
}
2629+
object.getPlayerEntity().removeResourcePack(packUUID);
2630+
});
2631+
2632+
// <--[mechanism]
2633+
// @object PlayerTag
2634+
// @name remove_resource_packs
2635+
// @input None
2636+
// @description
2637+
// Removes all server resource packs from a player.
2638+
// To apply a resource pack, use <@link command resourcepack>.
2639+
// -->
2640+
registerOnlineOnlyMechanism("remove_resource_packs", (object, mechanism) -> {
2641+
object.getPlayerEntity().removeResourcePacks();
2642+
});
2643+
}
2644+
26102645
// <--[mechanism]
26112646
// @object PlayerTag
26122647
// @name refresh_player

plugin/src/main/java/com/denizenscript/denizen/scripts/commands/player/ResourcePackCommand.java

Lines changed: 58 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,34 @@
11
package com.denizenscript.denizen.scripts.commands.player;
22

3+
import com.denizenscript.denizen.nms.NMSHandler;
4+
import com.denizenscript.denizen.nms.NMSVersion;
35
import com.denizenscript.denizen.objects.PlayerTag;
46
import com.denizenscript.denizen.utilities.PaperAPITools;
57
import com.denizenscript.denizen.utilities.Utilities;
68
import com.denizenscript.denizencore.exceptions.InvalidArgumentsRuntimeException;
7-
import com.denizenscript.denizencore.scripts.commands.generator.ArgDefaultNull;
8-
import com.denizenscript.denizencore.scripts.commands.generator.ArgName;
9-
import com.denizenscript.denizencore.scripts.commands.generator.ArgPrefixed;
10-
import com.denizenscript.denizencore.scripts.commands.generator.ArgSubType;
9+
import com.denizenscript.denizencore.scripts.commands.generator.*;
1110
import com.denizenscript.denizencore.utilities.debugging.Debug;
1211
import com.denizenscript.denizencore.scripts.ScriptEntry;
1312
import com.denizenscript.denizencore.scripts.commands.AbstractCommand;
1413

14+
import java.nio.charset.StandardCharsets;
1515
import java.util.Collections;
1616
import java.util.List;
17+
import java.util.UUID;
1718

1819
public class ResourcePackCommand extends AbstractCommand {
1920

2021
public ResourcePackCommand() {
2122
setName("resourcepack");
22-
setSyntax("resourcepack [url:<url>] [hash:<hash>] (forced) (prompt:<text>) (targets:<player>|...)");
23-
setRequiredArguments(2, 5);
23+
setSyntax("resourcepack ({set}/add) (id:<id>) [url:<url>] [hash:<hash>] (forced) (prompt:<text>) (targets:<player>|...)");
24+
setRequiredArguments(2, 7);
2425
isProcedural = false;
2526
autoCompile();
2627
}
2728

2829
// <--[command]
2930
// @Name ResourcePack
30-
// @Syntax resourcepack [url:<url>] [hash:<hash>] (forced) (prompt:<text>) (targets:<player>|...)
31+
// @Syntax resourcepack ({set}/add) (id:<id>) [url:<url>] [hash:<hash>] (forced) (prompt:<text>) (targets:<player>|...)
3132
// @Required 2
3233
// @Maximum 5
3334
// @Short Prompts a player to download a server resource pack.
@@ -36,6 +37,9 @@ public ResourcePackCommand() {
3637
// @Description
3738
// Sets the current resource pack by specifying a valid URL to a resource pack.
3839
//
40+
// Optionally, you can send the player additional resource packs by using the "add" argument.
41+
// The "id" argument allows you to overwrite a specific resource pack or remove one via <@link mechanism PlayerTag.remove_resource_pack>.
42+
//
3943
// The player will be prompted to download the pack, with the optional prompt text or a default vanilla message.
4044
// 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.
4145
// Players can change the automatic setting from their server list in the main menu.
@@ -56,12 +60,21 @@ public ResourcePackCommand() {
5660
// None
5761
//
5862
// @Usage
59-
// Use to send a resource pack with a pre-known hash.
63+
// Use to set a resource pack with a pre-known hash.
6064
// - resourcepack url:https://example.com/pack.zip hash:0102030405060708090a0b0c0d0e0f1112131415
6165
//
66+
// @Usage
67+
// Use to send multiple resource packs to a player.
68+
// - resourcepack add id:first_pack url:https://example.com/pack1.zip hash:0102030405060708090a0b0c0d0e0f1112131415
69+
// - resourcepack add id:second_pack url:https://example.com/pack2.zip hash:0102030405060708090a0b0c0d0e0f1112131415
70+
//
6271
// -->
6372

73+
public enum Actions {SET, ADD}
74+
6475
public static void autoExecute(ScriptEntry scriptEntry,
76+
@ArgName("action") @ArgDefaultText("set") Actions action,
77+
@ArgName("id") @ArgPrefixed @ArgDefaultNull String id,
6578
@ArgName("url") @ArgPrefixed String url,
6679
@ArgName("hash") @ArgPrefixed String hash,
6780
@ArgName("prompt") @ArgPrefixed @ArgDefaultNull String prompt,
@@ -77,12 +90,44 @@ public static void autoExecute(ScriptEntry scriptEntry,
7790
Debug.echoError("Invalid resource_pack hash. Should be 40 characters of hexadecimal data.");
7891
return;
7992
}
80-
for (PlayerTag player : targets) {
81-
if (!player.isOnline()) {
82-
Debug.echoDebug(scriptEntry, "Player is offline, can't send resource pack to them. Skipping.");
83-
continue;
93+
if (!NMSHandler.getVersion().isAtLeast(NMSVersion.v1_20) && (action == Actions.ADD || id != null)) {
94+
throw new UnsupportedOperationException();
95+
}
96+
switch (action) {
97+
case SET -> {
98+
for (PlayerTag player : targets) {
99+
if (!player.isOnline()) {
100+
Debug.echoDebug(scriptEntry, "Player is offline, can't send resource pack to them. Skipping.");
101+
continue;
102+
}
103+
PaperAPITools.instance.setResourcePack(player.getPlayerEntity(), url, hash, forced, prompt, id);
104+
}
105+
}
106+
case ADD -> {
107+
byte[] hashData = new byte[20];
108+
for (int i = 0; i < 20; i++) {
109+
hashData[i] = (byte) Integer.parseInt(hash.substring(i * 2, i * 2 + 2), 16);
110+
}
111+
UUID packUUID;
112+
if (id == null) {
113+
packUUID = UUID.nameUUIDFromBytes(url.getBytes(StandardCharsets.UTF_8));
114+
}
115+
else {
116+
try {
117+
packUUID = UUID.fromString(id);
118+
}
119+
catch (IllegalArgumentException e) {
120+
packUUID = UUID.nameUUIDFromBytes(id.getBytes(StandardCharsets.UTF_8));
121+
}
122+
}
123+
for (PlayerTag player : targets) {
124+
if (!player.isOnline()) {
125+
Debug.echoDebug(scriptEntry, "Player is offline, can't send resource pack to them. Skipping.");
126+
continue;
127+
}
128+
player.getPlayerEntity().addResourcePack(packUUID, url, hashData, prompt, forced);
129+
}
84130
}
85-
PaperAPITools.instance.sendResourcePack(player.getPlayerEntity(), url, hash, forced, prompt);
86131
}
87132
}
88133
}

plugin/src/main/java/com/denizenscript/denizen/utilities/PaperAPITools.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public void setSignLine(Sign sign, int line, String text) {
8787
sign.setLine(line, text == null ? "" : text);
8888
}
8989

90-
public void sendResourcePack(Player player, String url, String hash, boolean forced, String prompt) {
90+
public void setResourcePack(Player player, String url, String hash, boolean forced, String prompt, String id) {
9191
byte[] hashData = new byte[20];
9292
for (int i = 0; i < 20; i++) {
9393
hashData[i] = (byte) Integer.parseInt(hash.substring(i * 2, i * 2 + 2), 16);

0 commit comments

Comments
 (0)