Commits

DerFrZocker authored and md_5 committed 9973ccccfdd
#1519: Add RegistryAware to indicate better that not all registry items are registered
No tags

src/main/java/org/bukkit/craftbukkit/attribute/CraftAttribute.java

Modified
4 4 import java.util.Locale;
5 5 import net.minecraft.core.Holder;
6 6 import net.minecraft.core.IRegistry;
7 7 import net.minecraft.core.registries.Registries;
8 8 import net.minecraft.world.entity.ai.attributes.AttributeBase;
9 9 import org.bukkit.NamespacedKey;
10 10 import org.bukkit.Registry;
11 11 import org.bukkit.attribute.Attribute;
12 12 import org.bukkit.craftbukkit.CraftRegistry;
13 13 import org.bukkit.craftbukkit.legacy.FieldRename;
14 +import org.bukkit.craftbukkit.registry.CraftOldEnumRegistryItem;
14 15 import org.bukkit.craftbukkit.util.ApiVersion;
15 -import org.bukkit.craftbukkit.util.Handleable;
16 16 import org.jetbrains.annotations.NotNull;
17 17
18 -public class CraftAttribute implements Attribute, Handleable<AttributeBase> {
18 +public class CraftAttribute extends CraftOldEnumRegistryItem<Attribute, AttributeBase> implements Attribute {
19 19
20 20 private static int count = 0;
21 21
22 22 public static Attribute minecraftToBukkit(AttributeBase minecraft) {
23 23 return CraftRegistry.minecraftToBukkit(minecraft, Registries.ATTRIBUTE, Registry.ATTRIBUTE);
24 24 }
25 25
26 26 public static Attribute minecraftHolderToBukkit(Holder<AttributeBase> minecraft) {
27 27 return minecraftToBukkit(minecraft.value());
28 28 }
56 56 throw new IllegalArgumentException("No Reference holder found for " + bukkit
57 57 + ", this can happen if a plugin creates its own sound effect with out properly registering it.");
58 58 }
59 59
60 60 public static String bukkitToString(Attribute bukkit) {
61 61 Preconditions.checkArgument(bukkit != null);
62 62
63 63 return bukkit.getKey().toString();
64 64 }
65 65
66 - private final NamespacedKey key;
67 - private final AttributeBase attributeBase;
68 - private final String name;
69 - private final int ordinal;
70 -
71 - public CraftAttribute(NamespacedKey key, AttributeBase attributeBase) {
72 - this.key = key;
73 - this.attributeBase = attributeBase;
74 - // For backwards compatibility, minecraft values will stile return the uppercase name without the namespace,
75 - // in case plugins use for example the name as key in a config file to receive attribute specific values.
76 - // Custom attributes will return the key with namespace. For a plugin this should look than like a new attribute
77 - // (which can always be added in new minecraft versions and the plugin should therefore handle it accordingly).
78 - if (NamespacedKey.MINECRAFT.equals(key.getNamespace())) {
79 - this.name = key.getKey().toUpperCase(Locale.ROOT);
80 - } else {
81 - this.name = key.toString();
82 - }
83 - this.ordinal = count++;
84 - }
85 -
86 - @Override
87 - public AttributeBase getHandle() {
88 - return attributeBase;
66 + public CraftAttribute(NamespacedKey key, Holder<AttributeBase> handle) {
67 + super(key, handle, count++);
89 68 }
90 69
91 70 @NotNull
92 71 @Override
93 72 public NamespacedKey getKey() {
94 - return key;
73 + return getKeyOrThrow();
95 74 }
96 75
97 76 @NotNull
98 77 @Override
99 78 public String getTranslationKey() {
100 - return attributeBase.getDescriptionId();
101 - }
102 -
103 - @Override
104 - public int compareTo(@NotNull Attribute attribute) {
105 - return ordinal - attribute.ordinal();
106 - }
107 -
108 - @NotNull
109 - @Override
110 - public String name() {
111 - return name;
112 - }
113 -
114 - @Override
115 - public int ordinal() {
116 - return ordinal;
117 - }
118 -
119 - @Override
120 - public String toString() {
121 - // For backwards compatibility
122 - return name();
123 - }
124 -
125 - @Override
126 - public boolean equals(Object other) {
127 - if (this == other) {
128 - return true;
129 - }
130 -
131 - if (!(other instanceof CraftAttribute otherAttribute)) {
132 - return false;
133 - }
134 -
135 - return getKey().equals(otherAttribute.getKey());
136 - }
137 -
138 - @Override
139 - public int hashCode() {
140 - return getKey().hashCode();
79 + return getHandle().getDescriptionId();
141 80 }
142 81 }

Everything looks good. We'll let you know here if there's anything you should know about.

Add shortcut