Skip to content

Support adding/removing multiple resource packs #2712

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
wants to merge 2 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import org.bukkit.scoreboard.Team;
import org.bukkit.util.Consumer;

import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.function.Predicate;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -109,13 +110,23 @@ public void setSignLine(Sign sign, int line, String text) {
}

@Override
public void sendResourcePack(Player player, String url, String hash, boolean forced, String prompt) {
if (prompt == null && !forced) {
super.sendResourcePack(player, url, hash, false, null);
public void setResourcePack(Player player, String url, String hash, boolean forced, String prompt, String id) {
if (prompt == null && !forced && id == null) {
super.setResourcePack(player, url, hash, false, null, null);
}
else {
else if (id == null) {
player.setResourcePack(url, CoreUtilities.toLowerCase(hash), forced, PaperModule.parseFormattedText(prompt, ChatColor.WHITE));
}
else {
UUID packUUID;
try {
packUUID = UUID.fromString(id);
}
catch (IllegalArgumentException e) {
packUUID = UUID.nameUUIDFromBytes(id.getBytes(StandardCharsets.UTF_8));
}
Comment on lines +122 to +127
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd probably put this in a util method on ResourcePackCommand, just because it's repeated a couple times

player.setResourcePack(packUUID, url, CoreUtilities.toLowerCase(hash), PaperModule.parseFormattedText(prompt, ChatColor.WHITE), forced);
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
import org.bukkit.scoreboard.Team;
import org.bukkit.util.RayTraceResult;

import java.nio.charset.StandardCharsets;
import java.util.*;

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

if (NMSHandler.getVersion().isAtLeast(NMSVersion.v1_20)) {

// <--[mechanism]
// @object PlayerTag
// @name remove_resource_pack
// @input ElementTag
// @description
// Removes a server resource pack from a player by id.
// To apply a resource pack, use <@link command resourcepack>.
// -->
registerOnlineOnlyMechanism("remove_resource_pack", ElementTag.class, (object, mechanism, input) -> {
UUID packUUID;
try {
packUUID = UUID.fromString(input.asString());
}
catch (IllegalArgumentException e) {
packUUID = UUID.nameUUIDFromBytes(input.asString().getBytes(StandardCharsets.UTF_8));
}
object.getPlayerEntity().removeResourcePack(packUUID);
});

// <--[mechanism]
// @object PlayerTag
// @name remove_resource_packs
// @input None
// @description
// Removes all server resource packs from a player.
// To apply a resource pack, use <@link command resourcepack>.
// -->
registerOnlineOnlyMechanism("remove_resource_packs", (object, mechanism) -> {
object.getPlayerEntity().removeResourcePacks();
});
}

// <--[mechanism]
// @object PlayerTag
// @name refresh_player
Expand Down
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) (id:<id>) [url:<url>] [hash:<hash>] (forced) (prompt:<text>) (targets:<player>|...)");
setRequiredArguments(2, 7);
isProcedural = false;
autoCompile();
}

// <--[command]
// @Name ResourcePack
// @Syntax resourcepack [url:<url>] [hash:<hash>] (forced) (prompt:<text>) (targets:<player>|...)
// @Syntax resourcepack ({set}/add) (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.
Expand All @@ -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 via <@link mechanism PlayerTag.remove_resource_pack>.
//
// 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.
Expand All @@ -56,12 +60,21 @@ 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
//
// -->

public enum Actions {SET, ADD}

public static void autoExecute(ScriptEntry scriptEntry,
@ArgName("action") @ArgDefaultText("set") Actions action,
@ArgName("id") @ArgPrefixed @ArgDefaultNull String id,
@ArgName("url") @ArgPrefixed String url,
@ArgName("hash") @ArgPrefixed String hash,
@ArgName("prompt") @ArgPrefixed @ArgDefaultNull String prompt,
Expand All @@ -77,12 +90,44 @@ public static void autoExecute(ScriptEntry scriptEntry,
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;
if (!NMSHandler.getVersion().isAtLeast(NMSVersion.v1_20) && (action == Actions.ADD || id != null)) {
throw new UnsupportedOperationException();
}
switch (action) {
case SET -> {
for (PlayerTag player : targets) {
if (!player.isOnline()) {
Debug.echoDebug(scriptEntry, "Player is offline, can't send resource pack to them. Skipping.");
continue;
}
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;
if (id == null) {
packUUID = UUID.nameUUIDFromBytes(url.getBytes(StandardCharsets.UTF_8));
}
else {
try {
packUUID = UUID.fromString(id);
}
catch (IllegalArgumentException e) {
packUUID = UUID.nameUUIDFromBytes(id.getBytes(StandardCharsets.UTF_8));
}
}
for (PlayerTag player : targets) {
if (!player.isOnline()) {
Debug.echoDebug(scriptEntry, "Player is offline, can't send resource pack to them. Skipping.");
continue;
}
player.getPlayerEntity().addResourcePack(packUUID, url, hashData, prompt, forced);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using Spigot API here vs Paper API for setting means that set allows advanced text stuff in the prompt and add doesn't - might be better to use Paper API here as well for consistency?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There isn't a paper api version of this that I saw, other than the Audience stuff. Can you link me what you're referencing?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't think Paper has a direct alternative to the Spigot method? but you can just use adventure, untested but should be something like this:

ResourcePackRequest.Builder builder = ResourcePackRequest.resourcePackRequest();
builder.packs(ResourcePackInfo.resourcePackInfo(UUID.fromString("123"), URI.create("https://www.pack.com/pack.zip"), "hash-13th0aing"));
builder.prompt(Component.text("Use pack pls").color(NamedTextColor.AQUA));
builder.replace(false);
builder.required(true);
player.sendResourcePacks(builder);

}
}
PaperAPITools.instance.sendResourcePack(player.getPlayerEntity(), url, hash, forced, prompt);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public void setSignLine(Sign sign, int line, String text) {
sign.setLine(line, text == null ? "" : text);
}

public void sendResourcePack(Player player, String url, String hash, boolean forced, String prompt) {
public void setResourcePack(Player player, String url, String hash, boolean forced, String prompt, String id) {
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);
Expand Down