1
1
package com .denizenscript .denizen .scripts .commands .player ;
2
2
3
+ import com .denizenscript .denizen .nms .NMSHandler ;
4
+ import com .denizenscript .denizen .nms .NMSVersion ;
3
5
import com .denizenscript .denizen .objects .PlayerTag ;
4
6
import com .denizenscript .denizen .utilities .PaperAPITools ;
5
7
import com .denizenscript .denizen .utilities .Utilities ;
6
8
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 .*;
11
10
import com .denizenscript .denizencore .utilities .debugging .Debug ;
12
11
import com .denizenscript .denizencore .scripts .ScriptEntry ;
13
12
import com .denizenscript .denizencore .scripts .commands .AbstractCommand ;
14
13
14
+ import java .nio .charset .StandardCharsets ;
15
15
import java .util .Collections ;
16
16
import java .util .List ;
17
+ import java .util .UUID ;
17
18
18
19
public class ResourcePackCommand extends AbstractCommand {
19
20
20
21
public ResourcePackCommand () {
21
22
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 );
24
25
isProcedural = false ;
25
26
autoCompile ();
26
27
}
27
28
28
29
// <--[command]
29
30
// @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>|...)
31
32
// @Required 2
32
33
// @Maximum 5
33
34
// @Short Prompts a player to download a server resource pack.
@@ -36,6 +37,9 @@ public ResourcePackCommand() {
36
37
// @Description
37
38
// Sets the current resource pack by specifying a valid URL to a resource pack.
38
39
//
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
+ //
39
43
// The player will be prompted to download the pack, with the optional prompt text or a default vanilla message.
40
44
// 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.
41
45
// Players can change the automatic setting from their server list in the main menu.
@@ -56,12 +60,21 @@ public ResourcePackCommand() {
56
60
// None
57
61
//
58
62
// @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.
60
64
// - resourcepack url:https://example.com/pack.zip hash:0102030405060708090a0b0c0d0e0f1112131415
61
65
//
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
+ //
62
71
// -->
63
72
73
+ public enum Actions {SET , ADD }
74
+
64
75
public static void autoExecute (ScriptEntry scriptEntry ,
76
+ @ ArgName ("action" ) @ ArgDefaultText ("set" ) Actions action ,
77
+ @ ArgName ("id" ) @ ArgPrefixed @ ArgDefaultNull String id ,
65
78
@ ArgName ("url" ) @ ArgPrefixed String url ,
66
79
@ ArgName ("hash" ) @ ArgPrefixed String hash ,
67
80
@ ArgName ("prompt" ) @ ArgPrefixed @ ArgDefaultNull String prompt ,
@@ -77,12 +90,44 @@ public static void autoExecute(ScriptEntry scriptEntry,
77
90
Debug .echoError ("Invalid resource_pack hash. Should be 40 characters of hexadecimal data." );
78
91
return ;
79
92
}
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
+ }
84
130
}
85
- PaperAPITools .instance .sendResourcePack (player .getPlayerEntity (), url , hash , forced , prompt );
86
131
}
87
132
}
88
133
}
0 commit comments