From cc919bf976151c6b30219133591f57b16547b709 Mon Sep 17 00:00:00 2001 From: Bukkit/Spigot Date: Sat, 31 Aug 2024 08:57:51 +1000 Subject: [PATCH] SPIGOT-7890: AttributeModifier#getUniqueId() doesn't match the UUID passed to its constructor By: md_5 --- .../bukkit/attribute/AttributeModifier.java | 23 ++++++++++++++----- 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/paper-api/src/main/java/org/bukkit/attribute/AttributeModifier.java b/paper-api/src/main/java/org/bukkit/attribute/AttributeModifier.java index 2a7ac892c2..6ffc895138 100644 --- a/paper-api/src/main/java/org/bukkit/attribute/AttributeModifier.java +++ b/paper-api/src/main/java/org/bukkit/attribute/AttributeModifier.java @@ -7,6 +7,7 @@ import java.util.Locale; import java.util.Map; import java.util.Objects; import java.util.UUID; +import java.util.regex.Pattern; import org.bukkit.Keyed; import org.bukkit.NamespacedKey; import org.bukkit.configuration.serialization.ConfigurationSerializable; @@ -21,27 +22,28 @@ import org.jetbrains.annotations.Nullable; */ public class AttributeModifier implements ConfigurationSerializable, Keyed { + private static final Pattern UUID_PATTERN = Pattern.compile("^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$"); private final NamespacedKey key; private final double amount; private final Operation operation; private final EquipmentSlotGroup slot; - @Deprecated + @Deprecated(since = "1.21", forRemoval = true) public AttributeModifier(@NotNull String name, double amount, @NotNull Operation operation) { this(UUID.randomUUID(), name, amount, operation); } - @Deprecated + @Deprecated(since = "1.21", forRemoval = true) public AttributeModifier(@NotNull UUID uuid, @NotNull String name, double amount, @NotNull Operation operation) { this(uuid, name, amount, operation, (EquipmentSlot) null); } - @Deprecated + @Deprecated(since = "1.21", forRemoval = true) public AttributeModifier(@NotNull UUID uuid, @NotNull String name, double amount, @NotNull Operation operation, @Nullable EquipmentSlot slot) { this(uuid, name, amount, operation, (slot) == null ? EquipmentSlotGroup.ANY : slot.getGroup()); } - @Deprecated + @Deprecated(since = "1.21", forRemoval = true) public AttributeModifier(@NotNull UUID uuid, @NotNull String name, double amount, @NotNull Operation operation, @NotNull EquipmentSlotGroup slot) { this(NamespacedKey.fromString(uuid.toString()), amount, operation, slot); } @@ -64,9 +66,18 @@ public class AttributeModifier implements ConfigurationSerializable, Keyed { * @deprecated attributes are now identified by keys */ @NotNull - @Deprecated + @Deprecated(since = "1.21", forRemoval = true) public UUID getUniqueId() { - return UUID.nameUUIDFromBytes(getKey().toString().getBytes(StandardCharsets.UTF_8)); + NamespacedKey namespacedKey = getKey(); + if (namespacedKey.getNamespace().equals(NamespacedKey.MINECRAFT)) { + String key = namespacedKey.getKey(); + + if (key.length() == 36 && UUID_PATTERN.matcher(key).matches()) { + return UUID.fromString(key); + } + } + + return UUID.nameUUIDFromBytes(namespacedKey.toString().getBytes(StandardCharsets.UTF_8)); } @NotNull