Skip to content

Commit a632ca1

Browse files
committed
Use plugin NamespacedKey for data conversion key
Can't use API one cause if multiple API plugins need to convert they will clash :(
1 parent fa12984 commit a632ca1

File tree

5 files changed

+23
-27
lines changed

5 files changed

+23
-27
lines changed

src/main/java/xyz/srnyx/annoyingapi/data/EntityData.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,22 +50,22 @@ public EntityData(@NotNull AnnoyingPlugin plugin, @NotNull Entity entity) {
5050
* <br>For 1.14+ (PDC), the entity will receive the {@code api_converted} key which indicates that the data has been converted, this will avoid duplicate conversion checks
5151
* <br>All old data (PDC/file) will be removed after conversion (to avoid duplicate/overwriting data)
5252
*
53-
* @param onlyTryOnce 1.14+ only. {@code true} to only try once to convert the data, even if it fails (so if run again, nothing will happen), all old data will be removed even if it fails. If {@code false} and this is run again, it will try to convert again. If the data is successfully converted, this option doesn't matter
53+
* @param onlyTryOnce 1.14+ only | {@code true} to only try once to convert the data, even if it fails (so if run again, nothing will happen). If {@code false}, the conversion failed previously, and this is run again, it will try to convert again. If the data is successfully converted, this option doesn't matter
5454
* @param keys only applicable for 1.14-1.16, otherwise it will convert all keys (no matter what is provided)
5555
*
5656
* @return a map of keys that failed to convert (key, value) or {@code null} if an error occurred (only returns {@code null} if 1.14+ fails)
5757
*/
5858
@Nullable @SuppressWarnings("deprecation")
5959
public Map<String, String> convertOldData(boolean onlyTryOnce, @Nullable Collection<String> keys) {
6060
// 1.14+ (persistent data container)
61-
if (NAMESPACED_KEY_CONSTRUCTOR_STRING != null && NAMESPACED_KEY_CONSTRUCTOR_PLUGIN != null && PERSISTENT_DATA_HOLDER_GET_PERSISTENT_DATA_CONTAINER_METHOD != null && PERSISTENT_DATA_CONTAINER_GET_METHOD != null && PERSISTENT_DATA_CONTAINER_SET_METHOD != null && PERSISTENT_DATA_TYPE_STRING != null && PERSISTENT_DATA_TYPE_BYTE != null) {
61+
if (NAMESPACED_KEY_CONSTRUCTOR != null && PERSISTENT_DATA_HOLDER_GET_PERSISTENT_DATA_CONTAINER_METHOD != null && PERSISTENT_DATA_CONTAINER_GET_METHOD != null && PERSISTENT_DATA_CONTAINER_SET_METHOD != null && PERSISTENT_DATA_TYPE_STRING != null && PERSISTENT_DATA_TYPE_BYTE != null) {
6262
final Object persistentDataContainer;
6363
final Object convertedKey;
6464
try {
6565
// Get PDC
6666
persistentDataContainer = PERSISTENT_DATA_HOLDER_GET_PERSISTENT_DATA_CONTAINER_METHOD.invoke(entity);
6767
// Check if already converted
68-
convertedKey = NAMESPACED_KEY_CONSTRUCTOR_STRING.newInstance("annoyingapi", "converted");
68+
convertedKey = NAMESPACED_KEY_CONSTRUCTOR.newInstance(plugin, "annoyingapi_converted");
6969
if (PERSISTENT_DATA_CONTAINER_GET_METHOD.invoke(persistentDataContainer, convertedKey, PERSISTENT_DATA_TYPE_BYTE) != null) return Collections.emptyMap();
7070
// Set converted key if onlyTryOnce is true
7171
if (onlyTryOnce) PERSISTENT_DATA_CONTAINER_SET_METHOD.invoke(persistentDataContainer, convertedKey, PERSISTENT_DATA_TYPE_BYTE, (byte) 1);
@@ -95,7 +95,7 @@ public Map<String, String> convertOldData(boolean onlyTryOnce, @Nullable Collect
9595
if (keys == null || keys.isEmpty()) return Collections.emptyMap();
9696
try {
9797
for (final String key : keys) {
98-
final Object namespacedKey = NAMESPACED_KEY_CONSTRUCTOR_PLUGIN.newInstance(plugin, key);
98+
final Object namespacedKey = NAMESPACED_KEY_CONSTRUCTOR.newInstance(plugin, key);
9999
namespacedKeys.put(key, namespacedKey);
100100
}
101101
} catch (final ReflectiveOperationException e) {
@@ -151,7 +151,7 @@ public Map<String, String> convertOldData(boolean onlyTryOnce, @Nullable Collect
151151
/**
152152
* Calls {@link #convertOldData(boolean, Collection)} with the given keys
153153
*
154-
* @param onlyTryOnce 1.14+ only. {@code true} to only try once to convert the data, even if it fails (so if run again, nothing will happen), all old data will be removed even if it fails. If {@code false} and this is run again, it will try to convert again. If the data is successfully converted, this option doesn't matter
154+
* @param onlyTryOnce 1.14+ only | {@code true} to only try once to convert the data, even if it fails (so if run again, nothing will happen). If {@code false}, the conversion failed previously, and this is run again, it will try to convert again. If the data is successfully converted, this option doesn't matter
155155
* @param keys only applicable for 1.14-1.16, otherwise it will convert all keys (no matter what is provided)
156156
*
157157
* @return a map of keys that failed to convert (key, value) or {@code null} if an error occurred (only returns {@code null} if 1.14+ fails)
@@ -194,7 +194,7 @@ public Map<String, String> convertOldData(@NotNull String... keys) {
194194
/**
195195
* Calls {@link #convertOldData(boolean, Collection)} with {@code onlyTryOnce} set to {@code true} and {@code keys} set to {@code null}
196196
*
197-
* @param onlyTryOnce 1.14+ only. {@code true} to only try once to convert the data, even if it fails (so if run again, nothing will happen), all old data will be removed even if it fails. If {@code false} and this is run again, it will try to convert again. If the data is successfully converted, this option doesn't matter
197+
* @param onlyTryOnce 1.14+ only | {@code true} to only try once to convert the data, even if it fails (so if run again, nothing will happen). If {@code false}, the conversion failed previously, and this is run again, it will try to convert again. If the data is successfully converted, this option doesn't matter
198198
*
199199
* @return a map of keys that failed to convert (key, value) or {@code null} if an error occurred (only returns {@code null} if 1.14+ fails)
200200
*

src/main/java/xyz/srnyx/annoyingapi/data/ItemData.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
import java.util.function.Function;
1919
import java.util.function.Supplier;
2020

21-
import static xyz.srnyx.annoyingapi.reflection.org.bukkit.RefNamespacedKey.NAMESPACED_KEY_CONSTRUCTOR_PLUGIN;
21+
import static xyz.srnyx.annoyingapi.reflection.org.bukkit.RefNamespacedKey.NAMESPACED_KEY_CONSTRUCTOR;
2222
import static xyz.srnyx.annoyingapi.reflection.org.bukkit.inventory.meta.RefItemMeta.ITEM_META_GET_CUSTOM_TAG_CONTAINER_METHOD;
2323
import static xyz.srnyx.annoyingapi.reflection.org.bukkit.inventory.meta.tags.RefCustomItemTagContainer.*;
2424
import static xyz.srnyx.annoyingapi.reflection.org.bukkit.inventory.meta.tags.RefItemTagType.ITEM_TAG_TYPE_STRING;
@@ -74,21 +74,21 @@ public void attemptItemNbtApi(@NotNull Runnable runnable) {
7474
@Override @Nullable
7575
public String get(@NotNull String key) {
7676
// 1.13.2+ (persistent data container or custom item tag container)
77-
if (NAMESPACED_KEY_CONSTRUCTOR_PLUGIN != null) {
77+
if (NAMESPACED_KEY_CONSTRUCTOR != null) {
7878
final ItemMeta meta = target.getItemMeta();
7979
if (meta == null) return null;
8080

8181
// 1.14+ (persistent data container)
8282
if (PERSISTENT_DATA_HOLDER_GET_PERSISTENT_DATA_CONTAINER_METHOD != null && PERSISTENT_DATA_CONTAINER_GET_METHOD != null && PERSISTENT_DATA_TYPE_STRING != null) try {
83-
return (String) PERSISTENT_DATA_CONTAINER_GET_METHOD.invoke(PERSISTENT_DATA_HOLDER_GET_PERSISTENT_DATA_CONTAINER_METHOD.invoke(meta), NAMESPACED_KEY_CONSTRUCTOR_PLUGIN.newInstance(plugin, key), PERSISTENT_DATA_TYPE_STRING);
83+
return (String) PERSISTENT_DATA_CONTAINER_GET_METHOD.invoke(PERSISTENT_DATA_HOLDER_GET_PERSISTENT_DATA_CONTAINER_METHOD.invoke(meta), NAMESPACED_KEY_CONSTRUCTOR.newInstance(plugin, key), PERSISTENT_DATA_TYPE_STRING);
8484
} catch (final ReflectiveOperationException e) {
8585
sendError("get", e);
8686
return null;
8787
}
8888

8989
// 1.13.2 (custom item tag container)
9090
if (ITEM_META_GET_CUSTOM_TAG_CONTAINER_METHOD != null && CUSTOM_ITEM_TAG_CONTAINER_GET_CUSTOM_TAG_METHOD != null) try {
91-
return (String) CUSTOM_ITEM_TAG_CONTAINER_GET_CUSTOM_TAG_METHOD.invoke(ITEM_META_GET_CUSTOM_TAG_CONTAINER_METHOD.invoke(meta), NAMESPACED_KEY_CONSTRUCTOR_PLUGIN.newInstance(plugin, key), ITEM_TAG_TYPE_STRING);
91+
return (String) CUSTOM_ITEM_TAG_CONTAINER_GET_CUSTOM_TAG_METHOD.invoke(ITEM_META_GET_CUSTOM_TAG_CONTAINER_METHOD.invoke(meta), NAMESPACED_KEY_CONSTRUCTOR.newInstance(plugin, key), ITEM_TAG_TYPE_STRING);
9292
} catch (final ReflectiveOperationException e) {
9393
sendError("get", e);
9494
return null;
@@ -110,7 +110,7 @@ public String get(@NotNull String key) {
110110
@Override
111111
protected boolean set(@NotNull String key, @NotNull String value) {
112112
// 1.13.2+ (persistent data container or custom item tag container)
113-
if (NAMESPACED_KEY_CONSTRUCTOR_PLUGIN != null) {
113+
if (NAMESPACED_KEY_CONSTRUCTOR != null) {
114114
final ItemMeta meta = target.getItemMeta();
115115
if (meta == null) {
116116
sendError("set", null);
@@ -119,7 +119,7 @@ protected boolean set(@NotNull String key, @NotNull String value) {
119119

120120
// 1.14+ (persistent data container)
121121
if (PERSISTENT_DATA_HOLDER_GET_PERSISTENT_DATA_CONTAINER_METHOD != null && PERSISTENT_DATA_CONTAINER_SET_METHOD != null) try {
122-
PERSISTENT_DATA_CONTAINER_SET_METHOD.invoke(PERSISTENT_DATA_HOLDER_GET_PERSISTENT_DATA_CONTAINER_METHOD.invoke(meta), NAMESPACED_KEY_CONSTRUCTOR_PLUGIN.newInstance(plugin, key), PERSISTENT_DATA_TYPE_STRING, value);
122+
PERSISTENT_DATA_CONTAINER_SET_METHOD.invoke(PERSISTENT_DATA_HOLDER_GET_PERSISTENT_DATA_CONTAINER_METHOD.invoke(meta), NAMESPACED_KEY_CONSTRUCTOR.newInstance(plugin, key), PERSISTENT_DATA_TYPE_STRING, value);
123123
target.setItemMeta(meta);
124124
return true;
125125
} catch (final ReflectiveOperationException e) {
@@ -129,7 +129,7 @@ protected boolean set(@NotNull String key, @NotNull String value) {
129129

130130
// 1.13.2 (custom item tag container)
131131
if (ITEM_META_GET_CUSTOM_TAG_CONTAINER_METHOD != null && CUSTOM_ITEM_TAG_CONTAINER_SET_CUSTOM_TAG_METHOD != null) try {
132-
CUSTOM_ITEM_TAG_CONTAINER_SET_CUSTOM_TAG_METHOD.invoke(ITEM_META_GET_CUSTOM_TAG_CONTAINER_METHOD.invoke(meta), NAMESPACED_KEY_CONSTRUCTOR_PLUGIN.newInstance(plugin, key), ITEM_TAG_TYPE_STRING, value);
132+
CUSTOM_ITEM_TAG_CONTAINER_SET_CUSTOM_TAG_METHOD.invoke(ITEM_META_GET_CUSTOM_TAG_CONTAINER_METHOD.invoke(meta), NAMESPACED_KEY_CONSTRUCTOR.newInstance(plugin, key), ITEM_TAG_TYPE_STRING, value);
133133
target.setItemMeta(meta);
134134
return true;
135135
} catch (final ReflectiveOperationException e) {
@@ -153,7 +153,7 @@ protected boolean set(@NotNull String key, @NotNull String value) {
153153
@Override
154154
public boolean remove(@NotNull String key) {
155155
// 1.13.2+ (persistent data container or custom item tag container)
156-
if (NAMESPACED_KEY_CONSTRUCTOR_PLUGIN != null) {
156+
if (NAMESPACED_KEY_CONSTRUCTOR != null) {
157157
final ItemMeta meta = target.getItemMeta();
158158
if (meta == null) {
159159
sendError("remove", null);
@@ -162,7 +162,7 @@ public boolean remove(@NotNull String key) {
162162

163163
// 1.14+ (persistent data container)
164164
if (PERSISTENT_DATA_HOLDER_GET_PERSISTENT_DATA_CONTAINER_METHOD != null && PERSISTENT_DATA_CONTAINER_REMOVE_METHOD != null) try {
165-
PERSISTENT_DATA_CONTAINER_REMOVE_METHOD.invoke(PERSISTENT_DATA_HOLDER_GET_PERSISTENT_DATA_CONTAINER_METHOD.invoke(meta), NAMESPACED_KEY_CONSTRUCTOR_PLUGIN.newInstance(plugin, key));
165+
PERSISTENT_DATA_CONTAINER_REMOVE_METHOD.invoke(PERSISTENT_DATA_HOLDER_GET_PERSISTENT_DATA_CONTAINER_METHOD.invoke(meta), NAMESPACED_KEY_CONSTRUCTOR.newInstance(plugin, key));
166166
target.setItemMeta(meta);
167167
return true;
168168
} catch (final ReflectiveOperationException e) {
@@ -172,7 +172,7 @@ public boolean remove(@NotNull String key) {
172172

173173
// 1.13.2 (custom item tag container)
174174
if (ITEM_META_GET_CUSTOM_TAG_CONTAINER_METHOD != null && CUSTOM_ITEM_TAG_CONTAINER_REMOVE_CUSTOM_TAG_METHOD != null) try {
175-
CUSTOM_ITEM_TAG_CONTAINER_REMOVE_CUSTOM_TAG_METHOD.invoke(ITEM_META_GET_CUSTOM_TAG_CONTAINER_METHOD.invoke(meta), NAMESPACED_KEY_CONSTRUCTOR_PLUGIN.newInstance(plugin, key));
175+
CUSTOM_ITEM_TAG_CONTAINER_REMOVE_CUSTOM_TAG_METHOD.invoke(ITEM_META_GET_CUSTOM_TAG_CONTAINER_METHOD.invoke(meta), NAMESPACED_KEY_CONSTRUCTOR.newInstance(plugin, key));
176176
target.setItemMeta(meta);
177177
return true;
178178
} catch (final ReflectiveOperationException e) {

src/main/java/xyz/srnyx/annoyingapi/file/AnnoyingFile.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
import java.util.logging.Level;
3636
import java.util.stream.Collectors;
3737

38-
import static xyz.srnyx.annoyingapi.reflection.org.bukkit.RefNamespacedKey.NAMESPACED_KEY_CONSTRUCTOR_PLUGIN;
38+
import static xyz.srnyx.annoyingapi.reflection.org.bukkit.RefNamespacedKey.NAMESPACED_KEY_CONSTRUCTOR;
3939
import static xyz.srnyx.annoyingapi.reflection.org.bukkit.attribute.RefAttribute.ATTRIBUTE_ENUM;
4040
import static xyz.srnyx.annoyingapi.reflection.org.bukkit.attribute.RefAttributeModifier.*;
4141
import static xyz.srnyx.annoyingapi.reflection.org.bukkit.attribute.RefAttributeModifier.RefOperation.ATTRIBUTE_MODIFIER_OPERATION_ENUM;
@@ -662,10 +662,10 @@ public Optional<Recipe> getRecipe(@NotNull String path, @Nullable UnaryOperator<
662662
// Shapeless
663663
if (section.getBoolean("shapeless")) {
664664
ShapelessRecipe shapeless;
665-
if (SHAPELESS_RECIPE_CONSTRUCTOR != null && NAMESPACED_KEY_CONSTRUCTOR_PLUGIN != null) {
665+
if (SHAPELESS_RECIPE_CONSTRUCTOR != null && NAMESPACED_KEY_CONSTRUCTOR != null) {
666666
try {
667667
// 1.12+
668-
shapeless = SHAPELESS_RECIPE_CONSTRUCTOR.newInstance(NAMESPACED_KEY_CONSTRUCTOR_PLUGIN.newInstance(plugin, name), result);
668+
shapeless = SHAPELESS_RECIPE_CONSTRUCTOR.newInstance(NAMESPACED_KEY_CONSTRUCTOR.newInstance(plugin, name), result);
669669
} catch (final InstantiationException | IllegalAccessException | InvocationTargetException e) {
670670
// 1.11-
671671
shapeless = new ShapelessRecipe(result);
@@ -682,10 +682,10 @@ public Optional<Recipe> getRecipe(@NotNull String path, @Nullable UnaryOperator<
682682

683683
// Shaped
684684
ShapedRecipe shaped;
685-
if (SHAPED_RECIPE_CONSTRUCTOR != null && NAMESPACED_KEY_CONSTRUCTOR_PLUGIN != null) {
685+
if (SHAPED_RECIPE_CONSTRUCTOR != null && NAMESPACED_KEY_CONSTRUCTOR != null) {
686686
try {
687687
// 1.12+
688-
shaped = SHAPED_RECIPE_CONSTRUCTOR.newInstance(NAMESPACED_KEY_CONSTRUCTOR_PLUGIN.newInstance(plugin, name), result);
688+
shaped = SHAPED_RECIPE_CONSTRUCTOR.newInstance(NAMESPACED_KEY_CONSTRUCTOR.newInstance(plugin, name), result);
689689
} catch (final InstantiationException | IllegalAccessException | InvocationTargetException e) {
690690
// 1.11-
691691
shaped = new ShapedRecipe(result);

src/main/java/xyz/srnyx/annoyingapi/reflection/org/bukkit/RefNamespacedKey.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,10 @@ public class RefNamespacedKey {
2222
* 1.12+ org.bukkit.NamespacedKey#minecraft(String)
2323
*/
2424
@Nullable public static final Method MINECRAFT_METHOD = ReflectionUtility.getMethod(1, 12, 0, NAMESPACED_KEY_CLASS, "minecraft", String.class);
25-
/**
26-
* 1.12+ org.bukkit.NamespacedKey(String, String)
27-
*/
28-
@Nullable public static final Constructor<?> NAMESPACED_KEY_CONSTRUCTOR_STRING = ReflectionUtility.getConstructor(1, 12, 0, NAMESPACED_KEY_CLASS, String.class, String.class);
2925
/**
3026
* 1.12+ org.bukkit.NamespacedKey(org.bukkit.plugin.Plugin, String)
3127
*/
32-
@Nullable public static final Constructor<?> NAMESPACED_KEY_CONSTRUCTOR_PLUGIN = ReflectionUtility.getConstructor(1, 12, 0, NAMESPACED_KEY_CLASS, Plugin.class, String.class);
28+
@Nullable public static final Constructor<?> NAMESPACED_KEY_CONSTRUCTOR = ReflectionUtility.getConstructor(1, 12, 0, NAMESPACED_KEY_CLASS, Plugin.class, String.class);
3329
/**
3430
* 1.12+ org.bukkit.NamespacedKey#getNamespace()
3531
*/

src/main/resources/plugin.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ authors:
44
- srnyx
55
website: https://srnyx.com
66

7-
main: ${mainPackage}.AnnoyingPlugin
7+
main: ${mainPackage}.${name}
88
version: ${version}
99
api-version: 1.13
1010

0 commit comments

Comments
 (0)