Skip to content
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

Add ability to use texture url in /skull #5120

Merged
merged 5 commits into from
Aug 8, 2023
Merged
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 @@ -5,34 +5,74 @@
import com.earth2me.essentials.utils.EnumUtil;
import com.earth2me.essentials.utils.MaterialUtil;
import com.google.common.collect.Lists;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import org.bukkit.Material;
import org.bukkit.Server;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.SkullMeta;
import org.bukkit.profile.PlayerProfile;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.Base64;
import java.util.Collections;
import java.util.List;
import java.util.UUID;
import java.util.regex.Pattern;

import static com.earth2me.essentials.I18n.tl;

public class Commandskull extends EssentialsCommand {

private static final Pattern NAME_PATTERN = Pattern.compile("^[A-Za-z0-9_]+$");
private static final Pattern URL_VALUE_PATTERN = Pattern.compile("^[0-9a-fA-F]{64}$");
private static final Pattern BASE_64_PATTERN = Pattern.compile("^[A-Za-z0-9+/=]{180}$");

private static final Material SKULL_ITEM = EnumUtil.getMaterial("PLAYER_HEAD", "SKULL_ITEM");

private final boolean playerProfileSupported;

public Commandskull() {
super("skull");

// The player profile API is only available in newer versions of Spigot 1.18.1 and above
boolean playerProfileSupported = true;
try {
Class.forName("org.bukkit.profile.PlayerProfile");
} catch (final ClassNotFoundException e) {
playerProfileSupported = false;
}
this.playerProfileSupported = playerProfileSupported;
}

@Override
protected void run(final Server server, final User user, final String commandLabel, final String[] args) throws Exception {
final String owner;
if (args.length > 0 && user.isAuthorized("essentials.skull.others")) {
if (!NAME_PATTERN.matcher(args[0]).matches()) {
if (BASE_64_PATTERN.matcher(args[0]).matches()) {
try {
final String decoded = new String(Base64.getDecoder().decode(args[0]));
final JsonObject jsonObject = JsonParser.parseString(decoded).getAsJsonObject();
final String url = jsonObject
.getAsJsonObject("textures")
.getAsJsonObject("SKIN")
.get("url")
.getAsString();
owner = url.substring(url.lastIndexOf("/") + 1);
} catch (final Exception e) {
// Any exception that can realistically happen here is caused by an invalid texture value
throw new IllegalArgumentException(tl("skullInvalidBase64"));
}

if (!URL_VALUE_PATTERN.matcher(owner).matches()) {
throw new IllegalArgumentException(tl("skullInvalidBase64"));
}
} else if (!NAME_PATTERN.matcher(args[0]).matches()) {
throw new IllegalArgumentException(tl("alphaNames"));
} else {
owner = args[0];
}
owner = args[0];
} else {
owner = user.getName();
}
Expand Down Expand Up @@ -60,18 +100,43 @@ protected void run(final Server server, final User user, final String commandLab

private void editSkull(final User user, final ItemStack stack, final SkullMeta skullMeta, final String owner, final boolean spawn) {
ess.runTaskAsynchronously(() -> {
//Run this stuff async because SkullMeta#setOwner causes a http request.
skullMeta.setDisplayName("§fSkull of " + owner);
//noinspection deprecation
skullMeta.setOwner(owner);
// Run this stuff async because it causes an HTTP request

final String shortOwnerName;
if (URL_VALUE_PATTERN.matcher(owner).matches()) {
if (!playerProfileSupported) {
user.sendMessage(tl("unsupportedFeature"));
return;
}

final URL url;
try {
url = new URL("https://textures.minecraft.net/texture/" + owner);
} catch (final MalformedURLException e) {
// The URL should never be malformed
throw new RuntimeException(e);
}

final PlayerProfile profile = ess.getServer().createPlayerProfile(UUID.randomUUID());
profile.getTextures().setSkin(url);
skullMeta.setOwnerProfile(profile);

shortOwnerName = owner.substring(0, 7);
} else {
//noinspection deprecation
skullMeta.setOwner(owner);
shortOwnerName = owner;
}
skullMeta.setDisplayName("§fSkull of " + shortOwnerName);

ess.scheduleSyncDelayedTask(() -> {
stack.setItemMeta(skullMeta);
if (spawn) {
Inventories.addItem(user.getBase(), stack);
user.sendMessage(tl("givenSkull", owner));
user.sendMessage(tl("givenSkull", shortOwnerName));
return;
}
user.sendMessage(tl("skullChanged", owner));
user.sendMessage(tl("skullChanged", shortOwnerName));
});
});
}
Expand Down
3 changes: 3 additions & 0 deletions Essentials/src/main/resources/messages.properties
Original file line number Diff line number Diff line change
Expand Up @@ -1208,6 +1208,9 @@ skullCommandUsage1=/<command>
skullCommandUsage1Description=Gets your own skull
skullCommandUsage2=/<command> <player>
skullCommandUsage2Description=Gets the skull of the specified player
skullCommandUsage3=/<command> <texture>
skullCommandUsage3Description=Gets a skull with the specified texture (either the hash from a texture URL or a Base64 texture value)
skullInvalidBase64=\u00a74The texture value is invalid.
slimeMalformedSize=\u00a74Malformed size.
smithingtableCommandDescription=Opens up a smithing table.
smithingtableCommandUsage=/<command>
Expand Down