Skip to content

Commit

Permalink
Fixed BaseHeadHook & TextureHeadHook
Browse files Browse the repository at this point in the history
  • Loading branch information
Tanguygab committed Nov 17, 2024
1 parent adb52a1 commit 4b263ca
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.bukkit.Bukkit;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.NotNull;

Expand Down Expand Up @@ -37,8 +36,12 @@ public boolean isItem(@NotNull ItemStack item, @NotNull String... arguments) {
if (arguments.length == 0) {
return false;
}
ItemStack skull = cache.computeIfAbsent(arguments[0], SkullUtils::getSkullByBase64EncodedTextureUrl).clone();
return Bukkit.getItemFactory().equals(item.getItemMeta(), skull.getItemMeta());
String itemTexture = SkullUtils.getTextureFromSkull(item);
String texture = SkullUtils.decodeSkinUrl(arguments[0]);
if (itemTexture == null || texture == null) return false;

texture = texture.substring("https://textures.minecraft.net/texture/".length()-1);
return texture.equals(itemTexture);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.bukkit.Bukkit;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.NotNull;

Expand Down Expand Up @@ -37,8 +36,7 @@ public boolean isItem(@NotNull ItemStack item, @NotNull String... arguments) {
if (arguments.length == 0) {
return false;
}
ItemStack skull = cache.computeIfAbsent(arguments[0], key -> SkullUtils.getSkullByBase64EncodedTextureUrl(SkullUtils.getEncoded(key))).clone();
return Bukkit.getItemFactory().equals(item.getItemMeta(), skull.getItemMeta());
return arguments[0].equals(SkullUtils.getTextureFromSkull(item));
}

@Override
Expand Down
37 changes: 36 additions & 1 deletion src/main/java/com/extendedclip/deluxemenus/utils/SkullUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,41 @@ public static ItemStack getSkullByBase64EncodedTextureUrl(@NotNull final String
return head;
}

public static String getTextureFromSkull(ItemStack item) {
if (!(item.getItemMeta() instanceof SkullMeta)) return null;
SkullMeta meta = (SkullMeta) item.getItemMeta();

if (VersionHelper.HAS_PLAYER_PROFILES) {
PlayerProfile profile = meta.getOwnerProfile();
if (profile == null) return null;

URL url = profile.getTextures().getSkin();
if (url == null) return null;

return url.toString().substring("https://textures.minecraft.net/texture/".length()-1);
}

GameProfile profile;
try {
final Field profileField = meta.getClass().getDeclaredField("profile");
profileField.setAccessible(true);
profile = (GameProfile) profileField.get(meta);
} catch (final NoSuchFieldException | IllegalArgumentException | IllegalAccessException exception) {
DeluxeMenus.printStacktrace(
"Failed to get base64 texture url from head item",
exception
);
return null;
}

for (Property property : profile.getProperties().get("textures")) {
if (property.getName().equals("textures")) {
return decodeSkinUrl(property.getValue());
}
}
return null;
}


/**
* Get the skull from a player name
Expand Down Expand Up @@ -164,7 +199,7 @@ private static PlayerProfile getPlayerProfile(@NotNull final String base64Url) {
* @return the url of the texture if found, otherwise {@code null}
*/
@Nullable
private static String decodeSkinUrl(@NotNull final String base64Texture) {
public static String decodeSkinUrl(@NotNull final String base64Texture) {
final String decoded = new String(Base64.getDecoder().decode(base64Texture));
final JsonObject object = GSON.fromJson(decoded, JsonObject.class);

Expand Down

0 comments on commit 4b263ca

Please sign in to comment.